Split up CommonTest into CommonRuntimeTest and CommonCompilerTest

Change-Id: I8dcf6b29a5aecd445f1a3ddb06386cf81dbc9c70
diff --git a/compiler/common_compiler_test.h b/compiler/common_compiler_test.h
new file mode 100644
index 0000000..f935095
--- /dev/null
+++ b/compiler/common_compiler_test.h
@@ -0,0 +1,417 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_COMPILER_COMMON_COMPILER_TEST_H_
+#define ART_COMPILER_COMMON_COMPILER_TEST_H_
+
+#include "compiler_backend.h"
+#include "compiler_callbacks.h"
+#include "common_runtime_test.h"
+#include "dex/quick/dex_file_to_method_inliner_map.h"
+#include "dex/verification_results.h"
+#include "driver/compiler_callbacks_impl.h"
+#include "driver/compiler_driver.h"
+#include "driver/compiler_options.h"
+
+namespace art {
+
+#if defined(__arm__)
+
+#include <sys/ucontext.h>
+
+// A signal handler called when have an illegal instruction.  We record the fact in
+// a global boolean and then increment the PC in the signal context to return to
+// the next instruction.  We know the instruction is an sdiv (4 bytes long).
+static void baddivideinst(int signo, siginfo *si, void *data) {
+  (void)signo;
+  (void)si;
+  struct ucontext *uc = (struct ucontext *)data;
+  struct sigcontext *sc = &uc->uc_mcontext;
+  sc->arm_r0 = 0;     // set R0 to #0 to signal error
+  sc->arm_pc += 4;    // skip offending instruction
+}
+
+// This is in arch/arm/arm_sdiv.S.  It does the following:
+// mov r1,#1
+// sdiv r0,r1,r1
+// bx lr
+//
+// the result will be the value 1 if sdiv is supported.  If it is not supported
+// a SIGILL signal will be raised and the signal handler (baddivideinst) called.
+// The signal handler sets r0 to #0 and then increments pc beyond the failed instruction.
+// Thus if the instruction is not supported, the result of this function will be #0
+
+extern "C" bool CheckForARMSDIVInstruction();
+
+static InstructionSetFeatures GuessInstructionFeatures() {
+  InstructionSetFeatures f;
+
+  // Uncomment this for processing of /proc/cpuinfo.
+  if (false) {
+    // Look in /proc/cpuinfo for features we need.  Only use this when we can guarantee that
+    // the kernel puts the appropriate feature flags in here.  Sometimes it doesn't.
+    std::ifstream in("/proc/cpuinfo");
+    if (in) {
+      while (!in.eof()) {
+        std::string line;
+        std::getline(in, line);
+        if (!in.eof()) {
+          if (line.find("Features") != std::string::npos) {
+            if (line.find("idivt") != std::string::npos) {
+              f.SetHasDivideInstruction(true);
+            }
+          }
+        }
+        in.close();
+      }
+    } else {
+      LOG(INFO) << "Failed to open /proc/cpuinfo";
+    }
+  }
+
+  // See if have a sdiv instruction.  Register a signal handler and try to execute
+  // an sdiv instruction.  If we get a SIGILL then it's not supported.  We can't use
+  // the /proc/cpuinfo method for this because Krait devices don't always put the idivt
+  // feature in the list.
+  struct sigaction sa, osa;
+  sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
+  sa.sa_sigaction = baddivideinst;
+  sigaction(SIGILL, &sa, &osa);
+
+  if (CheckForARMSDIVInstruction()) {
+    f.SetHasDivideInstruction(true);
+  }
+
+  // Restore the signal handler.
+  sigaction(SIGILL, &osa, nullptr);
+
+  // Other feature guesses in here.
+  return f;
+}
+
+#endif
+
+// Given a set of instruction features from the build, parse it.  The
+// input 'str' is a comma separated list of feature names.  Parse it and
+// return the InstructionSetFeatures object.
+static InstructionSetFeatures ParseFeatureList(std::string str) {
+  InstructionSetFeatures result;
+  typedef std::vector<std::string> FeatureList;
+  FeatureList features;
+  Split(str, ',', features);
+  for (FeatureList::iterator i = features.begin(); i != features.end(); i++) {
+    std::string feature = Trim(*i);
+    if (feature == "default") {
+      // Nothing to do.
+    } else if (feature == "div") {
+      // Supports divide instruction.
+      result.SetHasDivideInstruction(true);
+    } else if (feature == "nodiv") {
+      // Turn off support for divide instruction.
+      result.SetHasDivideInstruction(false);
+    } else {
+      LOG(FATAL) << "Unknown instruction set feature: '" << feature << "'";
+    }
+  }
+  // Others...
+  return result;
+}
+
+class CommonCompilerTest : public CommonRuntimeTest {
+ public:
+  static void MakeExecutable(const std::vector<uint8_t>& code) {
+    CHECK_NE(code.size(), 0U);
+    MakeExecutable(&code[0], code.size());
+  }
+
+  // Create an OatMethod based on pointers (for unit tests).
+  OatFile::OatMethod CreateOatMethod(const void* code,
+                                     const size_t frame_size_in_bytes,
+                                     const uint32_t core_spill_mask,
+                                     const uint32_t fp_spill_mask,
+                                     const uint8_t* mapping_table,
+                                     const uint8_t* vmap_table,
+                                     const uint8_t* gc_map) {
+    const byte* base;
+    uint32_t code_offset, mapping_table_offset, vmap_table_offset, gc_map_offset;
+    if (mapping_table == nullptr && vmap_table == nullptr && gc_map == nullptr) {
+      base = reinterpret_cast<const byte*>(code);  // Base of data points at code.
+      base -= kPointerSize;  // Move backward so that code_offset != 0.
+      code_offset = kPointerSize;
+      mapping_table_offset = 0;
+      vmap_table_offset = 0;
+      gc_map_offset = 0;
+    } else {
+      // TODO: 64bit support.
+      base = nullptr;  // Base of data in oat file, ie 0.
+      code_offset = PointerToLowMemUInt32(code);
+      mapping_table_offset = PointerToLowMemUInt32(mapping_table);
+      vmap_table_offset = PointerToLowMemUInt32(vmap_table);
+      gc_map_offset = PointerToLowMemUInt32(gc_map);
+    }
+    return OatFile::OatMethod(base,
+                              code_offset,
+                              frame_size_in_bytes,
+                              core_spill_mask,
+                              fp_spill_mask,
+                              mapping_table_offset,
+                              vmap_table_offset,
+                              gc_map_offset);
+  }
+
+  void MakeExecutable(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    CHECK(method != nullptr);
+
+    const CompiledMethod* compiled_method = nullptr;
+    if (!method->IsAbstract()) {
+      mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
+      const DexFile& dex_file = *dex_cache->GetDexFile();
+      compiled_method =
+          compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
+                                                              method->GetDexMethodIndex()));
+    }
+    if (compiled_method != nullptr) {
+      const std::vector<uint8_t>* code = compiled_method->GetQuickCode();
+      if (code == nullptr) {
+        code = compiled_method->GetPortableCode();
+      }
+      MakeExecutable(*code);
+      const void* method_code = CompiledMethod::CodePointer(&(*code)[0],
+                                                            compiled_method->GetInstructionSet());
+      LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
+      OatFile::OatMethod oat_method = CreateOatMethod(method_code,
+                                                      compiled_method->GetFrameSizeInBytes(),
+                                                      compiled_method->GetCoreSpillMask(),
+                                                      compiled_method->GetFpSpillMask(),
+                                                      &compiled_method->GetMappingTable()[0],
+                                                      &compiled_method->GetVmapTable()[0],
+                                                      nullptr);
+      oat_method.LinkMethod(method);
+      method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);
+    } else {
+      // No code? You must mean to go into the interpreter.
+      const void* method_code = kUsePortableCompiler ? GetPortableToInterpreterBridge()
+                                                     : GetQuickToInterpreterBridge();
+      OatFile::OatMethod oat_method = CreateOatMethod(method_code,
+                                                      kStackAlignment,
+                                                      0,
+                                                      0,
+                                                      nullptr,
+                                                      nullptr,
+                                                      nullptr);
+      oat_method.LinkMethod(method);
+      method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge);
+    }
+    // Create bridges to transition between different kinds of compiled bridge.
+    if (method->GetEntryPointFromPortableCompiledCode() == nullptr) {
+      method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge());
+    } else {
+      CHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr);
+      method->SetEntryPointFromQuickCompiledCode(GetQuickToPortableBridge());
+      method->SetIsPortableCompiled();
+    }
+  }
+
+  static void MakeExecutable(const void* code_start, size_t code_length) {
+    CHECK(code_start != nullptr);
+    CHECK_NE(code_length, 0U);
+    uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
+    uintptr_t base = RoundDown(data, kPageSize);
+    uintptr_t limit = RoundUp(data + code_length, kPageSize);
+    uintptr_t len = limit - base;
+    int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
+    CHECK_EQ(result, 0);
+
+    // Flush instruction cache
+    // Only uses __builtin___clear_cache if GCC >= 4.3.3
+#if GCC_VERSION >= 40303
+    __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
+#else
+    LOG(FATAL) << "UNIMPLEMENTED: cache flush";
+#endif
+  }
+
+  void MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::string class_descriptor(DotToDescriptor(class_name));
+    Thread* self = Thread::Current();
+    SirtRef<mirror::ClassLoader> loader(self, class_loader);
+    mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
+    CHECK(klass != nullptr) << "Class not found " << class_name;
+    for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
+      MakeExecutable(klass->GetDirectMethod(i));
+    }
+    for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
+      MakeExecutable(klass->GetVirtualMethod(i));
+    }
+  }
+
+ protected:
+  virtual void SetUp() {
+    CommonRuntimeTest::SetUp();
+    {
+      ScopedObjectAccess soa(Thread::Current());
+
+      InstructionSet instruction_set = kNone;
+
+      // Take the default set of instruction features from the build.
+      InstructionSetFeatures instruction_set_features =
+          ParseFeatureList(STRINGIFY(ART_DEFAULT_INSTRUCTION_SET_FEATURES));
+
+#if defined(__arm__)
+      instruction_set = kThumb2;
+      InstructionSetFeatures runtime_features = GuessInstructionFeatures();
+
+      // for ARM, do a runtime check to make sure that the features we are passed from
+      // the build match the features we actually determine at runtime.
+      ASSERT_EQ(instruction_set_features, runtime_features);
+#elif defined(__mips__)
+      instruction_set = kMips;
+#elif defined(__i386__)
+      instruction_set = kX86;
+#elif defined(__x86_64__)
+      instruction_set = kX86_64;
+      // TODO: x86_64 compilation support.
+      compiler_options_->SetCompilerFilter(Runtime::kInterpretOnly);
+#endif
+
+      for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
+        Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
+        if (!runtime_->HasCalleeSaveMethod(type)) {
+          runtime_->SetCalleeSaveMethod(
+              runtime_->CreateCalleeSaveMethod(instruction_set, type), type);
+        }
+      }
+
+      // TODO: make selectable
+      CompilerBackend::Kind compiler_backend
+          = (kUsePortableCompiler) ? CompilerBackend::kPortable : CompilerBackend::kQuick;
+      timer_.reset(new CumulativeLogger("Compilation times"));
+      compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
+                                                verification_results_.get(),
+                                                method_inliner_map_.get(),
+                                                compiler_backend, instruction_set,
+                                                instruction_set_features,
+                                                true, new CompilerDriver::DescriptorSet,
+                                                2, true, true, timer_.get()));
+    }
+    // We typically don't generate an image in unit tests, disable this optimization by default.
+    compiler_driver_->SetSupportBootImageFixup(false);
+  }
+
+  virtual void SetUpRuntimeOptions(Runtime::Options *options) {
+    CommonRuntimeTest::SetUpRuntimeOptions(options);
+
+    compiler_options_.reset(new CompilerOptions);
+    verification_results_.reset(new VerificationResults(compiler_options_.get()));
+    method_inliner_map_.reset(new DexFileToMethodInlinerMap);
+    callbacks_.reset(new CompilerCallbacksImpl(verification_results_.get(),
+                                               method_inliner_map_.get()));
+    options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
+  }
+
+  virtual void TearDown() {
+    timer_.reset();
+    compiler_driver_.reset();
+    callbacks_.reset();
+    method_inliner_map_.reset();
+    verification_results_.reset();
+    compiler_options_.reset();
+
+    CommonRuntimeTest::TearDown();
+  }
+
+  void CompileClass(mirror::ClassLoader* class_loader, const char* class_name)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::string class_descriptor(DotToDescriptor(class_name));
+    Thread* self = Thread::Current();
+    SirtRef<mirror::ClassLoader> loader(self, class_loader);
+    mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
+    CHECK(klass != nullptr) << "Class not found " << class_name;
+    for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
+      CompileMethod(klass->GetDirectMethod(i));
+    }
+    for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
+      CompileMethod(klass->GetVirtualMethod(i));
+    }
+  }
+
+  void CompileMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    CHECK(method != nullptr);
+    TimingLogger timings("CommonTest::CompileMethod", false, false);
+    timings.StartSplit("CompileOne");
+    compiler_driver_->CompileOne(method, timings);
+    MakeExecutable(method);
+    timings.EndSplit();
+  }
+
+  void CompileDirectMethod(SirtRef<mirror::ClassLoader>& class_loader, const char* class_name,
+                           const char* method_name, const char* signature)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::string class_descriptor(DotToDescriptor(class_name));
+    Thread* self = Thread::Current();
+    mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
+    CHECK(klass != nullptr) << "Class not found " << class_name;
+    mirror::ArtMethod* method = klass->FindDirectMethod(method_name, signature);
+    CHECK(method != nullptr) << "Direct method not found: "
+                             << class_name << "." << method_name << signature;
+    CompileMethod(method);
+  }
+
+  void CompileVirtualMethod(SirtRef<mirror::ClassLoader>& class_loader, const char* class_name,
+                            const char* method_name, const char* signature)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::string class_descriptor(DotToDescriptor(class_name));
+    Thread* self = Thread::Current();
+    mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
+    CHECK(klass != nullptr) << "Class not found " << class_name;
+    mirror::ArtMethod* method = klass->FindVirtualMethod(method_name, signature);
+    CHECK(method != NULL) << "Virtual method not found: "
+                          << class_name << "." << method_name << signature;
+    CompileMethod(method);
+  }
+
+  void ReserveImageSpace() {
+    // Reserve where the image will be loaded up front so that other parts of test set up don't
+    // accidentally end up colliding with the fixed memory address when we need to load the image.
+    std::string error_msg;
+    image_reservation_.reset(MemMap::MapAnonymous("image reservation",
+                                                  reinterpret_cast<byte*>(ART_BASE_ADDRESS),
+                                                  (size_t)100 * 1024 * 1024,  // 100MB
+                                                  PROT_NONE,
+                                                  false /* no need for 4gb flag with fixed mmap*/,
+                                                  &error_msg));
+    CHECK(image_reservation_.get() != nullptr) << error_msg;
+  }
+
+  void UnreserveImageSpace() {
+    image_reservation_.reset();
+  }
+
+  UniquePtr<CompilerOptions> compiler_options_;
+  UniquePtr<VerificationResults> verification_results_;
+  UniquePtr<DexFileToMethodInlinerMap> method_inliner_map_;
+  UniquePtr<CompilerCallbacksImpl> callbacks_;
+  UniquePtr<CompilerDriver> compiler_driver_;
+  UniquePtr<CumulativeLogger> timer_;
+
+ private:
+  UniquePtr<MemMap> image_reservation_;
+};
+
+}  // namespace art
+
+#endif  // ART_COMPILER_COMMON_COMPILER_TEST_H_
diff --git a/compiler/dex/quick/mir_to_lir.h b/compiler/dex/quick/mir_to_lir.h
index 6e97c53..b74052c 100644
--- a/compiler/dex/quick/mir_to_lir.h
+++ b/compiler/dex/quick/mir_to_lir.h
@@ -23,7 +23,7 @@
 #include "dex/compiler_ir.h"
 #include "dex/backend.h"
 #include "driver/compiler_driver.h"
