Allow compilation of classes in different ClassLoaders

Change-Id: Ib0420471590a4c6d16bc25285ef3876098eacb36
diff --git a/src/compiler_test.cc b/src/compiler_test.cc
index 124668e..229235a 100644
--- a/src/compiler_test.cc
+++ b/src/compiler_test.cc
@@ -16,23 +16,76 @@
 
 class CompilerTest : public CommonTest {
  protected:
-  void CompileDex(const char* name) {
-    dex_file_.reset(OpenTestDexFile(name));
+
+  const ClassLoader* LoadDex(const char* dex_name) {
+    dex_file_.reset(OpenTestDexFile(dex_name));
     class_linker_->RegisterDexFile(*dex_file_.get());
     std::vector<const DexFile*> class_path;
     class_path.push_back(dex_file_.get());
-    Compiler compiler;
-    const ClassLoader* class_loader = compiler.Compile(class_path);
+    const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
     Thread::Current()->SetClassLoaderOverride(class_loader);
+    return class_loader;
   }
 
-  void AssertStaticIntMethod(const char* klass, const char* method, const char* signature,
+
+  void CompileDex(const char* dex_name) {
+    Compile(LoadDex(dex_name));
+  }
+
+  void CompileSystem() {
+    Compile(NULL);
+  }
+
+  void Compile(const ClassLoader* class_loader) {
+    Compiler compiler;
+    compiler.CompileAll(NULL);
+  }
+
+  std::string ConvertClassNameToClassDescriptor(const char* class_name) {
+    std::string desc;
+    desc += "L";
+    desc += class_name;
+    desc += ";";
+    std::replace(desc.begin(), desc.end(), '.', '/');
+    return desc;
+  }
+
+
+  void CompileDirectMethod(const ClassLoader* class_loader,
+                           const char* class_name,
+                           const char* method_name,
+                           const char* signature) {
+    std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
+    Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
+    CHECK(klass != NULL) << "Class not found " << class_name;
+    Method* method = klass->FindDirectMethod(method_name, signature);
+    CHECK(method != NULL) << "Method not found " << method_name;
+    Compiler compiler;
+    compiler.CompileOne(method);
+  }
+
+  void CompileVirtualMethod(const ClassLoader* class_loader,
+                            const char* class_name,
+                            const char* method_name,
+                            const char* signature) {
+    std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
+    Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
+    CHECK(klass != NULL) << "Class not found " << class_name;
+    Method* method = klass->FindVirtualMethod(method_name, signature);
+    CHECK(method != NULL) << "Method not found " << method_name;
+    Compiler compiler;
+    compiler.CompileOne(method);
+  }
+
+  void AssertStaticIntMethod(const ClassLoader* class_loader,
+                             const char* klass, const char* method, const char* signature,
                              jint expected, ...) {
+    CompileDirectMethod(class_loader, klass, method, signature);
     JNIEnv* env = Thread::Current()->GetJniEnv();
     jclass c = env->FindClass(klass);
-    CHECK(c != NULL);
+    CHECK(c != NULL) << "Class not found " << klass;
     jmethodID m = env->GetStaticMethodID(c, method, signature);
-    CHECK(m != NULL);
+    CHECK(m != NULL) << "Method not found " << method;
 #if defined(__arm__)
     va_list args;
     va_start(args, expected);
@@ -42,13 +95,15 @@
     EXPECT_EQ(expected, result);
 #endif // __arm__
   }
-  void AssertStaticLongMethod(const char* klass, const char* method,
-                              const char* signature, jlong expected, ...) {
+  void AssertStaticLongMethod(const ClassLoader* class_loader,
+                              const char* klass, const char* method, const char* signature,
+                              jlong expected, ...) {
+    CompileDirectMethod(class_loader, klass, method, signature);
     JNIEnv* env = Thread::Current()->GetJniEnv();
     jclass c = env->FindClass(klass);
-    CHECK(c != NULL);
+    CHECK(c != NULL) << "Class not found " << klass;
     jmethodID m = env->GetStaticMethodID(c, method, signature);
-    CHECK(m != NULL);
+    CHECK(m != NULL) << "Method not found " << method;
 #if defined(__arm__)
     va_list args;
     va_start(args, expected);
@@ -62,13 +117,10 @@
   scoped_ptr<const DexFile> dex_file_;
 };
 
-TEST_F(CompilerTest, CompileDexLibCore) {
-  // TODO renenable when compiler can handle libcore
-  if (true) {
-    return;
-  }
+// TODO renenable when compiler can handle libcore
+TEST_F(CompilerTest, DISABLED_CompileDexLibCore) {
   Compiler compiler;
-  compiler.Compile(boot_class_path_);
+  compiler.CompileAll(NULL);
 
   // All libcore references should resolve
   const DexFile* dex = java_lang_dex_file_.get();
@@ -108,132 +160,110 @@
 }
 
 TEST_F(CompilerTest, BasicCodegen) {
-  CompileDex("Fibonacci");
-  AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55,
+  AssertStaticIntMethod(LoadDex("Fibonacci"), "Fibonacci", "fibonacci", "(I)I", 55,
                         10);
 }
 
 TEST_F(CompilerTest, StaticFieldTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "staticFieldTest", "(I)I", 1404,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "staticFieldTest", "(I)I", 1404,
                         404);
 }
 
 TEST_F(CompilerTest, UnopTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "unopTest", "(I)I", 37,
                         38);
 }
 
 TEST_F(CompilerTest, ShiftTest1) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "shiftTest1", "()I", 0);
 }
 
 TEST_F(CompilerTest, ShiftTest2) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "shiftTest2", "()I", 0);
 }
 
 TEST_F(CompilerTest, UnsignedShiftTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "unsignedShiftTest", "()I", 0);
 }
 
 TEST_F(CompilerTest, ConvTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "convTest", "()I", 0);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "convTest", "()I", 0);
 }
 
 TEST_F(CompilerTest, CharSubTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "charSubTest", "()I", 0);
 }
 
 TEST_F(CompilerTest, IntOperTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "intOperTest", "(II)I", 0,
                         70000, -3);
 }
 
 TEST_F(CompilerTest, Lit16Test) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "lit16Test", "(I)I", 0,
                         77777);
 }
 
 TEST_F(CompilerTest, Lit8Test) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "lit8Test", "(I)I", 0,
                         -55555);
 }
 
 TEST_F(CompilerTest, IntShiftTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "intShiftTest", "(II)I", 0,
                         0xff00aa01, 8);
 }
 
 TEST_F(CompilerTest, LongOperTest) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "longOperTest", "(JJ)I", 0,
                         70000000000LL, -3LL);
 }
 
 TEST_F(CompilerTest, LongShiftTest) {
-  CompileDex("IntMath");
-  AssertStaticLongMethod("IntMath", "longShiftTest", "(JI)J",
+  AssertStaticLongMethod(LoadDex("IntMath"), "IntMath", "longShiftTest", "(JI)J",
                          0x96deff00aa010000LL, 0xd5aa96deff00aa01LL, 16);
 }
 
 TEST_F(CompilerTest, SwitchTest1) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "switchTest", "(I)I", 1234,
                         1);
 }
 
 TEST_F(CompilerTest, IntCompare) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "testIntCompare", "(IIII)I", 1111,
                         -5, 4, 4, 0);
 }
 
 TEST_F(CompilerTest, LongCompare) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "testLongCompare", "(JJJJ)I", 2222,
                         -5LL, -4294967287LL, 4LL, 8LL);
 }
 
 TEST_F(CompilerTest, FloatCompare) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "testFloatCompare", "(FFFF)I", 3333,
                         -5.0f, 4.0f, 4.0f,
                         (1.0f/0.0f) / (1.0f/0.0f));
 }
 
 TEST_F(CompilerTest, DoubleCompare) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "testDoubleCompare", "(DDDD)I", 4444,
                                     -5.0, 4.0, 4.0,
                                     (1.0/0.0) / (1.0/0.0));
 }
 
 TEST_F(CompilerTest, RecursiveFibonacci) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "fibonacci", "(I)I", 55,
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "fibonacci", "(I)I", 55,
                         10);
 }
 
 #if 0 // Need to complete try/catch block handling
 TEST_F(CompilerTest, ThrowAndCatch) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "throwAndCatch", "()I", 4);
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "throwAndCatch", "()I", 4);
 }
 #endif
 
 TEST_F(CompilerTest, ManyArgs) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "manyArgs",
