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" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 18 | #include "code_generator_arm.h" |
| 19 | #include "code_generator_x86.h" |
| 20 | #include "code_generator_x86_64.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | #include "common_compiler_test.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 22 | #include "dex_file.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 23 | #include "dex_instruction.h" |
| 24 | #include "instruction_set.h" |
| 25 | #include "nodes.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 26 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 32 | class InternalCodeAllocator : public CodeAllocator { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 33 | public: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 34 | InternalCodeAllocator() { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 35 | |
| 36 | virtual uint8_t* Allocate(size_t size) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 37 | size_ = size; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 38 | memory_.reset(new uint8_t[size]); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 39 | return memory_.get(); |
| 40 | } |
| 41 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 42 | size_t GetSize() const { return size_; } |
| 43 | uint8_t* GetMemory() const { return memory_.get(); } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 44 | |
| 45 | private: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 46 | size_t size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 47 | std::unique_ptr<uint8_t[]> memory_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 48 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | DISALLOW_COPY_AND_ASSIGN(InternalCodeAllocator); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 52 | static void Run(const InternalCodeAllocator& allocator, |
| 53 | const CodeGenerator& codegen, |
| 54 | bool has_result, |
| 55 | int32_t expected) { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 56 | typedef int32_t (*fptr)(); |
| 57 | CommonCompilerTest::MakeExecutable(allocator.GetMemory(), allocator.GetSize()); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 58 | fptr f = reinterpret_cast<fptr>(allocator.GetMemory()); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 59 | if (codegen.GetInstructionSet() == kThumb2) { |
| 60 | // For thumb we need the bottom bit set. |
| 61 | f = reinterpret_cast<fptr>(reinterpret_cast<uintptr_t>(f) + 1); |
| 62 | } |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 63 | int32_t result = f(); |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 64 | if (has_result) { |
| 65 | CHECK_EQ(result, expected); |
| 66 | } |
| 67 | } |
| 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 | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 75 | // Remove suspend checks, they cannot be executed in this context. |
| 76 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 77 | ASSERT_NE(graph, nullptr); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 78 | InternalCodeAllocator allocator; |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 79 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 80 | x86::CodeGeneratorX86 codegenX86(graph); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 81 | // We avoid doing a stack overflow check that requires the runtime being setup, |
| 82 | // by making sure the compiler knows the methods we are running are leaf methods. |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 83 | codegenX86.CompileBaseline(&allocator, true); |
| 84 | if (kRuntimeISA == kX86) { |
| 85 | Run(allocator, codegenX86, has_result, expected); |
| 86 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 87 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 88 | arm::CodeGeneratorARM codegenARM(graph); |
| 89 | codegenARM.CompileBaseline(&allocator, true); |
| 90 | if (kRuntimeISA == kArm || kRuntimeISA == kThumb2) { |
| 91 | Run(allocator, codegenARM, has_result, expected); |
| 92 | } |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 93 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 94 | x86_64::CodeGeneratorX86_64 codegenX86_64(graph); |
| 95 | codegenX86_64.CompileBaseline(&allocator, true); |
| 96 | if (kRuntimeISA == kX86_64) { |
| 97 | Run(allocator, codegenX86_64, has_result, expected); |
| 98 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 102 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 103 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 106 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 107 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 108 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 109 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 110 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 111 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 114 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 115 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 116 | Instruction::GOTO | 0x100, |
| 117 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 118 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 119 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 120 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 123 | TEST(CodegenTest, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 124 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 125 | Instruction::GOTO | 0x200, |
| 126 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 127 | Instruction::GOTO | 0xFF00); |
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 | TestCode(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 130 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 131 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 132 | Instruction::GOTO_16, 3, |
| 133 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 134 | Instruction::GOTO_16, 0xFFFF); |
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 | TestCode(data2); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 137 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 138 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 139 | Instruction::GOTO_32, 4, 0, |
| 140 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 141 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 142 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 143 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 146 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 147 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 148 | Instruction::RETURN_VOID, |
| 149 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 150 | Instruction::GOTO | 0xFE00); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 151 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 152 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 155 | TEST(CodegenTest, CFG5) { |
| 156 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 157 | Instruction::CONST_4 | 0 | 0, |
| 158 | Instruction::IF_EQ, 3, |
| 159 | Instruction::GOTO | 0x100, |
| 160 | Instruction::RETURN_VOID); |
| 161 | |
| 162 | TestCode(data); |
| 163 | } |
| 164 | |
| 165 | TEST(CodegenTest, IntConstant) { |
| 166 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 167 | Instruction::CONST_4 | 0 | 0, |
| 168 | Instruction::RETURN_VOID); |
| 169 | |
| 170 | TestCode(data); |
| 171 | } |
| 172 | |
| 173 | TEST(CodegenTest, Return1) { |
| 174 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 175 | Instruction::CONST_4 | 0 | 0, |
| 176 | Instruction::RETURN | 0); |
| 177 | |
| 178 | TestCode(data, true, 0); |
| 179 | } |
| 180 | |
| 181 | TEST(CodegenTest, Return2) { |
| 182 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 183 | Instruction::CONST_4 | 0 | 0, |
| 184 | Instruction::CONST_4 | 0 | 1 << 8, |
| 185 | Instruction::RETURN | 1 << 8); |
| 186 | |
| 187 | TestCode(data, true, 0); |
| 188 | } |
| 189 | |
| 190 | TEST(CodegenTest, Return3) { |
| 191 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 192 | Instruction::CONST_4 | 0 | 0, |
| 193 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 194 | Instruction::RETURN | 1 << 8); |
| 195 | |
| 196 | TestCode(data, true, 1); |
| 197 | } |
| 198 | |
| 199 | TEST(CodegenTest, ReturnIf1) { |
| 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, 3, |
| 204 | Instruction::RETURN | 0 << 8, |
| 205 | Instruction::RETURN | 1 << 8); |
| 206 | |
| 207 | TestCode(data, true, 1); |
| 208 | } |
| 209 | |
| 210 | TEST(CodegenTest, ReturnIf2) { |
| 211 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 212 | Instruction::CONST_4 | 0 | 0, |
| 213 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 214 | Instruction::IF_EQ | 0 << 4 | 1 << 8, 3, |
| 215 | Instruction::RETURN | 0 << 8, |
| 216 | Instruction::RETURN | 1 << 8); |
| 217 | |
| 218 | TestCode(data, true, 0); |
| 219 | } |
| 220 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 221 | TEST(CodegenTest, ReturnAdd1) { |
| 222 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 223 | Instruction::CONST_4 | 3 << 12 | 0, |
| 224 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 225 | Instruction::ADD_INT, 1 << 8 | 0, |
| 226 | Instruction::RETURN); |
| 227 | |
| 228 | TestCode(data, true, 7); |
| 229 | } |
| 230 | |
| 231 | TEST(CodegenTest, ReturnAdd2) { |
| 232 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 233 | Instruction::CONST_4 | 3 << 12 | 0, |
| 234 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 235 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 236 | Instruction::RETURN); |
| 237 | |
| 238 | TestCode(data, true, 7); |
| 239 | } |
| 240 | |
| 241 | TEST(CodegenTest, ReturnAdd3) { |
| 242 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 243 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 244 | Instruction::ADD_INT_LIT8, 3 << 8 | 0, |
| 245 | Instruction::RETURN); |
| 246 | |
| 247 | TestCode(data, true, 7); |
| 248 | } |
| 249 | |
| 250 | TEST(CodegenTest, ReturnAdd4) { |
| 251 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 252 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 253 | Instruction::ADD_INT_LIT16, 3, |
| 254 | Instruction::RETURN); |
| 255 | |
| 256 | TestCode(data, true, 7); |
| 257 | } |
| 258 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 259 | } // namespace art |