blob: bfdc30f26b0ac358cb3f404e6a7cfbf5b52f66a1 [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 Geoffray8d486732014-07-16 16:23:40 +010051static void Run(const InternalCodeAllocator& allocator,
52 const CodeGenerator& codegen,
53 bool has_result,
54 int32_t expected) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010055 typedef int32_t (*fptr)();
56 CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize());
Dave Allison20dfc792014-06-16 20:44:29 -070057 fptr f = reinterpret_cast<fptr>(allocator.GetMemory());
Nicolas Geoffray8d486732014-07-16 16:23:40 +010058 if (codegen.GetInstructionSet() == kThumb2) {
59 // For thumb we need the bottom bit set.
60 f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1);
61 }
Dave Allison20dfc792014-06-16 20:44:29 -070062 int32_t result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010063 if (has_result) {
64 CHECK_EQ(result, expected);
65 }
66}
Nicolas Geoffray49c105d2014-06-13 10:29:43 +010067#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010068
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000070 ArenaPool pool;
71 ArenaAllocator arena(&pool);
72 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
74 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000075 ASSERT_NE(graph, nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000076 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010077
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010079 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000080#if defined(__i386__)
Nicolas Geoffray8d486732014-07-16 16:23:40 +010081 Run(allocator, *codegen, has_result, expected);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000082#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010083
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084 codegen = CodeGenerator::Create(&arena, graph, kArm);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010085 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000086#if defined(__arm__)
Nicolas Geoffray8d486732014-07-16 16:23:40 +010087 Run(allocator, *codegen, has_result, expected);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010088#endif
89
90 codegen = CodeGenerator::Create(&arena, graph, kX86_64);
91 codegen->CompileBaseline(&allocator);
92#if defined(__x86_64__)
Nicolas Geoffray8d486732014-07-16 16:23:40 +010093 Run(allocator, *codegen, has_result, expected);
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000094#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000095}
96
97TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
99 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100}
101
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000102TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000103 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000104 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000105 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108}
109
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000110TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000111 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112 Instruction::GOTO | 0x100,
113 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000114 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117}
118
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000119TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121 Instruction::GOTO | 0x200,
122 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000128 Instruction::GOTO_16, 3,
129 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000130 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000133
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000134 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000135 Instruction::GOTO_32, 4, 0,
136 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000137 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000138
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000139 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000140}
141
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000142TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000144 Instruction::RETURN_VOID,
145 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000146 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000147
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000149}
150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000151TEST(CodegenTest, CFG5) {
152 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
153 Instruction::CONST_4 | 0 | 0,
154 Instruction::IF_EQ, 3,
155 Instruction::GOTO | 0x100,
156 Instruction::RETURN_VOID);
157
158 TestCode(data);
159}
160
161TEST(CodegenTest, IntConstant) {
162 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
163 Instruction::CONST_4 | 0 | 0,
164 Instruction::RETURN_VOID);
165
166 TestCode(data);
167}
168
169TEST(CodegenTest, Return1) {
170 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
171 Instruction::CONST_4 | 0 | 0,
172 Instruction::RETURN | 0);
173
174 TestCode(data, true, 0);
175}
176
177TEST(CodegenTest, Return2) {
178 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
179 Instruction::CONST_4 | 0 | 0,
180 Instruction::CONST_4 | 0 | 1 << 8,
181 Instruction::RETURN | 1 << 8);
182
183 TestCode(data, true, 0);
184}
185
186TEST(CodegenTest, Return3) {
187 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
188 Instruction::CONST_4 | 0 | 0,
189 Instruction::CONST_4 | 1 << 8 | 1 << 12,
190 Instruction::RETURN | 1 << 8);
191
192 TestCode(data, true, 1);
193}
194
195TEST(CodegenTest, ReturnIf1) {
196 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
197 Instruction::CONST_4 | 0 | 0,
198 Instruction::CONST_4 | 1 << 8 | 1 << 12,
199 Instruction::IF_EQ, 3,
200 Instruction::RETURN | 0 << 8,
201 Instruction::RETURN | 1 << 8);
202
203 TestCode(data, true, 1);
204}
205
206TEST(CodegenTest, ReturnIf2) {
207 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
208 Instruction::CONST_4 | 0 | 0,
209 Instruction::CONST_4 | 1 << 8 | 1 << 12,
210 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
211 Instruction::RETURN | 0 << 8,
212 Instruction::RETURN | 1 << 8);
213
214 TestCode(data, true, 0);
215}
216
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000217TEST(CodegenTest, ReturnAdd1) {
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, 1 << 8 | 0,
222 Instruction::RETURN);
223
224 TestCode(data, true, 7);
225}
226
227TEST(CodegenTest, ReturnAdd2) {
228 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
229 Instruction::CONST_4 | 3 << 12 | 0,
230 Instruction::CONST_4 | 4 << 12 | 1 << 8,
231 Instruction::ADD_INT_2ADDR | 1 << 12,
232 Instruction::RETURN);
233
234 TestCode(data, true, 7);
235}
236
237TEST(CodegenTest, ReturnAdd3) {
238 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
239 Instruction::CONST_4 | 4 << 12 | 0 << 8,
240 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
241 Instruction::RETURN);
242
243 TestCode(data, true, 7);
244}
245
246TEST(CodegenTest, ReturnAdd4) {
247 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
248 Instruction::CONST_4 | 4 << 12 | 0 << 8,
249 Instruction::ADD_INT_LIT16, 3,
250 Instruction::RETURN);
251
252 TestCode(data, true, 7);
253}
254
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000255} // namespace art