Refactor the compilers out of libart.

This builds three separate compilers and dynamically links with the right one
at runtime.

Change-Id: I59d22b9884f41de733c09f97e29ee290236d5f4b
diff --git a/src/compiler/Compiler.h b/src/compiler/Compiler.h
index 1e0439b..8636e11 100644
--- a/src/compiler/Compiler.h
+++ b/src/compiler/Compiler.h
@@ -40,15 +40,7 @@
  */
 #define MAX_ASSEMBLER_RETRIES 50
 
-typedef enum OatInstructionSetType {
-    DALVIK_OAT_NONE = 0,
-    DALVIK_OAT_ARM,
-    DALVIK_OAT_THUMB2,
-    DALVIK_OAT_X86,
-    DALVIK_OAT_MIPS32
-} OatInstructionSetType;
-
-/* Supress optimization if corresponding bit set */
+/* Suppress optimization if corresponding bit set */
 enum optControlVector {
     kLoadStoreElimination = 0,
     kLoadHoisting,
@@ -183,9 +175,6 @@
 bool oatArchInit(void);
 bool oatStartup(void);
 void oatShutdown(void);
-CompiledMethod* oatCompileMethod(Compiler& compiler, bool is_direct,
-                                 uint32_t method_idx, const ClassLoader* class_loader,
-                                 const DexFile& dex_file, OatInstructionSetType);
 void oatScanAllClassPointers(void (*callback)(void* ptr));
 void oatInitializeSSAConversion(struct CompilationUnit* cUnit);
 int oatConvertSSARegToDalvik(const struct CompilationUnit* cUnit, int ssaReg);
@@ -220,4 +209,10 @@
 
 }  // namespace art
 
+extern "C" art::CompiledMethod* oatCompileMethod(art::Compiler& compiler,
+                                                 const art::DexFile::CodeItem* code_item,
+                                                 uint32_t access_flags, uint32_t method_idx,
+                                                 const art::ClassLoader* class_loader,
+                                                 const art::DexFile& dex_file);
+
 #endif // ART_SRC_COMPILER_COMPILER_H_
diff --git a/src/compiler/CompilerIR.h b/src/compiler/CompilerIR.h
index 6bda522..2515713 100644
--- a/src/compiler/CompilerIR.h
+++ b/src/compiler/CompilerIR.h
@@ -307,7 +307,7 @@
     bool methodTraceSupport;            // For TraceView profiling
     struct RegisterPool* regPool;
     int optRound;                       // round number to tell an LIR's age
-    OatInstructionSetType instructionSet;
+    InstructionSet instructionSet;
     /* Number of total regs used in the whole cUnit after SSA transformation */
     int numSSARegs;
     /* Map SSA reg i to the Dalvik[15..0]/Sub[31..16] pair. */
diff --git a/src/compiler/Frontend.cc b/src/compiler/Frontend.cc
index 7988829..4271722 100644
--- a/src/compiler/Frontend.cc
+++ b/src/compiler/Frontend.cc
@@ -724,15 +724,20 @@
     }
 }
 
-/*
- * Compile a method.
- */
-CompiledMethod* oatCompileMethod(Compiler& compiler,
-                                 const DexFile::CodeItem* code_item,
-                                 uint32_t access_flags, uint32_t method_idx,
-                                 const ClassLoader* class_loader,
-                                 const DexFile& dex_file,
-                                 InstructionSet insnSet)
+void oatInit(CompilationUnit* cUnit, const Compiler& compiler) {
+  if (!oatArchInit()) {
+    LOG(FATAL) << "Failed to initialize oat";
+  }
+  if (!oatHeapInit(cUnit)) {
+    LOG(FATAL) << "Failed to initialize oat heap";
+  }
+}
+
+CompiledMethod* oatCompileMethodInternal(Compiler& compiler,
+                                         const DexFile::CodeItem* code_item,
+                                         uint32_t access_flags, uint32_t method_idx,
+                                         const ClassLoader* class_loader,
+                                         const DexFile& dex_file)
 {
     VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
 
@@ -746,6 +751,7 @@
     memset(cUnit.get(), 0, sizeof(*cUnit));
 
     oatInit(cUnit.get(), compiler);
+
     cUnit->compiler = &compiler;
     cUnit->class_linker = class_linker;
     cUnit->dex_file = &dex_file;
@@ -754,7 +760,7 @@
     cUnit->code_item = code_item;
     cUnit->access_flags = access_flags;
     cUnit->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
-    cUnit->instructionSet = (OatInstructionSetType)insnSet;
+    cUnit->instructionSet = compiler.GetInstructionSet();
     cUnit->insns = code_item->insns_;
     cUnit->insnsSize = code_item->insns_size_in_code_units_;
     cUnit->numIns = code_item->ins_size_;
@@ -943,7 +949,7 @@
                 (1 << kPromoteRegs);
             if (cUnit->printMe) {
                 LOG(INFO) << "Compiler: " << PrettyMethod(method_idx, dex_file)
-                   << " too big: " << cUnit->numBlocks;
+                          << " too big: " << cUnit->numBlocks;
             }
         }
     }
