Remove ExtractCodeAndPrelink and switch Portable to MCLinker

Change-Id: Ia2459c7da6b79e0a1c0f1148c6e28ad9cbbe27a2
diff --git a/src/compiler/dex/compiler_ir.h b/src/compiler/dex/compiler_ir.h
index 4ab98a6..f8cdd34 100644
--- a/src/compiler/dex/compiler_ir.h
+++ b/src/compiler/dex/compiler_ir.h
@@ -18,15 +18,18 @@
 #define ART_SRC_COMPILER_DEX_COMPILER_IR_H_
 
 #include <vector>
-#include "dex_instruction.h"
+
+#include <llvm/Module.h>
+
+#include "compiler/dex/quick/codegen.h"
 #include "compiler/driver/compiler_driver.h"
 #include "compiler/driver/dex_compilation_unit.h"
-#include "compiler_utility.h"
-#include "safe_map.h"
-#include "compiler/llvm/ir_builder.h"
 #include "compiler/llvm/intrinsic_helper.h"
-#include "llvm/Module.h"
+#include "compiler/llvm/ir_builder.h"
 #include "compiler_enums.h"
+#include "compiler_utility.h"
+#include "dex_instruction.h"
+#include "safe_map.h"
 
 namespace art {
 
@@ -43,6 +46,9 @@
 struct ArenaBitVector;
 struct LIR;
 class LLVMInfo;
+namespace llvm {
+class LlvmCompilationUnit;
+}  // namespace llvm
 
 struct PromotionMap {
   RegLocationType core_location:3;
@@ -348,6 +354,7 @@
       mstats(NULL),
       checkstats(NULL),
       gen_bitcode(false),
+      llvm_compilation_unit(NULL),
       llvm_info(NULL),
       context(NULL),
       module(NULL),
@@ -507,7 +514,11 @@
   Memstats* mstats;
   Checkstats* checkstats;
   bool gen_bitcode;
+
+  // Fields for Portable
+  llvm::LlvmCompilationUnit* llvm_compilation_unit;
   LLVMInfo* llvm_info;
+  std::string symbol;
   ::llvm::LLVMContext* context;
   ::llvm::Module* module;
   ::llvm::Function* func;
@@ -516,6 +527,7 @@
   ::llvm::BasicBlock* placeholder_bb;
   ::llvm::BasicBlock* entry_bb;
   ::llvm::BasicBlock* entryTarget_bb;
+
   std::string bitcode_filename;
   GrowableList llvm_values;
   int32_t temp_name;
diff --git a/src/compiler/dex/dataflow.cc b/src/compiler/dex/dataflow.cc
index ac3116e..f0f177a 100644
--- a/src/compiler/dex/dataflow.cc
+++ b/src/compiler/dex/dataflow.cc
@@ -2491,10 +2491,7 @@
       LOG(WARNING) << "Unexpected invoke op: " << opcode;
       return false;
   }
-  DexCompilationUnit m_unit(cu->class_loader, cu->class_linker,
-                            *cu->dex_file, cu->code_item,
-                            cu->class_def_idx, cu->method_idx,
-                            cu->access_flags);
+  DexCompilationUnit m_unit(cu);
   // TODO: add a flag so we don't counts the stats for this twice
   uint32_t dex_method_idx = mir->dalvikInsn.vB;
   int vtable_idx;
diff --git a/src/compiler/dex/frontend.cc b/src/compiler/dex/frontend.cc
index 482804c..5b8faf4 100644
--- a/src/compiler/dex/frontend.cc
+++ b/src/compiler/dex/frontend.cc
@@ -14,8 +14,13 @@
  * limitations under the License.
  */
 
+#include <llvm/Support/Threading.h>
+
 #include "compiler/driver/compiler_driver.h"
 #include "compiler_internals.h"
+#if defined(ART_USE_PORTABLE_COMPILER)
+#include "compiler/llvm/llvm_compilation_unit.h"
+#endif
 #include "dataflow.h"
 #include "ssa_transformation.h"
 #include "leb128.h"
@@ -25,8 +30,6 @@
 #include "portable/mir_to_gbc.h"
 #include "quick/mir_to_lir.h"
 
-#include <llvm/Support/Threading.h>
-
 namespace {
 #if !defined(ART_USE_PORTABLE_COMPILER)
   pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
@@ -782,8 +785,11 @@
                                      const DexFile::CodeItem* code_item,
                                      uint32_t access_flags, InvokeType invoke_type,
                                      uint32_t class_def_idx, uint32_t method_idx,
-                                     jobject class_loader, const DexFile& dex_file,
-                                     LLVMInfo* llvm_info)
+                                     jobject class_loader, const DexFile& dex_file
+#if defined(ART_USE_PORTABLE_COMPILER)
+                                     , llvm::LlvmCompilationUnit* llvm_compilation_unit
+#endif
+)
 {
   VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
 
@@ -818,7 +824,11 @@
   if ((compiler_backend == kQuickGBC) || (compiler_backend == kPortable)) {
     cu->gen_bitcode = true;
   }
-  cu->llvm_info = llvm_info;
+#if defined(ART_USE_PORTABLE_COMPILER)
+  cu->llvm_compilation_unit = llvm_compilation_unit;
+  cu->llvm_info = llvm_compilation_unit->GetQuickContext();
+  cu->symbol = llvm_compilation_unit->GetDexCompilationUnit()->GetSymbol();
+#endif
   /* Adjust this value accordingly once inlining is performed */
   cu->num_dalvik_registers = code_item->registers_size_;
   // TODO: set this from command line
@@ -1137,6 +1147,7 @@
   }
 
 
