blob: 124668e38de58847ec0bfcdf2e7f6705d9f2f26f [file] [log] [blame]
buzbeec143c552011-08-20 17:38:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "class_linker.h"
4#include "common_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07005#include "compiler.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07006#include "dex_cache.h"
buzbeec143c552011-08-20 17:38:58 -07007#include "dex_file.h"
8#include "heap.h"
9#include "object.h"
10#include "scoped_ptr.h"
11
12#include <stdint.h>
13#include <stdio.h>
buzbeec143c552011-08-20 17:38:58 -070014
15namespace art {
16
17class CompilerTest : public CommonTest {
Brian Carlstrombffb1552011-08-25 12:23:53 -070018 protected:
Brian Carlstrom9f30b382011-08-28 22:41:38 -070019 void CompileDex(const char* name) {
20 dex_file_.reset(OpenTestDexFile(name));
Brian Carlstrombffb1552011-08-25 12:23:53 -070021 class_linker_->RegisterDexFile(*dex_file_.get());
22 std::vector<const DexFile*> class_path;
23 class_path.push_back(dex_file_.get());
24 Compiler compiler;
25 const ClassLoader* class_loader = compiler.Compile(class_path);
26 Thread::Current()->SetClassLoaderOverride(class_loader);
27 }
28
29 void AssertStaticIntMethod(const char* klass, const char* method, const char* signature,
30 jint expected, ...) {
31 JNIEnv* env = Thread::Current()->GetJniEnv();
32 jclass c = env->FindClass(klass);
33 CHECK(c != NULL);
34 jmethodID m = env->GetStaticMethodID(c, method, signature);
35 CHECK(m != NULL);
36#if defined(__arm__)
37 va_list args;
38 va_start(args, expected);
39 jint result = env->CallStaticIntMethodV(c, m, args);
40 va_end(args);
41 LOG(INFO) << klass << "." << method << "(...) result is " << result;
42 EXPECT_EQ(expected, result);
43#endif // __arm__
44 }
buzbeebafc3422011-08-25 15:22:55 -070045 void AssertStaticLongMethod(const char* klass, const char* method,
46 const char* signature, jlong expected, ...) {
47 JNIEnv* env = Thread::Current()->GetJniEnv();
48 jclass c = env->FindClass(klass);
49 CHECK(c != NULL);
50 jmethodID m = env->GetStaticMethodID(c, method, signature);
51 CHECK(m != NULL);
52#if defined(__arm__)
53 va_list args;
54 va_start(args, expected);
buzbeec5ef0462011-08-25 18:44:49 -070055 jlong result = env->CallStaticLongMethodV(c, m, args);
buzbeebafc3422011-08-25 15:22:55 -070056 va_end(args);
57 LOG(INFO) << klass << "." << method << "(...) result is " << result;
58 EXPECT_EQ(expected, result);
59#endif // __arm__
60 }
Brian Carlstrombffb1552011-08-25 12:23:53 -070061 private:
Brian Carlstrom9f30b382011-08-28 22:41:38 -070062 scoped_ptr<const DexFile> dex_file_;
buzbeec143c552011-08-20 17:38:58 -070063};
64
Brian Carlstrombffb1552011-08-25 12:23:53 -070065TEST_F(CompilerTest, CompileDexLibCore) {
66 // TODO renenable when compiler can handle libcore
67 if (true) {
68 return;
69 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070070 Compiler compiler;
71 compiler.Compile(boot_class_path_);
72
73 // All libcore references should resolve
74 const DexFile* dex = java_lang_dex_file_.get();
75 DexCache* dex_cache = class_linker_->FindDexCache(*dex);
76 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings());
77 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
78 String* string = dex_cache->GetResolvedString(i);
79 EXPECT_TRUE(string != NULL);
80 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070081 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
82 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070083 Class* type = dex_cache->GetResolvedType(i);
84 EXPECT_TRUE(type != NULL);
85 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070086 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
87 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070088 Method* method = dex_cache->GetResolvedMethod(i);
89 EXPECT_TRUE(method != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070090 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070091 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
92 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070093 Field* field = dex_cache->GetResolvedField(i);
94 EXPECT_TRUE(field != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095 }
96
Brian Carlstrom83db7722011-08-26 17:32:56 -070097 // TODO check Class::IsVerified for all classes
98
99 // TODO: check that all Method::GetCode() values are non-null
100
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700101 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumCodeAndDirectMethods());
102 CodeAndDirectMethods* code_and_direct_methods = dex_cache->GetCodeAndDirectMethods();
103 for (size_t i = 0; i < dex_cache->NumCodeAndDirectMethods(); i++) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700104 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700105 EXPECT_EQ(method->GetCode(), code_and_direct_methods->GetResolvedCode(i));
106 EXPECT_EQ(method, code_and_direct_methods->GetResolvedMethod(i));
Brian Carlstrom83db7722011-08-26 17:32:56 -0700107 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700108}
109
buzbeec143c552011-08-20 17:38:58 -0700110TEST_F(CompilerTest, BasicCodegen) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700111 CompileDex("Fibonacci");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700112 AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55,
113 10);
buzbeec143c552011-08-20 17:38:58 -0700114}
buzbee3ea4ec52011-08-22 17:37:19 -0700115
buzbeee1931742011-08-28 21:15:53 -0700116TEST_F(CompilerTest, StaticFieldTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700117 CompileDex("IntMath");
buzbeee1931742011-08-28 21:15:53 -0700118 AssertStaticIntMethod("IntMath", "staticFieldTest", "(I)I", 1404,
119 404);
120}
121
buzbee3ea4ec52011-08-22 17:37:19 -0700122TEST_F(CompilerTest, UnopTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700123 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700124 AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37,
125 38);
buzbee3ea4ec52011-08-22 17:37:19 -0700126}
127
buzbee3ea4ec52011-08-22 17:37:19 -0700128TEST_F(CompilerTest, ShiftTest1) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700129 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700130 AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700131}
buzbeec143c552011-08-20 17:38:58 -0700132
buzbee3ea4ec52011-08-22 17:37:19 -0700133TEST_F(CompilerTest, ShiftTest2) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700134 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700135 AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700136}
buzbee3ea4ec52011-08-22 17:37:19 -0700137
138TEST_F(CompilerTest, UnsignedShiftTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700139 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700140 AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700141}
142
buzbee3ea4ec52011-08-22 17:37:19 -0700143TEST_F(CompilerTest, ConvTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700144 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700145 AssertStaticIntMethod("IntMath", "convTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700146}
buzbee3ea4ec52011-08-22 17:37:19 -0700147
148TEST_F(CompilerTest, CharSubTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700149 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700150 AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700151}
152
buzbee3ea4ec52011-08-22 17:37:19 -0700153TEST_F(CompilerTest, IntOperTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700154 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700155 AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0,
156 70000, -3);
buzbee3ea4ec52011-08-22 17:37:19 -0700157}
buzbee3ea4ec52011-08-22 17:37:19 -0700158
buzbee3ea4ec52011-08-22 17:37:19 -0700159TEST_F(CompilerTest, Lit16Test) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700160 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700161 AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0,
162 77777);
buzbee3ea4ec52011-08-22 17:37:19 -0700163}
buzbee3ea4ec52011-08-22 17:37:19 -0700164
buzbee3ea4ec52011-08-22 17:37:19 -0700165TEST_F(CompilerTest, Lit8Test) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700166 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700167 AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0,
168 -55555);
buzbee3ea4ec52011-08-22 17:37:19 -0700169}
buzbee3ea4ec52011-08-22 17:37:19 -0700170
buzbee3ea4ec52011-08-22 17:37:19 -0700171TEST_F(CompilerTest, IntShiftTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700172 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700173 AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0,
174 0xff00aa01, 8);
buzbee3ea4ec52011-08-22 17:37:19 -0700175}
buzbee3ea4ec52011-08-22 17:37:19 -0700176
buzbee3ea4ec52011-08-22 17:37:19 -0700177TEST_F(CompilerTest, LongOperTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700178 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700179 AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0,
buzbee439c4fa2011-08-27 15:59:07 -0700180 70000000000LL, -3LL);
buzbee3ea4ec52011-08-22 17:37:19 -0700181}
buzbee3ea4ec52011-08-22 17:37:19 -0700182
buzbee3ea4ec52011-08-22 17:37:19 -0700183TEST_F(CompilerTest, LongShiftTest) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700184 CompileDex("IntMath");
buzbee7b1b86d2011-08-26 18:59:10 -0700185 AssertStaticLongMethod("IntMath", "longShiftTest", "(JI)J",
186 0x96deff00aa010000LL, 0xd5aa96deff00aa01LL, 16);
buzbee3ea4ec52011-08-22 17:37:19 -0700187}
buzbee3ea4ec52011-08-22 17:37:19 -0700188
buzbee9e0f9b02011-08-24 15:32:46 -0700189TEST_F(CompilerTest, SwitchTest1) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700190 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700191 AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234,
192 1);
buzbee9e0f9b02011-08-24 15:32:46 -0700193}
194
195TEST_F(CompilerTest, IntCompare) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700196 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700197 AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111,
198 -5, 4, 4, 0);
buzbee9e0f9b02011-08-24 15:32:46 -0700199}
200
201TEST_F(CompilerTest, LongCompare) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700202 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700203 AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222,
204 -5LL, -4294967287LL, 4LL, 8LL);
buzbee9e0f9b02011-08-24 15:32:46 -0700205}
206
207TEST_F(CompilerTest, FloatCompare) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700208 CompileDex("IntMath");
buzbeee6d61962011-08-27 11:58:19 -0700209 AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333,
Brian Carlstrombffb1552011-08-25 12:23:53 -0700210 -5.0f, 4.0f, 4.0f,
211 (1.0f/0.0f) / (1.0f/0.0f));
buzbee9e0f9b02011-08-24 15:32:46 -0700212}
213
214TEST_F(CompilerTest, DoubleCompare) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700215 CompileDex("IntMath");
Brian Carlstrombffb1552011-08-25 12:23:53 -0700216 AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444,
217 -5.0, 4.0, 4.0,
218 (1.0/0.0) / (1.0/0.0));
buzbee9e0f9b02011-08-24 15:32:46 -0700219}
220
buzbeec5ef0462011-08-25 18:44:49 -0700221TEST_F(CompilerTest, RecursiveFibonacci) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700222 CompileDex("IntMath");
buzbeec5ef0462011-08-25 18:44:49 -0700223 AssertStaticIntMethod("IntMath", "fibonacci", "(I)I", 55,
224 10);
225}
buzbeec5ef0462011-08-25 18:44:49 -0700226
buzbee7b1b86d2011-08-26 18:59:10 -0700227#if 0 // Need to complete try/catch block handling
228TEST_F(CompilerTest, ThrowAndCatch) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700229 CompileDex("IntMath");
buzbee7b1b86d2011-08-26 18:59:10 -0700230 AssertStaticIntMethod("IntMath", "throwAndCatch", "()I", 4);
231}
232#endif
233
234TEST_F(CompilerTest, ManyArgs) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700235 CompileDex("IntMath");
buzbee7b1b86d2011-08-26 18:59:10 -0700236 AssertStaticIntMethod("IntMath", "manyArgs",
237 "(IJIJIJIIDFDSICIIBZIIJJIIIII)I", -1,
238 0, 1LL, 2, 3LL, 4, 5LL, 6, 7, 8.0, 9.0f, 10.0,
239 (short)11, 12, (char)13, 14, 15, (int8_t)-16, true, 18,
240 19, 20LL, 21LL, 22, 23, 24, 25, 26);
241}
242
buzbee7b1b86d2011-08-26 18:59:10 -0700243TEST_F(CompilerTest, VirtualCall) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700244 CompileDex("IntMath");
buzbee7b1b86d2011-08-26 18:59:10 -0700245 AssertStaticIntMethod("IntMath", "staticCall", "(I)I", 6,
246 3);
247}
buzbee7b1b86d2011-08-26 18:59:10 -0700248
buzbeedd3efae2011-08-28 14:39:07 -0700249TEST_F(CompilerTest, TestIGetPut) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700250 CompileDex("IntMath");
buzbeedd3efae2011-08-28 14:39:07 -0700251 AssertStaticIntMethod("IntMath", "testIGetPut", "(I)I", 333,
252 111);
253}
254
buzbeec143c552011-08-20 17:38:58 -0700255} // namespace art