[lld] Introduce registry and Reference kind tuple

The main changes are in:
  include/lld/Core/Reference.h
  include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.

1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers.  It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.

The new model is that there is a global Registry object. You programmatically 
register the Readers you want with the registry object. Whenever you need to 
read/parse a file, you ask the registry to do it, and the registry tries each 
registered reader.

For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object. 


2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings.  Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values.  The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value.  This tuple system allows conversion to and from strings with 
no ambiguities.

llvm-svn: 197727
diff --git a/lld/lib/ReaderWriter/Reader.cpp b/lld/lib/ReaderWriter/Reader.cpp
index a3c939f4..69c5c34 100644
--- a/lld/lib/ReaderWriter/Reader.cpp
+++ b/lld/lib/ReaderWriter/Reader.cpp
@@ -11,10 +11,94 @@
 
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/system_error.h"
 
 namespace lld {
+
 Reader::~Reader() {
 }
+
+void Registry::add(std::unique_ptr<Reader> reader) {
+  _readers.push_back(std::move(reader));
+}
+
+error_code 
+Registry::parseFile(std::unique_ptr<MemoryBuffer> &mb,
+                          std::vector<std::unique_ptr<File>> &result) const {
+  // Get file type.
+  StringRef content(mb->getBufferStart(), mb->getBufferSize());
+  llvm::sys::fs::file_magic fileType = llvm::sys::fs::identify_magic(content);
+  // Get file extension.
+  StringRef extension = llvm::sys::path::extension(mb->getBufferIdentifier());
+
+  // Ask each registered reader if it can handle this file type or extension.
+  for (const std::unique_ptr<Reader> &reader : _readers) {
+    if (reader->canParse(fileType, extension, *mb))
+      return reader->parseFile(mb, *this, result);
+  }
+  
+  // No Reader could parse this file.     
+  return llvm::make_error_code(llvm::errc::executable_format_error);
+}
+
+static const Registry::KindStrings kindStrings[] = {
+  { Reference::kindInGroup,       "in-group" },
+  { Reference::kindLayoutAfter,   "layout-after" },
+  { Reference::kindLayoutBefore,  "layout-before" },
+  LLD_KIND_STRING_END
+};
+
+Registry::Registry() {
+  addKindTable(Reference::KindNamespace::all, Reference::KindArch::all, 
+                                                                  kindStrings);
+}
+
+void Registry::addKindTable(Reference::KindNamespace ns, 
+                            Reference::KindArch arch,
+                            const KindStrings array[]) {
+  KindEntry entry = {ns, arch, array};
+  _kindEntries.push_back(entry);
+}
+
+bool Registry::referenceKindFromString(StringRef inputStr, 
+                                       Reference::KindNamespace &ns,
+                                       Reference::KindArch &arch, 
+                                       Reference::KindValue &value) const {
+  for (const KindEntry &entry : _kindEntries) {
+    for (const KindStrings *pair=entry.array; !pair->name.empty(); ++pair) {
+      if (!inputStr.equals(pair->name))
+        continue;
+      ns = entry.ns;
+      arch = entry.arch;
+      value = pair->value;
+      return true;
+    }
+  }
+  return false;
+}
+
+
+bool Registry::referenceKindToString(Reference::KindNamespace ns, 
+                                     Reference::KindArch arch, 
+                                     Reference::KindValue value, 
+                                     StringRef &str) const {
+  for (const KindEntry &entry : _kindEntries) {
+    if (entry.ns != ns)
+      continue;
+    if (entry.arch != arch)
+      continue;
+    for (const KindStrings *pair=entry.array; !pair->name.empty(); ++pair) {
+      if (pair->value != value)
+        continue;
+      str = pair->name;
+      return true;
+    }
+  }
+  return false;
+}
+
+
 } // end namespace lld