Added first bit of support for the dwarf .file directive. This patch collects
[oota-llvm.git] / lib / MC / MCContext.cpp
index 11370642530a1947e8a8ca127dae9540b62cacfe..7470e8d0031c39e9c478441aaf55492ca849d13c 100644 (file)
@@ -14,6 +14,7 @@
 #include "llvm/MC/MCSectionCOFF.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/MC/MCLabel.h"
+#include "llvm/MC/MCDwarf.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/Twine.h"
 using namespace llvm;
@@ -181,3 +182,63 @@ const MCSection *MCContext::getCOFFSection(StringRef Section,
   Entry.setValue(Result);
   return Result;
 }
+
+//===----------------------------------------------------------------------===//
+// Dwarf Management
+//===----------------------------------------------------------------------===//
+
+/// GetDwarfFile - takes a file name an number to place in the dwarf file and
+/// directory tables.  If the file number has already been allocated it is an
+/// error and zero is returned and the client reports the error, else the
+/// allocated file number is returned.  The file numbers may be in any order.
+unsigned MCContext::GetDwarfFile(StringRef FileName, unsigned FileNumber) {
+  // TODO: a FileNumber of zero says to use the next available file number.
+  // Note: in GenericAsmParser::ParseDirectiveFile() FileNumber was checked
+  // to not be less than one.  This needs to be change to be not less than zero.
+
+  // Make space for this FileNumber in the MCDwarfFiles vector if needed.
+  if (FileNumber >= MCDwarfFiles.size()) {
+    MCDwarfFiles.resize(FileNumber + 1);
+  } else {
+    MCDwarfFile *&ExistingFile = MCDwarfFiles[FileNumber];
+    if (ExistingFile)
+      // It is an error to use see the same number more than once.
+      return 0;
+  }
+
+  // Get the new MCDwarfFile slot for this FileNumber.
+  MCDwarfFile *&File = MCDwarfFiles[FileNumber];
+
+  // Separate the directory part from the basename of the FileName.
+  std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
+
+  // Find or make a entry in the MCDwarfDirs vector for this Directory.
+  StringRef Directory;
+  StringRef Name;
+  unsigned DirIndex;
+  // Capture directory name.
+  if (Slash.second.empty()) {
+    Name = Slash.first;
+    DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
+  } else {
+    Directory = Slash.first;
+    Name = Slash.second;
+    for (DirIndex = 1; DirIndex < MCDwarfDirs.size(); DirIndex++) {
+      std::string *&Dir = MCDwarfDirs[DirIndex];
+      if (Directory == *Dir)
+       break;
+    }
+    if (DirIndex >= MCDwarfDirs.size()) {
+      MCDwarfDirs.resize(DirIndex + 1);
+      std::string *&NewDir = MCDwarfDirs[DirIndex];
+      NewDir = new (*this) std::string(Directory);
+    }
+  }
+  
+  // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
+  // vector.
+  File = new (*this) MCDwarfFile(Name, DirIndex);
+
+  // return the allocated FileNumber.
+  return FileNumber;
+}