blob: 3b91ca15f44ce4bd200fa4888929f0f22e8275ad [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_;
Ian Rogers700a4022014-05-19 16:49:03 -070045 std::unique_ptr<uint8_t[]> memory_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000046
Nicolas Geoffray787c3072014-03-17 10:20:19 +000047 DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048};
49
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010050static void Run(const InternalCodeAllocator& allocator, bool has_result, int32_t expected) {
51 typedef int32_t (*fptr)();
52 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
53 int32_t result = reinterpret_cast<fptr>(allocator.GetMemory())();
54 if (has_result) {
55 CHECK_EQ(result, expected);
56 }
57}
58
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000060 ArenaPool pool;
61 ArenaAllocator arena(&pool);
62 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000063 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
64 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000065 ASSERT_NE(graph, nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000066 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010067
Nicolas Geoffray787c3072014-03-17 10:20:19 +000068 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010069 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000070#if defined(__i386__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010071 Run(allocator, has_result, expected);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010073
Nicolas Geoffray787c3072014-03-17 10:20:19 +000074 codegen = CodeGenerator::Create(&arena, graph, kArm);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010075 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076#if defined(__arm__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010077 Run(allocator, has_result, expected);
78#endif
79
80 codegen = CodeGenerator::Create(&arena, graph, kX86_64);
81 codegen->CompileBaseline(&allocator);
82#if defined(__x86_64__)
83 Run(allocator, has_result, expected);
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000084#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085}
86
87TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000088 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
89 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090}
91
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000092TEST(CodegenTest, CFG1) {
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,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000095 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000096
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000097 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098}
99
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000100TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000101 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102 Instruction::GOTO | 0x100,
103 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000104 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000107}
108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000109TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000110 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111 Instruction::GOTO | 0x200,
112 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000115 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000116
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000117 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000118 Instruction::GOTO_16, 3,
119 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000123
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000124 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125 Instruction::GOTO_32, 4, 0,
126 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000129 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000130}
131
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000132TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000133 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000134 Instruction::RETURN_VOID,
135 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000137
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000138 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000139}
140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000141TEST(CodegenTest, CFG5) {
142 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
143 Instruction::CONST_4 | 0 | 0,
144 Instruction::IF_EQ, 3,
145 Instruction::GOTO | 0x100,
146 Instruction::RETURN_VOID);
147
148 TestCode(data);
149}
150
151TEST(CodegenTest, IntConstant) {
152 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
153 Instruction::CONST_4 | 0 | 0,
154 Instruction::RETURN_VOID);
155
156 TestCode(data);
157}
158
159TEST(CodegenTest, Return1) {
160 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
161 Instruction::CONST_4 | 0 | 0,
162 Instruction::RETURN | 0);
163
164 TestCode(data, true, 0);
165}
166
167TEST(CodegenTest, Return2) {
168 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
169 Instruction::CONST_4 | 0 | 0,
170 Instruction::CONST_4 | 0 | 1 << 8,
171 Instruction::RETURN | 1 << 8);
172
173 TestCode(data, true, 0);
174}
175
176TEST(CodegenTest, Return3) {
177 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
178 Instruction::CONST_4 | 0 | 0,
179 Instruction::CONST_4 | 1 << 8 | 1 << 12,
180 Instruction::RETURN | 1 << 8);
181
182 TestCode(data, true, 1);
183}
184
185TEST(CodegenTest, ReturnIf1) {
186 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
187 Instruction::CONST_4 | 0 | 0,
188 Instruction::CONST_4 | 1 << 8 | 1 << 12,
189 Instruction::IF_EQ, 3,
190 Instruction::RETURN | 0 << 8,
191 Instruction::RETURN | 1 << 8);
192
193 TestCode(data, true, 1);
194}
195
196TEST(CodegenTest, ReturnIf2) {
197 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
198 Instruction::CONST_4 | 0 | 0,
199 Instruction::CONST_4 | 1 << 8 | 1 << 12,
200 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
201 Instruction::RETURN | 0 << 8,
202 Instruction::RETURN | 1 << 8);
203
204 TestCode(data, true, 0);
205}
206
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000207TEST(CodegenTest, ReturnAdd1) {
208 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
209 Instruction::CONST_4 | 3 << 12 | 0,
210 Instruction::CONST_4 | 4 << 12 | 1 << 8,
211 Instruction::ADD_INT, 1 << 8 | 0,
212 Instruction::RETURN);
213
214 TestCode(data, true, 7);
215}
216
217TEST(CodegenTest, ReturnAdd2) {
218 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
219 Instruction::CONST_4 | 3 << 12 | 0,
220 Instruction::CONST_4 | 4 << 12 | 1 << 8,
221 Instruction::ADD_INT_2ADDR | 1 << 12,
222 Instruction::RETURN);
223
224 TestCode(data, true, 7);
225}
226
227TEST(CodegenTest, ReturnAdd3) {
228 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
229 Instruction::CONST_4 | 4 << 12 | 0 << 8,
230 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
231 Instruction::RETURN);
232
233 TestCode(data, true, 7);
234}
235
236TEST(CodegenTest, ReturnAdd4) {
237 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
238 Instruction::CONST_4 | 4 << 12 | 0 << 8,
239 Instruction::ADD_INT_LIT16, 3,
240 Instruction::RETURN);
241
242 TestCode(data, true, 7);
243}
244
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000245} // namespace art