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 | |
| 30 | class 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 Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 48 | 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] | 49 | ArenaPool pool; |
| 50 | ArenaAllocator arena(&pool); |
| 51 | HGraphBuilder builder(&arena); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 53 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 54 | ASSERT_NE(graph, nullptr); |
| 55 | ExecutableMemoryAllocator allocator; |
| 56 | CHECK(CodeGenerator::CompileGraph(graph, kX86, &allocator)); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 57 | typedef int32_t (*fptr)(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 58 | #if defined(__i386__) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 59 | int32_t result = reinterpret_cast<fptr>(allocator.memory())(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 60 | #endif |
| 61 | CHECK(CodeGenerator::CompileGraph(graph, kArm, &allocator)); |
| 62 | #if defined(__arm__) |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 63 | int32_t result = reinterpret_cast<fptr>(allocator.memory())(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 64 | #endif |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 65 | if (has_result) { |
| 66 | CHECK_EQ(result, expected); |
| 67 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(CodegenTest, ReturnVoid) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 71 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID); |
| 72 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 75 | TEST(CodegenTest, CFG1) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 76 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 77 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 78 | Instruction::RETURN_VOID); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 79 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 80 | TestCode(data); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 83 | TEST(CodegenTest, CFG2) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 84 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 85 | Instruction::GOTO | 0x100, |
| 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, CFG3) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 93 | const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 94 | Instruction::GOTO | 0x200, |
| 95 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 96 | Instruction::GOTO | 0xFF00); |
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(data1); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 99 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 100 | const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 101 | Instruction::GOTO_16, 3, |
| 102 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 103 | Instruction::GOTO_16, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 104 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 105 | TestCode(data2); |
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 | const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 108 | Instruction::GOTO_32, 4, 0, |
| 109 | Instruction::RETURN_VOID, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 110 | Instruction::GOTO_32, 0xFFFF, 0xFFFF); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 111 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 112 | TestCode(data3); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame^] | 115 | TEST(CodegenTest, CFG4) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 116 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 117 | Instruction::RETURN_VOID, |
| 118 | Instruction::GOTO | 0x100, |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 119 | Instruction::GOTO | 0xFE00); |
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(data); |
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, 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 | |
| 134 | TEST(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 | |
| 142 | TEST(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 | |
| 150 | TEST(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 | |
| 159 | TEST(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 | |
| 168 | TEST(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 | |
| 179 | TEST(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 Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 190 | } // namespace art |