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_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 45 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 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 | 49c105d | 2014-06-13 10:29:43 +0100 | [diff] [blame] | 50 | #if defined(__i386__) || defined(__arm__) || defined(__x86_64__) |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 51 | static void Run(const InternalCodeAllocator& allocator, |
| 52 | const CodeGenerator& codegen, |
| 53 | bool has_result, |
| 54 | int32_t expected) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 55 | typedef int32_t (*fptr)(); |
| 56 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 57 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 58 | 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 Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 62 | int32_t result = f(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 63 | if (has_result) { |
| 64 | CHECK_EQ(result, expected); |
| 65 | } |
| 66 | } |
Nicolas Geoffray | 49c105d | 2014-06-13 10:29:43 +0100 | [diff] [blame] | 67 | #endif |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 68 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 69 | 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] | 70 | ArenaPool pool; |
| 71 | ArenaAllocator arena(&pool); |
| 72 | HGraphBuilder builder(&arena); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 73 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 74 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 75 | ASSERT_NE(graph, nullptr); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 76 | InternalCodeAllocator allocator; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 77 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 78 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, kX86); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 79 | // We avoid doing a stack overflow check that requires the runtime being setup, |
| 80 | // by making sure the compiler knows the methods we are running are leaf methods. |
| 81 | codegen->CompileBaseline(&allocator, true); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 82 | #if defined(__i386__) |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 83 | Run(allocator, *codegen, has_result, expected); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 84 | #endif |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 85 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 86 | codegen = CodeGenerator::Create(&arena, graph, kArm); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 87 | codegen->CompileBaseline(&allocator, true); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 88 | #if defined(__arm__) |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 89 | Run(allocator, *codegen, has_result, expected); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | codegen = CodeGenerator::Create(&arena, graph, kX86_64); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 93 | codegen->CompileBaseline(&allocator, true); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 94 | #if defined(__x86_64__) |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 95 | Run(allocator, *codegen, has_result, expected); |
Nicolas Geoffray | 39d57e2 | 2014-03-13 10:28:41 +0000 | [diff] [blame] | 96 | #endif |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 100 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 101 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 104 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 105 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 106 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 107 | Instruction::RETURN_VOID); |
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 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 112 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 113 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 114 | Instruction::GOTO | 0x100, |
| 115 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 116 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 117 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 118 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 121 | TEST(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 122 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 123 | Instruction::GOTO | 0x200, |
| 124 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 125 | Instruction::GOTO | 0xFF00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 126 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 127 | TestCode(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 128 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 129 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 130 | Instruction::GOTO_16, 3, |
| 131 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 132 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 133 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 134 | TestCode(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 135 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 136 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 137 | Instruction::GOTO_32, 4, 0, |
| 138 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 139 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 140 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 141 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 144 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 145 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 146 | Instruction::RETURN_VOID, |
| 147 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 148 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 149 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 150 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 153 | TEST(CodegenTest, CFG5) { |
| 154 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 155 | Instruction::CONST_4 | 0 | 0, |
| 156 | Instruction::IF_EQ, 3, |
| 157 | Instruction::GOTO | 0x100, |
| 158 | Instruction::RETURN_VOID); |
| 159 | |
| 160 | TestCode(data); |
| 161 | } |
| 162 | |
| 163 | TEST(CodegenTest, IntConstant) { |
| 164 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 165 | Instruction::CONST_4 | 0 | 0, |
| 166 | Instruction::RETURN_VOID); |
| 167 | |
| 168 | TestCode(data); |
| 169 | } |
| 170 | |
| 171 | TEST(CodegenTest, Return1) { |
| 172 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 173 | Instruction::CONST_4 | 0 | 0, |
| 174 | Instruction::RETURN | 0); |
| 175 | |
| 176 | TestCode(data, true, 0); |
| 177 | } |
| 178 | |
| 179 | TEST(CodegenTest, Return2) { |
| 180 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 181 | Instruction::CONST_4 | 0 | 0, |
| 182 | Instruction::CONST_4 | 0 | 1 << 8, |
| 183 | Instruction::RETURN | 1 << 8); |
| 184 | |
| 185 | TestCode(data, true, 0); |
| 186 | } |
| 187 | |
| 188 | TEST(CodegenTest, Return3) { |
| 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::RETURN | 1 << 8); |
| 193 | |
| 194 | TestCode(data, true, 1); |
| 195 | } |
| 196 | |
| 197 | TEST(CodegenTest, ReturnIf1) { |
| 198 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 199 | Instruction::CONST_4 | 0 | 0, |
| 200 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 201 | Instruction::IF_EQ, 3, |
| 202 | Instruction::RETURN | 0 << 8, |
| 203 | Instruction::RETURN | 1 << 8); |
| 204 | |
| 205 | TestCode(data, true, 1); |
| 206 | } |
| 207 | |
| 208 | TEST(CodegenTest, ReturnIf2) { |
| 209 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 210 | Instruction::CONST_4 | 0 | 0, |
| 211 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 212 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 213 | Instruction::RETURN | 0 << 8, |
| 214 | Instruction::RETURN | 1 << 8); |
| 215 | |
| 216 | TestCode(data, true, 0); |
| 217 | } |
| 218 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 219 | TEST(CodegenTest, ReturnAdd1) { |
| 220 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 221 | Instruction::CONST_4 | 3 << 12 | 0, |
| 222 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 223 | Instruction::ADD_INT, 1 << 8 | 0, |
| 224 | Instruction::RETURN); |
| 225 | |
| 226 | TestCode(data, true, 7); |
| 227 | } |
| 228 | |
| 229 | TEST(CodegenTest, ReturnAdd2) { |
| 230 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 231 | Instruction::CONST_4 | 3 << 12 | 0, |
| 232 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 233 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 234 | Instruction::RETURN); |
| 235 | |
| 236 | TestCode(data, true, 7); |
| 237 | } |
| 238 | |
| 239 | TEST(CodegenTest, ReturnAdd3) { |
| 240 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 241 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 242 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 243 | Instruction::RETURN); |
| 244 | |
| 245 | TestCode(data, true, 7); |
| 246 | } |
| 247 | |
| 248 | TEST(CodegenTest, ReturnAdd4) { |
| 249 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 250 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 251 | Instruction::ADD_INT_LIT16, 3, |
| 252 | Instruction::RETURN); |
| 253 | |
| 254 | TestCode(data, true, 7); |
| 255 | } |
| 256 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 257 | } // namespace art |