codegen_test.cc revision d4dd255db1d110ceb5551f6d95ff31fb57420994
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" 20#include "dex_instruction.h" 21#include "instruction_set.h" 22#include "nodes.h" 23 24#include "gtest/gtest.h" 25 26namespace art { 27 28class ExecutableMemoryAllocator : public CodeAllocator { 29 public: 30 ExecutableMemoryAllocator() { } 31 32 virtual uint8_t* Allocate(size_t size) { 33 memory_.reset(new uint8_t[size]); 34 CommonCompilerTest::MakeExecutable(memory_.get(), size); 35 return memory_.get(); 36 } 37 38 uint8_t* memory() const { return memory_.get(); } 39 40 private: 41 UniquePtr<uint8_t[]> memory_; 42 43 DISALLOW_COPY_AND_ASSIGN(ExecutableMemoryAllocator); 44}; 45 46static void TestCode(const uint16_t* data, int length) { 47 ArenaPool pool; 48 ArenaAllocator arena(&pool); 49 HGraphBuilder builder(&arena); 50 HGraph* graph = builder.BuildGraph(data, data + length); 51 ASSERT_NE(graph, nullptr); 52 ExecutableMemoryAllocator allocator; 53 CHECK(CodeGenerator::CompileGraph(graph, kX86, &allocator)); 54 typedef void (*fptr)(); 55#if defined(__i386__) 56 reinterpret_cast<fptr>(allocator.memory())(); 57#endif 58 CHECK(CodeGenerator::CompileGraph(graph, kArm, &allocator)); 59#if defined(__arm__) 60 reinterpret_cast<fptr>(allocator.memory())(); 61#endif 62} 63 64TEST(CodegenTest, ReturnVoid) { 65 const uint16_t data[] = { Instruction::RETURN_VOID }; 66 TestCode(data, sizeof(data) / sizeof(uint16_t)); 67} 68 69TEST(PrettyPrinterTest, CFG1) { 70 const uint16_t data[] = { 71 Instruction::GOTO | 0x100, 72 Instruction::RETURN_VOID 73 }; 74 75 TestCode(data, sizeof(data) / sizeof(uint16_t)); 76} 77 78TEST(PrettyPrinterTest, CFG2) { 79 const uint16_t data[] = { 80 Instruction::GOTO | 0x100, 81 Instruction::GOTO | 0x100, 82 Instruction::RETURN_VOID 83 }; 84 85 TestCode(data, sizeof(data) / sizeof(uint16_t)); 86} 87 88TEST(PrettyPrinterTest, CFG3) { 89 const uint16_t data1[] = { 90 Instruction::GOTO | 0x200, 91 Instruction::RETURN_VOID, 92 Instruction::GOTO | 0xFF00 93 }; 94 95 TestCode(data1, sizeof(data1) / sizeof(uint16_t)); 96 97 const uint16_t data2[] = { 98 Instruction::GOTO_16, 3, 99 Instruction::RETURN_VOID, 100 Instruction::GOTO_16, 0xFFFF 101 }; 102 103 TestCode(data2, sizeof(data2) / sizeof(uint16_t)); 104 105 const uint16_t data3[] = { 106 Instruction::GOTO_32, 4, 0, 107 Instruction::RETURN_VOID, 108 Instruction::GOTO_32, 0xFFFF, 0xFFFF 109 }; 110 111 TestCode(data3, sizeof(data3) / sizeof(uint16_t)); 112} 113 114TEST(PrettyPrinterTest, CFG4) { 115 const uint16_t data[] = { 116 Instruction::RETURN_VOID, 117 Instruction::GOTO | 0x100, 118 Instruction::GOTO | 0xFE00 119 }; 120 121 TestCode(data, sizeof(data) / sizeof(uint16_t)); 122} 123 124} // namespace art 125