Debug info: Unique types before emitting them to DWARF, where applicable.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfUnit.cpp
1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
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 // This file contains support for constructing a dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "dwarfdebug"
15
16 #include "DwarfUnit.h"
17 #include "DwarfAccelTable.h"
18 #include "DwarfDebug.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DIBuilder.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetFrameLowering.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35
36 using namespace llvm;
37
38 static cl::opt<bool>
39 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40                        cl::desc("Generate DWARF4 type units."),
41                        cl::init(false));
42
43 /// Unit - Unit constructor.
44 DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45                      DwarfDebug *DW, DwarfFile *DWU)
46     : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47       DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
48   DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49 }
50
51 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52                                    AsmPrinter *A, DwarfDebug *DW,
53                                    DwarfFile *DWU)
54     : DwarfUnit(UID, D, Node, A, DW, DWU) {
55   insertDIE(Node, D);
56 }
57
58 DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
59                              AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
60     : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU) {}
61
62 /// ~Unit - Destructor for compile unit.
63 DwarfUnit::~DwarfUnit() {
64   for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
65     DIEBlocks[j]->~DIEBlock();
66   for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
67     DIELocs[j]->~DIELoc();
68 }
69
70 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
71 /// information entry.
72 DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
73   DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
74   return Value;
75 }
76
77 /// getDefaultLowerBound - Return the default lower bound for an array. If the
78 /// DWARF version doesn't handle the language, return -1.
79 int64_t DwarfUnit::getDefaultLowerBound() const {
80   switch (getLanguage()) {
81   default:
82     break;
83
84   case dwarf::DW_LANG_C89:
85   case dwarf::DW_LANG_C99:
86   case dwarf::DW_LANG_C:
87   case dwarf::DW_LANG_C_plus_plus:
88   case dwarf::DW_LANG_ObjC:
89   case dwarf::DW_LANG_ObjC_plus_plus:
90     return 0;
91
92   case dwarf::DW_LANG_Fortran77:
93   case dwarf::DW_LANG_Fortran90:
94   case dwarf::DW_LANG_Fortran95:
95     return 1;
96
97   // The languages below have valid values only if the DWARF version >= 4.
98   case dwarf::DW_LANG_Java:
99   case dwarf::DW_LANG_Python:
100   case dwarf::DW_LANG_UPC:
101   case dwarf::DW_LANG_D:
102     if (dwarf::DWARF_VERSION >= 4)
103       return 0;
104     break;
105
106   case dwarf::DW_LANG_Ada83:
107   case dwarf::DW_LANG_Ada95:
108   case dwarf::DW_LANG_Cobol74:
109   case dwarf::DW_LANG_Cobol85:
110   case dwarf::DW_LANG_Modula2:
111   case dwarf::DW_LANG_Pascal83:
112   case dwarf::DW_LANG_PLI:
113     if (dwarf::DWARF_VERSION >= 4)
114       return 1;
115     break;
116   }
117
118   return -1;
119 }
120
121 /// Check whether the DIE for this MDNode can be shared across CUs.
122 static bool isShareableAcrossCUs(DIDescriptor D) {
123   // When the MDNode can be part of the type system, the DIE can be shared
124   // across CUs.
125   // Combining type units and cross-CU DIE sharing is lower value (since
126   // cross-CU DIE sharing is used in LTO and removes type redundancy at that
127   // level already) but may be implementable for some value in projects
128   // building multiple independent libraries with LTO and then linking those
129   // together.
130   return (D.isType() ||
131           (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
132          !GenerateDwarfTypeUnits;
133 }
134
135 /// getDIE - Returns the debug information entry map slot for the
136 /// specified debug variable. We delegate the request to DwarfDebug
137 /// when the DIE for this MDNode can be shared across CUs. The mappings
138 /// will be kept in DwarfDebug for shareable DIEs.
139 DIE *DwarfUnit::getDIE(DIDescriptor D) const {
140   if (isShareableAcrossCUs(D))
141     return DD->getDIE(D);
142   return MDNodeToDieMap.lookup(D);
143 }
144
145 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
146 /// when the DIE for this MDNode can be shared across CUs. The mappings
147 /// will be kept in DwarfDebug for shareable DIEs.
148 void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
149   if (isShareableAcrossCUs(Desc)) {
150     DD->insertDIE(Desc, D);
151     return;
152   }
153   MDNodeToDieMap.insert(std::make_pair(Desc, D));
154 }
155
156 /// addFlag - Add a flag that is true.
157 void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
158   if (DD->getDwarfVersion() >= 4)
159     Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
160   else
161     Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
162 }
163
164 /// addUInt - Add an unsigned integer attribute data and value.
165 ///
166 void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
167                         Optional<dwarf::Form> Form, uint64_t Integer) {
168   if (!Form)
169     Form = DIEInteger::BestForm(false, Integer);
170   DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
171                         DIEInteger(Integer);
172   Die->addValue(Attribute, *Form, Value);
173 }
174
175 void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
176   addUInt(Block, (dwarf::Attribute)0, Form, Integer);
177 }
178
179 /// addSInt - Add an signed integer attribute data and value.
180 ///
181 void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
182                         Optional<dwarf::Form> Form, int64_t Integer) {
183   if (!Form)
184     Form = DIEInteger::BestForm(true, Integer);
185   DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
186   Die->addValue(Attribute, *Form, Value);
187 }
188
189 void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
190                         int64_t Integer) {
191   addSInt(Die, (dwarf::Attribute)0, Form, Integer);
192 }
193
194 /// addString - Add a string attribute data and value. We always emit a
195 /// reference to the string pool instead of immediate strings so that DIEs have
196 /// more predictable sizes. In the case of split dwarf we emit an index
197 /// into another table which gets us the static offset into the string
198 /// table.
199 void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
200                           StringRef String) {
201
202   if (!DD->useSplitDwarf())
203     return addLocalString(Die, Attribute, String);
204
205   unsigned idx = DU->getStringPoolIndex(String);
206   DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
207   DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
208   Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
209 }
210
211 /// addLocalString - Add a string attribute data and value. This is guaranteed
212 /// to be in the local string pool instead of indirected.
213 void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
214                                StringRef String) {
215   MCSymbol *Symb = DU->getStringPoolEntry(String);
216   DIEValue *Value;
217   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
218     Value = new (DIEValueAllocator) DIELabel(Symb);
219   else {
220     MCSymbol *StringPool = DU->getStringPoolSym();
221     Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
222   }
223   DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
224   Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
225 }
226
227 /// addExpr - Add a Dwarf expression attribute data and value.
228 ///
229 void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
230   DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
231   Die->addValue((dwarf::Attribute)0, Form, Value);
232 }
233
234 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
235 ///
236 void DwarfUnit::addLocationList(DIE *Die, dwarf::Attribute Attribute,
237                                 unsigned Index) {
238   DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
239   dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
240                                                 : dwarf::DW_FORM_data4;
241   Die->addValue(Attribute, Form, Value);
242 }
243
244 /// addLabel - Add a Dwarf label attribute data and value.
245 ///
246 void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
247                          const MCSymbol *Label) {
248   DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
249   Die->addValue(Attribute, Form, Value);
250 }
251
252 void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
253   addLabel(Die, (dwarf::Attribute)0, Form, Label);
254 }
255
256 /// addSectionLabel - Add a Dwarf section label attribute data and value.
257 ///
258 void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
259                                 const MCSymbol *Label) {
260   if (DD->getDwarfVersion() >= 4)
261     addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
262   else
263     addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
264 }
265
266 /// addSectionOffset - Add an offset into a section attribute data and value.
267 ///
268 void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
269                                  uint64_t Integer) {
270   if (DD->getDwarfVersion() >= 4)
271     addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
272   else
273     addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
274 }
275
276 /// addLabelAddress - Add a dwarf label attribute data and value using
277 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
278 ///
279 void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
280                                        MCSymbol *Label) {
281   if (Label)
282     DD->addArangeLabel(SymbolCU(this, Label));
283
284   if (!DD->useSplitDwarf()) {
285     if (Label) {
286       DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
287       Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
288     } else {
289       DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
290       Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
291     }
292   } else {
293     unsigned idx = DU->getAddrPoolIndex(Label);
294     DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
295     Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
296   }
297 }
298
299 /// addOpAddress - Add a dwarf op address data and value using the
300 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
301 ///
302 void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
303   if (!DD->useSplitDwarf()) {
304     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
305     addLabel(Die, dwarf::DW_FORM_udata, Sym);
306   } else {
307     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
308     addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
309   }
310 }
311
312 /// addSectionDelta - Add a section label delta attribute data and value.
313 ///
314 void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
315                                 const MCSymbol *Hi, const MCSymbol *Lo) {
316   DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
317   if (DD->getDwarfVersion() >= 4)
318     Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
319   else
320     Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
321 }
322
323 void DwarfUnit::addLabelDelta(DIE *Die, dwarf::Attribute Attribute,
324                               const MCSymbol *Hi, const MCSymbol *Lo) {
325   DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
326   Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
327 }
328
329 /// addDIEEntry - Add a DIE attribute data and value.
330 ///
331 void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
332   addDIEEntry(Die, Attribute, createDIEEntry(Entry));
333 }
334
335 void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
336   Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
337                 new (DIEValueAllocator) DIETypeSignature(Type));
338 }
339
340 void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
341                             DIEEntry *Entry) {
342   const DIE *DieCU = Die->getUnitOrNull();
343   const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
344   if (!DieCU)
345     // We assume that Die belongs to this CU, if it is not linked to any CU yet.
346     DieCU = getUnitDie();
347   if (!EntryCU)
348     EntryCU = getUnitDie();
349   Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
350                                             : dwarf::DW_FORM_ref_addr,
351                 Entry);
352 }
353
354 /// Create a DIE with the given Tag, add the DIE to its parent, and
355 /// call insertDIE if MD is not null.
356 DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
357   DIE *Die = new DIE(Tag);
358   Parent.addChild(Die);
359   if (N)
360     insertDIE(N, Die);
361   return Die;
362 }
363
364 /// addBlock - Add block data.
365 ///
366 void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
367   Loc->ComputeSize(Asm);
368   DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
369   Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
370 }
371
372 void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
373                          DIEBlock *Block) {
374   Block->ComputeSize(Asm);
375   DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
376   Die->addValue(Attribute, Block->BestForm(), Block);
377 }
378
379 /// addSourceLine - Add location information to specified debug information
380 /// entry.
381 void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
382                               StringRef Directory) {
383   if (Line == 0)
384     return;
385
386   unsigned FileID =
387       DD->getOrCreateSourceID(File, Directory, getCU().getUniqueID());
388   assert(FileID && "Invalid file id");
389   addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
390   addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
391 }
392
393 /// addSourceLine - Add location information to specified debug information
394 /// entry.
395 void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
396   assert(V.isVariable());
397
398   addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
399                 V.getContext().getDirectory());
400 }
401
402 /// addSourceLine - Add location information to specified debug information
403 /// entry.
404 void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
405   assert(G.isGlobalVariable());
406
407   addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
408 }
409
410 /// addSourceLine - Add location information to specified debug information
411 /// entry.
412 void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
413   assert(SP.isSubprogram());
414
415   addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
416 }
417
418 /// addSourceLine - Add location information to specified debug information
419 /// entry.
420 void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
421   assert(Ty.isType());
422
423   addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
424 }
425
426 /// addSourceLine - Add location information to specified debug information
427 /// entry.
428 void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
429   assert(Ty.isObjCProperty());
430
431   DIFile File = Ty.getFile();
432   addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
433                 File.getDirectory());
434 }
435
436 /// addSourceLine - Add location information to specified debug information
437 /// entry.
438 void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
439   assert(NS.Verify());
440
441   addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
442 }
443
444 /// addVariableAddress - Add DW_AT_location attribute for a
445 /// DbgVariable based on provided MachineLocation.
446 void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
447                                    MachineLocation Location) {
448   if (DV.variableHasComplexAddress())
449     addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
450   else if (DV.isBlockByrefVariable())
451     addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
452   else
453     addAddress(Die, dwarf::DW_AT_location, Location,
454                DV.getVariable().isIndirect());
455 }
456
457 /// addRegisterOp - Add register operand.
458 void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
459   const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
460   int DWReg = RI->getDwarfRegNum(Reg, false);
461   bool isSubRegister = DWReg < 0;
462
463   unsigned Idx = 0;
464
465   // Go up the super-register chain until we hit a valid dwarf register number.
466   for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
467     DWReg = RI->getDwarfRegNum(*SR, false);
468     if (DWReg >= 0)
469       Idx = RI->getSubRegIndex(*SR, Reg);
470   }
471
472   if (DWReg < 0) {
473     DEBUG(dbgs() << "Invalid Dwarf register number.\n");
474     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
475     return;
476   }
477
478   // Emit register
479   if (DWReg < 32)
480     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
481   else {
482     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
483     addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
484   }
485
486   // Emit Mask
487   if (isSubRegister) {
488     unsigned Size = RI->getSubRegIdxSize(Idx);
489     unsigned Offset = RI->getSubRegIdxOffset(Idx);
490     if (Offset > 0) {
491       addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
492       addUInt(TheDie, dwarf::DW_FORM_data1, Size);
493       addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
494     } else {
495       unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
496       addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
497       addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
498     }
499   }
500 }
501
502 /// addRegisterOffset - Add register offset.
503 void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
504                                   int64_t Offset) {
505   const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
506   unsigned DWReg = RI->getDwarfRegNum(Reg, false);
507   const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
508   if (Reg == TRI->getFrameRegister(*Asm->MF))
509     // If variable offset is based in frame register then use fbreg.
510     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
511   else if (DWReg < 32)
512     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
513   else {
514     addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
515     addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
516   }
517   addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
518 }
519
520 /// addAddress - Add an address attribute to a die based on the location
521 /// provided.
522 void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
523                            const MachineLocation &Location, bool Indirect) {
524   DIELoc *Loc = new (DIEValueAllocator) DIELoc();
525
526   if (Location.isReg() && !Indirect)
527     addRegisterOp(Loc, Location.getReg());
528   else {
529     addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
530     if (Indirect && !Location.isReg()) {
531       addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
532     }
533   }
534
535   // Now attach the location information to the DIE.
536   addBlock(Die, Attribute, Loc);
537 }
538
539 /// addComplexAddress - Start with the address based on the location provided,
540 /// and generate the DWARF information necessary to find the actual variable
541 /// given the extra address information encoded in the DbgVariable, starting
542 /// from the starting location.  Add the DWARF information to the die.
543 ///
544 void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
545                                   dwarf::Attribute Attribute,
546                                   const MachineLocation &Location) {
547   DIELoc *Loc = new (DIEValueAllocator) DIELoc();
548   unsigned N = DV.getNumAddrElements();
549   unsigned i = 0;
550   if (Location.isReg()) {
551     if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
552       // If first address element is OpPlus then emit
553       // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
554       addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
555       i = 2;
556     } else
557       addRegisterOp(Loc, Location.getReg());
558   } else
559     addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
560
561   for (; i < N; ++i) {
562     uint64_t Element = DV.getAddrElement(i);
563     if (Element == DIBuilder::OpPlus) {
564       addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
565       addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
566     } else if (Element == DIBuilder::OpDeref) {
567       if (!Location.isReg())
568         addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
569     } else
570       llvm_unreachable("unknown DIBuilder Opcode");
571   }
572
573   // Now attach the location information to the DIE.
574   addBlock(Die, Attribute, Loc);
575 }
576
577 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
578    VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
579    gives the variable VarName either the struct, or a pointer to the struct, as
580    its type.  This is necessary for various behind-the-scenes things the
581    compiler needs to do with by-reference variables in Blocks.
582
583    However, as far as the original *programmer* is concerned, the variable
584    should still have type 'SomeType', as originally declared.
585
586    The function getBlockByrefType dives into the __Block_byref_x_VarName
587    struct to find the original type of the variable, which is then assigned to
588    the variable's Debug Information Entry as its real type.  So far, so good.
589    However now the debugger will expect the variable VarName to have the type
590    SomeType.  So we need the location attribute for the variable to be an
591    expression that explains to the debugger how to navigate through the
592    pointers and struct to find the actual variable of type SomeType.
593
594    The following function does just that.  We start by getting
595    the "normal" location for the variable. This will be the location
596    of either the struct __Block_byref_x_VarName or the pointer to the
597    struct __Block_byref_x_VarName.
598
599    The struct will look something like:
600
601    struct __Block_byref_x_VarName {
602      ... <various fields>
603      struct __Block_byref_x_VarName *forwarding;
604      ... <various other fields>
605      SomeType VarName;
606      ... <maybe more fields>
607    };
608
609    If we are given the struct directly (as our starting point) we
610    need to tell the debugger to:
611
612    1).  Add the offset of the forwarding field.
613
614    2).  Follow that pointer to get the real __Block_byref_x_VarName
615    struct to use (the real one may have been copied onto the heap).
616
617    3).  Add the offset for the field VarName, to find the actual variable.
618
619    If we started with a pointer to the struct, then we need to
620    dereference that pointer first, before the other steps.
621    Translating this into DWARF ops, we will need to append the following
622    to the current location description for the variable:
623
624    DW_OP_deref                    -- optional, if we start with a pointer
625    DW_OP_plus_uconst <forward_fld_offset>
626    DW_OP_deref
627    DW_OP_plus_uconst <varName_fld_offset>
628
629    That is what this function does.  */
630
631 /// addBlockByrefAddress - Start with the address based on the location
632 /// provided, and generate the DWARF information necessary to find the
633 /// actual Block variable (navigating the Block struct) based on the
634 /// starting location.  Add the DWARF information to the die.  For
635 /// more information, read large comment just above here.
636 ///
637 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
638                                      dwarf::Attribute Attribute,
639                                      const MachineLocation &Location) {
640   DIType Ty = DV.getType();
641   DIType TmpTy = Ty;
642   uint16_t Tag = Ty.getTag();
643   bool isPointer = false;
644
645   StringRef varName = DV.getName();
646
647   if (Tag == dwarf::DW_TAG_pointer_type) {
648     DIDerivedType DTy(Ty);
649     TmpTy = resolve(DTy.getTypeDerivedFrom());
650     isPointer = true;
651   }
652
653   DICompositeType blockStruct(TmpTy);
654
655   // Find the __forwarding field and the variable field in the __Block_byref
656   // struct.
657   DIArray Fields = blockStruct.getTypeArray();
658   DIDerivedType varField;
659   DIDerivedType forwardingField;
660
661   for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
662     DIDerivedType DT(Fields.getElement(i));
663     StringRef fieldName = DT.getName();
664     if (fieldName == "__forwarding")
665       forwardingField = DT;
666     else if (fieldName == varName)
667       varField = DT;
668   }
669
670   // Get the offsets for the forwarding field and the variable field.
671   unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
672   unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
673
674   // Decode the original location, and use that as the start of the byref
675   // variable's location.
676   DIELoc *Loc = new (DIEValueAllocator) DIELoc();
677
678   if (Location.isReg())
679     addRegisterOp(Loc, Location.getReg());
680   else
681     addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
682
683   // If we started with a pointer to the __Block_byref... struct, then
684   // the first thing we need to do is dereference the pointer (DW_OP_deref).
685   if (isPointer)
686     addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
687
688   // Next add the offset for the '__forwarding' field:
689   // DW_OP_plus_uconst ForwardingFieldOffset.  Note there's no point in
690   // adding the offset if it's 0.
691   if (forwardingFieldOffset > 0) {
692     addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
693     addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
694   }
695
696   // Now dereference the __forwarding field to get to the real __Block_byref
697   // struct:  DW_OP_deref.
698   addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
699
700   // Now that we've got the real __Block_byref... struct, add the offset
701   // for the variable's field to get to the location of the actual variable:
702   // DW_OP_plus_uconst varFieldOffset.  Again, don't add if it's 0.
703   if (varFieldOffset > 0) {
704     addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
705     addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
706   }
707
708   // Now attach the location information to the DIE.
709   addBlock(Die, Attribute, Loc);
710 }
711
712 /// isTypeSigned - Return true if the type is signed.
713 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
714   if (Ty.isDerivedType())
715     return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
716                         SizeInBits);
717   if (Ty.isBasicType())
718     if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
719         DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
720       *SizeInBits = Ty.getSizeInBits();
721       return true;
722     }
723   return false;
724 }
725
726 /// Return true if type encoding is unsigned.
727 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
728   DIDerivedType DTy(Ty);
729   if (DTy.isDerivedType())
730     return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
731
732   DIBasicType BTy(Ty);
733   if (BTy.isBasicType()) {
734     unsigned Encoding = BTy.getEncoding();
735     if (Encoding == dwarf::DW_ATE_unsigned ||
736         Encoding == dwarf::DW_ATE_unsigned_char ||
737         Encoding == dwarf::DW_ATE_boolean)
738       return true;
739   }
740   return false;
741 }
742
743 /// If this type is derived from a base type then return base type size.
744 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
745   unsigned Tag = Ty.getTag();
746
747   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
748       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
749       Tag != dwarf::DW_TAG_restrict_type)
750     return Ty.getSizeInBits();
751
752   DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
753
754   // If this type is not derived from any type or the type is a declaration then
755   // take conservative approach.
756   if (!BaseType.isValid() || BaseType.isForwardDecl())
757     return Ty.getSizeInBits();
758
759   // If this is a derived type, go ahead and get the base type, unless it's a
760   // reference then it's just the size of the field. Pointer types have no need
761   // of this since they're a different type of qualification on the type.
762   if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
763       BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
764     return Ty.getSizeInBits();
765
766   if (BaseType.isDerivedType())
767     return getBaseTypeSize(DD, DIDerivedType(BaseType));
768
769   return BaseType.getSizeInBits();
770 }
771
772 /// addConstantValue - Add constant value entry in variable DIE.
773 void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
774                                  DIType Ty) {
775   // FIXME: This is a bit conservative/simple - it emits negative values at
776   // their maximum bit width which is a bit unfortunate (& doesn't prefer
777   // udata/sdata over dataN as suggested by the DWARF spec)
778   assert(MO.isImm() && "Invalid machine operand!");
779   int SizeInBits = -1;
780   bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
781   dwarf::Form Form;
782
783   // If we're a signed constant definitely use sdata.
784   if (SignedConstant) {
785     addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
786     return;
787   }
788
789   // Else use data for now unless it's larger than we can deal with.
790   switch (SizeInBits) {
791   case 8:
792     Form = dwarf::DW_FORM_data1;
793     break;
794   case 16:
795     Form = dwarf::DW_FORM_data2;
796     break;
797   case 32:
798     Form = dwarf::DW_FORM_data4;
799     break;
800   case 64:
801     Form = dwarf::DW_FORM_data8;
802     break;
803   default:
804     Form = dwarf::DW_FORM_udata;
805     addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
806     return;
807   }
808   addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
809 }
810
811 /// addConstantFPValue - Add constant value entry in variable DIE.
812 void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
813   assert(MO.isFPImm() && "Invalid machine operand!");
814   DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
815   APFloat FPImm = MO.getFPImm()->getValueAPF();
816
817   // Get the raw data form of the floating point.
818   const APInt FltVal = FPImm.bitcastToAPInt();
819   const char *FltPtr = (const char *)FltVal.getRawData();
820
821   int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
822   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
823   int Incr = (LittleEndian ? 1 : -1);
824   int Start = (LittleEndian ? 0 : NumBytes - 1);
825   int Stop = (LittleEndian ? NumBytes : -1);
826
827   // Output the constant to DWARF one byte at a time.
828   for (; Start != Stop; Start += Incr)
829     addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
830
831   addBlock(Die, dwarf::DW_AT_const_value, Block);
832 }
833
834 /// addConstantFPValue - Add constant value entry in variable DIE.
835 void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
836   // Pass this down to addConstantValue as an unsigned bag of bits.
837   addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
838 }
839
840 /// addConstantValue - Add constant value entry in variable DIE.
841 void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
842                                  bool Unsigned) {
843   addConstantValue(Die, CI->getValue(), Unsigned);
844 }
845
846 // addConstantValue - Add constant value entry in variable DIE.
847 void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
848   unsigned CIBitWidth = Val.getBitWidth();
849   if (CIBitWidth <= 64) {
850     // If we're a signed constant definitely use sdata.
851     if (!Unsigned) {
852       addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
853               Val.getSExtValue());
854       return;
855     }
856
857     // Else use data for now unless it's larger than we can deal with.
858     dwarf::Form Form;
859     switch (CIBitWidth) {
860     case 8:
861       Form = dwarf::DW_FORM_data1;
862       break;
863     case 16:
864       Form = dwarf::DW_FORM_data2;
865       break;
866     case 32:
867       Form = dwarf::DW_FORM_data4;
868       break;
869     case 64:
870       Form = dwarf::DW_FORM_data8;
871       break;
872     default:
873       addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
874               Val.getZExtValue());
875       return;
876     }
877     addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
878     return;
879   }
880
881   DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
882
883   // Get the raw data form of the large APInt.
884   const uint64_t *Ptr64 = Val.getRawData();
885
886   int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
887   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
888
889   // Output the constant to DWARF one byte at a time.
890   for (int i = 0; i < NumBytes; i++) {
891     uint8_t c;
892     if (LittleEndian)
893       c = Ptr64[i / 8] >> (8 * (i & 7));
894     else
895       c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
896     addUInt(Block, dwarf::DW_FORM_data1, c);
897   }
898
899   addBlock(Die, dwarf::DW_AT_const_value, Block);
900 }
901
902 /// addTemplateParams - Add template parameters into buffer.
903 void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
904   // Add template parameters.
905   for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
906     DIDescriptor Element = TParams.getElement(i);
907     if (Element.isTemplateTypeParameter())
908       constructTemplateTypeParameterDIE(Buffer,
909                                         DITemplateTypeParameter(Element));
910     else if (Element.isTemplateValueParameter())
911       constructTemplateValueParameterDIE(Buffer,
912                                          DITemplateValueParameter(Element));
913   }
914 }
915
916 /// getOrCreateContextDIE - Get context owner's DIE.
917 DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
918   if (!Context || Context.isFile())
919     return getUnitDie();
920   if (Context.isType())
921     return getOrCreateTypeDIE(DIType(Context));
922   if (Context.isNameSpace())
923     return getOrCreateNameSpace(DINameSpace(Context));
924   if (Context.isSubprogram())
925     return getOrCreateSubprogramDIE(DISubprogram(Context));
926   return getDIE(Context);
927 }
928
929 DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
930   DIScope Context = resolve(Ty.getContext());
931   DIE *ContextDIE = getOrCreateContextDIE(Context);
932
933   DIE *TyDIE = getDIE(Ty);
934   if (TyDIE)
935     return TyDIE;
936
937   // Create new type.
938   TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
939
940   constructTypeDIE(*TyDIE, Ty);
941
942   updateAcceleratorTables(Context, Ty, TyDIE);
943   return TyDIE;
944 }
945
946 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
947 /// given DIType.
948 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
949   if (!TyNode)
950     return NULL;
951
952   DIType Ty(TyNode);
953   assert(Ty.isType());
954
955   // Construct the context before querying for the existence of the DIE in case
956   // such construction creates the DIE.
957   DIScope Context = resolve(Ty.getContext());
958   DIE *ContextDIE = getOrCreateContextDIE(Context);
959   assert(ContextDIE);
960
961   // Unique the type. This is a noop if the type has no unique identifier.
962   Ty = DIType(resolve(Ty.getRef()));
963
964   DIE *TyDIE = getDIE(Ty);
965   if (TyDIE)
966     return TyDIE;
967
968   // Create new type.
969   TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
970
971   updateAcceleratorTables(Context, Ty, TyDIE);
972
973   if (Ty.isBasicType())
974     constructTypeDIE(*TyDIE, DIBasicType(Ty));
975   else if (Ty.isCompositeType()) {
976     DICompositeType CTy(Ty);
977     if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
978       if (MDString *TypeId = CTy.getIdentifier()) {
979         DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
980         // Skip updating the accelerator tables since this is not the full type.
981         return TyDIE;
982       }
983     constructTypeDIE(*TyDIE, CTy);
984   } else {
985     assert(Ty.isDerivedType() && "Unknown kind of DIType");
986     constructTypeDIE(*TyDIE, DIDerivedType(Ty));
987   }
988
989   return TyDIE;
990 }
991
992 void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
993                                         const DIE *TyDIE) {
994   if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
995     bool IsImplementation = 0;
996     if (Ty.isCompositeType()) {
997       DICompositeType CT(Ty);
998       // A runtime language of 0 actually means C/C++ and that any
999       // non-negative value is some version of Objective-C/C++.
1000       IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
1001     }
1002     unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
1003     addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
1004
1005     if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1006          Context.isNameSpace()) &&
1007         getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
1008       GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
1009   }
1010 }
1011
1012 /// addType - Add a new type attribute to the specified entity.
1013 void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
1014   assert(Ty && "Trying to add a type that doesn't exist?");
1015
1016   // Check for pre-existence.
1017   DIEEntry *Entry = getDIEEntry(Ty);
1018   // If it exists then use the existing value.
1019   if (Entry) {
1020     addDIEEntry(Entity, Attribute, Entry);
1021     return;
1022   }
1023
1024   // Construct type.
1025   DIE *Buffer = getOrCreateTypeDIE(Ty);
1026
1027   // Set up proxy.
1028   Entry = createDIEEntry(Buffer);
1029   insertDIEEntry(Ty, Entry);
1030   addDIEEntry(Entity, Attribute, Entry);
1031 }
1032
1033 // Accelerator table mutators - add each name along with its companion
1034 // DIE to the proper table while ensuring that the name that we're going
1035 // to reference is in the string table. We do this since the names we
1036 // add may not only be identical to the names in the DIE.
1037 void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
1038   if (!DD->useDwarfAccelTables())
1039     return;
1040   DU->getStringPoolEntry(Name);
1041   std::vector<const DIE *> &DIEs = AccelNames[Name];
1042   DIEs.push_back(Die);
1043 }
1044
1045 void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
1046   if (!DD->useDwarfAccelTables())
1047     return;
1048   DU->getStringPoolEntry(Name);
1049   std::vector<const DIE *> &DIEs = AccelObjC[Name];
1050   DIEs.push_back(Die);
1051 }
1052
1053 void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
1054   if (!DD->useDwarfAccelTables())
1055     return;
1056   DU->getStringPoolEntry(Name);
1057   std::vector<const DIE *> &DIEs = AccelNamespace[Name];
1058   DIEs.push_back(Die);
1059 }
1060
1061 void DwarfUnit::addAccelType(StringRef Name,
1062                              std::pair<const DIE *, unsigned> Die) {
1063   if (!DD->useDwarfAccelTables())
1064     return;
1065   DU->getStringPoolEntry(Name);
1066   std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
1067   DIEs.push_back(Die);
1068 }
1069
1070 /// addGlobalName - Add a new global name to the compile unit.
1071 void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
1072   if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1073     return;
1074   std::string FullName = getParentContextString(Context) + Name.str();
1075   GlobalNames[FullName] = Die;
1076 }
1077
1078 /// getParentContextString - Walks the metadata parent chain in a language
1079 /// specific manner (using the compile unit language) and returns
1080 /// it as a string. This is done at the metadata level because DIEs may
1081 /// not currently have been added to the parent context and walking the
1082 /// DIEs looking for names is more expensive than walking the metadata.
1083 std::string DwarfUnit::getParentContextString(DIScope Context) const {
1084   if (!Context)
1085     return "";
1086
1087   // FIXME: Decide whether to implement this for non-C++ languages.
1088   if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1089     return "";
1090
1091   std::string CS;
1092   SmallVector<DIScope, 1> Parents;
1093   while (!Context.isCompileUnit()) {
1094     Parents.push_back(Context);
1095     if (Context.getContext())
1096       Context = resolve(Context.getContext());
1097     else
1098       // Structure, etc types will have a NULL context if they're at the top
1099       // level.
1100       break;
1101   }
1102
1103   // Reverse iterate over our list to go from the outermost construct to the
1104   // innermost.
1105   for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1106                                                   E = Parents.rend();
1107        I != E; ++I) {
1108     DIScope Ctx = *I;
1109     StringRef Name = Ctx.getName();
1110     if (!Name.empty()) {
1111       CS += Name;
1112       CS += "::";
1113     }
1114   }
1115   return CS;
1116 }
1117
1118 /// constructTypeDIE - Construct basic type die from DIBasicType.
1119 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1120   // Get core information.
1121   StringRef Name = BTy.getName();
1122   // Add name if not anonymous or intermediate type.
1123   if (!Name.empty())
1124     addString(&Buffer, dwarf::DW_AT_name, Name);
1125
1126   // An unspecified type only has a name attribute.
1127   if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
1128     return;
1129
1130   addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1131           BTy.getEncoding());
1132
1133   uint64_t Size = BTy.getSizeInBits() >> 3;
1134   addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1135 }
1136
1137 /// constructTypeDIE - Construct derived type die from DIDerivedType.
1138 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1139   // Get core information.
1140   StringRef Name = DTy.getName();
1141   uint64_t Size = DTy.getSizeInBits() >> 3;
1142   uint16_t Tag = Buffer.getTag();
1143
1144   // Map to main type, void will not have a type.
1145   DIType FromTy = resolve(DTy.getTypeDerivedFrom());
1146   if (FromTy)
1147     addType(&Buffer, FromTy);
1148
1149   // Add name if not anonymous or intermediate type.
1150   if (!Name.empty())
1151     addString(&Buffer, dwarf::DW_AT_name, Name);
1152
1153   // Add size if non-zero (derived types might be zero-sized.)
1154   if (Size && Tag != dwarf::DW_TAG_pointer_type)
1155     addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1156
1157   if (Tag == dwarf::DW_TAG_ptr_to_member_type)
1158     addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1159                 getOrCreateTypeDIE(resolve(DTy.getClassType())));
1160   // Add source line info if available and TyDesc is not a forward declaration.
1161   if (!DTy.isForwardDecl())
1162     addSourceLine(&Buffer, DTy);
1163 }
1164
1165 /// constructSubprogramArguments - Construct function argument DIEs.
1166 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
1167   for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1168     DIDescriptor Ty = Args.getElement(i);
1169     if (Ty.isUnspecifiedParameter()) {
1170       assert(i == N-1 && "Unspecified parameter must be the last argument");
1171       createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1172     } else {
1173       DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1174       addType(Arg, DIType(Ty));
1175       if (DIType(Ty).isArtificial())
1176         addFlag(Arg, dwarf::DW_AT_artificial);
1177     }
1178   }
1179 }
1180
1181 /// constructTypeDIE - Construct type DIE from DICompositeType.
1182 void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1183   // Add name if not anonymous or intermediate type.
1184   StringRef Name = CTy.getName();
1185
1186   uint64_t Size = CTy.getSizeInBits() >> 3;
1187   uint16_t Tag = Buffer.getTag();
1188
1189   switch (Tag) {
1190   case dwarf::DW_TAG_array_type:
1191     constructArrayTypeDIE(Buffer, CTy);
1192     break;
1193   case dwarf::DW_TAG_enumeration_type:
1194     constructEnumTypeDIE(Buffer, CTy);
1195     break;
1196   case dwarf::DW_TAG_subroutine_type: {
1197     // Add return type. A void return won't have a type.
1198     DIArray Elements = CTy.getTypeArray();
1199     DIType RTy(Elements.getElement(0));
1200     if (RTy)
1201       addType(&Buffer, RTy);
1202
1203     bool isPrototyped = true;
1204     if (Elements.getNumElements() == 2 &&
1205         Elements.getElement(1).isUnspecifiedParameter())
1206       isPrototyped = false;
1207
1208     constructSubprogramArguments(Buffer, Elements);
1209
1210     // Add prototype flag if we're dealing with a C language and the
1211     // function has been prototyped.
1212     uint16_t Language = getLanguage();
1213     if (isPrototyped &&
1214         (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1215          Language == dwarf::DW_LANG_ObjC))
1216       addFlag(&Buffer, dwarf::DW_AT_prototyped);
1217
1218     if (CTy.isLValueReference())
1219       addFlag(&Buffer, dwarf::DW_AT_reference);
1220
1221     if (CTy.isRValueReference())
1222       addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
1223   } break;
1224   case dwarf::DW_TAG_structure_type:
1225   case dwarf::DW_TAG_union_type:
1226   case dwarf::DW_TAG_class_type: {
1227     // Add elements to structure type.
1228     DIArray Elements = CTy.getTypeArray();
1229     for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1230       DIDescriptor Element = Elements.getElement(i);
1231       DIE *ElemDie = NULL;
1232       if (Element.isSubprogram())
1233         ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1234       else if (Element.isDerivedType()) {
1235         DIDerivedType DDTy(Element);
1236         if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1237           ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1238           addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
1239                   dwarf::DW_AT_friend);
1240         } else if (DDTy.isStaticMember()) {
1241           getOrCreateStaticMemberDIE(DDTy);
1242         } else {
1243           constructMemberDIE(Buffer, DDTy);
1244         }
1245       } else if (Element.isObjCProperty()) {
1246         DIObjCProperty Property(Element);
1247         ElemDie = createAndAddDIE(Property.getTag(), Buffer);
1248         StringRef PropertyName = Property.getObjCPropertyName();
1249         addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1250         if (Property.getType())
1251           addType(ElemDie, Property.getType());
1252         addSourceLine(ElemDie, Property);
1253         StringRef GetterName = Property.getObjCPropertyGetterName();
1254         if (!GetterName.empty())
1255           addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1256         StringRef SetterName = Property.getObjCPropertySetterName();
1257         if (!SetterName.empty())
1258           addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1259         unsigned PropertyAttributes = 0;
1260         if (Property.isReadOnlyObjCProperty())
1261           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1262         if (Property.isReadWriteObjCProperty())
1263           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1264         if (Property.isAssignObjCProperty())
1265           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1266         if (Property.isRetainObjCProperty())
1267           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1268         if (Property.isCopyObjCProperty())
1269           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1270         if (Property.isNonAtomicObjCProperty())
1271           PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1272         if (PropertyAttributes)
1273           addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
1274                   PropertyAttributes);
1275
1276         DIEEntry *Entry = getDIEEntry(Element);
1277         if (!Entry) {
1278           Entry = createDIEEntry(ElemDie);
1279           insertDIEEntry(Element, Entry);
1280         }
1281       } else
1282         continue;
1283     }
1284
1285     if (CTy.isAppleBlockExtension())
1286       addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1287
1288     DICompositeType ContainingType(resolve(CTy.getContainingType()));
1289     if (ContainingType)
1290       addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1291                   getOrCreateTypeDIE(ContainingType));
1292
1293     if (CTy.isObjcClassComplete())
1294       addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1295
1296     // Add template parameters to a class, structure or union types.
1297     // FIXME: The support isn't in the metadata for this yet.
1298     if (Tag == dwarf::DW_TAG_class_type ||
1299         Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1300       addTemplateParams(Buffer, CTy.getTemplateParams());
1301
1302     break;
1303   }
1304   default:
1305     break;
1306   }
1307
1308   // Add name if not anonymous or intermediate type.
1309   if (!Name.empty())
1310     addString(&Buffer, dwarf::DW_AT_name, Name);
1311
1312   if (Tag == dwarf::DW_TAG_enumeration_type ||
1313       Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1314       Tag == dwarf::DW_TAG_union_type) {
1315     // Add size if non-zero (derived types might be zero-sized.)
1316     // TODO: Do we care about size for enum forward declarations?
1317     if (Size)
1318       addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1319     else if (!CTy.isForwardDecl())
1320       // Add zero size if it is not a forward declaration.
1321       addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
1322
1323     // If we're a forward decl, say so.
1324     if (CTy.isForwardDecl())
1325       addFlag(&Buffer, dwarf::DW_AT_declaration);
1326
1327     // Add source line info if available.
1328     if (!CTy.isForwardDecl())
1329       addSourceLine(&Buffer, CTy);
1330
1331     // No harm in adding the runtime language to the declaration.
1332     unsigned RLang = CTy.getRunTimeLang();
1333     if (RLang)
1334       addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1335               RLang);
1336   }
1337 }
1338
1339 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
1340 /// DITemplateTypeParameter.
1341 void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1342                                                   DITemplateTypeParameter TP) {
1343   DIE *ParamDIE =
1344       createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1345   // Add the type if it exists, it could be void and therefore no type.
1346   if (TP.getType())
1347     addType(ParamDIE, resolve(TP.getType()));
1348   if (!TP.getName().empty())
1349     addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1350 }
1351
1352 /// constructTemplateValueParameterDIE - Construct new DIE for the given
1353 /// DITemplateValueParameter.
1354 void
1355 DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1356                                               DITemplateValueParameter VP) {
1357   DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
1358
1359   // Add the type if there is one, template template and template parameter
1360   // packs will not have a type.
1361   if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
1362     addType(ParamDIE, resolve(VP.getType()));
1363   if (!VP.getName().empty())
1364     addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1365   if (Value *Val = VP.getValue()) {
1366     if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1367       addConstantValue(ParamDIE, CI,
1368                        isUnsignedDIType(DD, resolve(VP.getType())));
1369     else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1370       // For declaration non-type template parameters (such as global values and
1371       // functions)
1372       DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1373       addOpAddress(Loc, Asm->getSymbol(GV));
1374       // Emit DW_OP_stack_value to use the address as the immediate value of the
1375       // parameter, rather than a pointer to it.
1376       addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1377       addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1378     } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1379       assert(isa<MDString>(Val));
1380       addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1381                 cast<MDString>(Val)->getString());
1382     } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1383       assert(isa<MDNode>(Val));
1384       DIArray A(cast<MDNode>(Val));
1385       addTemplateParams(*ParamDIE, A);
1386     }
1387   }
1388 }
1389
1390 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1391 DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
1392   // Construct the context before querying for the existence of the DIE in case
1393   // such construction creates the DIE.
1394   DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
1395
1396   DIE *NDie = getDIE(NS);
1397   if (NDie)
1398     return NDie;
1399   NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1400
1401   if (!NS.getName().empty()) {
1402     addString(NDie, dwarf::DW_AT_name, NS.getName());
1403     addAccelNamespace(NS.getName(), NDie);
1404     addGlobalName(NS.getName(), NDie, NS.getContext());
1405   } else
1406     addAccelNamespace("(anonymous namespace)", NDie);
1407   addSourceLine(NDie, NS);
1408   return NDie;
1409 }
1410
1411 /// Unique C++ member function declarations based on their
1412 /// context and mangled name.
1413 DISubprogram
1414 DwarfUnit::getOdrUniqueSubprogram(DIScope Context, DISubprogram SP) const {
1415   if (!hasODR() ||
1416       !Context.isCompositeType() ||
1417       SP.getLinkageName().empty() ||
1418       SP.isDefinition())
1419     return SP;
1420   // Create a key with the UID of the parent class and this SP's name.
1421   Twine Key = SP.getContext().getName() + SP.getLinkageName();
1422   const MDNode *&Entry = DD->getOrCreateOdrMember(Key.str());
1423   if (!Entry)
1424     Entry = &*SP;
1425
1426   return DISubprogram(Entry);
1427 }
1428
1429 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1430 DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1431   // Construct the context before querying for the existence of the DIE in case
1432   // such construction creates the DIE (as is the case for member function
1433   // declarations).
1434   DIScope Context = resolve(SP.getContext());
1435   DIE *ContextDIE = getOrCreateContextDIE(Context);
1436   // Unique declarations based on the ODR, where applicable.
1437   SP = getOdrUniqueSubprogram(Context, SP);
1438
1439   DIE *SPDie = getDIE(SP);
1440   if (SPDie)
1441     return SPDie;
1442
1443   DISubprogram SPDecl = SP.getFunctionDeclaration();
1444   if (SPDecl.isSubprogram())
1445     // Add subprogram definitions to the CU die directly.
1446     ContextDIE = UnitDie.get();
1447
1448   // DW_TAG_inlined_subroutine may refer to this DIE.
1449   SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1450
1451   DIE *DeclDie = NULL;
1452   if (SPDecl.isSubprogram())
1453     DeclDie = getOrCreateSubprogramDIE(SPDecl);
1454
1455   // Add function template parameters.
1456   addTemplateParams(*SPDie, SP.getTemplateParams());
1457
1458   // If this DIE is going to refer declaration info using AT_specification
1459   // then there is no need to add other attributes.
1460   if (DeclDie) {
1461     // Refer function declaration directly.
1462     addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
1463
1464     return SPDie;
1465   }
1466
1467   // Add the linkage name if we have one.
1468   StringRef LinkageName = SP.getLinkageName();
1469   if (!LinkageName.empty())
1470     addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1471               GlobalValue::getRealLinkageName(LinkageName));
1472
1473   // Constructors and operators for anonymous aggregates do not have names.
1474   if (!SP.getName().empty())
1475     addString(SPDie, dwarf::DW_AT_name, SP.getName());
1476
1477   addSourceLine(SPDie, SP);
1478
1479   // Add the prototype if we have a prototype and we have a C like
1480   // language.
1481   uint16_t Language = getLanguage();
1482   if (SP.isPrototyped() &&
1483       (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1484        Language == dwarf::DW_LANG_ObjC))
1485     addFlag(SPDie, dwarf::DW_AT_prototyped);
1486
1487   DICompositeType SPTy = SP.getType();
1488   assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1489          "the type of a subprogram should be a subroutine");
1490
1491   DIArray Args = SPTy.getTypeArray();
1492   // Add a return type. If this is a type like a C/C++ void type we don't add a
1493   // return type.
1494   if (Args.getElement(0))
1495     addType(SPDie, DIType(Args.getElement(0)));
1496
1497   unsigned VK = SP.getVirtuality();
1498   if (VK) {
1499     addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1500     DIELoc *Block = getDIELoc();
1501     addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1502     addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1503     addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1504     ContainingTypeMap.insert(
1505         std::make_pair(SPDie, resolve(SP.getContainingType())));
1506   }
1507
1508   if (!SP.isDefinition()) {
1509     addFlag(SPDie, dwarf::DW_AT_declaration);
1510
1511     // Add arguments. Do not add arguments for subprogram definition. They will
1512     // be handled while processing variables.
1513     constructSubprogramArguments(*SPDie, Args);
1514   }
1515
1516   if (SP.isArtificial())
1517     addFlag(SPDie, dwarf::DW_AT_artificial);
1518
1519   if (!SP.isLocalToUnit())
1520     addFlag(SPDie, dwarf::DW_AT_external);
1521
1522   if (SP.isOptimized())
1523     addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1524
1525   if (unsigned isa = Asm->getISAEncoding()) {
1526     addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1527   }
1528
1529   if (SP.isLValueReference())
1530     addFlag(SPDie, dwarf::DW_AT_reference);
1531
1532   if (SP.isRValueReference())
1533     addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1534
1535   if (SP.isProtected())
1536     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1537             dwarf::DW_ACCESS_protected);
1538   else if (SP.isPrivate())
1539     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1540             dwarf::DW_ACCESS_private);
1541   else
1542     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1543             dwarf::DW_ACCESS_public);
1544
1545   if (SP.isExplicit())
1546     addFlag(SPDie, dwarf::DW_AT_explicit);
1547
1548   return SPDie;
1549 }
1550
1551 // Return const expression if value is a GEP to access merged global
1552 // constant. e.g.
1553 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1554 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1555   const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1556   if (!CE || CE->getNumOperands() != 3 ||
1557       CE->getOpcode() != Instruction::GetElementPtr)
1558     return NULL;
1559
1560   // First operand points to a global struct.
1561   Value *Ptr = CE->getOperand(0);
1562   if (!isa<GlobalValue>(Ptr) ||
1563       !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1564     return NULL;
1565
1566   // Second operand is zero.
1567   const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1568   if (!CI || !CI->isZero())
1569     return NULL;
1570
1571   // Third operand is offset.
1572   if (!isa<ConstantInt>(CE->getOperand(2)))
1573     return NULL;
1574
1575   return CE;
1576 }
1577
1578 /// createGlobalVariableDIE - create global variable DIE.
1579 void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
1580   // Check for pre-existence.
1581   if (getDIE(GV))
1582     return;
1583
1584   assert(GV.isGlobalVariable());
1585
1586   DIScope GVContext = GV.getContext();
1587   DIType GTy = GV.getType();
1588
1589   // If this is a static data member definition, some attributes belong
1590   // to the declaration DIE.
1591   DIE *VariableDIE = NULL;
1592   bool IsStaticMember = false;
1593   DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1594   if (SDMDecl.Verify()) {
1595     assert(SDMDecl.isStaticMember() && "Expected static member decl");
1596     // We need the declaration DIE that is in the static member's class.
1597     VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
1598     IsStaticMember = true;
1599   }
1600
1601   // If this is not a static data member definition, create the variable
1602   // DIE and add the initial set of attributes to it.
1603   if (!VariableDIE) {
1604     // Construct the context before querying for the existence of the DIE in
1605     // case such construction creates the DIE.
1606     DIE *ContextDIE = getOrCreateContextDIE(GVContext);
1607
1608     // Add to map.
1609     VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
1610
1611     // Add name and type.
1612     addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1613     addType(VariableDIE, GTy);
1614
1615     // Add scoping info.
1616     if (!GV.isLocalToUnit())
1617       addFlag(VariableDIE, dwarf::DW_AT_external);
1618
1619     // Add line number info.
1620     addSourceLine(VariableDIE, GV);
1621   }
1622
1623   // Add location.
1624   bool addToAccelTable = false;
1625   DIE *VariableSpecDIE = NULL;
1626   bool isGlobalVariable = GV.getGlobal() != NULL;
1627   if (isGlobalVariable) {
1628     addToAccelTable = true;
1629     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1630     const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
1631     if (GV.getGlobal()->isThreadLocal()) {
1632       // FIXME: Make this work with -gsplit-dwarf.
1633       unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1634       assert((PointerSize == 4 || PointerSize == 8) &&
1635              "Add support for other sizes if necessary");
1636       // Based on GCC's support for TLS:
1637       if (!DD->useSplitDwarf()) {
1638         // 1) Start with a constNu of the appropriate pointer size
1639         addUInt(Loc, dwarf::DW_FORM_data1,
1640                 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1641         // 2) containing the (relocated) offset of the TLS variable
1642         //    within the module's TLS block.
1643         addExpr(Loc, dwarf::DW_FORM_udata,
1644                 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
1645       } else {
1646         addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1647         addUInt(Loc, dwarf::DW_FORM_udata,
1648                 DU->getAddrPoolIndex(Sym, /* TLS */ true));
1649       }
1650       // 3) followed by a custom OP to make the debugger do a TLS lookup.
1651       addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1652     } else {
1653       DD->addArangeLabel(SymbolCU(this, Sym));
1654       addOpAddress(Loc, Sym);
1655     }
1656     // Do not create specification DIE if context is either compile unit
1657     // or a subprogram.
1658     if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1659         !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
1660       // Create specification DIE.
1661       VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
1662       addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1663       addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
1664       // A static member's declaration is already flagged as such.
1665       if (!SDMDecl.Verify())
1666         addFlag(VariableDIE, dwarf::DW_AT_declaration);
1667     } else {
1668       addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
1669     }
1670     // Add the linkage name.
1671     StringRef LinkageName = GV.getLinkageName();
1672     if (!LinkageName.empty())
1673       // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1674       // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1675       // TAG_variable.
1676       addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1677                                                   : VariableDIE,
1678                 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1679                                            : dwarf::DW_AT_MIPS_linkage_name,
1680                 GlobalValue::getRealLinkageName(LinkageName));
1681   } else if (const ConstantInt *CI =
1682                  dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1683     // AT_const_value was added when the static member was created. To avoid
1684     // emitting AT_const_value multiple times, we only add AT_const_value when
1685     // it is not a static member.
1686     if (!IsStaticMember)
1687       addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
1688   } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
1689     addToAccelTable = true;
1690     // GV is a merged global.
1691     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1692     Value *Ptr = CE->getOperand(0);
1693     MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1694     DD->addArangeLabel(SymbolCU(this, Sym));
1695     addOpAddress(Loc, Sym);
1696     addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1697     SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
1698     addUInt(Loc, dwarf::DW_FORM_udata,
1699             Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1700     addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1701     addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
1702   }
1703
1704   if (addToAccelTable) {
1705     DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1706     addAccelName(GV.getName(), AddrDIE);
1707
1708     // If the linkage name is different than the name, go ahead and output
1709     // that as well into the name table.
1710     if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1711       addAccelName(GV.getLinkageName(), AddrDIE);
1712   }
1713
1714   if (!GV.isLocalToUnit())
1715     addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1716                   GV.getContext());
1717 }
1718
1719 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1720 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1721   DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1722   addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
1723
1724   // The LowerBound value defines the lower bounds which is typically zero for
1725   // C/C++. The Count value is the number of elements.  Values are 64 bit. If
1726   // Count == -1 then the array is unbounded and we do not emit
1727   // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1728   // Count == 0, then the array has zero elements in which case we do not emit
1729   // an upper bound.
1730   int64_t LowerBound = SR.getLo();
1731   int64_t DefaultLowerBound = getDefaultLowerBound();
1732   int64_t Count = SR.getCount();
1733
1734   if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1735     addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1736
1737   if (Count != -1 && Count != 0)
1738     // FIXME: An unbounded array should reference the expression that defines
1739     // the array.
1740     addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1741             LowerBound + Count - 1);
1742 }
1743
1744 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1745 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1746   if (CTy.isVector())
1747     addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1748
1749   // Emit the element type.
1750   addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
1751
1752   // Get an anonymous type for index type.
1753   // FIXME: This type should be passed down from the front end
1754   // as different languages may have different sizes for indexes.
1755   DIE *IdxTy = getIndexTyDie();
1756   if (!IdxTy) {
1757     // Construct an anonymous type for index type.
1758     IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
1759     addString(IdxTy, dwarf::DW_AT_name, "int");
1760     addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
1761     addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1762             dwarf::DW_ATE_signed);
1763     setIndexTyDie(IdxTy);
1764   }
1765
1766   // Add subranges to array type.
1767   DIArray Elements = CTy.getTypeArray();
1768   for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1769     DIDescriptor Element = Elements.getElement(i);
1770     if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1771       constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1772   }
1773 }
1774
1775 /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1776 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1777   DIArray Elements = CTy.getTypeArray();
1778
1779   // Add enumerators to enumeration type.
1780   for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1781     DIEnumerator Enum(Elements.getElement(i));
1782     if (Enum.isEnumerator()) {
1783       DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1784       StringRef Name = Enum.getName();
1785       addString(Enumerator, dwarf::DW_AT_name, Name);
1786       int64_t Value = Enum.getEnumValue();
1787       addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1788               Value);
1789     }
1790   }
1791   DIType DTy = resolve(CTy.getTypeDerivedFrom());
1792   if (DTy) {
1793     addType(&Buffer, DTy);
1794     addFlag(&Buffer, dwarf::DW_AT_enum_class);
1795   }
1796 }
1797
1798 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1799 /// vtables.
1800 void DwarfUnit::constructContainingTypeDIEs() {
1801   for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1802                                                  CE = ContainingTypeMap.end();
1803        CI != CE; ++CI) {
1804     DIE *SPDie = CI->first;
1805     DIDescriptor D(CI->second);
1806     if (!D)
1807       continue;
1808     DIE *NDie = getDIE(D);
1809     if (!NDie)
1810       continue;
1811     addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
1812   }
1813 }
1814
1815 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1816 DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1817   StringRef Name = DV.getName();
1818
1819   // Define variable debug information entry.
1820   DIE *VariableDie = new DIE(DV.getTag());
1821   DbgVariable *AbsVar = DV.getAbstractVariable();
1822   DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1823   if (AbsDIE)
1824     addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
1825   else {
1826     if (!Name.empty())
1827       addString(VariableDie, dwarf::DW_AT_name, Name);
1828     addSourceLine(VariableDie, DV.getVariable());
1829     addType(VariableDie, DV.getType());
1830   }
1831
1832   if (DV.isArtificial())
1833     addFlag(VariableDie, dwarf::DW_AT_artificial);
1834
1835   if (isScopeAbstract) {
1836     DV.setDIE(VariableDie);
1837     return VariableDie;
1838   }
1839
1840   // Add variable address.
1841
1842   unsigned Offset = DV.getDotDebugLocOffset();
1843   if (Offset != ~0U) {
1844     addLocationList(VariableDie, dwarf::DW_AT_location, Offset);
1845     DV.setDIE(VariableDie);
1846     return VariableDie;
1847   }
1848
1849   // Check if variable is described by a DBG_VALUE instruction.
1850   if (const MachineInstr *DVInsn = DV.getMInsn()) {
1851     assert(DVInsn->getNumOperands() == 3);
1852     if (DVInsn->getOperand(0).isReg()) {
1853       const MachineOperand RegOp = DVInsn->getOperand(0);
1854       // If the second operand is an immediate, this is an indirect value.
1855       if (DVInsn->getOperand(1).isImm()) {
1856         MachineLocation Location(RegOp.getReg(),
1857                                  DVInsn->getOperand(1).getImm());
1858         addVariableAddress(DV, VariableDie, Location);
1859       } else if (RegOp.getReg())
1860         addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
1861     } else if (DVInsn->getOperand(0).isImm())
1862       addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
1863     else if (DVInsn->getOperand(0).isFPImm())
1864       addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1865     else if (DVInsn->getOperand(0).isCImm())
1866       addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1867                        isUnsignedDIType(DD, DV.getType()));
1868
1869     DV.setDIE(VariableDie);
1870     return VariableDie;
1871   } else {
1872     // .. else use frame index.
1873     int FI = DV.getFrameIndex();
1874     if (FI != ~0) {
1875       unsigned FrameReg = 0;
1876       const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1877       int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1878       MachineLocation Location(FrameReg, Offset);
1879       addVariableAddress(DV, VariableDie, Location);
1880     }
1881   }
1882
1883   DV.setDIE(VariableDie);
1884   return VariableDie;
1885 }
1886
1887 /// constructMemberDIE - Construct member DIE from DIDerivedType.
1888 void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
1889   DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
1890   StringRef Name = DT.getName();
1891   if (!Name.empty())
1892     addString(MemberDie, dwarf::DW_AT_name, Name);
1893
1894   addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
1895
1896   addSourceLine(MemberDie, DT);
1897
1898   if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
1899
1900     // For C++, virtual base classes are not at fixed offset. Use following
1901     // expression to extract appropriate offset from vtable.
1902     // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1903
1904     DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
1905     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1906     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1907     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1908     addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1909     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1910     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1911     addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1912
1913     addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1914   } else {
1915     uint64_t Size = DT.getSizeInBits();
1916     uint64_t FieldSize = getBaseTypeSize(DD, DT);
1917     uint64_t OffsetInBytes;
1918
1919     if (Size != FieldSize) {
1920       // Handle bitfield, assume bytes are 8 bits.
1921       addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1922       addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1923
1924       uint64_t Offset = DT.getOffsetInBits();
1925       uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1926       uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1927       uint64_t FieldOffset = (HiMark - FieldSize);
1928       Offset -= FieldOffset;
1929
1930       // Maybe we need to work from the other end.
1931       if (Asm->getDataLayout().isLittleEndian())
1932         Offset = FieldSize - (Offset + Size);
1933       addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1934
1935       // Here DW_AT_data_member_location points to the anonymous
1936       // field that includes this bit field.
1937       OffsetInBytes = FieldOffset >> 3;
1938     } else
1939       // This is not a bitfield.
1940       OffsetInBytes = DT.getOffsetInBits() >> 3;
1941
1942     if (DD->getDwarfVersion() <= 2) {
1943       DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
1944       addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1945       addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1946       addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1947     } else
1948       addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1949               OffsetInBytes);
1950   }
1951
1952   if (DT.isProtected())
1953     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1954             dwarf::DW_ACCESS_protected);
1955   else if (DT.isPrivate())
1956     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1957             dwarf::DW_ACCESS_private);
1958   // Otherwise C++ member and base classes are considered public.
1959   else
1960     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1961             dwarf::DW_ACCESS_public);
1962   if (DT.isVirtual())
1963     addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1964             dwarf::DW_VIRTUALITY_virtual);
1965
1966   // Objective-C properties.
1967   if (MDNode *PNode = DT.getObjCProperty())
1968     if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1969       MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1970                           PropertyDie);
1971
1972   if (DT.isArtificial())
1973     addFlag(MemberDie, dwarf::DW_AT_artificial);
1974 }
1975
1976 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
1977 DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
1978   if (!DT.Verify())
1979     return NULL;
1980
1981   // Construct the context before querying for the existence of the DIE in case
1982   // such construction creates the DIE.
1983   DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
1984   assert(dwarf::isType(ContextDIE->getTag()) &&
1985          "Static member should belong to a type.");
1986
1987   DIE *StaticMemberDIE = getDIE(DT);
1988   if (StaticMemberDIE)
1989     return StaticMemberDIE;
1990
1991   StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
1992
1993   DIType Ty = resolve(DT.getTypeDerivedFrom());
1994
1995   addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1996   addType(StaticMemberDIE, Ty);
1997   addSourceLine(StaticMemberDIE, DT);
1998   addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1999   addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2000
2001   // FIXME: We could omit private if the parent is a class_type, and
2002   // public if the parent is something else.
2003   if (DT.isProtected())
2004     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2005             dwarf::DW_ACCESS_protected);
2006   else if (DT.isPrivate())
2007     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2008             dwarf::DW_ACCESS_private);
2009   else
2010     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2011             dwarf::DW_ACCESS_public);
2012
2013   if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
2014     addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
2015   if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2016     addConstantFPValue(StaticMemberDIE, CFP);
2017
2018   return StaticMemberDIE;
2019 }
2020
2021 void DwarfUnit::emitHeader(const MCSection *ASection,
2022                            const MCSymbol *ASectionSym) const {
2023   Asm->OutStreamer.AddComment("DWARF version number");
2024   Asm->EmitInt16(DD->getDwarfVersion());
2025   Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2026   // We share one abbreviations table across all units so it's always at the
2027   // start of the section. Use a relocatable offset where needed to ensure
2028   // linking doesn't invalidate that offset.
2029   Asm->EmitSectionOffset(ASectionSym, ASectionSym);
2030   Asm->OutStreamer.AddComment("Address Size (in bytes)");
2031   Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2032 }
2033
2034 void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2035   // Define start line table label for each Compile Unit.
2036   MCSymbol *LineTableStartSym =
2037       Asm->GetTempSymbol("line_table_start", getUniqueID());
2038   Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
2039                                                      getUniqueID());
2040
2041   // Use a single line table if we are generating assembly.
2042   bool UseTheFirstCU =
2043       Asm->OutStreamer.hasRawTextSupport() || (getUniqueID() == 0);
2044
2045   stmtListIndex = UnitDie->getValues().size();
2046
2047   // DW_AT_stmt_list is a offset of line number information for this
2048   // compile unit in debug_line section. For split dwarf this is
2049   // left in the skeleton CU and so not included.
2050   // The line table entries are not always emitted in assembly, so it
2051   // is not okay to use line_table_start here.
2052   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2053     addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list,
2054                     UseTheFirstCU ? DwarfLineSectionSym : LineTableStartSym);
2055   else if (UseTheFirstCU)
2056     addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
2057   else
2058     addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2059                     DwarfLineSectionSym);
2060 }
2061
2062 void DwarfCompileUnit::applyStmtList(DIE &D) {
2063   D.addValue(dwarf::DW_AT_stmt_list,
2064              UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2065              UnitDie->getValues()[stmtListIndex]);
2066 }
2067
2068 void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2069                                const MCSymbol *ASectionSym) const {
2070   DwarfUnit::emitHeader(ASection, ASectionSym);
2071   Asm->OutStreamer.AddComment("Type Signature");
2072   Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2073   Asm->OutStreamer.AddComment("Type DIE Offset");
2074   // In a skeleton type unit there is no type DIE so emit a zero offset.
2075   Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2076                                 sizeof(Ty->getOffset()));
2077 }
2078
2079 void DwarfTypeUnit::initSection(const MCSection *Section) {
2080   assert(!this->Section);
2081   this->Section = Section;
2082   // Since each type unit is contained in its own COMDAT section, the begin
2083   // label and the section label are the same. Using the begin label emission in
2084   // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2085   // the only other alternative of lazily constructing start-of-section labels
2086   // and storing a mapping in DwarfDebug (or AsmPrinter).
2087   this->SectionSym = this->LabelBegin =
2088       Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2089   this->LabelEnd =
2090       Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2091   this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2092 }