blob: ca357f27ab2095bb78b3ad8de107144966b28b45 [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"
buzbee9e0f9b02011-08-24 15:32:46 -07006#include "compiler_test.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07007#include "dex_cache.h"
buzbeec143c552011-08-20 17:38:58 -07008#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>
buzbeec143c552011-08-20 17:38:58 -070015
16namespace art {
17
18class CompilerTest : public CommonTest {
Brian Carlstrombffb1552011-08-25 12:23:53 -070019 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 }
buzbeebafc3422011-08-25 15:22:55 -070046 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);
buzbeec5ef0462011-08-25 18:44:49 -070056 jlong result = env->CallStaticLongMethodV(c, m, args);
buzbeebafc3422011-08-25 15:22:55 -070057 va_end(args);
58 LOG(INFO) << klass << "." << method << "(...) result is " << result;
59 EXPECT_EQ(expected, result);
60#endif // __arm__
61 }
Brian Carlstrombffb1552011-08-25 12:23:53 -070062 private:
63 scoped_ptr<DexFile> dex_file_;
buzbeec143c552011-08-20 17:38:58 -070064};
65
Brian Carlstrombffb1552011-08-25 12:23:53 -070066TEST_F(CompilerTest, CompileDexLibCore) {
67 // TODO renenable when compiler can handle libcore
68 if (true) {
69 return;
70 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070071 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 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070082 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes());
83 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070084 Class* type = dex_cache->GetResolvedType(i);
85 EXPECT_TRUE(type != NULL);
86 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070087 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods());
88 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070089 Method* method = dex_cache->GetResolvedMethod(i);
90 EXPECT_TRUE(method != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091 }
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070092 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields());
93 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070094 Field* field = dex_cache->GetResolvedField(i);
95 EXPECT_TRUE(field != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096 }
97
Brian Carlstrom83db7722011-08-26 17:32:56 -070098 // TODO check Class::IsVerified for all classes
99
100 // TODO: check that all Method::GetCode() values are non-null
101
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700102 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 Carlstrom83db7722011-08-26 17:32:56 -0700105 Method* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700106 EXPECT_EQ(method->GetCode(), code_and_direct_methods->GetResolvedCode(i));
107 EXPECT_EQ(method, code_and_direct_methods->GetResolvedMethod(i));
Brian Carlstrom83db7722011-08-26 17:32:56 -0700108 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700109}
110
buzbeec143c552011-08-20 17:38:58 -0700111TEST_F(CompilerTest, BasicCodegen) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700112 CompileDex(kFibonacciDex, "kFibonacciDex");
113 AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55,
114 10);
buzbeec143c552011-08-20 17:38:58 -0700115}
buzbee3ea4ec52011-08-22 17:37:19 -0700116
buzbeee1931742011-08-28 21:15:53 -0700117TEST_F(CompilerTest, StaticFieldTest) {
118 CompileDex(kIntMathDex, "kIntMathDex");
119 AssertStaticIntMethod("IntMath", "staticFieldTest", "(I)I", 1404,
120 404);
121}
122
buzbee3ea4ec52011-08-22 17:37:19 -0700123TEST_F(CompilerTest, UnopTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700124 CompileDex(kIntMathDex, "kIntMathDex");
125 AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37,
126 38);
buzbee3ea4ec52011-08-22 17:37:19 -0700127}
128
buzbee3ea4ec52011-08-22 17:37:19 -0700129TEST_F(CompilerTest, ShiftTest1) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700130 CompileDex(kIntMathDex, "kIntMathDex");
131 AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700132}
buzbeec143c552011-08-20 17:38:58 -0700133
buzbee3ea4ec52011-08-22 17:37:19 -0700134TEST_F(CompilerTest, ShiftTest2) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700135 CompileDex(kIntMathDex, "kIntMathDex");
136 AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700137}
buzbee3ea4ec52011-08-22 17:37:19 -0700138
139TEST_F(CompilerTest, UnsignedShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700140 CompileDex(kIntMathDex, "kIntMathDex");
141 AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700142}
143
buzbee3ea4ec52011-08-22 17:37:19 -0700144TEST_F(CompilerTest, ConvTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700145 CompileDex(kIntMathDex, "kIntMathDex");
146 AssertStaticIntMethod("IntMath", "convTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700147}
buzbee3ea4ec52011-08-22 17:37:19 -0700148
149TEST_F(CompilerTest, CharSubTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700150 CompileDex(kIntMathDex, "kIntMathDex");
151 AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700152}
153
buzbee3ea4ec52011-08-22 17:37:19 -0700154TEST_F(CompilerTest, IntOperTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700155 CompileDex(kIntMathDex, "kIntMathDex");
156 AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0,
157 70000, -3);
buzbee3ea4ec52011-08-22 17:37:19 -0700158}
buzbee3ea4ec52011-08-22 17:37:19 -0700159
buzbee3ea4ec52011-08-22 17:37:19 -0700160TEST_F(CompilerTest, Lit16Test) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700161 CompileDex(kIntMathDex, "kIntMathDex");
162 AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0,
163 77777);
buzbee3ea4ec52011-08-22 17:37:19 -0700164}
buzbee3ea4ec52011-08-22 17:37:19 -0700165
buzbee3ea4ec52011-08-22 17:37:19 -0700166TEST_F(CompilerTest, Lit8Test) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700167 CompileDex(kIntMathDex, "kIntMathDex");
168 AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0,
169 -55555);
buzbee3ea4ec52011-08-22 17:37:19 -0700170}
buzbee3ea4ec52011-08-22 17:37:19 -0700171
buzbee3ea4ec52011-08-22 17:37:19 -0700172TEST_F(CompilerTest, IntShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700173 CompileDex(kIntMathDex, "kIntMathDex");
174 AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0,
175 0xff00aa01, 8);
buzbee3ea4ec52011-08-22 17:37:19 -0700176}
buzbee3ea4ec52011-08-22 17:37:19 -0700177
buzbee3ea4ec52011-08-22 17:37:19 -0700178TEST_F(CompilerTest, LongOperTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700179 CompileDex(kIntMathDex, "kIntMathDex");
180 AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0,
buzbee439c4fa2011-08-27 15:59:07 -0700181 70000000000LL, -3LL);
buzbee3ea4ec52011-08-22 17:37:19 -0700182}
buzbee3ea4ec52011-08-22 17:37:19 -0700183
buzbee3ea4ec52011-08-22 17:37:19 -0700184TEST_F(CompilerTest, LongShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700185 CompileDex(kIntMathDex, "kIntMathDex");
buzbee7b1b86d2011-08-26 18:59:10 -0700186 AssertStaticLongMethod("IntMath", "longShiftTest", "(JI)J",
187 0x96deff00aa010000LL, 0xd5aa96deff00aa01LL, 16);
buzbee3ea4ec52011-08-22 17:37:19 -0700188}
buzbee3ea4ec52011-08-22 17:37:19 -0700189
buzbee9e0f9b02011-08-24 15:32:46 -0700190TEST_F(CompilerTest, SwitchTest1) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700191 CompileDex(kIntMathDex, "kIntMathDex");
192 AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234,
193 1);
buzbee9e0f9b02011-08-24 15:32:46 -0700194}
195
196TEST_F(CompilerTest, IntCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700197 CompileDex(kIntMathDex, "kIntMathDex");
198 AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111,
199 -5, 4, 4, 0);
buzbee9e0f9b02011-08-24 15:32:46 -0700200}
201
202TEST_F(CompilerTest, LongCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700203 CompileDex(kIntMathDex, "kIntMathDex");
204 AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222,
205 -5LL, -4294967287LL, 4LL, 8LL);
buzbee9e0f9b02011-08-24 15:32:46 -0700206}
207
208TEST_F(CompilerTest, FloatCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700209 CompileDex(kIntMathDex, "kIntMathDex");
buzbeee6d61962011-08-27 11:58:19 -0700210 AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333,
Brian Carlstrombffb1552011-08-25 12:23:53 -0700211 -5.0f, 4.0f, 4.0f,
212 (1.0f/0.0f) / (1.0f/0.0f));
buzbee9e0f9b02011-08-24 15:32:46 -0700213}
214
215TEST_F(CompilerTest, DoubleCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700216 CompileDex(kIntMathDex, "kIntMathDex");
217 AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444,
218 -5.0, 4.0, 4.0,
219 (1.0/0.0) / (1.0/0.0));
buzbee9e0f9b02011-08-24 15:32:46 -0700220}
221
buzbeec5ef0462011-08-25 18:44:49 -0700222TEST_F(CompilerTest, RecursiveFibonacci) {
223 CompileDex(kIntMathDex, "kIntMathDex");
224 AssertStaticIntMethod("IntMath", "fibonacci", "(I)I", 55,
225 10);
226}
buzbeec5ef0462011-08-25 18:44:49 -0700227
buzbee7b1b86d2011-08-26 18:59:10 -0700228#if 0 // Need to complete try/catch block handling
229TEST_F(CompilerTest, ThrowAndCatch) {
230 CompileDex(kIntMathDex, "kIntMathDex");
231 AssertStaticIntMethod("IntMath", "throwAndCatch", "()I", 4);
232}
233#endif
234
235TEST_F(CompilerTest, ManyArgs) {
236 CompileDex(kIntMathDex, "kIntMathDex");
237 AssertStaticIntMethod("IntMath", "manyArgs",
238 "(IJIJIJIIDFDSICIIBZIIJJIIIII)I", -1,
239 0, 1LL, 2, 3LL, 4, 5LL, 6, 7, 8.0, 9.0f, 10.0,
240 (short)11, 12, (char)13, 14, 15, (int8_t)-16, true, 18,
241 19, 20LL, 21LL, 22, 23, 24, 25, 26);
242}
243
buzbee7b1b86d2011-08-26 18:59:10 -0700244TEST_F(CompilerTest, VirtualCall) {
245 CompileDex(kIntMathDex, "kIntMathDex");
246 AssertStaticIntMethod("IntMath", "staticCall", "(I)I", 6,
247 3);
248}
buzbee7b1b86d2011-08-26 18:59:10 -0700249
buzbeedd3efae2011-08-28 14:39:07 -0700250TEST_F(CompilerTest, TestIGetPut) {
251 CompileDex(kIntMathDex, "kIntMathDex");
252 AssertStaticIntMethod("IntMath", "testIGetPut", "(I)I", 333,
253 111);
254}
255
buzbeec143c552011-08-20 17:38:58 -0700256} // namespace art