-#include "leb128_encoder.h"
+#include "leb128.h"
 #include "safe_map.h"
 #include "utils/arena_allocator.h"
 #include "utils/growable_array.h"
diff --git a/compiler/driver/compiler_driver_test.cc b/compiler/driver/compiler_driver_test.cc
index ec0a8bd..34806ce 100644
--- a/compiler/driver/compiler_driver_test.cc
+++ b/compiler/driver/compiler_driver_test.cc
@@ -21,7 +21,7 @@
 
 #include "UniquePtr.h"
 #include "class_linker.h"
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "dex_file.h"
 #include "gc/heap.h"
 #include "mirror/art_method-inl.h"
@@ -33,7 +33,7 @@
 
 namespace art {
 
-class CompilerDriverTest : public CommonTest {
+class CompilerDriverTest : public CommonCompilerTest {
  protected:
   void CompileAll(jobject class_loader) LOCKS_EXCLUDED(Locks::mutator_lock_) {
     TimingLogger timings("CompilerDriverTest::CompileAll", false, false);
diff --git a/compiler/elf_writer_test.cc b/compiler/elf_writer_test.cc
index 5bad0d0..8175c35 100644
--- a/compiler/elf_writer_test.cc
+++ b/compiler/elf_writer_test.cc
@@ -14,18 +14,18 @@
  * limitations under the License.
  */
 
-#include "common_test.h"
-
-#include "oat.h"
 #include "elf_file.h"
 
+#include "common_compiler_test.h"
+#include "oat.h"
+
 namespace art {
 
-class ElfWriterTest : public CommonTest {
+class ElfWriterTest : public CommonCompilerTest {
  protected:
   virtual void SetUp() {
     ReserveImageSpace();
-    CommonTest::SetUp();
+    CommonCompilerTest::SetUp();
   }
 };
 
diff --git a/compiler/image_test.cc b/compiler/image_test.cc
index 49cabdc..16e2aa2 100644
--- a/compiler/image_test.cc
+++ b/compiler/image_test.cc
@@ -14,15 +14,16 @@
  * limitations under the License.
  */
 
+#include "image.h"
+
 #include <string>
 #include <vector>
 
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "compiler/elf_fixup.h"
 #include "compiler/image_writer.h"
 #include "compiler/oat_writer.h"
 #include "gc/space/image_space.h"
-#include "image.h"
 #include "lock_word.h"
 #include "mirror/object-inl.h"
 #include "signal_catcher.h"
@@ -32,11 +33,11 @@
 
 namespace art {
 
-class ImageTest : public CommonTest {
+class ImageTest : public CommonCompilerTest {
  protected:
   virtual void SetUp() {
     ReserveImageSpace();
-    CommonTest::SetUp();
+    CommonCompilerTest::SetUp();
   }
 };
 
diff --git a/compiler/jni/jni_compiler_test.cc b/compiler/jni/jni_compiler_test.cc
index 1bdff37..f48cf6c 100644
--- a/compiler/jni/jni_compiler_test.cc
+++ b/compiler/jni/jni_compiler_test.cc
@@ -15,7 +15,7 @@
  */
 
 #include "class_linker.h"
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "dex_file.h"
 #include "gtest/gtest.h"
 #include "indirect_reference_table.h"
@@ -43,7 +43,7 @@
 
 namespace art {
 
-class JniCompilerTest : public CommonTest {
+class JniCompilerTest : public CommonCompilerTest {
  protected:
   void CompileForTest(jobject class_loader, bool direct,
                       const char* method_name, const char* method_sig) {
diff --git a/compiler/leb128_encoder.h b/compiler/leb128_encoder.h
deleted file mode 100644
index 6766683..0000000
--- a/compiler/leb128_encoder.h
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ART_COMPILER_LEB128_ENCODER_H_
-#define ART_COMPILER_LEB128_ENCODER_H_
-
-#include "base/macros.h"
-#include "leb128.h"
-
-namespace art {
-
-static inline uint8_t* EncodeUnsignedLeb128(uint8_t* dest, uint32_t value) {
-  uint8_t out = value & 0x7f;
-  value >>= 7;
-  while (value != 0) {
-    *dest++ = out | 0x80;
-    out = value & 0x7f;
-    value >>= 7;
-  }
-  *dest++ = out;
-  return dest;
-}
-
-static inline uint8_t* EncodeSignedLeb128(uint8_t* dest, int32_t value) {
-  uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
-  uint8_t out = value & 0x7f;
-  while (extra_bits != 0u) {
-    *dest++ = out | 0x80;
-    value >>= 7;
-    out = value & 0x7f;
-    extra_bits >>= 7;
-  }
-  *dest++ = out;
-  return dest;
-}
-
-// An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format.
-class Leb128EncodingVector {
- public:
-  Leb128EncodingVector() {
-  }
-
-  void Reserve(uint32_t size) {
-    data_.reserve(size);
-  }
-
-  void PushBackUnsigned(uint32_t value) {
-    uint8_t out = value & 0x7f;
-    value >>= 7;
-    while (value != 0) {
-      data_.push_back(out | 0x80);
-      out = value & 0x7f;
-      value >>= 7;
-    }
-    data_.push_back(out);
-  }
-
-  template<typename It>
-  void InsertBackUnsigned(It cur, It end) {
-    for (; cur != end; ++cur) {
-      PushBackUnsigned(*cur);
-    }
-  }
-
-  void PushBackSigned(int32_t value) {
-    uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
-    uint8_t out = value & 0x7f;
-    while (extra_bits != 0u) {
-      data_.push_back(out | 0x80);
-      value >>= 7;
-      out = value & 0x7f;
-      extra_bits >>= 7;
-    }
-    data_.push_back(out);
-  }
-
-  template<typename It>
-  void InsertBackSigned(It cur, It end) {
-    for (; cur != end; ++cur) {
-      PushBackSigned(*cur);
-    }
-  }
-
-  const std::vector<uint8_t>& GetData() const {
-    return data_;
-  }
-
- private:
-  std::vector<uint8_t> data_;
-
-  DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector);
-};
-
-}  // namespace art
-
-#endif  // ART_COMPILER_LEB128_ENCODER_H_
diff --git a/compiler/leb128_encoder_test.cc b/compiler/leb128_encoder_test.cc
deleted file mode 100644
index 7af8518..0000000
--- a/compiler/leb128_encoder_test.cc
+++ /dev/null
@@ -1,291 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "leb128.h"
-#include "leb128_encoder.h"
-
-#include "gtest/gtest.h"
-#include "base/histogram-inl.h"
-
-namespace art {
-
-struct DecodeUnsignedLeb128TestCase {
-  uint32_t decoded;
-  uint8_t leb128_data[5];
-};
-
-static DecodeUnsignedLeb128TestCase uleb128_tests[] = {
-    {0,          {0, 0, 0, 0, 0}},
-    {1,          {1, 0, 0, 0, 0}},
-    {0x7F,       {0x7F, 0, 0, 0, 0}},
-    {0x80,       {0x80, 1, 0, 0, 0}},
-    {0x81,       {0x81, 1, 0, 0, 0}},
-    {0xFF,       {0xFF, 1, 0, 0, 0}},
-    {0x4000,     {0x80, 0x80, 1, 0, 0}},
-    {0x4001,     {0x81, 0x80, 1, 0, 0}},
-    {0x4081,     {0x81, 0x81, 1, 0, 0}},
-    {0x0FFFFFFF, {0xFF, 0xFF, 0xFF, 0x7F, 0}},
-    {0xFFFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0xF}},
-};
-
-struct DecodeSignedLeb128TestCase {
-  int32_t decoded;
-  uint8_t leb128_data[5];
-};
-
-static DecodeSignedLeb128TestCase sleb128_tests[] = {
-    {0,          {0, 0, 0, 0, 0}},
-    {1,          {1, 0, 0, 0, 0}},
-    {0x3F,       {0x3F, 0, 0, 0, 0}},
-    {0x40,       {0xC0, 0 /* sign bit */, 0, 0, 0}},
-    {0x41,       {0xC1, 0 /* sign bit */, 0, 0, 0}},
-    {0x80,       {0x80, 1, 0, 0, 0}},
-    {0xFF,       {0xFF, 1, 0, 0, 0}},
-    {0x1FFF,     {0xFF, 0x3F, 0, 0, 0}},
-    {0x2000,     {0x80, 0xC0, 0 /* sign bit */, 0, 0}},
-    {0x2001,     {0x81, 0xC0, 0 /* sign bit */, 0, 0}},
-    {0x2081,     {0x81, 0xC1, 0 /* sign bit */, 0, 0}},
-    {0x4000,     {0x80, 0x80, 1, 0, 0}},
-    {0x0FFFFF,   {0xFF, 0xFF, 0x3F, 0, 0}},
-    {0x100000,   {0x80, 0x80, 0xC0, 0 /* sign bit */, 0}},
-    {0x100001,   {0x81, 0x80, 0xC0, 0 /* sign bit */, 0}},
-    {0x100081,   {0x81, 0x81, 0xC0, 0 /* sign bit */, 0}},
-    {0x104081,   {0x81, 0x81, 0xC1, 0 /* sign bit */, 0}},
-    {0x200000,   {0x80, 0x80, 0x80, 1, 0}},
-    {0x7FFFFFF,  {0xFF, 0xFF, 0xFF, 0x3F, 0}},
-    {0x8000000,  {0x80, 0x80, 0x80, 0xC0, 0 /* sign bit */}},
-    {0x8000001,  {0x81, 0x80, 0x80, 0xC0, 0 /* sign bit */}},
-    {0x8000081,  {0x81, 0x81, 0x80, 0xC0, 0 /* sign bit */}},
-    {0x8004081,  {0x81, 0x81, 0x81, 0xC0, 0 /* sign bit */}},
-    {0x8204081,  {0x81, 0x81, 0x81, 0xC1, 0 /* sign bit */}},
-    {0x0FFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0 /* sign bit */}},
-    {0x10000000, {0x80, 0x80, 0x80, 0x80, 1}},
-    {0x7FFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0x7}},
-    {-1,         {0x7F, 0, 0, 0, 0}},
-    {-2,         {0x7E, 0, 0, 0, 0}},
-    {-0x3F,      {0x41, 0, 0, 0, 0}},
-    {-0x40,      {0x40, 0, 0, 0, 0}},
-    {-0x41,      {0xBF, 0x7F, 0, 0, 0}},
-    {-0x80,      {0x80, 0x7F, 0, 0, 0}},
-    {-0x81,      {0xFF, 0x7E, 0, 0, 0}},
-    {-0x00002000, {0x80, 0x40, 0, 0, 0}},
-    {-0x00002001, {0xFF, 0xBF, 0x7F, 0, 0}},
-    {-0x00100000, {0x80, 0x80, 0x40, 0, 0}},
-    {-0x00100001, {0xFF, 0xFF, 0xBF, 0x7F, 0}},
-    {-0x08000000, {0x80, 0x80, 0x80, 0x40, 0}},
-    {-0x08000001, {0xFF, 0xFF, 0xFF, 0xBF, 0x7F}},
-    {-0x20000000, {0x80, 0x80, 0x80, 0x80, 0x7E}},
-    {(-1) << 31, {0x80, 0x80, 0x80, 0x80, 0x78}},
-};
-
-TEST(Leb128Test, UnsignedSinglesVector) {
-  // Test individual encodings.
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    Leb128EncodingVector builder;
-    builder.PushBackUnsigned(uleb128_tests[i].decoded);
-    EXPECT_EQ(UnsignedLeb128Size(uleb128_tests[i].decoded), builder.GetData().size());
-    const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
-    const uint8_t* encoded_data_ptr = &builder.GetData()[0];
-    for (size_t j = 0; j < 5; ++j) {
-      if (j < builder.GetData().size()) {
-        EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-      } else {
-        EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
-      }
-    }
-    EXPECT_EQ(DecodeUnsignedLeb128(&data_ptr), uleb128_tests[i].decoded) << " i = " << i;
-  }
-}
-
-TEST(Leb128Test, UnsignedSingles) {
-  // Test individual encodings.
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    uint8_t encoded_data[5];
-    uint8_t* end = EncodeUnsignedLeb128(encoded_data, uleb128_tests[i].decoded);
-    size_t data_size = static_cast<size_t>(end - encoded_data);
-    EXPECT_EQ(UnsignedLeb128Size(uleb128_tests[i].decoded), data_size);
-    const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < 5; ++j) {
-      if (j < data_size) {
-        EXPECT_EQ(data_ptr[j], encoded_data[j]) << " i = " << i << " j = " << j;
-      } else {
-        EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
-      }
-    }
-    EXPECT_EQ(DecodeUnsignedLeb128(&data_ptr), uleb128_tests[i].decoded) << " i = " << i;
-  }
-}
-
-TEST(Leb128Test, UnsignedStreamVector) {
-  // Encode a number of entries.
-  Leb128EncodingVector builder;
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    builder.PushBackUnsigned(uleb128_tests[i].decoded);
-  }
-  const uint8_t* encoded_data_ptr = &builder.GetData()[0];
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < UnsignedLeb128Size(uleb128_tests[i].decoded); ++j) {
-      EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-    }
-    for (size_t j = UnsignedLeb128Size(uleb128_tests[i].decoded); j < 5; ++j) {
-      EXPECT_EQ(data_ptr[j], 0) << " i = " << i << " j = " << j;
-    }
-    EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), uleb128_tests[i].decoded) << " i = " << i;
-  }
-  EXPECT_EQ(builder.GetData().size(),
-            static_cast<size_t>(encoded_data_ptr - &builder.GetData()[0]));
-}
-
-TEST(Leb128Test, UnsignedStream) {
-  // Encode a number of entries.
-  uint8_t encoded_data[5 * arraysize(uleb128_tests)];
-  uint8_t* end = encoded_data;
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    end = EncodeUnsignedLeb128(end, uleb128_tests[i].decoded);
-  }
-  size_t data_size = static_cast<size_t>(end - encoded_data);
-  const uint8_t* encoded_data_ptr = encoded_data;
-  for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
-    const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < UnsignedLeb128Size(uleb128_tests[i].decoded); ++j) {
-      EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-    }
-    for (size_t j = UnsignedLeb128Size(uleb128_tests[i].decoded); j < 5; ++j) {
-      EXPECT_EQ(data_ptr[j], 0) << " i = " << i << " j = " << j;
-    }
-    EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), uleb128_tests[i].decoded) << " i = " << i;
-  }
-  EXPECT_EQ(data_size, static_cast<size_t>(encoded_data_ptr - encoded_data));
-}
-
-TEST(Leb128Test, SignedSinglesVector) {
-  // Test individual encodings.
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    Leb128EncodingVector builder;
-    builder.PushBackSigned(sleb128_tests[i].decoded);
-    EXPECT_EQ(SignedLeb128Size(sleb128_tests[i].decoded), builder.GetData().size());
-    const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
-    const uint8_t* encoded_data_ptr = &builder.GetData()[0];
-    for (size_t j = 0; j < 5; ++j) {
-      if (j < builder.GetData().size()) {
-        EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-      } else {
-        EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
-      }
-    }
-    EXPECT_EQ(DecodeSignedLeb128(&data_ptr), sleb128_tests[i].decoded) << " i = " << i;
-  }
-}
-
-TEST(Leb128Test, SignedSingles) {
-  // Test individual encodings.
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    uint8_t encoded_data[5];
-    uint8_t* end = EncodeSignedLeb128(encoded_data, sleb128_tests[i].decoded);
-    size_t data_size = static_cast<size_t>(end - encoded_data);
-    EXPECT_EQ(SignedLeb128Size(sleb128_tests[i].decoded), data_size);
-    const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < 5; ++j) {
-      if (j < data_size) {
-        EXPECT_EQ(data_ptr[j], encoded_data[j]) << " i = " << i << " j = " << j;
-      } else {
-        EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
-      }
-    }
-    EXPECT_EQ(DecodeSignedLeb128(&data_ptr), sleb128_tests[i].decoded) << " i = " << i;
-  }
-}
-
-TEST(Leb128Test, SignedStreamVector) {
-  // Encode a number of entries.
-  Leb128EncodingVector builder;
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    builder.PushBackSigned(sleb128_tests[i].decoded);
-  }
-  const uint8_t* encoded_data_ptr = &builder.GetData()[0];
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < SignedLeb128Size(sleb128_tests[i].decoded); ++j) {
-      EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-    }
-    for (size_t j = SignedLeb128Size(sleb128_tests[i].decoded); j < 5; ++j) {
-      EXPECT_EQ(data_ptr[j], 0) << " i = " << i << " j = " << j;
-    }
-    EXPECT_EQ(DecodeSignedLeb128(&encoded_data_ptr), sleb128_tests[i].decoded) << " i = " << i;
-  }
-  EXPECT_EQ(builder.GetData().size(),
-            static_cast<size_t>(encoded_data_ptr - &builder.GetData()[0]));
-}
-
-TEST(Leb128Test, SignedStream) {
-  // Encode a number of entries.
-  uint8_t encoded_data[5 * arraysize(sleb128_tests)];
-  uint8_t* end = encoded_data;
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    end = EncodeSignedLeb128(end, sleb128_tests[i].decoded);
-  }
-  size_t data_size = static_cast<size_t>(end - encoded_data);
-  const uint8_t* encoded_data_ptr = encoded_data;
-  for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
-    const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
-    for (size_t j = 0; j < SignedLeb128Size(sleb128_tests[i].decoded); ++j) {
-      EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
-    }
-    for (size_t j = SignedLeb128Size(sleb128_tests[i].decoded); j < 5; ++j) {
-      EXPECT_EQ(data_ptr[j], 0) << " i = " << i << " j = " << j;
-    }
-    EXPECT_EQ(DecodeSignedLeb128(&encoded_data_ptr), sleb128_tests[i].decoded) << " i = " << i;
-  }
-  EXPECT_EQ(data_size, static_cast<size_t>(encoded_data_ptr - encoded_data));
-}
-
-TEST(Leb128Test, Speed) {
-  UniquePtr<Histogram<uint64_t> > enc_hist(new Histogram<uint64_t>("Leb128EncodeSpeedTest", 5));
-  UniquePtr<Histogram<uint64_t> > dec_hist(new Histogram<uint64_t>("Leb128DecodeSpeedTest", 5));
-  Leb128EncodingVector builder;
-  // Push back 1024 chunks of 1024 values measuring encoding speed.
-  uint64_t last_time = NanoTime();
-  for (size_t i = 0; i < 1024; i++) {
-    for (size_t j = 0; j < 1024; j++) {
-      builder.PushBackUnsigned((i * 1024) + j);
-    }
-    uint64_t cur_time = NanoTime();
-    enc_hist->AddValue(cur_time - last_time);
-    last_time = cur_time;
-  }
-  // Verify encoding and measure decode speed.
-  const uint8_t* encoded_data_ptr = &builder.GetData()[0];
-  last_time = NanoTime();
-  for (size_t i = 0; i < 1024; i++) {
-    for (size_t j = 0; j < 1024; j++) {
-      EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), (i * 1024) + j);
-    }
-    uint64_t cur_time = NanoTime();
-    dec_hist->AddValue(cur_time - last_time);
-    last_time = cur_time;
-  }
-
-  Histogram<uint64_t>::CumulativeData enc_data;
-  enc_hist->CreateHistogram(&enc_data);
-  enc_hist->PrintConfidenceIntervals(std::cout, 0.99, enc_data);
-
-  Histogram<uint64_t>::CumulativeData dec_data;
-  dec_hist->CreateHistogram(&dec_data);
-  dec_hist->PrintConfidenceIntervals(std::cout, 0.99, dec_data);
-}
-
-}  // namespace art
diff --git a/compiler/oat_test.cc b/compiler/oat_test.cc
index e91ffcb..55a962f 100644
--- a/compiler/oat_test.cc
+++ b/compiler/oat_test.cc
@@ -14,20 +14,19 @@
  * limitations under the License.
  */
 
