codegen_test.cc revision 3ff386aafefd5282bb76c8a50506a70a4321e698
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_file.h"
21#include "dex_instruction.h"
22#include "instruction_set.h"
23#include "nodes.h"
24#include "optimizing_unit_test.h"
25
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
48static void TestCode(const uint16_t* data) {
49  ArenaPool pool;
50  ArenaAllocator arena(&pool);
51  HGraphBuilder builder(&arena);
52  const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
53  HGraph* graph = builder.BuildGraph(*item);
54  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) {
68  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(Instruction::RETURN_VOID);
69  TestCode(data);
70}
71
72TEST(PrettyPrinterTest, CFG1) {
73  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
74    Instruction::GOTO | 0x100,
75    Instruction::RETURN_VOID);
76
77  TestCode(data);
78}
79
80TEST(PrettyPrinterTest, CFG2) {
81  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
82    Instruction::GOTO | 0x100,
83    Instruction::GOTO | 0x100,
84    Instruction::RETURN_VOID);
85
86  TestCode(data);
87}
88
89TEST(PrettyPrinterTest, CFG3) {
90  const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
91    Instruction::GOTO | 0x200,
92    Instruction::RETURN_VOID,
93    Instruction::GOTO | 0xFF00);
94
95  TestCode(data1);
96
97  const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
98    Instruction::GOTO_16, 3,
99    Instruction::RETURN_VOID,
100    Instruction::GOTO_16, 0xFFFF);
101
102  TestCode(data2);
103
104  const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
105    Instruction::GOTO_32, 4, 0,
106    Instruction::RETURN_VOID,
107    Instruction::GOTO_32, 0xFFFF, 0xFFFF);
108
109  TestCode(data3);
110}
111
112TEST(PrettyPrinterTest, CFG4) {
113  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
114    Instruction::RETURN_VOID,
115    Instruction::GOTO | 0x100,
116    Instruction::GOTO | 0xFE00);
117
118  TestCode(data);
119}
120
121}  // namespace art
122