Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 20 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | #include "dex_instruction.h" |
| 22 | #include "instruction_set.h" |
| 23 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 24 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 25 | |
| 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 30 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 31 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 32 | InternalCodeAllocator() { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | |
| 34 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 35 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 36 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 37 | return memory_.get(); |
| 38 | } |
| 39 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 40 | size_t GetSize() const { return size_; } |
| 41 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 42 | |
| 43 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 44 | size_t size_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 45 | UniquePtr<uint8_t[]> memory_; |
| 46 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 47 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 50 | static void TestCode(const uint16_t* data, bool has_result = false, int32_t expected = 0) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 51 | ArenaPool pool; |
| 52 | ArenaAllocator arena(&pool); |
| 53 | HGraphBuilder builder(&arena); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 54 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 55 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 56 | ASSERT_NE(graph, nullptr); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 57 | InternalCodeAllocator allocator; |
| 58 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86); |
| 59 | codegen->Compile(&allocator); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 60 | typedef int32_t (*fptr)(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 61 | #if defined(__i386__) |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 62 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
| 63 | int32_t result = reinterpret_cast<fptr>(allocator.GetMemory())(); |
Nicolas Geoffray | 39d57e2 | 2014-03-13 10:28:41 +0000 | [diff] [blame] | 64 | if (has_result) { |
| 65 | CHECK_EQ(result, expected); |
| 66 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 67 | #endif |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 68 | codegen = CodeGenerator::Create(&arena, graph, kArm); |
| 69 | codegen->Compile(&allocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 70 | #if defined(__arm__) |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 71 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
| 72 | int32_t result = reinterpret_cast<fptr>(allocator.GetMemory())(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 73 | if (has_result) { |
| 74 | CHECK_EQ(result, expected); |
| 75 | } |
Nicolas Geoffray | 39d57e2 | 2014-03-13 10:28:41 +0000 | [diff] [blame] | 76 | #endif |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 80 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 81 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 84 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 85 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 86 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 87 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 88 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 89 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 92 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 93 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 94 | Instruction::GOTO | 0x100, |
| 95 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 96 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 97 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 98 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 101 | TEST(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 102 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 103 | Instruction::GOTO | 0x200, |
| 104 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 105 | Instruction::GOTO | 0xFF00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 106 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 107 | TestCode(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 108 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 109 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | Instruction::GOTO_16, 3, |
| 111 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 112 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 113 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 114 | TestCode(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 115 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 116 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 117 | Instruction::GOTO_32, 4, 0, |
| 118 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 119 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 120 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 121 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 124 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 125 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 126 | Instruction::RETURN_VOID, |
| 127 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 128 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 129 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 130 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 133 | TEST(CodegenTest, CFG5) { |
| 134 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 135 | Instruction::CONST_4 | 0 | 0, |
| 136 | Instruction::IF_EQ, 3, |
| 137 | Instruction::GOTO | 0x100, |
| 138 | Instruction::RETURN_VOID); |
| 139 | |
| 140 | TestCode(data); |
| 141 | } |
| 142 | |
| 143 | TEST(CodegenTest, IntConstant) { |
| 144 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 145 | Instruction::CONST_4 | 0 | 0, |
| 146 | Instruction::RETURN_VOID); |
| 147 | |
| 148 | TestCode(data); |
| 149 | } |
| 150 | |
| 151 | TEST(CodegenTest, Return1) { |
| 152 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 153 | Instruction::CONST_4 | 0 | 0, |
| 154 | Instruction::RETURN | 0); |
| 155 | |
| 156 | TestCode(data, true, 0); |
| 157 | } |
| 158 | |
| 159 | TEST(CodegenTest, Return2) { |
| 160 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 161 | Instruction::CONST_4 | 0 | 0, |
| 162 | Instruction::CONST_4 | 0 | 1 << 8, |
| 163 | Instruction::RETURN | 1 << 8); |
| 164 | |
| 165 | TestCode(data, true, 0); |
| 166 | } |
| 167 | |
| 168 | TEST(CodegenTest, Return3) { |
| 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::RETURN | 1 << 8); |
| 173 | |
| 174 | TestCode(data, true, 1); |
| 175 | } |
| 176 | |
| 177 | TEST(CodegenTest, ReturnIf1) { |
| 178 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 179 | Instruction::CONST_4 | 0 | 0, |
| 180 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 181 | Instruction::IF_EQ, 3, |
| 182 | Instruction::RETURN | 0 << 8, |
| 183 | Instruction::RETURN | 1 << 8); |
| 184 | |
| 185 | TestCode(data, true, 1); |
| 186 | } |
| 187 | |
| 188 | TEST(CodegenTest, ReturnIf2) { |
| 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 | 0 << 4 | 1 << 8, 3, |
| 193 | Instruction::RETURN | 0 << 8, |
| 194 | Instruction::RETURN | 1 << 8); |
| 195 | |
| 196 | TestCode(data, true, 0); |
| 197 | } |
| 198 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 199 | TEST(CodegenTest, ReturnAdd1) { |
| 200 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 201 | Instruction::CONST_4 | 3 << 12 | 0, |
| 202 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 203 | Instruction::ADD_INT, 1 << 8 | 0, |
| 204 | Instruction::RETURN); |
| 205 | |
| 206 | TestCode(data, true, 7); |
| 207 | } |
| 208 | |
| 209 | TEST(CodegenTest, ReturnAdd2) { |
| 210 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 211 | Instruction::CONST_4 | 3 << 12 | 0, |
| 212 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 213 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 214 | Instruction::RETURN); |
| 215 | |
| 216 | TestCode(data, true, 7); |
| 217 | } |
| 218 | |
| 219 | TEST(CodegenTest, ReturnAdd3) { |
| 220 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 221 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 222 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 223 | Instruction::RETURN); |
| 224 | |
| 225 | TestCode(data, true, 7); |
| 226 | } |
| 227 | |
| 228 | TEST(CodegenTest, ReturnAdd4) { |
| 229 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 230 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 231 | Instruction::ADD_INT_LIT16, 3, |
| 232 | Instruction::RETURN); |
| 233 | |
| 234 | TestCode(data, true, 7); |
| 235 | } |
| 236 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 237 | } // namespace art |