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 | } |
buzbee | bafc342 | 2011-08-25 15:22:55 -0700 | [diff] [blame] | 46 | void AssertStaticLongMethod(const char* klass, const char* method, |
| 47 | const char* signature, jlong expected, ...) { |
| 48 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 49 | jclass c = env->FindClass(klass); |
| 50 | CHECK(c != NULL); |
| 51 | jmethodID m = env->GetStaticMethodID(c, method, signature); |
| 52 | CHECK(m != NULL); |
| 53 | #if defined(__arm__) |
| 54 | va_list args; |
| 55 | va_start(args, expected); |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 56 | jlong result = env->CallStaticLongMethodV(c, m, args); |
buzbee | bafc342 | 2011-08-25 15:22:55 -0700 | [diff] [blame] | 57 | va_end(args); |
| 58 | LOG(INFO) << klass << "." << method << "(...) result is " << result; |
| 59 | EXPECT_EQ(expected, result); |
| 60 | #endif // __arm__ |
| 61 | } |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 62 | private: |
| 63 | scoped_ptr<DexFile> dex_file_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 66 | TEST_F(CompilerTest, CompileDexLibCore) { |
| 67 | // TODO renenable when compiler can handle libcore |
| 68 | if (true) { |
| 69 | return; |
| 70 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 71 | Compiler compiler; |
| 72 | compiler.Compile(boot_class_path_); |
| 73 | |
| 74 | // All libcore references should resolve |
| 75 | const DexFile* dex = java_lang_dex_file_.get(); |
| 76 | DexCache* dex_cache = class_linker_->FindDexCache(*dex); |
| 77 | EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings()); |
| 78 | for (size_t i = 0; i < dex_cache->NumStrings(); i++) { |
| 79 | String* string = dex_cache->GetResolvedString(i); |
| 80 | EXPECT_TRUE(string != NULL); |
| 81 | } |
| 82 | EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumTypes()); |
| 83 | for (size_t i = 0; i < dex_cache->NumTypes(); i++) { |
| 84 | Class* type = dex_cache->GetResolvedType(i); |
| 85 | EXPECT_TRUE(type != NULL); |
| 86 | } |
| 87 | EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumMethods()); |
| 88 | for (size_t i = 0; i < dex_cache->NumMethods(); i++) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 89 | Method* method = dex_cache->GetResolvedMethod(i); |
| 90 | EXPECT_TRUE(method != NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 91 | } |
| 92 | EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumFields()); |
| 93 | for (size_t i = 0; i < dex_cache->NumFields(); i++) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 94 | Field* field = dex_cache->GetResolvedField(i); |
| 95 | EXPECT_TRUE(field != NULL); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 98 | // TODO check Class::IsVerified for all classes |
| 99 | |
| 100 | // TODO: check that all Method::GetCode() values are non-null |
| 101 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame^] | 102 | EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumCodeAndDirectMethods()); |
| 103 | CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods(); |
| 104 | for (size_t i = 0; i < dex_cache->NumCodeAndDirectMethods(); i++) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 105 | Method* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame^] | 106 | EXPECT_EQ(method->GetCode(), code_and_direct_methods->GetResolvedCode(i)); |
| 107 | EXPECT_EQ(method, code_and_direct_methods->GetResolvedMethod(i)); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 108 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 109 | } |
| 110 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 111 | TEST_F(CompilerTest, BasicCodegen) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 112 | CompileDex(kFibonacciDex, "kFibonacciDex"); |
| 113 | AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55, |
| 114 | 10); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 115 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 116 | |
| 117 | TEST_F(CompilerTest, UnopTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 118 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 119 | AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37, |
| 120 | 38); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 121 | } |
| 122 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 123 | TEST_F(CompilerTest, ShiftTest1) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 124 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 125 | AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 126 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 127 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 128 | TEST_F(CompilerTest, ShiftTest2) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 129 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 130 | AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 131 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 132 | |
| 133 | TEST_F(CompilerTest, UnsignedShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 134 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 135 | AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 136 | } |
| 137 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 138 | TEST_F(CompilerTest, ConvTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 139 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 140 | AssertStaticIntMethod("IntMath", "convTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 141 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 142 | |
| 143 | TEST_F(CompilerTest, CharSubTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 144 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 145 | AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 146 | } |
| 147 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 148 | TEST_F(CompilerTest, IntOperTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 149 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 150 | AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0, |
| 151 | 70000, -3); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 152 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 153 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 154 | TEST_F(CompilerTest, Lit16Test) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 155 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 156 | AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0, |
| 157 | 77777); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 158 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 159 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 160 | TEST_F(CompilerTest, Lit8Test) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 161 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 162 | AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0, |
| 163 | -55555); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 164 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 165 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 166 | TEST_F(CompilerTest, IntShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 167 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 168 | AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0, |
| 169 | 0xff00aa01, 8); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 170 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 171 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 172 | TEST_F(CompilerTest, LongOperTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 173 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 174 | AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0, |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 175 | 70000000000LL, -3LL); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 176 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 177 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 178 | TEST_F(CompilerTest, LongShiftTest) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 179 | CompileDex(kIntMathDex, "kIntMathDex"); |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 180 | AssertStaticLongMethod("IntMath", "longShiftTest", "(JI)J", |
| 181 | 0x96deff00aa010000LL, 0xd5aa96deff00aa01LL, 16); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 182 | } |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 183 | |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 184 | TEST_F(CompilerTest, SwitchTest1) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 185 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 186 | AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234, |
| 187 | 1); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | TEST_F(CompilerTest, IntCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 191 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 192 | AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111, |
| 193 | -5, 4, 4, 0); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | TEST_F(CompilerTest, LongCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 197 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 198 | AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222, |
| 199 | -5LL, -4294967287LL, 4LL, 8LL); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_F(CompilerTest, FloatCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 203 | CompileDex(kIntMathDex, "kIntMathDex"); |
buzbee | e6d6196 | 2011-08-27 11:58:19 -0700 | [diff] [blame] | 204 | AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333, |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 205 | -5.0f, 4.0f, 4.0f, |
| 206 | (1.0f/0.0f) / (1.0f/0.0f)); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(CompilerTest, DoubleCompare) { |
Brian Carlstrom | bffb155 | 2011-08-25 12:23:53 -0700 | [diff] [blame] | 210 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 211 | AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444, |
| 212 | -5.0, 4.0, 4.0, |
| 213 | (1.0/0.0) / (1.0/0.0)); |
buzbee | 9e0f9b0 | 2011-08-24 15:32:46 -0700 | [diff] [blame] | 214 | } |
| 215 | |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 216 | TEST_F(CompilerTest, RecursiveFibonacci) { |
| 217 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 218 | AssertStaticIntMethod("IntMath", "fibonacci", "(I)I", 55, |
| 219 | 10); |
| 220 | } |
buzbee | c5ef046 | 2011-08-25 18:44:49 -0700 | [diff] [blame] | 221 | |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 222 | #if 0 // Need to complete try/catch block handling |
| 223 | TEST_F(CompilerTest, ThrowAndCatch) { |
| 224 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 225 | AssertStaticIntMethod("IntMath", "throwAndCatch", "()I", 4); |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | TEST_F(CompilerTest, ManyArgs) { |
| 230 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 231 | AssertStaticIntMethod("IntMath", "manyArgs", |
| 232 | "(IJIJIJIIDFDSICIIBZIIJJIIIII)I", -1, |
| 233 | 0, 1LL, 2, 3LL, 4, 5LL, 6, 7, 8.0, 9.0f, 10.0, |
| 234 | (short)11, 12, (char)13, 14, 15, (int8_t)-16, true, 18, |
| 235 | 19, 20LL, 21LL, 22, 23, 24, 25, 26); |
| 236 | } |
| 237 | |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 238 | #if 0 // Need bdc's new Alloc(Method*, type_idx, [length]) |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 239 | TEST_F(CompilerTest, VirtualCall) { |
| 240 | CompileDex(kIntMathDex, "kIntMathDex"); |
| 241 | AssertStaticIntMethod("IntMath", "staticCall", "(I)I", 6, |
| 242 | 3); |
| 243 | } |
| 244 | #endif |
| 245 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 246 | } // namespace art |