-#include "compiler/oat_writer.h"
+#include "common_compiler_test.h"
 #include "compiler/compiler_backend.h"
+#include "compiler/oat_writer.h"
 #include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
-#include "mirror/object_array-inl.h"
 #include "mirror/object-inl.h"
+#include "mirror/object_array-inl.h"
 #include "oat_file.h"
 #include "vector_output_stream.h"
 
-#include "common_test.h"
-
 namespace art {
 
-class OatTest : public CommonTest {
+class OatTest : public CommonCompilerTest {
  protected:
   static const bool kCompile = false;  // DISABLED_ due to the time to compile libcore
 
@@ -81,7 +80,7 @@
 };
 
 TEST_F(OatTest, WriteRead) {
-  TimingLogger timings("CommonTest::WriteRead", false, false);
+  TimingLogger timings("OatTest::WriteRead", false, false);
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
 
   // TODO: make selectable.
diff --git a/compiler/output_stream_test.cc b/compiler/output_stream_test.cc
index a957ee3..290bf25 100644
--- a/compiler/output_stream_test.cc
+++ b/compiler/output_stream_test.cc
@@ -14,15 +14,16 @@
  * limitations under the License.
  */
 
-#include "base/logging.h"
-#include "buffered_output_stream.h"
-#include "common_test.h"
 #include "file_output_stream.h"
 #include "vector_output_stream.h"
 
+#include "base/logging.h"
+#include "buffered_output_stream.h"
+#include "common_runtime_test.h"
+
 namespace art {
 
-class OutputStreamTest : public CommonTest {
+class OutputStreamTest : public CommonRuntimeTest {
  protected:
   void CheckOffset(off_t expected) {
     off_t actual = output_stream_->Seek(0, kSeekCurrent);
diff --git a/compiler/sea_ir/ir/regions_test.cc b/compiler/sea_ir/ir/regions_test.cc
index 8ca51e4..95bd310 100644
--- a/compiler/sea_ir/ir/regions_test.cc
+++ b/compiler/sea_ir/ir/regions_test.cc
@@ -14,15 +14,14 @@
  * limitations under the License.
  */
 
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "sea_ir/ir/sea.h"
 
 using utils::ScopedHashtable;
 
 namespace sea_ir {
 
-class RegionsTest : public art::CommonTest {
-};
+class RegionsTest : public art::CommonCompilerTest {};
 
 TEST_F(RegionsTest, Basics) {
   sea_ir::SeaGraph sg(*java_lang_dex_file_);
diff --git a/compiler/sea_ir/types/type_data_test.cc b/compiler/sea_ir/types/type_data_test.cc
index f7a5362..42c6973 100644
--- a/compiler/sea_ir/types/type_data_test.cc
+++ b/compiler/sea_ir/types/type_data_test.cc
@@ -14,13 +14,12 @@
  * limitations under the License.
  */
 
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "sea_ir/types/types.h"
 
 namespace sea_ir {
 
-class TypeDataTest : public art::CommonTest {
-};
+class TypeDataTest : public art::CommonCompilerTest {};
 
 TEST_F(TypeDataTest, Basics) {
   TypeData td;
diff --git a/compiler/sea_ir/types/type_inference_visitor_test.cc b/compiler/sea_ir/types/type_inference_visitor_test.cc
index 77acb3d..ccb6991 100644
--- a/compiler/sea_ir/types/type_inference_visitor_test.cc
+++ b/compiler/sea_ir/types/type_inference_visitor_test.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "common_test.h"
+#include "common_compiler_test.h"
 #include "sea_ir/types/type_inference_visitor.h"
 #include "sea_ir/ir/sea.h"
 
@@ -31,8 +31,7 @@
   std::vector<InstructionNode*> producers_;
 };
 
-class TypeInferenceVisitorTest : public art::CommonTest {
-};
+class TypeInferenceVisitorTest : public art::CommonCompilerTest {};
 
 TEST_F(TypeInferenceVisitorTest, MergeIntWithByte) {
   TypeData td;
diff --git a/compiler/utils/scoped_hashtable_test.cc b/compiler/utils/scoped_hashtable_test.cc
index 68608f0..1c843eb 100644
--- a/compiler/utils/scoped_hashtable_test.cc
+++ b/compiler/utils/scoped_hashtable_test.cc
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#include "common_test.h"
-#include "utils/scoped_hashtable.h"
+#include "scoped_hashtable.h"
+
+#include "common_runtime_test.h"
 
 using utils::ScopedHashtable;
 
@@ -27,8 +28,7 @@
   int value_;
 };
 
-class ScopedHashtableTest : public CommonTest {
-};
+class ScopedHashtableTest : public testing::Test {};
 
 TEST_F(ScopedHashtableTest, Basics) {
   ScopedHashtable<int, Value*> sht;