blob: 6d4588dd3d2eca0ec15abc6c0eae59c71c45c6a9 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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 Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "dex_instruction.h"
22#include "instruction_set.h"
23#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "optimizing_unit_test.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025
26#include "gtest/gtest.h"
27
28namespace art {
29
30class 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 Geoffray3ff386a2014-03-04 14:46:47 +000048static void TestCode(const uint16_t* data) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000049 ArenaPool pool;
50 ArenaAllocator arena(&pool);
51 HGraphBuilder builder(&arena);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
53 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054 ASSERT_NE(graph, nullptr);
55 ExecutableMemoryAllocator allocator;
56 CHECK(CodeGenerator::CompileGraph(graph, kX86, &allocator));
57 typedef void (*fptr)();
58#if defined(__i386__)
59 reinterpret_cast<fptr>(allocator.memory())();
60#endif
61 CHECK(CodeGenerator::CompileGraph(graph, kArm, &allocator));
62#if defined(__arm__)
63 reinterpret_cast<fptr>(allocator.memory())();
64#endif
65}
66
67TEST(CodegenTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000068 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
69 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000070}
71
72TEST(PrettyPrinterTest, CFG1) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000074 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000075 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000077 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000078}
79
80TEST(PrettyPrinterTest, CFG2) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000081 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000082 Instruction::GOTO | 0x100,
83 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000084 Instruction::RETURN_VOID);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000086 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000087}
88
89TEST(PrettyPrinterTest, CFG3) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000090 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091 Instruction::GOTO | 0x200,
92 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000093 Instruction::GOTO | 0xFF00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000095 TestCode(data1);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000096
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000097 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098 Instruction::GOTO_16, 3,
99 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000101
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000102 TestCode(data2);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000104 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105 Instruction::GOTO_32, 4, 0,
106 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000109 TestCode(data3);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000110}
111
112TEST(PrettyPrinterTest, CFG4) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000114 Instruction::RETURN_VOID,
115 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 Instruction::GOTO | 0xFE00);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 TestCode(data);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000119}
120
121} // namespace art