+#if defined(ART_USE_PORTABLE_COMPILER)
   /* Go the LLVM path? */
   if (cu->gen_bitcode) {
     // MIR->Bitcode
@@ -1148,7 +1159,9 @@
     }
     // Bitcode->LIR
     MethodBitcode2LIR(cu.get());
-  } else {
+  } else
+#endif
+  {
     if (special_case != kNoHandler) {
       /*
        * Custom codegen for special cases.  If for any reason the
@@ -1231,13 +1244,20 @@
 CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
                                  const CompilerBackend backend,
                                  const DexFile::CodeItem* code_item,
-                                 uint32_t access_flags, InvokeType invoke_type,
-                                 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
+                                 uint32_t access_flags,
+                                 InvokeType invoke_type,
+                                 uint32_t class_def_idx,
+                                 uint32_t method_idx,
+                                 jobject class_loader,
                                  const DexFile& dex_file,
-                                 LLVMInfo* llvm_info)
+                                 llvm::LlvmCompilationUnit* llvm_compilation_unit)
 {
   return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
-                       method_idx, class_loader, dex_file, llvm_info);
+                       method_idx, class_loader, dex_file
+#if defined(ART_USE_PORTABLE_COMPILER)
+                       , llvm_compilation_unit
+#endif
+                       );
 }
 
 }  // namespace art
diff --git a/src/compiler/dex/portable/mir_to_gbc.cc b/src/compiler/dex/portable/mir_to_gbc.cc
index 8319b4d..e6900df 100644
--- a/src/compiler/dex/portable/mir_to_gbc.cc
+++ b/src/compiler/dex/portable/mir_to_gbc.cc
@@ -33,6 +33,8 @@
 #include "compiler/dex/quick/codegen_util.h"
 #include "compiler/dex/quick/local_optimizations.h"
 #include "compiler/dex/quick/ralloc_util.h"
+#include "compiler/llvm/llvm_compilation_unit.h"
+#include "compiler/llvm/utils_llvm.h"
 
 static const char* kLabelFormat = "%c0x%x_%d";
 static const char kInvalidBlock = 0xff;
@@ -1982,17 +1984,14 @@
 }
 
 static bool CreateFunction(CompilationUnit* cu) {
-  std::string func_name(PrettyMethod(cu->method_idx, *cu->dex_file,
-                                     /* with_signature */ false));
   ::llvm::FunctionType* func_type = GetFunctionType(cu);
