pretty_printer_test.cc revision fbc695f9b8e2084697e19c1355ab925f99f0d235
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 "base/stringprintf.h"
18#include "builder.h"
19#include "dex_file.h"
20#include "dex_instruction.h"
21#include "nodes.h"
22#include "optimizing_unit_test.h"
23#include "pretty_printer.h"
24#include "utils/arena_allocator.h"
25
26#include "gtest/gtest.h"
27
28namespace art {
29
30static void TestCode(const uint16_t* data, const char* expected) {
31  ArenaPool pool;
32  ArenaAllocator allocator(&pool);
33  HGraphBuilder builder(&allocator);
34  const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
35  HGraph* graph = builder.BuildGraph(*item);
36  ASSERT_NE(graph, nullptr);
37  StringPrettyPrinter printer(graph);
38  printer.VisitInsertionOrder();
39  ASSERT_STREQ(expected, printer.str().c_str());
40}
41
42TEST(PrettyPrinterTest, ReturnVoid) {
43  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
44      Instruction::RETURN_VOID);
45
46  const char* expected =
47      "BasicBlock 0, succ: 1\n"
48      "  2: SuspendCheck\n"
49      "  3: Goto 1\n"
50      "BasicBlock 1, pred: 0, succ: 2\n"
51      "  0: ReturnVoid\n"
52      "BasicBlock 2, pred: 1\n"
53      "  1: Exit\n";
54
55  TestCode(data, expected);
56}
57
58TEST(PrettyPrinterTest, CFG1) {
59  const char* expected =
60    "BasicBlock 0, succ: 1\n"
61    "  3: SuspendCheck\n"
62    "  4: Goto 1\n"
63    "BasicBlock 1, pred: 0, succ: 2\n"
64    "  0: Goto 2\n"
65    "BasicBlock 2, pred: 1, succ: 3\n"
66    "  1: ReturnVoid\n"
67    "BasicBlock 3, pred: 2\n"
68    "  2: Exit\n";
69
70  const uint16_t data[] =
71    ZERO_REGISTER_CODE_ITEM(
72      Instruction::GOTO | 0x100,
73      Instruction::RETURN_VOID);
74
75  TestCode(data, expected);
76}
77
78TEST(PrettyPrinterTest, CFG2) {
79  const char* expected =
80    "BasicBlock 0, succ: 1\n"
81    "  4: SuspendCheck\n"
82    "  5: Goto 1\n"
83    "BasicBlock 1, pred: 0, succ: 2\n"
84    "  0: Goto 2\n"
85    "BasicBlock 2, pred: 1, succ: 3\n"
86    "  1: Goto 3\n"
87    "BasicBlock 3, pred: 2, succ: 4\n"
88    "  2: ReturnVoid\n"
89    "BasicBlock 4, pred: 3\n"
90    "  3: Exit\n";
91
92  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
93    Instruction::GOTO | 0x100,
94    Instruction::GOTO | 0x100,
95    Instruction::RETURN_VOID);
96
97  TestCode(data, expected);
98}
99
100TEST(PrettyPrinterTest, CFG3) {
101  const char* expected =
102    "BasicBlock 0, succ: 1\n"
103    "  5: SuspendCheck\n"
104    "  6: Goto 1\n"
105    "BasicBlock 1, pred: 0, succ: 3\n"
106    "  0: Goto 3\n"
107    "BasicBlock 2, pred: 3, succ: 4\n"
108    "  1: ReturnVoid\n"
109    "BasicBlock 3, pred: 1, succ: 2\n"
110    "  2: SuspendCheck\n"
111    "  3: Goto 2\n"
112    "BasicBlock 4, pred: 2\n"
113    "  4: Exit\n";
114
115  const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
116    Instruction::GOTO | 0x200,
117    Instruction::RETURN_VOID,
118    Instruction::GOTO | 0xFF00);
119
120  TestCode(data1, expected);
121
122  const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
123    Instruction::GOTO_16, 3,
124    Instruction::RETURN_VOID,
125    Instruction::GOTO_16, 0xFFFF);
126
127  TestCode(data2, expected);
128
129  const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
130    Instruction::GOTO_32, 4, 0,
131    Instruction::RETURN_VOID,
132    Instruction::GOTO_32, 0xFFFF, 0xFFFF);
133
134  TestCode(data3, expected);
135}
136
137TEST(PrettyPrinterTest, CFG4) {
138  const char* expected =
139    "BasicBlock 0, succ: 1\n"
140    "  3: SuspendCheck\n"
141    "  4: Goto 1\n"
142    "BasicBlock 1, pred: 0, 1, succ: 1\n"
143    "  0: SuspendCheck\n"
144    "  1: Goto 1\n"
145    "BasicBlock 2\n"
146    "  2: Exit\n";
147
148  const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
149    Instruction::NOP,
150    Instruction::GOTO | 0xFF00);
151
152  TestCode(data1, expected);
153
154  const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
155    Instruction::GOTO_32, 0, 0);
156
157  TestCode(data2, expected);
158}
159
160TEST(PrettyPrinterTest, CFG5) {
161  const char* expected =
162    "BasicBlock 0, succ: 1\n"
163    "  4: SuspendCheck\n"
164    "  5: Goto 1\n"
165    "BasicBlock 1, pred: 0, 2, succ: 3\n"
166    "  0: ReturnVoid\n"
167    "BasicBlock 2, succ: 1\n"
168    "  1: SuspendCheck\n"
169    "  2: Goto 1\n"
170    "BasicBlock 3, pred: 1\n"
171    "  3: Exit\n";
172
173  const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
174    Instruction::RETURN_VOID,
175    Instruction::GOTO | 0x100,
176    Instruction::GOTO | 0xFE00);
177
178  TestCode(data, expected);
179}
180
181TEST(PrettyPrinterTest, CFG6) {
182  const char* expected =
183    "BasicBlock 0, succ: 1\n"
184    "  0: Local [4, 3, 2]\n"
185    "  1: IntConstant [2]\n"
186    "  10: SuspendCheck\n"
187    "  11: Goto 1\n"
188    "BasicBlock 1, pred: 0, succ: 3, 2\n"
189    "  2: StoreLocal(0, 1)\n"
190    "  3: LoadLocal(0) [5]\n"
191    "  4: LoadLocal(0) [5]\n"
192    "  5: Equal(3, 4) [6]\n"
193    "  6: If(5)\n"
194    "BasicBlock 2, pred: 1, succ: 3\n"
195    "  7: Goto 3\n"
196    "BasicBlock 3, pred: 1, 2, succ: 4\n"
197    "  8: ReturnVoid\n"
198    "BasicBlock 4, pred: 3\n"
199    "  9: Exit\n";
200
201  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
202    Instruction::CONST_4 | 0 | 0,
203    Instruction::IF_EQ, 3,
204    Instruction::GOTO | 0x100,
205    Instruction::RETURN_VOID);
206
207  TestCode(data, expected);
208}
209
210TEST(PrettyPrinterTest, CFG7) {
211  const char* expected =
212    "BasicBlock 0, succ: 1\n"
213    "  0: Local [4, 3, 2]\n"
214    "  1: IntConstant [2]\n"
215    "  11: SuspendCheck\n"
216    "  12: Goto 1\n"
217    "BasicBlock 1, pred: 0, succ: 3, 2\n"
218    "  2: StoreLocal(0, 1)\n"
219    "  3: LoadLocal(0) [5]\n"
220    "  4: LoadLocal(0) [5]\n"
221    "  5: Equal(3, 4) [6]\n"
222    "  6: If(5)\n"
223    "BasicBlock 2, pred: 1, 3, succ: 3\n"
224    "  7: Goto 3\n"
225    "BasicBlock 3, pred: 1, 2, succ: 2\n"
226    "  8: SuspendCheck\n"
227    "  9: Goto 2\n"
228    "BasicBlock 4\n"
229    "  10: Exit\n";
230
231  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
232    Instruction::CONST_4 | 0 | 0,
233    Instruction::IF_EQ, 3,
234    Instruction::GOTO | 0x100,
235    Instruction::GOTO | 0xFF00);
236
237  TestCode(data, expected);
238}
239
240TEST(PrettyPrinterTest, IntConstant) {
241  const char* expected =
242    "BasicBlock 0, succ: 1\n"
243    "  0: Local [2]\n"
244    "  1: IntConstant [2]\n"
245    "  5: SuspendCheck\n"
246    "  6: Goto 1\n"
247    "BasicBlock 1, pred: 0, succ: 2\n"
248    "  2: StoreLocal(0, 1)\n"
249    "  3: ReturnVoid\n"
250    "BasicBlock 2, pred: 1\n"
251    "  4: Exit\n";
252
253  const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
254    Instruction::CONST_4 | 0 | 0,
255    Instruction::RETURN_VOID);
256
257  TestCode(data, expected);
258}
259}  // namespace art
260