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