Fixed a nasty layering violation in the edis source
code.  It used to #include the enhanced disassembly
information for the targets it supported straight
out of lib/Target/{X86,ARM,...} but now it uses a
new interface provided by MCDisassembler, and (so
far) implemented by X86 and ARM.

Also removed hacky #define-controlled initialization
of targets in edis.  If clients only want edis to
initialize a limited set of targets, they can set
--enable-targets on the configure command line.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101179 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/edis/EDDisassembler.cpp b/tools/edis/EDDisassembler.cpp
index a766d2f..b87fe62 100644
--- a/tools/edis/EDDisassembler.cpp
+++ b/tools/edis/EDDisassembler.cpp
@@ -18,6 +18,7 @@
 
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/MC/EDInstInfo.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCDisassembler.h"
@@ -39,47 +40,34 @@
 #include "llvm/Target/TargetRegisterInfo.h"
 #include "llvm/Target/TargetSelect.h"
 
-#ifdef EDIS_X86
-#include "../../lib/Target/X86/X86GenEDInfo.inc"
-#endif
-
-#ifdef EDIS_ARM
-#include "../../lib/Target/ARM/ARMGenEDInfo.inc"
-#endif
-
 using namespace llvm;
 
 bool EDDisassembler::sInitialized = false;
 EDDisassembler::DisassemblerMap_t EDDisassembler::sDisassemblers;
 
