blob: 7ec0c84167a2028de75c1bc70021132f779390c1 [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());
Dave Allison20dfc792014-06-16 20:44:29 -070055 int32_t result = f();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010056 if (has_result) {
57 CHECK_EQ(result, expected);
58 }
59}
Nicolas Geoffray49c105d2014-06-13 10:29:43 +010060#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010061
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000063 ArenaPool pool;
64 ArenaAllocator arena(&pool);
65 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000066 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
67 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068 ASSERT_NE(graph, nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000069 InternalCodeAllocator allocator;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010070
Nicolas Geoffray787c3072014-03-17 10:20:19 +000071 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010072 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073#if defined(__i386__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010074 Run(allocator, has_result, expected);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000075#endif
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010076
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 codegen = CodeGenerator::Create(&arena, graph, kArm);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010078 codegen->CompileBaseline(&allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079#if defined(__arm__)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010080 Run(allocator, has_result, expected);
81#endif
82
83 codegen = CodeGenerator::Create(&arena, graph, kX86_64);
84 codegen->CompileBaseline(&allocator);
85#if defined(__x86_64__)
86 Run(allocator, has_result, expected);
Nicolas Geoffray39d57e22014-03-13 10:28:41 +000087#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088}
89
90TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000091 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
92 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000093}
94
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000095TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000101}
102
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000103TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000104 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 Instruction::GOTO | 0x100,
106 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000109 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110}
111
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000112TEST(CodegenTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114 Instruction::GOTO | 0x200,
115 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000121 Instruction::GOTO_16, 3,
122 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000128 Instruction::GOTO_32, 4, 0,
129 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000130 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000133}
134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000135TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000136 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000137 Instruction::RETURN_VOID,
138 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000139 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000141 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000142}
143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000144TEST(CodegenTest, CFG5) {
145 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
146 Instruction::CONST_4 | 0 | 0,
147 Instruction::IF_EQ, 3,
148 Instruction::GOTO | 0x100,
149 Instruction::RETURN_VOID);
150
151 TestCode(data);
152}
153
154TEST(CodegenTest, IntConstant) {
155 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
156 Instruction::CONST_4 | 0 | 0,
157 Instruction::RETURN_VOID);
158
159 TestCode(data);
160}
161
162TEST(CodegenTest, Return1) {
163 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
164 Instruction::CONST_4 | 0 | 0,
165 Instruction::RETURN | 0);
166
167 TestCode(data, true, 0);
168}
169
170TEST(CodegenTest, Return2) {
171 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
172 Instruction::CONST_4 | 0 | 0,
173 Instruction::CONST_4 | 0 | 1 << 8,
174 Instruction::RETURN | 1 << 8);
175
176 TestCode(data, true, 0);
177}
178
179TEST(CodegenTest, Return3) {
180 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
181 Instruction::CONST_4 | 0 | 0,
182 Instruction::CONST_4 | 1 << 8 | 1 << 12,
183 Instruction::RETURN | 1 << 8);
184
185 TestCode(data, true, 1);
186}
187
188TEST(CodegenTest, ReturnIf1) {
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, 3,
193 Instruction::RETURN | 0 << 8,
194 Instruction::RETURN | 1 << 8);
195
196 TestCode(data, true, 1);
197}
198
199TEST(CodegenTest, ReturnIf2) {
200 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
201 Instruction::CONST_4 | 0 | 0,
202 Instruction::CONST_4 | 1 << 8 | 1 << 12,
203 Instruction::IF_EQ | 0 << 4 | 1 << 8, 3,
204 Instruction::RETURN | 0 << 8,
205 Instruction::RETURN | 1 << 8);
206
207 TestCode(data, true, 0);
208}
209
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000210TEST(CodegenTest, ReturnAdd1) {
211 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
212 Instruction::CONST_4 | 3 << 12 | 0,
213 Instruction::CONST_4 | 4 << 12 | 1 << 8,
214 Instruction::ADD_INT, 1 << 8 | 0,
215 Instruction::RETURN);
216
217 TestCode(data, true, 7);
218}
219
220TEST(CodegenTest, ReturnAdd2) {
221 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
222 Instruction::CONST_4 | 3 << 12 | 0,
223 Instruction::CONST_4 | 4 << 12 | 1 << 8,
224 Instruction::ADD_INT_2ADDR | 1 << 12,
225 Instruction::RETURN);
226
227 TestCode(data, true, 7);
228}
229
230TEST(CodegenTest, ReturnAdd3) {
231 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
232 Instruction::CONST_4 | 4 << 12 | 0 << 8,
233 Instruction::ADD_INT_LIT8, 3 << 8 | 0,
234 Instruction::RETURN);
235
236 TestCode(data, true, 7);
237}
238
239TEST(CodegenTest, ReturnAdd4) {
240 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
241 Instruction::CONST_4 | 4 << 12 | 0 << 8,
242 Instruction::ADD_INT_LIT16, 3,
243 Instruction::RETURN);
244
245 TestCode(data, true, 7);
246}
247
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000248} // namespace art