codegen_test.cc revision d4dd255db1d110ceb5551f6d95ff31fb57420994
1d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray/* 2d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 4d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * you may not use this file except in compliance with the License. 6d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * You may obtain a copy of the License at 7d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 8d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * 10d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * See the License for the specific language governing permissions and 14d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * limitations under the License. 15d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray */ 16d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 17d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "builder.h" 18d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "code_generator.h" 19d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "common_compiler_test.h" 20d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "dex_instruction.h" 21d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "instruction_set.h" 22d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "nodes.h" 23d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 24d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "gtest/gtest.h" 25d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 26d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffraynamespace art { 27d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 28d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffrayclass ExecutableMemoryAllocator : public CodeAllocator { 29d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray public: 30d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray ExecutableMemoryAllocator() { } 31d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 32d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 33d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray memory_.reset(new uint8_t[size]); 34d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray CommonCompilerTest::MakeExecutable(memory_.get(), size); 35d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray return memory_.get(); 36d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray } 37d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 38d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray uint8_t* memory() const { return memory_.get(); } 39d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 40d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray private: 41d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray UniquePtr<uint8_t[]> memory_; 42d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 43d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(ExecutableMemoryAllocator); 44d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}; 45d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 46d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffraystatic void TestCode(const uint16_t* data, int length) { 47d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray ArenaPool pool; 48d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray ArenaAllocator arena(&pool); 49d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray HGraphBuilder builder(&arena); 50d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray HGraph* graph = builder.BuildGraph(data, data + length); 51d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray ASSERT_NE(graph, nullptr); 52d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray ExecutableMemoryAllocator allocator; 53d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray CHECK(CodeGenerator::CompileGraph(graph, kX86, &allocator)); 54d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray typedef void (*fptr)(); 55d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#if defined(__i386__) 56d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray reinterpret_cast<fptr>(allocator.memory())(); 57d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#endif 58d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray CHECK(CodeGenerator::CompileGraph(graph, kArm, &allocator)); 59d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#if defined(__arm__) 60d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray reinterpret_cast<fptr>(allocator.memory())(); 61d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#endif 62d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 63d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 64d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas GeoffrayTEST(CodegenTest, ReturnVoid) { 65d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data[] = { Instruction::RETURN_VOID }; 66d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data, sizeof(data) / sizeof(uint16_t)); 67d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 68d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 69d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas GeoffrayTEST(PrettyPrinterTest, CFG1) { 70d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data[] = { 71d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0x100, 72d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID 73d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 74d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 75d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data, sizeof(data) / sizeof(uint16_t)); 76d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 77d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 78d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas GeoffrayTEST(PrettyPrinterTest, CFG2) { 79d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data[] = { 80d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0x100, 81d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0x100, 82d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID 83d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 84d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 85d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data, sizeof(data) / sizeof(uint16_t)); 86d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 87d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 88d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas GeoffrayTEST(PrettyPrinterTest, CFG3) { 89d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data1[] = { 90d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0x200, 91d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID, 92d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0xFF00 93d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 94d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 95d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data1, sizeof(data1) / sizeof(uint16_t)); 96d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 97d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data2[] = { 98d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO_16, 3, 99d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID, 100d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO_16, 0xFFFF 101d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 102d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 103d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data2, sizeof(data2) / sizeof(uint16_t)); 104d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 105d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data3[] = { 106d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO_32, 4, 0, 107d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID, 108d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO_32, 0xFFFF, 0xFFFF 109d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 110d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 111d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data3, sizeof(data3) / sizeof(uint16_t)); 112d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 113d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 114d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas GeoffrayTEST(PrettyPrinterTest, CFG4) { 115d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray const uint16_t data[] = { 116d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::RETURN_VOID, 117d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0x100, 118d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray Instruction::GOTO | 0xFE00 119d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray }; 120d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 121d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray TestCode(data, sizeof(data) / sizeof(uint16_t)); 122d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} 123d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray 124d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray} // namespace art 125