-struct InfoMap {
+struct TripleMap {
   Triple::ArchType Arch;
   const char *String;
-  const InstInfo *Info;
 };
 
-static struct InfoMap infomap[] = {
-#ifdef EDIS_X86
-  { Triple::x86,          "i386-unknown-unknown",   instInfoX86 },
-  { Triple::x86_64,       "x86_64-unknown-unknown", instInfoX86 },
-#endif
-#ifdef EDIS_ARM
-  { Triple::arm,          "arm-unknown-unknown",    instInfoARM },
-  { Triple::thumb,        "thumb-unknown-unknown",  instInfoARM },
-#endif
-  { Triple::InvalidArch,  NULL,                     NULL        }
+static struct TripleMap triplemap[] = {
+  { Triple::x86,          "i386-unknown-unknown"    },
+  { Triple::x86_64,       "x86_64-unknown-unknown"  },
+  { Triple::arm,          "arm-unknown-unknown"     },
+  { Triple::thumb,        "thumb-unknown-unknown"   },
+  { Triple::InvalidArch,  NULL,                     }
 };
 
-/// infoFromArch - Returns the InfoMap corresponding to a given architecture,
+/// infoFromArch - Returns the TripleMap corresponding to a given architecture,
 ///   or NULL if there is an error
 ///
 /// @arg arch - The Triple::ArchType for the desired architecture
-static const InfoMap *infoFromArch(Triple::ArchType arch) {
+static const char *tripleFromArch(Triple::ArchType arch) {
   unsigned int infoIndex;
   
-  for (infoIndex = 0; infomap[infoIndex].String != NULL; ++infoIndex) {
-    if (arch == infomap[infoIndex].Arch)
-      return &infomap[infoIndex];
+  for (infoIndex = 0; triplemap[infoIndex].String != NULL; ++infoIndex) {
+    if (arch == triplemap[infoIndex].Arch)
+      return triplemap[infoIndex].String;
   }
   
   return NULL;
@@ -115,25 +103,17 @@
   }
 }
 
-#define BRINGUP_TARGET(tgt)           \
-  LLVMInitialize##tgt##TargetInfo();  \
-  LLVMInitialize##tgt##Target();      \
-  LLVMInitialize##tgt##AsmPrinter();  \
-  LLVMInitialize##tgt##AsmParser();   \
-  LLVMInitialize##tgt##Disassembler();
-
 void EDDisassembler::initialize() {
   if (sInitialized)
     return;
   
   sInitialized = true;
   
-#ifdef EDIS_X86
-  BRINGUP_TARGET(X86)
-#endif
-#ifdef EDIS_ARM
-  BRINGUP_TARGET(ARM)
-#endif
+  InitializeAllTargetInfos();
+  InitializeAllTargets();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+  InitializeAllDisassemblers();
 }
 
 #undef BRINGUP_TARGET
@@ -175,14 +155,10 @@
   HasSemantics(false), 
   ErrorStream(nulls()), 
   Key(key) {
-  const InfoMap *infoMap = infoFromArch(key.Arch);
-  
-  if (!infoMap)
-    return;
+  const char *triple = tripleFromArch(key.Arch);
     
-  InstInfos = infoMap->Info;
-  
-  const char *triple = infoMap->String;
+  if (!triple)
+    return;
   
   LLVMSyntaxVariant = getLLVMSyntaxVariant(key.Arch, key.Syntax);
   
@@ -220,6 +196,8 @@
   
   if (!Disassembler)
     return;
+    
+  InstInfos = Disassembler->getEDInfo();
   
   InstString.reset(new std::string);
   InstStream.reset(new raw_string_ostream(*InstString));
@@ -231,8 +209,6 @@
   GenericAsmLexer.reset(new AsmLexer(*AsmInfo));
   SpecificAsmLexer.reset(Tgt->createAsmLexer(*AsmInfo));
   SpecificAsmLexer->InstallLexer(*GenericAsmLexer);
-                          
-  InstInfos = infoMap->Info;
   
   initMaps(*targetMachine->getRegisterInfo());
     
@@ -285,7 +261,7 @@
     delete inst;
     return NULL;
   } else {
-    const InstInfo *thisInstInfo;
+    const llvm::EDInstInfo *thisInstInfo;
 
     thisInstInfo = &InstInfos[inst->getOpcode()];
     
@@ -308,7 +284,6 @@
   switch (Key.Arch) {
   default:
     break;
-#ifdef EDIS_X86
   case Triple::x86:
   case Triple::x86_64:
     stackPointers.insert(registerIDWithName("SP"));
@@ -319,15 +294,12 @@
     programCounters.insert(registerIDWithName("EIP"));
     programCounters.insert(registerIDWithName("RIP"));
     break;
-#endif
-#ifdef EDIS_ARM
   case Triple::arm:
   case Triple::thumb:
     stackPointers.insert(registerIDWithName("SP"));
     
     programCounters.insert(registerIDWithName("PC"));
     break;  
-#endif
   }
 }
 
diff --git a/tools/edis/EDDisassembler.h b/tools/edis/EDDisassembler.h
index 9b27fe8..74a260e 100644
--- a/tools/edis/EDDisassembler.h
+++ b/tools/edis/EDDisassembler.h
@@ -48,6 +48,8 @@
 class SourceMgr;
 class Target;
 class TargetRegisterInfo;
+
+struct EDInstInfo;
 }
 
 /// EDDisassembler - Encapsulates a disassembler for a single architecture and
@@ -143,7 +145,7 @@
   llvm::sys::Mutex PrinterMutex;
   /// The array of instruction information provided by the TableGen backend for
   ///   the target architecture
-  const InstInfo *InstInfos;
+  const llvm::EDInstInfo *InstInfos;
   /// The target-specific lexer for use in tokenizing strings, in
   ///   target-independent and target-specific portions
   llvm::OwningPtr<llvm::AsmLexer> GenericAsmLexer;
diff --git a/tools/edis/EDInst.cpp b/tools/edis/EDInst.cpp
index de40770..af3a54a 100644
--- a/tools/edis/EDInst.cpp
+++ b/tools/edis/EDInst.cpp
@@ -18,6 +18,7 @@
 #include "EDOperand.h"
 #include "EDToken.h"
 
+#include "llvm/MC/EDInstInfo.h"
 #include "llvm/MC/MCInst.h"
 
 using namespace llvm;
@@ -25,7 +26,7 @@
 EDInst::EDInst(llvm::MCInst *inst,
                uint64_t byteSize, 
                EDDisassembler &disassembler,
-               const InstInfo *info) :
+               const llvm::EDInstInfo *info) :
   Disassembler(disassembler),
   Inst(inst),
   ThisInstInfo(info),
diff --git a/tools/edis/EDInst.h b/tools/edis/EDInst.h
index db03a78..c8a747f 100644
--- a/tools/edis/EDInst.h
+++ b/tools/edis/EDInst.h
@@ -23,6 +23,10 @@
 #include <string>
 #include <vector>
 
+namespace llvm {
+  struct EDInstInfo;
+}
+
 /// CachedResult - Encapsulates the result of a function along with the validity
 ///   of that result, so that slow functions don't need to run twice
 struct CachedResult {
@@ -54,7 +58,7 @@
   /// The containing MCInst
   llvm::MCInst *Inst;
   /// The instruction information provided by TableGen for this instruction
-  const InstInfo *ThisInstInfo;
+  const llvm::EDInstInfo *ThisInstInfo;
   /// The number of bytes for the machine code representation of the instruction
   uint64_t ByteSize;
   
@@ -95,7 +99,7 @@
   EDInst(llvm::MCInst *inst,
          uint64_t byteSize,
          EDDisassembler &disassembler,
-         const InstInfo *instInfo);
+         const llvm::EDInstInfo *instInfo);
   ~EDInst();
   
   /// byteSize - returns the number of bytes consumed by the machine code
diff --git a/tools/edis/EDOperand.cpp b/tools/edis/EDOperand.cpp
index 93efd47..495c057 100644
--- a/tools/edis/EDOperand.cpp
+++ b/tools/edis/EDOperand.cpp
@@ -17,6 +17,7 @@
 #include "EDInst.h"
 #include "EDOperand.h"
 
+#include "llvm/MC/EDInstInfo.h"
 #include "llvm/MC/MCInst.h"
 
 using namespace llvm;
diff --git a/tools/edis/Makefile b/tools/edis/Makefile
index 57b941e..0ef0dce 100644
--- a/tools/edis/Makefile
+++ b/tools/edis/Makefile
@@ -49,18 +49,6 @@
     endif
 endif
 
-EDIS_DEFINES = 
-
-ifneq (,$(findstring X86,$(TARGETS_TO_BUILD)))
-	EDIS_DEFINES := $(EDIS_DEFINES) -DEDIS_X86
-endif
-
-ifneq (,$(findstring ARM,$(TARGETS_TO_BUILD)))
-	EDIS_DEFINES := $(EDIS_DEFINES) -DEDIS_ARM
-endif
-
-CXXFLAGS := $(CXXFLAGS) $(EDIS_DEFINES)
-
 EDInfo.inc:	$(TBLGEN)
 	$(Echo) "Building semantic information header"
 	$(Verb) $(TableGen) -o $(call SYSPATH, $@) -gen-enhanced-disassembly-header /dev/null