blob: fd534ced1fd54f948e7ebacd39634434d1de09f2 [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 Geoffray49c105d2014-06-13 10:29:43 +010050#if defined(__i386__) || defined(__arm__) || defined(__x86_64__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010051static void Run(const InternalCodeAllocator& allocator, bool has_result, int32_t expected) {
52 typedef int32_t (*fptr)();
53 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070054 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
55#if defined(__arm__)
56 // For thumb we need the bottom bit set.
57 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
58#endif
59 int32_t result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010060 if (has_result) {
61 CHECK_EQ(result, expected);
62 }
63}
Nicolas Geoffray49c105d2014-06-13 10:29:43 +010064#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010065
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000066static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000067 ArenaPool pool;
68 ArenaAllocator arena(&pool);
69 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000070 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
71 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072 ASSERT_NE(graph, nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010074
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010076 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077#if defined(__i386__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010078 Run(allocator, has_result, expected);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010080
Nicolas Geoffray787c3072014-03-17 10:20:19 +000081 codegen = CodeGenerator::Create(&arena, graph, kArm);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010082 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000083#if defined(__arm__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010084 Run(allocator, has_result, expected);
85#endif
86
87 codegen = CodeGenerator::Create(&arena, graph, kX86_64);
88 codegen->CompileBaseline(&allocator);
89#if defined(__x86_64__)
90 Run(allocator, has_result, expected);
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000091#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000092}
93
94TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000095 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
96 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097}
98
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000099TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000101 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000102 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000104 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105}
106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000107TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000108 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109 Instruction::GOTO | 0x100,
110 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000111 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114}
115
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000116TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000117 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000118 Instruction::GOTO | 0x200,
119 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000123
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000124 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125 Instruction::GOTO_16, 3,
126 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000129 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000131 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000132 Instruction::GOTO_32, 4, 0,
133 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000134 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000137}
138
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000139TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000141 Instruction::RETURN_VOID,
142 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000144
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000145 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000146}
147
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000148TEST(CodegenTest, CFG5) {
149 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
150 Instruction::CONST_4 | 0 | 0,
151 Instruction::IF_EQ, 3,
152 Instruction::GOTO | 0x100,
153 Instruction::RETURN_VOID);
154
155 TestCode(data);
156}
157
158TEST(CodegenTest, IntConstant) {
159 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
160 Instruction::CONST_4 | 0 | 0,
161 Instruction::RETURN_VOID);
162
163 TestCode(data);
164}
165
166TEST(CodegenTest, Return1) {
167 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
168 Instruction::CONST_4 | 0 | 0,
169 Instruction::RETURN | 0);
170
171 TestCode(data, true, 0);
172}
173
174TEST(CodegenTest, Return2) {
175 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
176 Instruction::CONST_4 | 0 | 0,
177 Instruction::CONST_4 | 0 | 1 << 8,
178 Instruction::RETURN | 1 << 8);
179
180 TestCode(data, true, 0);
181}
182
183TEST(CodegenTest, Return3) {
184 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
185 Instruction::CONST_4 | 0 | 0,
186 Instruction::CONST_4 | 1 << 8 | 1 << 12,
187 Instruction::RETURN | 1 << 8);
188
189 TestCode(data, true, 1);
190}
191
192TEST(CodegenTest, ReturnIf1) {
193 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
194 Instruction::CONST_4 | 0 | 0,
195 Instruction::CONST_4 | 1 << 8 | 1 << 12,
196 Instruction::IF_EQ, 3,
197 Instruction::RETURN | 0 << 8,
198 Instruction::RETURN | 1 << 8);
199
200 TestCode(data, true, 1);
201}
202
203TEST(CodegenTest, ReturnIf2) {
204 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
205 Instruction::CONST_4 | 0 | 0,
206 Instruction::CONST_4 | 1 << 8 | 1 << 12,
207 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
208 Instruction::RETURN | 0 << 8,
209 Instruction::RETURN | 1 << 8);
210
211 TestCode(data, true, 0);
212}
213
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000214TEST(CodegenTest, ReturnAdd1) {
215 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
216 Instruction::CONST_4 | 3 << 12 | 0,
217 Instruction::CONST_4 | 4 << 12 | 1 << 8,
218 Instruction::ADD_INT, 1 << 8 | 0,
219 Instruction::RETURN);
220
221 TestCode(data, true, 7);
222}
223
224TEST(CodegenTest, ReturnAdd2) {
225 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
226 Instruction::CONST_4 | 3 << 12 | 0,
227 Instruction::CONST_4 | 4 << 12 | 1 << 8,
228 Instruction::ADD_INT_2ADDR | 1 << 12,
229 Instruction::RETURN);
230
231 TestCode(data, true, 7);
232}
233
234TEST(CodegenTest, ReturnAdd3) {
235 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
236 Instruction::CONST_4 | 4 << 12 | 0 << 8,
237 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
238 Instruction::RETURN);
239
240 TestCode(data, true, 7);
241}
242
243TEST(CodegenTest, ReturnAdd4) {
244 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
245 Instruction::CONST_4 | 4 << 12 | 0 << 8,
246 Instruction::ADD_INT_LIT16, 3,
247 Instruction::RETURN);
248
249 TestCode(data, true, 7);
250}
251
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000252} // namespace art