blob: d40990e86bfa495fb43649eac171fb1a7861d02b [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "builder.h"
18#include "code_generator.h"
19#include "common_compiler_test.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "dex_instruction.h"
22#include "instruction_set.h"
23#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "optimizing_unit_test.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025
26#include "gtest/gtest.h"
27
28namespace art {
29
Nicolas Geoffray787c3072014-03-17 10:20:19 +000030class InternalCodeAllocator : public CodeAllocator {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000032 InternalCodeAllocator() { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
34 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000035 size_ = size;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036 memory_.reset(new uint8_t[size]);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037 return memory_.get();
38 }
39
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040 size_t GetSize() const { return size_; }
41 uint8_t* GetMemory() const { return memory_.get(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042
43 private:
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044 size_t size_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045 UniquePtr<uint8_t[]> memory_;
46
Nicolas Geoffray787c3072014-03-17 10:20:19 +000047 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048};
49
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051 ArenaPool pool;
52 ArenaAllocator arena(&pool);
53 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
55 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056 ASSERT_NE(graph, nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057 InternalCodeAllocator allocator;
58 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86);
59 codegen->Compile(&allocator);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060 typedef int32_t (*fptr)();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000061#if defined(__i386__)
Nicolas Geoffray787c3072014-03-17 10:20:19 +000062 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
63 int32_t result = reinterpret_cast<fptr>(allocator.GetMemory())();
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000064 if (has_result) {
65 CHECK_EQ(result, expected);
66 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000067#endif
Nicolas Geoffray787c3072014-03-17 10:20:19 +000068 codegen = CodeGenerator::Create(&arena, graph, kArm);
69 codegen->Compile(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000070#if defined(__arm__)
Nicolas Geoffray787c3072014-03-17 10:20:19 +000071 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
72 int32_t result = reinterpret_cast<fptr>(allocator.GetMemory())();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073 if (has_result) {
74 CHECK_EQ(result, expected);
75 }
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000076#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077}
78
79TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000080 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
81 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000082}
83
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000084TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000085 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000087 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000089 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090}
91
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000092TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000093 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094 Instruction::GOTO | 0x100,
95 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099}
100
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000101TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000102 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103 Instruction::GOTO | 0x200,
104 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000105 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000109 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110 Instruction::GOTO_16, 3,
111 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000112 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000114 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117 Instruction::GOTO_32, 4, 0,
118 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000119 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000121 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000122}
123
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126 Instruction::RETURN_VOID,
127 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000128 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000130 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000131}
132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000133TEST(CodegenTest, CFG5) {
134 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
135 Instruction::CONST_4 | 0 | 0,
136 Instruction::IF_EQ, 3,
137 Instruction::GOTO | 0x100,
138 Instruction::RETURN_VOID);
139
140 TestCode(data);
141}
142
143TEST(CodegenTest, IntConstant) {
144 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
145 Instruction::CONST_4 | 0 | 0,
146 Instruction::RETURN_VOID);
147
148 TestCode(data);
149}
150
151TEST(CodegenTest, Return1) {
152 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
153 Instruction::CONST_4 | 0 | 0,
154 Instruction::RETURN | 0);
155
156 TestCode(data, true, 0);
157}
158
159TEST(CodegenTest, Return2) {
160 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
161 Instruction::CONST_4 | 0 | 0,
162 Instruction::CONST_4 | 0 | 1 << 8,
163 Instruction::RETURN | 1 << 8);
164
165 TestCode(data, true, 0);
166}
167
168TEST(CodegenTest, Return3) {
169 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
170 Instruction::CONST_4 | 0 | 0,
171 Instruction::CONST_4 | 1 << 8 | 1 << 12,
172 Instruction::RETURN | 1 << 8);
173
174 TestCode(data, true, 1);
175}
176
177TEST(CodegenTest, ReturnIf1) {
178 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
179 Instruction::CONST_4 | 0 | 0,
180 Instruction::CONST_4 | 1 << 8 | 1 << 12,
181 Instruction::IF_EQ, 3,
182 Instruction::RETURN | 0 << 8,
183 Instruction::RETURN | 1 << 8);
184
185 TestCode(data, true, 1);
186}
187
188TEST(CodegenTest, ReturnIf2) {
189 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
190 Instruction::CONST_4 | 0 | 0,
191 Instruction::CONST_4 | 1 << 8 | 1 << 12,
192 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
193 Instruction::RETURN | 0 << 8,
194 Instruction::RETURN | 1 << 8);
195
196 TestCode(data, true, 0);
197}
198
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000199TEST(CodegenTest, ReturnAdd1) {
200 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
201 Instruction::CONST_4 | 3 << 12 | 0,
202 Instruction::CONST_4 | 4 << 12 | 1 << 8,
203 Instruction::ADD_INT, 1 << 8 | 0,
204 Instruction::RETURN);
205
206 TestCode(data, true, 7);
207}
208
209TEST(CodegenTest, ReturnAdd2) {
210 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
211 Instruction::CONST_4 | 3 << 12 | 0,
212 Instruction::CONST_4 | 4 << 12 | 1 << 8,
213 Instruction::ADD_INT_2ADDR | 1 << 12,
214 Instruction::RETURN);
215
216 TestCode(data, true, 7);
217}
218
219TEST(CodegenTest, ReturnAdd3) {
220 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
221 Instruction::CONST_4 | 4 << 12 | 0 << 8,
222 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
223 Instruction::RETURN);
224
225 TestCode(data, true, 7);
226}
227
228TEST(CodegenTest, ReturnAdd4) {
229 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
230 Instruction::CONST_4 | 4 << 12 | 0 << 8,
231 Instruction::ADD_INT_LIT16, 3,
232 Instruction::RETURN);
233
234 TestCode(data, true, 7);
235}
236
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000237} // namespace art