@@ -1027,14 +1033,14 @@
     return result;
 }
 
-void oatInit(CompilationUnit* cUnit, const Compiler& compiler)
-{
-    if (!oatArchInit()) {
-        LOG(FATAL) << "Failed to initialize oat";
-    }
-    if (!oatHeapInit(cUnit)) {
-        LOG(FATAL) << "Failed to initialize oat heap";
-    }
-}
-
 }  // namespace art
+
+extern "C" art::CompiledMethod* oatCompileMethod(art::Compiler& compiler,
+                                                 const art::DexFile::CodeItem* code_item,
+                                                 uint32_t access_flags, uint32_t method_idx,
+                                                 const art::ClassLoader* class_loader,
+                                                 const art::DexFile& dex_file)
+{
+  CHECK_EQ(compiler.GetInstructionSet(), art::oatInstructionSet());
+  return art::oatCompileMethodInternal(compiler, code_item, access_flags, method_idx, class_loader, dex_file);
+}
diff --git a/src/compiler/Ralloc.cc b/src/compiler/Ralloc.cc
index 3353437..abc573e 100644
--- a/src/compiler/Ralloc.cc
+++ b/src/compiler/Ralloc.cc
@@ -406,9 +406,10 @@
     }
 
     /* Figure out the frame size */
-    cUnit->numPadding = (STACK_ALIGN_WORDS -
+    static const int kStackAlignWords = kStackAlignment/sizeof(uint32_t);
+    cUnit->numPadding = (kStackAlignWords -
         (cUnit->numCoreSpills + cUnit->numFPSpills + cUnit->numRegs +
-         cUnit->numOuts + 2)) & (STACK_ALIGN_WORDS-1);
+             cUnit->numOuts + 2)) & (kStackAlignWords - 1);
     cUnit->frameSize = (cUnit->numCoreSpills + cUnit->numFPSpills +
                         cUnit->numRegs + cUnit->numOuts +
                         cUnit->numPadding + 2) * 4;
diff --git a/src/compiler/codegen/CodegenFactory.cc b/src/compiler/codegen/CodegenFactory.cc
index b27efe0..8a6e1bc 100644
--- a/src/compiler/codegen/CodegenFactory.cc
+++ b/src/compiler/codegen/CodegenFactory.cc
@@ -266,7 +266,7 @@
 }
 
 /*
- * Utiltiy to load the current Method*.  Broken out
+ * Utility to load the current Method*.  Broken out
  * to allow easy change between placing the current Method* in a
  * dedicated register or its home location in the frame.
  */
diff --git a/src/compiler/codegen/CompilerCodegen.h b/src/compiler/codegen/CompilerCodegen.h
index 1b54be9..9537e46 100644
--- a/src/compiler/codegen/CompilerCodegen.h
+++ b/src/compiler/codegen/CompilerCodegen.h
@@ -51,7 +51,7 @@
 void oatInitializeRegAlloc(CompilationUnit* cUnit);
 
 /* Implemented in codegen/<target>/<target_variant>/ArchVariant.c */
-OatInstructionSetType oatInstructionSet(void);
+InstructionSet oatInstructionSet();
 
 /*
  * Implemented in codegen/<target>/<target_variant>/ArchVariant.c
diff --git a/src/compiler/codegen/Optimizer.h b/src/compiler/codegen/Optimizer.h
index 74a5b75..06c7732 100644
--- a/src/compiler/codegen/Optimizer.h
+++ b/src/compiler/codegen/Optimizer.h
@@ -21,9 +21,6 @@
 
 namespace art {
 
-#define STACK_ALIGN_WORDS 4
-#define STACK_ALIGNMENT (STACK_ALIGN_WORDS * 4)
-
 /* Forward declarations */
 struct CompilationUnit;
 struct LIR;