-
   if (func_type == NULL) {
     return false;
   }
 
   cu->func = ::llvm::Function::Create(func_type,
-                                       ::llvm::Function::ExternalLinkage,
-                                       func_name, cu->module);
+                                      ::llvm::Function::InternalLinkage,
+                                      cu->symbol, cu->module);
 
   ::llvm::Function::arg_iterator arg_iter(cu->func->arg_begin());
   ::llvm::Function::arg_iterator arg_end(cu->func->arg_end());
diff --git a/src/compiler/dex/quick/codegen.h b/src/compiler/dex/quick/codegen.h
index 63c8460..21290ca 100644
--- a/src/compiler/dex/quick/codegen.h
+++ b/src/compiler/dex/quick/codegen.h
@@ -17,6 +17,8 @@
 #ifndef ART_SRC_COMPILER_DEX_QUICK_CODEGEN_H_
 #define ART_SRC_COMPILER_DEX_QUICK_CODEGEN_H_
 
+#include "invoke_type.h"
+#include "compiler/dex/compiler_enums.h"
 #include "compiler/dex/compiler_ir.h"
 
 namespace art {
@@ -82,6 +84,14 @@
 #define REG_USE12            (REG_USE1 | REG_USE2)
 #define REG_USE23            (REG_USE2 | REG_USE3)
 
+struct BasicBlock;
+struct CallInfo;
+struct CompilationUnit;
+struct LIR;
+struct MIR;
+struct RegLocation;
+struct RegisterInfo;
+
 typedef int (*NextCallInsn)(CompilationUnit*, CallInfo*, int, uint32_t dex_idx,
                             uint32_t method_idx, uintptr_t direct_code,
                             uintptr_t direct_method, InvokeType type);
diff --git a/src/compiler/dex/quick/codegen_util.cc b/src/compiler/dex/quick/codegen_util.cc
index 5f8f6ef..110146f 100644
--- a/src/compiler/dex/quick/codegen_util.cc
+++ b/src/compiler/dex/quick/codegen_util.cc
@@ -54,10 +54,7 @@
 bool FastInstance(CompilationUnit* cu,  uint32_t field_idx,
                   int& field_offset, bool& is_volatile, bool is_put)
 {
-  DexCompilationUnit m_unit(cu->class_loader, cu->class_linker,
-                            *cu->dex_file, cu->code_item,
-                            cu->class_def_idx, cu->method_idx,
-                            cu->access_flags);
+  DexCompilationUnit m_unit(cu);
   return cu->compiler_driver->ComputeInstanceFieldInfo(field_idx, &m_unit,
            field_offset, is_volatile, is_put);
 }
diff --git a/src/compiler/dex/quick/gen_common.cc b/src/compiler/dex/quick/gen_common.cc
index f14d6cf..a289252 100644
--- a/src/compiler/dex/quick/gen_common.cc
+++ b/src/compiler/dex/quick/gen_common.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "codegen_util.h"
+#include "compiler/dex/quick/codegen_util.h"
 #include "compiler/dex/compiler_ir.h"
 #include "oat/runtime/oat_support_entrypoints.h"
 #include "ralloc_util.h"
@@ -354,8 +354,7 @@
   bool is_volatile;
   bool is_referrers_class;
 
-  DexCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file, cu->code_item,
-                            cu->class_def_idx, cu->method_idx, cu->access_flags);
+  DexCompilationUnit m_unit(cu);
 
   bool fast_path =
       cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
@@ -446,10 +445,7 @@
   bool is_volatile;
   bool is_referrers_class;
 
