blob: a6ecdfbe21130edc1d2525a675bf8a8402d5f6be [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
30class ExecutableMemoryAllocator : public CodeAllocator {
31 public:
32 ExecutableMemoryAllocator() { }
33
34 virtual uint8_t* Allocate(size_t size) {
35 memory_.reset(new uint8_t[size]);
36 CommonCompilerTest::MakeExecutable(memory_.get(), size);
37 return memory_.get();
38 }
39
40 uint8_t* memory() const { return memory_.get(); }
41
42 private:
43 UniquePtr<uint8_t[]> memory_;
44
45 DISALLOW_COPY_AND_ASSIGN(ExecutableMemoryAllocator);
46};
47
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000048static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000049 ArenaPool pool;
50 ArenaAllocator arena(&pool);
51 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
53 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054 ASSERT_NE(graph, nullptr);
55 ExecutableMemoryAllocator allocator;
56 CHECK(CodeGenerator::CompileGraph(graph, kX86, &allocator));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000057 typedef int32_t (*fptr)();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000058#if defined(__i386__)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059 int32_t result = reinterpret_cast<fptr>(allocator.memory())();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000060#endif
61 CHECK(CodeGenerator::CompileGraph(graph, kArm, &allocator));
62#if defined(__arm__)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000063 int32_t result = reinterpret_cast<fptr>(allocator.memory())();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000064#endif
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000065 if (has_result) {
66 CHECK_EQ(result, expected);
67 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068}
69
70TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000071 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
72 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000073}
74
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000075TEST(CodegenTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000076 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000077 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000080 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000081}
82
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000083TEST(CodegenTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000084 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085 Instruction::GOTO | 0x100,
86 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, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000093 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094 Instruction::GOTO | 0x200,
95 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000101 Instruction::GOTO_16, 3,
102 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000103 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000104
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000105 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000106
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108 Instruction::GOTO_32, 4, 0,
109 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000110 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000112 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000113}
114
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000115TEST(CodegenTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117 Instruction::RETURN_VOID,
118 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000119 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000121 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000122}
123
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124TEST(CodegenTest, CFG5) {
125 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
126 Instruction::CONST_4 | 0 | 0,
127 Instruction::IF_EQ, 3,
128 Instruction::GOTO | 0x100,
129 Instruction::RETURN_VOID);
130
131 TestCode(data);
132}
133
134TEST(CodegenTest, IntConstant) {
135 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
136 Instruction::CONST_4 | 0 | 0,
137 Instruction::RETURN_VOID);
138
139 TestCode(data);
140}
141
142TEST(CodegenTest, Return1) {
143 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
144 Instruction::CONST_4 | 0 | 0,
145 Instruction::RETURN | 0);
146
147 TestCode(data, true, 0);
148}
149
150TEST(CodegenTest, Return2) {
151 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
152 Instruction::CONST_4 | 0 | 0,
153 Instruction::CONST_4 | 0 | 1 << 8,
154 Instruction::RETURN | 1 << 8);
155
156 TestCode(data, true, 0);
157}
158
159TEST(CodegenTest, Return3) {
160 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
161 Instruction::CONST_4 | 0 | 0,
162 Instruction::CONST_4 | 1 << 8 | 1 << 12,
163 Instruction::RETURN | 1 << 8);
164
165 TestCode(data, true, 1);
166}
167
168TEST(CodegenTest, ReturnIf1) {
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::IF_EQ, 3,
173 Instruction::RETURN | 0 << 8,
174 Instruction::RETURN | 1 << 8);
175
176 TestCode(data, true, 1);
177}
178
179TEST(CodegenTest, ReturnIf2) {
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::IF_EQ | 0 << 4 | 1 << 8, 3,
184 Instruction::RETURN | 0 << 8,
185 Instruction::RETURN | 1 << 8);
186
187 TestCode(data, true, 0);
188}
189
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000190} // namespace art