blob: 63aeee2e47e556ddea1662a85620489475ad2043 [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 }
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 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 }
92 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumFields());
93 for (size_t i = 0; i < dex_cache->NumFields(); 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
98}
99
buzbeec143c552011-08-20 17:38:58 -0700100TEST_F(CompilerTest, BasicCodegen) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700101 CompileDex(kFibonacciDex, "kFibonacciDex");
102 AssertStaticIntMethod("Fibonacci", "fibonacci", "(I)I", 55,
103 10);
buzbeec143c552011-08-20 17:38:58 -0700104}
buzbee3ea4ec52011-08-22 17:37:19 -0700105
106TEST_F(CompilerTest, UnopTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700107 CompileDex(kIntMathDex, "kIntMathDex");
108 AssertStaticIntMethod("IntMath", "unopTest", "(I)I", 37,
109 38);
buzbee3ea4ec52011-08-22 17:37:19 -0700110}
111
buzbee3ea4ec52011-08-22 17:37:19 -0700112TEST_F(CompilerTest, ShiftTest1) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700113 CompileDex(kIntMathDex, "kIntMathDex");
114 AssertStaticIntMethod("IntMath", "shiftTest1", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700115}
buzbeec143c552011-08-20 17:38:58 -0700116
buzbee3ea4ec52011-08-22 17:37:19 -0700117TEST_F(CompilerTest, ShiftTest2) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700118 CompileDex(kIntMathDex, "kIntMathDex");
119 AssertStaticIntMethod("IntMath", "shiftTest2", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700120}
buzbee3ea4ec52011-08-22 17:37:19 -0700121
122TEST_F(CompilerTest, UnsignedShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700123 CompileDex(kIntMathDex, "kIntMathDex");
124 AssertStaticIntMethod("IntMath", "unsignedShiftTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700125}
126
buzbee3ea4ec52011-08-22 17:37:19 -0700127TEST_F(CompilerTest, ConvTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700128 CompileDex(kIntMathDex, "kIntMathDex");
129 AssertStaticIntMethod("IntMath", "convTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700130}
buzbee3ea4ec52011-08-22 17:37:19 -0700131
132TEST_F(CompilerTest, CharSubTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700133 CompileDex(kIntMathDex, "kIntMathDex");
134 AssertStaticIntMethod("IntMath", "charSubTest", "()I", 0);
buzbee3ea4ec52011-08-22 17:37:19 -0700135}
136
buzbee3ea4ec52011-08-22 17:37:19 -0700137TEST_F(CompilerTest, IntOperTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700138 CompileDex(kIntMathDex, "kIntMathDex");
139 AssertStaticIntMethod("IntMath", "intOperTest", "(II)I", 0,
140 70000, -3);
buzbee3ea4ec52011-08-22 17:37:19 -0700141}
buzbee3ea4ec52011-08-22 17:37:19 -0700142
buzbee3ea4ec52011-08-22 17:37:19 -0700143TEST_F(CompilerTest, Lit16Test) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700144 CompileDex(kIntMathDex, "kIntMathDex");
145 AssertStaticIntMethod("IntMath", "lit16Test", "(I)I", 0,
146 77777);
buzbee3ea4ec52011-08-22 17:37:19 -0700147}
buzbee3ea4ec52011-08-22 17:37:19 -0700148
buzbee3ea4ec52011-08-22 17:37:19 -0700149TEST_F(CompilerTest, Lit8Test) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700150 CompileDex(kIntMathDex, "kIntMathDex");
151 AssertStaticIntMethod("IntMath", "lit8Test", "(I)I", 0,
152 -55555);
buzbee3ea4ec52011-08-22 17:37:19 -0700153}
buzbee3ea4ec52011-08-22 17:37:19 -0700154
buzbee3ea4ec52011-08-22 17:37:19 -0700155TEST_F(CompilerTest, IntShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700156 CompileDex(kIntMathDex, "kIntMathDex");
157 AssertStaticIntMethod("IntMath", "intShiftTest", "(II)I", 0,
158 0xff00aa01, 8);
buzbee3ea4ec52011-08-22 17:37:19 -0700159}
buzbee3ea4ec52011-08-22 17:37:19 -0700160
buzbee3ea4ec52011-08-22 17:37:19 -0700161TEST_F(CompilerTest, LongOperTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700162 CompileDex(kIntMathDex, "kIntMathDex");
163 AssertStaticIntMethod("IntMath", "longOperTest", "(JJ)I", 0,
164 70000000000LL, 3);
buzbee3ea4ec52011-08-22 17:37:19 -0700165}
buzbee3ea4ec52011-08-22 17:37:19 -0700166
buzbee3ea4ec52011-08-22 17:37:19 -0700167TEST_F(CompilerTest, LongShiftTest) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700168 CompileDex(kIntMathDex, "kIntMathDex");
buzbeebafc3422011-08-25 15:22:55 -0700169 AssertStaticLongMethod("IntMath", "longShiftTest", "(JI)J", 0,
170 0xd5aa96deff00aa01LL, 8);
buzbee3ea4ec52011-08-22 17:37:19 -0700171}
buzbee3ea4ec52011-08-22 17:37:19 -0700172
buzbee9e0f9b02011-08-24 15:32:46 -0700173TEST_F(CompilerTest, SwitchTest1) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700174 CompileDex(kIntMathDex, "kIntMathDex");
175 AssertStaticIntMethod("IntMath", "switchTest", "(I)I", 1234,
176 1);
buzbee9e0f9b02011-08-24 15:32:46 -0700177}
178
179TEST_F(CompilerTest, IntCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700180 CompileDex(kIntMathDex, "kIntMathDex");
181 AssertStaticIntMethod("IntMath", "testIntCompare", "(IIII)I", 1111,
182 -5, 4, 4, 0);
buzbee9e0f9b02011-08-24 15:32:46 -0700183}
184
185TEST_F(CompilerTest, LongCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700186 CompileDex(kIntMathDex, "kIntMathDex");
187 AssertStaticIntMethod("IntMath", "testLongCompare", "(JJJJ)I", 2222,
188 -5LL, -4294967287LL, 4LL, 8LL);
buzbee9e0f9b02011-08-24 15:32:46 -0700189}
190
Brian Carlstrombffb1552011-08-25 12:23:53 -0700191#if 0
buzbee9e0f9b02011-08-24 15:32:46 -0700192TEST_F(CompilerTest, FloatCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700193 CompileDex(kIntMathDex, "kIntMathDex");
194 AssertStaticIntMethod("IntMath", "testFloatCompare", "(FFFF)I", 3333
195 -5.0f, 4.0f, 4.0f,
196 (1.0f/0.0f) / (1.0f/0.0f));
buzbee9e0f9b02011-08-24 15:32:46 -0700197}
Brian Carlstrombffb1552011-08-25 12:23:53 -0700198#endif
buzbee9e0f9b02011-08-24 15:32:46 -0700199
200TEST_F(CompilerTest, DoubleCompare) {
Brian Carlstrombffb1552011-08-25 12:23:53 -0700201 CompileDex(kIntMathDex, "kIntMathDex");
202 AssertStaticIntMethod("IntMath", "testDoubleCompare", "(DDDD)I", 4444,
203 -5.0, 4.0, 4.0,
204 (1.0/0.0) / (1.0/0.0));
buzbee9e0f9b02011-08-24 15:32:46 -0700205}
206
buzbeec5ef0462011-08-25 18:44:49 -0700207TEST_F(CompilerTest, RecursiveFibonacci) {
208 CompileDex(kIntMathDex, "kIntMathDex");
209 AssertStaticIntMethod("IntMath", "fibonacci", "(I)I", 55,
210 10);
211}
buzbeec5ef0462011-08-25 18:44:49 -0700212
buzbeec143c552011-08-20 17:38:58 -0700213} // namespace art