This patch is needed to make c++ exceptions work for mips16.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfAccelTable.cpp
index 7c93dbf5b9a55faa0379a8c1beba726f4753f593..6a172b981dc6536c7f169238133957bda1d1dedb 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#include "DwarfAccelTable.h"
+#include "DIE.h"
+#include "DwarfDebug.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/CodeGen/AsmPrinter.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/Support/Debug.h"
-#include "DwarfAccelTable.h"
-#include "DwarfDebug.h"
-#include "DIE.h"
 
 using namespace llvm;
 
 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
   switch (AT) {
-  default: llvm_unreachable("invalid AtomType!");
   case eAtomTypeNULL: return "eAtomTypeNULL";
   case eAtomTypeDIEOffset: return "eAtomTypeDIEOffset";
   case eAtomTypeCUOffset: return "eAtomTypeCUOffset";
@@ -32,35 +33,31 @@ const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
   case eAtomTypeNameFlags: return "eAtomTypeNameFlags";
   case eAtomTypeTypeFlags: return "eAtomTypeTypeFlags";
   } 
+  llvm_unreachable("invalid AtomType!");
 }
 
-// The general case would need to have a less hard coded size for the
-// length of the HeaderData, however, if we're constructing based on a
-// single Atom then we know it will always be: 4 + 4 + 2 + 2.
-DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
-  Header(12),
-  HeaderData(atom) {
-}
+// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
+DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList) :
+  Header(8 + (atomList.size() * 4)),
+  HeaderData(atomList),
+  Entries(Allocator) { }
 
-DwarfAccelTable::~DwarfAccelTable() {
-  for (size_t i = 0, e = Data.size() ; i < e; ++i)
-    delete Data[i];
-}
+DwarfAccelTable::~DwarfAccelTable() { }
 
-void DwarfAccelTable::AddName(StringRef Name, DIE* die) {
+void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
+  assert(Data.empty() && "Already finalized!");
   // If the string is in the list already then add this die to the list
   // otherwise add a new one.
-  DIEArray &DIEs = Entries[Name];
-  DIEs.push_back(die);
+  DataArray &DIEs = Entries[Name];
+  DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
 }
 
 void DwarfAccelTable::ComputeBucketCount(void) {
   // First get the number of unique hashes.
-  std::vector<uint32_t> uniques;
-  uniques.resize(Data.size());
+  std::vector<uint32_t> uniques(Data.size());
   for (size_t i = 0, e = Data.size(); i < e; ++i)
     uniques[i] = Data[i]->HashValue;
-  std::sort(uniques.begin(), uniques.end());
+  array_pod_sort(uniques.begin(), uniques.end());
   std::vector<uint32_t>::iterator p =
     std::unique(uniques.begin(), uniques.end());
   uint32_t num = std::distance(uniques.begin(), p);
@@ -73,15 +70,23 @@ void DwarfAccelTable::ComputeBucketCount(void) {
   Header.hashes_count = num;
 }
 
+// compareDIEs - comparison predicate that sorts DIEs by their offset.
+static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
+                        const DwarfAccelTable::HashDataContents *B) {
+  return A->Die->getOffset() < B->Die->getOffset();
+}
+
 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
   // Create the individual hash data outputs.
-  for (StringMap<DIEArray>::const_iterator
+  for (StringMap<DataArray>::iterator
          EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
-    struct HashData *Entry = new HashData((*EI).getKeyData());
-    for (DIEArray::const_iterator DI = (*EI).second.begin(),
-           DE = (*EI).second.end();
-         DI != DE; ++DI)
-      Entry->addOffset((*DI)->getOffset());
+
+    // Unique the entries.
+    std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
+    EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
+                       EI->second.end());
+
+    HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
     Data.push_back(Entry);
   }
 
@@ -128,8 +133,8 @@ void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
   }
 }
 
-// Walk through and emit the buckets for the table. This will look
-// like a list of numbers of how many elements are in each bucket.
+// Walk through and emit the buckets for the table. Each index is
+// an offset into the list of hashes.
 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
   unsigned index = 0;
   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
@@ -187,11 +192,18 @@ void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
       Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
                              D->getStringPool());
       Asm->OutStreamer.AddComment("Num DIEs");
-      Asm->EmitInt32((*HI)->DIEOffsets.size());
-      for (std::vector<uint32_t>::const_iterator
-             DI = (*HI)->DIEOffsets.begin(), DE = (*HI)->DIEOffsets.end();
+      Asm->EmitInt32((*HI)->Data.size());
+      for (ArrayRef<HashDataContents*>::const_iterator
+             DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
            DI != DE; ++DI) {
-        Asm->EmitInt32((*DI));
+        // Emit the DIE offset
+        Asm->EmitInt32((*DI)->Die->getOffset());
+        // If we have multiple Atoms emit that info too.
+        // FIXME: A bit of a hack, we either emit only one atom or all info.
+        if (HeaderData.Atoms.size() > 1) {
+          Asm->EmitInt16((*DI)->Die->getTag());
+          Asm->EmitInt8((*DI)->Flags);
+        }
       }
       // Emit a 0 to terminate the data unless we have a hash collision.
       if (PrevHash != (*HI)->HashValue)
@@ -227,11 +239,11 @@ void DwarfAccelTable::print(raw_ostream &O) {
   HeaderData.print(O);
 
   O << "Entries: \n";
-  for (StringMap<DIEArray>::const_iterator
+  for (StringMap<DataArray>::const_iterator
          EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
-    O << "Name: " << (*EI).getKeyData() << "\n";
-    for (DIEArray::const_iterator DI = (*EI).second.begin(),
-           DE = (*EI).second.end();
+    O << "Name: " << EI->getKeyData() << "\n";
+    for (DataArray::const_iterator DI = EI->second.begin(),
+           DE = EI->second.end();
          DI != DE; ++DI)
       (*DI)->print(O);
   }