OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
| 7 #include "src/api.h" |
| 8 #include "src/factory.h" |
7 #include "src/interpreter/bytecode-array-writer.h" | 9 #include "src/interpreter/bytecode-array-writer.h" |
| 10 #include "src/interpreter/bytecode-label.h" |
| 11 #include "src/interpreter/constant-array-builder.h" |
8 #include "src/interpreter/source-position-table.h" | 12 #include "src/interpreter/source-position-table.h" |
9 #include "src/isolate.h" | 13 #include "src/isolate.h" |
10 #include "src/utils.h" | 14 #include "src/utils.h" |
11 #include "test/unittests/interpreter/bytecode-utils.h" | 15 #include "test/unittests/interpreter/bytecode-utils.h" |
12 #include "test/unittests/test-utils.h" | 16 #include "test/unittests/test-utils.h" |
13 | 17 |
14 namespace v8 { | 18 namespace v8 { |
15 namespace internal { | 19 namespace internal { |
16 namespace interpreter { | 20 namespace interpreter { |
17 | 21 |
18 class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone { | 22 class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone { |
19 public: | 23 public: |
20 BytecodeArrayWriterUnittest() | 24 BytecodeArrayWriterUnittest() |
21 : source_position_builder_(isolate(), zone()), | 25 : constant_array_builder_(isolate(), zone()), |
22 bytecode_array_writer_(zone(), &source_position_builder_) {} | 26 bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {} |
23 ~BytecodeArrayWriterUnittest() override {} | 27 ~BytecodeArrayWriterUnittest() override {} |
24 | 28 |
25 void Write(BytecodeNode* node, const BytecodeSourceInfo& info); | 29 void Write(BytecodeNode* node, const BytecodeSourceInfo& info); |
26 void Write(Bytecode bytecode, | 30 void Write(Bytecode bytecode, |
27 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 31 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
28 void Write(Bytecode bytecode, uint32_t operand0, OperandScale operand_scale, | 32 void Write(Bytecode bytecode, uint32_t operand0, OperandScale operand_scale, |
29 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 33 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
30 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 34 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
31 OperandScale operand_scale, | 35 OperandScale operand_scale, |
32 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 36 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
33 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 37 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
34 uint32_t operand2, OperandScale operand_scale, | 38 uint32_t operand2, OperandScale operand_scale, |
35 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 39 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
36 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 40 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
37 uint32_t operand2, uint32_t operand3, OperandScale operand_scale, | 41 uint32_t operand2, uint32_t operand3, OperandScale operand_scale, |
38 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 42 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
39 | 43 |
40 SourcePositionTableBuilder* source_position_builder() { | 44 void WriteJump(Bytecode bytecode, BytecodeLabel* label, |
41 return &source_position_builder_; | 45 OperandScale operand_scale, |
| 46 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 47 |
| 48 BytecodeArrayWriter* writer() { return &bytecode_array_writer_; } |
| 49 ZoneVector<unsigned char>* bytecodes() { return writer()->bytecodes(); } |
| 50 SourcePositionTableBuilder* source_position_table_builder() { |
| 51 return writer()->source_position_table_builder(); |
42 } | 52 } |
43 BytecodeArrayWriter* writer() { return &bytecode_array_writer_; } | 53 int max_register_count() { return writer()->max_register_count(); } |
44 | 54 |
45 private: | 55 private: |
46 SourcePositionTableBuilder source_position_builder_; | 56 ConstantArrayBuilder constant_array_builder_; |
47 BytecodeArrayWriter bytecode_array_writer_; | 57 BytecodeArrayWriter bytecode_array_writer_; |
48 }; | 58 }; |
49 | 59 |
50 void BytecodeArrayWriterUnittest::Write(BytecodeNode* node, | 60 void BytecodeArrayWriterUnittest::Write(BytecodeNode* node, |
51 const BytecodeSourceInfo& info) { | 61 const BytecodeSourceInfo& info) { |
52 if (info.is_valid()) { | 62 if (info.is_valid()) { |
53 node->source_info().Update(info); | 63 node->source_info().Update(info); |
54 } | 64 } |
55 writer()->Write(node); | 65 writer()->Write(node); |
56 } | 66 } |
(...skipping 30 matching lines...) Expand all Loading... |
87 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, | 97 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
88 uint32_t operand1, uint32_t operand2, | 98 uint32_t operand1, uint32_t operand2, |
89 uint32_t operand3, | 99 uint32_t operand3, |
90 OperandScale operand_scale, | 100 OperandScale operand_scale, |
91 const BytecodeSourceInfo& info) { | 101 const BytecodeSourceInfo& info) { |
92 BytecodeNode node(bytecode, operand0, operand1, operand2, operand3, | 102 BytecodeNode node(bytecode, operand0, operand1, operand2, operand3, |
93 operand_scale); | 103 operand_scale); |
94 Write(&node, info); | 104 Write(&node, info); |
95 } | 105 } |
96 | 106 |
| 107 void BytecodeArrayWriterUnittest::WriteJump(Bytecode bytecode, |
| 108 BytecodeLabel* label, |
| 109 OperandScale operand_scale, |
| 110 const BytecodeSourceInfo& info) { |
| 111 BytecodeNode node(bytecode, 0, operand_scale); |
| 112 if (info.is_valid()) { |
| 113 node.source_info().Update(info); |
| 114 } |
| 115 writer()->WriteJump(&node, label); |
| 116 } |
| 117 |
97 TEST_F(BytecodeArrayWriterUnittest, SimpleExample) { | 118 TEST_F(BytecodeArrayWriterUnittest, SimpleExample) { |
98 CHECK_EQ(writer()->bytecodes()->size(), 0); | 119 CHECK_EQ(bytecodes()->size(), 0); |
99 | 120 |
100 Write(Bytecode::kStackCheck, {10, false}); | 121 Write(Bytecode::kStackCheck, {10, false}); |
101 CHECK_EQ(writer()->bytecodes()->size(), 1); | 122 CHECK_EQ(bytecodes()->size(), 1); |
102 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 0); | 123 CHECK_EQ(max_register_count(), 0); |
103 | 124 |
104 Write(Bytecode::kLdaSmi, 0xff, OperandScale::kSingle, {55, true}); | 125 Write(Bytecode::kLdaSmi, 0xff, OperandScale::kSingle, {55, true}); |
105 CHECK_EQ(writer()->bytecodes()->size(), 3); | 126 CHECK_EQ(bytecodes()->size(), 3); |
106 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 0); | 127 CHECK_EQ(max_register_count(), 0); |
107 | 128 |
108 Write(Bytecode::kLdar, Register(1).ToOperand(), OperandScale::kDouble); | 129 Write(Bytecode::kLdar, Register(1).ToOperand(), OperandScale::kDouble); |
109 CHECK_EQ(writer()->bytecodes()->size(), 7); | 130 CHECK_EQ(bytecodes()->size(), 7); |
110 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 2 * kPointerSize); | 131 CHECK_EQ(max_register_count(), 2); |
111 | 132 |
112 Write(Bytecode::kReturn, {70, true}); | 133 Write(Bytecode::kReturn, {70, true}); |
113 CHECK_EQ(writer()->bytecodes()->size(), 8); | 134 CHECK_EQ(bytecodes()->size(), 8); |
114 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 2 * kPointerSize); | 135 CHECK_EQ(max_register_count(), 2); |
115 | 136 |
116 static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(0xff), B(Wide), | 137 static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(0xff), B(Wide), |
117 B(Ldar), R16(1), B(Return)}; | 138 B(Ldar), R16(1), B(Return)}; |
118 CHECK_EQ(writer()->bytecodes()->size(), arraysize(bytes)); | 139 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); |
119 for (size_t i = 0; i < arraysize(bytes); ++i) { | 140 for (size_t i = 0; i < arraysize(bytes); ++i) { |
120 CHECK_EQ(writer()->bytecodes()->at(i), bytes[i]); | 141 CHECK_EQ(bytecodes()->at(i), bytes[i]); |
121 } | 142 } |
122 | 143 |
123 CHECK_EQ(writer()->FlushForOffset(), arraysize(bytes)); | 144 writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array()); |
124 writer()->FlushBasicBlock(); | 145 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); |
125 CHECK_EQ(writer()->bytecodes()->size(), arraysize(bytes)); | |
126 | 146 |
127 PositionTableEntry expected_positions[] = { | 147 PositionTableEntry expected_positions[] = { |
128 {0, 10, false}, {1, 55, true}, {7, 70, true}}; | 148 {0, 10, false}, {1, 55, true}, {7, 70, true}}; |
129 Handle<ByteArray> source_positions = | 149 Handle<ByteArray> source_positions = |
130 source_position_builder()->ToSourcePositionTable(); | 150 source_position_table_builder()->ToSourcePositionTable(); |
131 SourcePositionTableIterator source_iterator(*source_positions); | 151 SourcePositionTableIterator source_iterator(*source_positions); |
132 for (size_t i = 0; i < arraysize(expected_positions); ++i) { | 152 for (size_t i = 0; i < arraysize(expected_positions); ++i) { |
133 const PositionTableEntry& expected = expected_positions[i]; | 153 const PositionTableEntry& expected = expected_positions[i]; |
134 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset); | 154 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset); |
135 CHECK_EQ(source_iterator.source_position(), expected.source_position); | 155 CHECK_EQ(source_iterator.source_position(), expected.source_position); |
136 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); | 156 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); |
137 source_iterator.Advance(); | 157 source_iterator.Advance(); |
138 } | 158 } |
139 CHECK(source_iterator.done()); | 159 CHECK(source_iterator.done()); |
140 } | 160 } |
(...skipping 25 matching lines...) Expand all Loading... |
166 /* 41 */ B(Jump), U8(-24), | 186 /* 41 */ B(Jump), U8(-24), |
167 /* 43 */ B(LdaUndefined), | 187 /* 43 */ B(LdaUndefined), |
168 /* 44 85 S> */ B(Return), | 188 /* 44 85 S> */ B(Return), |
169 // clang-format on | 189 // clang-format on |
170 }; | 190 }; |
171 | 191 |
172 static const PositionTableEntry expected_positions[] = { | 192 static const PositionTableEntry expected_positions[] = { |
173 {0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true}, | 193 {0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true}, |
174 {17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}}; | 194 {17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}}; |
175 | 195 |
| 196 BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3; |
| 197 |
176 #define R(i) static_cast<uint32_t>(Register(i).ToOperand()) | 198 #define R(i) static_cast<uint32_t>(Register(i).ToOperand()) |
177 Write(Bytecode::kStackCheck, {30, false}); | 199 Write(Bytecode::kStackCheck, {30, false}); |
178 Write(Bytecode::kLdaConstant, U8(0), OperandScale::kSingle, {42, true}); | 200 Write(Bytecode::kLdaConstant, U8(0), OperandScale::kSingle, {42, true}); |
179 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 0 * kPointerSize); | 201 CHECK_EQ(max_register_count(), 0); |
180 Write(Bytecode::kStar, R(1), OperandScale::kSingle, {42, false}); | 202 Write(Bytecode::kStar, R(1), OperandScale::kSingle, {42, false}); |
181 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 2 * kPointerSize); | 203 CHECK_EQ(max_register_count(), 2); |
182 Write(Bytecode::kJumpIfUndefined, U8(38), OperandScale::kSingle, {68, true}); | 204 WriteJump(Bytecode::kJumpIfUndefined, &jump_end_1, OperandScale::kSingle, |
183 Write(Bytecode::kJumpIfNull, U8(36), OperandScale::kSingle); | 205 {68, true}); |
| 206 WriteJump(Bytecode::kJumpIfNull, &jump_end_2, OperandScale::kSingle); |
184 Write(Bytecode::kToObject); | 207 Write(Bytecode::kToObject); |
185 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 2 * kPointerSize); | 208 CHECK_EQ(max_register_count(), 2); |
186 Write(Bytecode::kStar, R(3), OperandScale::kSingle); | 209 Write(Bytecode::kStar, R(3), OperandScale::kSingle); |
187 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 4 * kPointerSize); | 210 CHECK_EQ(max_register_count(), 4); |
188 Write(Bytecode::kForInPrepare, R(4), OperandScale::kSingle); | 211 Write(Bytecode::kForInPrepare, R(4), OperandScale::kSingle); |
189 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 7 * kPointerSize); | 212 CHECK_EQ(max_register_count(), 7); |
190 Write(Bytecode::kLdaZero); | 213 Write(Bytecode::kLdaZero); |
191 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 7 * kPointerSize); | 214 CHECK_EQ(max_register_count(), 7); |
192 Write(Bytecode::kStar, R(7), OperandScale::kSingle); | 215 Write(Bytecode::kStar, R(7), OperandScale::kSingle); |
193 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 8 * kPointerSize); | 216 CHECK_EQ(max_register_count(), 8); |
| 217 writer()->BindLabel(&back_jump); |
194 Write(Bytecode::kForInDone, R(7), R(6), OperandScale::kSingle, {63, true}); | 218 Write(Bytecode::kForInDone, R(7), R(6), OperandScale::kSingle, {63, true}); |
195 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 8 * kPointerSize); | 219 CHECK_EQ(max_register_count(), 8); |
196 Write(Bytecode::kJumpIfTrue, U8(23), OperandScale::kSingle); | 220 WriteJump(Bytecode::kJumpIfTrue, &jump_end_3, OperandScale::kSingle); |
197 Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1), OperandScale::kSingle); | 221 Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1), OperandScale::kSingle); |
198 Write(Bytecode::kJumpIfUndefined, U8(10), OperandScale::kSingle); | 222 WriteJump(Bytecode::kJumpIfUndefined, &jump_for_in, OperandScale::kSingle); |
199 Write(Bytecode::kStar, R(0), OperandScale::kSingle); | 223 Write(Bytecode::kStar, R(0), OperandScale::kSingle); |
200 Write(Bytecode::kStackCheck, {54, false}); | 224 Write(Bytecode::kStackCheck, {54, false}); |
201 Write(Bytecode::kLdar, R(0), OperandScale::kSingle); | 225 Write(Bytecode::kLdar, R(0), OperandScale::kSingle); |
202 Write(Bytecode::kStar, R(2), OperandScale::kSingle); | 226 Write(Bytecode::kStar, R(2), OperandScale::kSingle); |
203 Write(Bytecode::kReturn, {85, true}); | 227 Write(Bytecode::kReturn, {85, true}); |
| 228 writer()->BindLabel(&jump_for_in); |
204 Write(Bytecode::kForInStep, R(7), OperandScale::kSingle); | 229 Write(Bytecode::kForInStep, R(7), OperandScale::kSingle); |
205 Write(Bytecode::kStar, R(7), OperandScale::kSingle); | 230 Write(Bytecode::kStar, R(7), OperandScale::kSingle); |
206 Write(Bytecode::kJump, U8(-24), OperandScale::kSingle); | 231 WriteJump(Bytecode::kJump, &back_jump, OperandScale::kSingle); |
| 232 writer()->BindLabel(&jump_end_1); |
| 233 writer()->BindLabel(&jump_end_2); |
| 234 writer()->BindLabel(&jump_end_3); |
207 Write(Bytecode::kLdaUndefined); | 235 Write(Bytecode::kLdaUndefined); |
208 Write(Bytecode::kReturn, {85, true}); | 236 Write(Bytecode::kReturn, {85, true}); |
209 CHECK_EQ(writer()->GetMaximumFrameSizeUsed(), 8 * kPointerSize); | 237 CHECK_EQ(max_register_count(), 8); |
210 #undef R | 238 #undef R |
211 | 239 |
212 CHECK_EQ(writer()->bytecodes()->size(), arraysize(expected_bytes)); | 240 CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes)); |
213 for (size_t i = 0; i < arraysize(expected_bytes); ++i) { | 241 for (size_t i = 0; i < arraysize(expected_bytes); ++i) { |
214 CHECK_EQ(static_cast<int>(writer()->bytecodes()->at(i)), | 242 CHECK_EQ(static_cast<int>(bytecodes()->at(i)), |
215 static_cast<int>(expected_bytes[i])); | 243 static_cast<int>(expected_bytes[i])); |
216 } | 244 } |
217 | 245 |
218 Handle<ByteArray> source_positions = | 246 Handle<ByteArray> source_positions = |
219 source_position_builder()->ToSourcePositionTable(); | 247 source_position_table_builder()->ToSourcePositionTable(); |
220 SourcePositionTableIterator source_iterator(*source_positions); | 248 SourcePositionTableIterator source_iterator(*source_positions); |
221 for (size_t i = 0; i < arraysize(expected_positions); ++i) { | 249 for (size_t i = 0; i < arraysize(expected_positions); ++i) { |
222 const PositionTableEntry& expected = expected_positions[i]; | 250 const PositionTableEntry& expected = expected_positions[i]; |
223 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset); | 251 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset); |
224 CHECK_EQ(source_iterator.source_position(), expected.source_position); | 252 CHECK_EQ(source_iterator.source_position(), expected.source_position); |
225 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); | 253 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); |
226 source_iterator.Advance(); | 254 source_iterator.Advance(); |
227 } | 255 } |
228 CHECK(source_iterator.done()); | 256 CHECK(source_iterator.done()); |
229 } | 257 } |
230 | 258 |
231 } // namespace interpreter | 259 } // namespace interpreter |
232 } // namespace internal | 260 } // namespace internal |
233 } // namespace v8 | 261 } // namespace v8 |
OLD | NEW |