buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "class_linker.h" |
| 4 | #include "common_test.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5 | #include "compiler.h" |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 6 | #include "compiler_test.h" |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7 | #include "dex_cache.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 8 | #include "dex_file.h" |
| 9 | #include "heap.h" |
| 10 | #include "object.h" |
| 11 | #include "scoped_ptr.h" |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | #include <stdio.h> |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | class CompilerTest : public CommonTest { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 19 | protected: |
| 20 | void CompileDex(const char* base64_dex, const char* base64_name) { |
| 21 | dex_file_.reset(OpenDexFileBase64(base64_dex, base64_name)); |
| 22 | class_linker_->RegisterDexFile(*dex_file_.get()); |
| 23 | std::vector<const DexFile*> class_path; |
| 24 | class_path.push_back(dex_file_.get()); |
| 25 | Compiler compiler; |
| 26 | const ClassLoader* class_loader = compiler.Compile(class_path); |
| 27 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 28 | } |
| 29 | |
| 30 | void AssertStaticIntMethod(const char* klass, const char* method, const char* signature, |
| 31 | jint expected, ...) { |
| 32 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 33 | jclass c = env->FindClass(klass); |
| 34 | CHECK(c != NULL); |
| 35 | jmethodID m = env->GetStaticMethodID(c, method, signature); |
| 36 | CHECK(m != NULL); |
| 37 | #if defined(__arm__) |
| 38 | va_list args; |
| 39 | va_start(args, expected); |
| 40 | jint result = env->CallStaticIntMethodV(c, m, args); |
| 41 | va_end(args); |
| 42 | LOG(INFO) << klass << "." << method << "(...) result is " << result; |
| 43 | EXPECT_EQ(expected, result); |
| 44 | #endif // __arm__ |
| 45 | } |
| 46 | private: |
| 47 | scoped_ptr<DexFile> dex_file_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 50 | TEST_F(CompilerTest, CompileDexLibCore) { |
| 51 | // TODO renenable when compiler can handle libcore |
| 52 | if (true) { |
| 53 | return; |
| 54 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 55 | Compiler compiler; |
| 56 | compiler.Compile(boot_class_path_); |
| 57 | |
| 58 | // All libcore references should resolve |
| 59 | const DexFile* dex = java_lang_dex_file_.get(); |
| 60 | DexCache* dex_cache = class_linker_->FindDexCache(*dex); |
| 61 | EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings()); |
| 62 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
| 63 | String* string = dex_cache->GetResolvedString(i); |
| 64 | EXPECT_TRUE(string != NULL); |
| 65 | } |
| 66 | EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumTypes()); |
| 67 | for (size_t i = 0; i < dex_cache->NumTypes(); i++) { |
| 68 | Class* type = dex_cache->GetResolvedType(i); |
| 69 | EXPECT_TRUE(type != NULL); |
| 70 | } |
| 71 | EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumMethods()); |
| 72 | for (size_t i = 0; i < dex_cache->NumMethods(); i++) { |
| 73 | // TODO: ClassLinker::ResolveMethod |
| 74 | // Method* method = dex_cache->GetResolvedMethod(i); |
| 75 | // EXPECT_TRUE(method != NULL); |
| 76 | } |
| 77 | EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumFields()); |
| 78 | for (size_t i = 0; i < dex_cache->NumFields(); i++) { |
| 79 | // TODO: ClassLinker::ResolveField |
| 80 | // Field* field = dex_cache->GetResolvedField(i); |
| 81 | // EXPECT_TRUE(field != NULL); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 86 | TEST_F(CompilerTest, BasicCodegen) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 87 | CompileDex(kFibonacciDex, "kFibonacciDex"); |
| 88 | AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55, |
| 89 | 10); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 90 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 91 | |
| 92 | TEST_F(CompilerTest, UnopTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 93 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 94 | AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37, |
| 95 | 38); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | #if 0 // Does filled array - needs load-time class resolution |
| 99 | TEST_F(CompilerTest, ShiftTest1) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 100 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 101 | AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 102 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 103 | #endif |
| 104 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 105 | TEST_F(CompilerTest, ShiftTest2) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 106 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 107 | AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 108 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 109 | |
| 110 | TEST_F(CompilerTest, UnsignedShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 111 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 112 | AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #if 0 // Fail subtest #3, long to int conversion w/ truncation. |
| 116 | TEST_F(CompilerTest, ConvTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 117 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 118 | AssertStaticIntMethod("IntMath", "convTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 119 | } |
| 120 | #endif |
| 121 | |
| 122 | TEST_F(CompilerTest, CharSubTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 123 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 124 | AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 128 | TEST_F(CompilerTest, IntOperTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 129 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 130 | AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0, |
| 131 | 70000, -3); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 132 | } |
| 133 | #endif |
| 134 | |
| 135 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 136 | TEST_F(CompilerTest, Lit16Test) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 137 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 138 | AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0, |
| 139 | 77777); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 140 | } |
| 141 | #endif |
| 142 | |
| 143 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 144 | TEST_F(CompilerTest, Lit8Test) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 145 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 146 | AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0, |
| 147 | -55555); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 148 | } |
| 149 | #endif |
| 150 | |
| 151 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 152 | TEST_F(CompilerTest, IntShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 153 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 154 | AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0, |
| 155 | 0xff00aa01, 8); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 156 | } |
| 157 | #endif |
| 158 | |
| 159 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 160 | TEST_F(CompilerTest, LongOperTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 161 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 162 | AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0, |
| 163 | 70000000000LL, 3); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #if 0 // Needs array allocation & r9 to be set up with Thread* |
| 168 | TEST_F(CompilerTest, LongShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 169 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 170 | AssertStaticIntMethod("IntMath", "longShiftTest", "(JJ)I", 0, |
| 171 | 0xd5aa96deff00aa01LL, 8); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 172 | } |
| 173 | #endif |
| 174 | |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 175 | TEST_F(CompilerTest, SwitchTest1) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 176 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 177 | AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234, |
| 178 | 1); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST_F(CompilerTest, IntCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 182 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 183 | AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111, |
| 184 | -5, 4, 4, 0); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(CompilerTest, LongCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 188 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 189 | AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222, |
| 190 | -5LL, -4294967287LL, 4LL, 8LL); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 193 | #if 0 |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 194 | TEST_F(CompilerTest, FloatCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 195 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 196 | AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333 |
| 197 | -5.0f, 4.0f, 4.0f, |
| 198 | (1.0f/0.0f) / (1.0f/0.0f)); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 199 | } |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 200 | #endif |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 201 | |
| 202 | TEST_F(CompilerTest, DoubleCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame^] | 203 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 204 | AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444, |
| 205 | -5.0, 4.0, 4.0, |
| 206 | (1.0/0.0) / (1.0/0.0)); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 209 | } // namespace art |