-  DexCompilationUnit m_unit(cu->class_loader, cu->class_linker,
-                            *cu->dex_file, cu->code_item,
-                            cu->class_def_idx, cu->method_idx,
-                            cu->access_flags);
+  DexCompilationUnit m_unit(cu);
 
   bool fast_path =
       cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
diff --git a/src/compiler/dex/quick/gen_invoke.cc b/src/compiler/dex/quick/gen_invoke.cc
index 2c01c19..1ae29be 100644
--- a/src/compiler/dex/quick/gen_invoke.cc
+++ b/src/compiler/dex/quick/gen_invoke.cc
@@ -16,6 +16,7 @@
 
 #include "codegen_util.h"
 #include "compiler/dex/compiler_ir.h"
+#include "invoke_type.h"
 #include "oat/runtime/oat_support_entrypoints.h"
 #include "ralloc_util.h"
 #include "x86/codegen_x86.h"
@@ -1335,10 +1336,7 @@
   // Explicit register usage
   LockCallTemps(cu);
 
-  DexCompilationUnit m_unit(cu->class_loader, cu->class_linker,
-                            *cu->dex_file, cu->code_item,
-                            cu->class_def_idx, cu->method_idx,
-                            cu->access_flags);
+  DexCompilationUnit m_unit(cu);
 
   uint32_t dex_method_idx = info->index;
   int vtable_idx;
diff --git a/src/compiler/dex/quick/gen_loadstore.cc b/src/compiler/dex/quick/gen_loadstore.cc
index b945e31..a7baea4 100644
--- a/src/compiler/dex/quick/gen_loadstore.cc
+++ b/src/compiler/dex/quick/gen_loadstore.cc
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#include "codegen_util.h"
+#include "compiler/dex/quick/codegen_util.h"
 #include "compiler/dex/compiler_ir.h"
+#include "invoke_type.h"
 #include "ralloc_util.h"
 
 namespace art {
diff --git a/src/compiler/dex/quick/ralloc_util.cc b/src/compiler/dex/quick/ralloc_util.cc
index a782264..5b7de2c 100644
--- a/src/compiler/dex/quick/ralloc_util.cc
+++ b/src/compiler/dex/quick/ralloc_util.cc
@@ -16,10 +16,10 @@
 
 /* This file contains register alloction support. */
 
-#include "codegen_util.h"
 #include "compiler/dex/compiler_ir.h"
 #include "compiler/dex/compiler_utility.h"
 #include "compiler/dex/dataflow.h"
+#include "compiler/dex/quick/codegen_util.h"
 #include "ralloc_util.h"
 
 namespace art {
diff --git a/src/compiler/dex/write_elf.cc b/src/compiler/dex/write_elf.cc
index a78d98e..7e3d512 100644
--- a/src/compiler/dex/write_elf.cc
+++ b/src/compiler/dex/write_elf.cc
@@ -19,12 +19,17 @@
 
 namespace art {
 class CompilerDriver;
+class DexFile;
 }  // namespace art
 
 extern "C" bool WriteElf(art::CompilerDriver& driver,
+                         const std::string* host_prefix,
+                         bool is_host,
+                         const std::vector<const art::DexFile*>& dex_files,
                          std::vector<uint8_t>& oat_contents,
-                         art::File* file) {
-  return art::ElfWriter::Create(file, oat_contents, driver);
+                         art::File* file)
+    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+  return art::ElfWriter::Create(file, oat_contents, dex_files, host_prefix, is_host, driver);
 }
 extern "C" bool FixupElf(art::File* file, uintptr_t oat_data_begin) {
   return art::ElfWriter::Fixup(file, oat_data_begin);
@@ -34,3 +39,6 @@
                                      size_t& oat_data_offset) {
   art::ElfWriter::GetOatElfInformation(file, oat_loaded_size, oat_data_offset);
 }
+extern "C" bool StripElf(art::File* file) {
+  return art::ElfWriter::Strip(file);
+}