diff --git a/src/compiler/codegen/RallocUtil.cc b/src/compiler/codegen/RallocUtil.cc
index 232b304..cfda721 100644
--- a/src/compiler/codegen/RallocUtil.cc
+++ b/src/compiler/codegen/RallocUtil.cc
@@ -1227,23 +1227,4 @@
     return oatVRegOffset(cUnit, oatS2VReg(cUnit, sReg));
 }
 
-
-/* Return sp-relative offset in bytes using Method* */
-extern int oatVRegOffset(const DexFile::CodeItem* code_item,
-                         uint32_t core_spills, uint32_t fp_spills,
-                         size_t frame_size, int reg)
-{
-    int numIns = code_item->ins_size_;
-    int numRegs = code_item->registers_size_ - numIns;
-    int numOuts = code_item->outs_size_;
-    int numSpills = __builtin_popcount(core_spills) +
-                    __builtin_popcount(fp_spills);
-    int numPadding = (STACK_ALIGN_WORDS -
-        (numSpills + numRegs + numOuts + 2)) & (STACK_ALIGN_WORDS-1);
-    int regsOffset = (numOuts + numPadding + 1) * 4;
-    int insOffset = frame_size + 4;
-    return (reg < numRegs) ? regsOffset + (reg << 2) :
-           insOffset + ((reg - numRegs) << 2);
-}
-
 }  // namespace art
diff --git a/src/compiler/codegen/arm/armv7-a-neon/ArchVariant.cc b/src/compiler/codegen/arm/armv7-a-neon/ArchVariant.cc
index 77844fc..dcf3a99 100644
--- a/src/compiler/codegen/arm/armv7-a-neon/ArchVariant.cc
+++ b/src/compiler/codegen/arm/armv7-a-neon/ArchVariant.cc
@@ -20,9 +20,9 @@
  * Determine the initial instruction set to be used for this trace.
  * Later components may decide to change this.
  */
-OatInstructionSetType oatInstructionSet(void)
+InstructionSet oatInstructionSet()
 {
-    return DALVIK_OAT_THUMB2;
+    return kThumb2;
 }
 
 /* Architecture-specific initializations and checks go here */
diff --git a/src/compiler/codegen/arm/armv7-a/ArchVariant.cc b/src/compiler/codegen/arm/armv7-a/ArchVariant.cc
index 77844fc..dcf3a99 100644
--- a/src/compiler/codegen/arm/armv7-a/ArchVariant.cc
+++ b/src/compiler/codegen/arm/armv7-a/ArchVariant.cc
@@ -20,9 +20,9 @@
  * Determine the initial instruction set to be used for this trace.
  * Later components may decide to change this.
  */
-OatInstructionSetType oatInstructionSet(void)
+InstructionSet oatInstructionSet()
 {
-    return DALVIK_OAT_THUMB2;
+    return kThumb2;
 }
 
 /* Architecture-specific initializations and checks go here */
diff --git a/src/compiler/codegen/mips/mips/ArchVariant.cc b/src/compiler/codegen/mips/mips/ArchVariant.cc
index 6d29fc5..6b04d70 100644
--- a/src/compiler/codegen/mips/mips/ArchVariant.cc
+++ b/src/compiler/codegen/mips/mips/ArchVariant.cc
@@ -25,9 +25,9 @@
  * Determine the initial instruction set to be used for this trace.
  * Later components may decide to change this.
  */
-OatInstructionSetType oatInstructionSet(void)
+InstructionSet oatInstructionSet()
 {
-    return DALVIK_OAT_MIPS32;
+    return kMips;
 }
 
 /* Architecture-specific initializations and checks go here */
diff --git a/src/compiler/codegen/x86/x86/ArchVariant.cc b/src/compiler/codegen/x86/x86/ArchVariant.cc
index 93b17e1..944311c 100644
--- a/src/compiler/codegen/x86/x86/ArchVariant.cc
+++ b/src/compiler/codegen/x86/x86/ArchVariant.cc
@@ -25,9 +25,9 @@
  * Determine the initial instruction set to be used for this trace.
  * Later components may decide to change this.
  */
-OatInstructionSetType oatInstructionSet(void)
+InstructionSet oatInstructionSet()
 {
-    return DALVIK_OAT_X86;
+    return kX86;
 }
 
 /* Architecture-specific initializations and checks go here */