+  AssertStaticIntMethod(LoadDex("IntMath"), "IntMath", "manyArgs",
                         "(IJIJIJIIDFDSICIIBZIIJJIIIII)I", -1,
                         0, 1LL, 2, 3LL, 4, 5LL, 6, 7, 8.0, 9.0f, 10.0,
                         (short)11, 12, (char)13, 14, 15, (int8_t)-16, true, 18,
@@ -241,14 +271,22 @@
 }
 
 TEST_F(CompilerTest, VirtualCall) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "staticCall", "(I)I", 6,
+  CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
+  const ClassLoader* class_loader = LoadDex("IntMath");
+  CompileDirectMethod(class_loader, "IntMath", "<init>", "()V");
+  CompileVirtualMethod(class_loader, "IntMath", "virtualCall", "(I)I");
+  AssertStaticIntMethod(class_loader, "IntMath", "staticCall", "(I)I", 6,
                         3);
 }
 
 TEST_F(CompilerTest, TestIGetPut) {
-  CompileDex("IntMath");
-  AssertStaticIntMethod("IntMath", "testIGetPut", "(I)I", 333,
+  CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V");
+  const ClassLoader* class_loader = LoadDex("IntMath");
+  CompileDirectMethod(class_loader, "IntMath", "<init>", "(I)V");
+  CompileDirectMethod(class_loader, "IntMath", "<init>", "()V");
+  CompileVirtualMethod(class_loader, "IntMath", "getFoo", "()I");
+  CompileVirtualMethod(class_loader, "IntMath", "setFoo", "(I)V");
+  AssertStaticIntMethod(class_loader, "IntMath", "testIGetPut", "(I)I", 333,
                         111);
 }