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" | 7 #include "src/api.h" |
8 #include "src/factory.h" | 8 #include "src/factory.h" |
9 #include "src/interpreter/bytecode-array-writer.h" | 9 #include "src/interpreter/bytecode-array-writer.h" |
10 #include "src/interpreter/bytecode-label.h" | 10 #include "src/interpreter/bytecode-label.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone { | 22 class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone { |
23 public: | 23 public: |
24 BytecodeArrayWriterUnittest() | 24 BytecodeArrayWriterUnittest() |
25 : constant_array_builder_(isolate(), zone()), | 25 : constant_array_builder_(isolate(), zone()), |
26 bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {} | 26 bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {} |
27 ~BytecodeArrayWriterUnittest() override {} | 27 ~BytecodeArrayWriterUnittest() override {} |
28 | 28 |
29 void Write(BytecodeNode* node, const BytecodeSourceInfo& info); | 29 void Write(BytecodeNode* node, const BytecodeSourceInfo& info); |
30 void Write(Bytecode bytecode, | 30 void Write(Bytecode bytecode, |
31 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 31 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
32 void Write(Bytecode bytecode, uint32_t operand0, OperandScale operand_scale, | 32 void Write(Bytecode bytecode, uint32_t operand0, |
33 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 33 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
34 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 34 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
35 OperandScale operand_scale, | 35 |
36 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 36 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
37 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 37 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
38 uint32_t operand2, OperandScale operand_scale, | 38 uint32_t operand2, |
39 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 39 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
40 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, | 40 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
41 uint32_t operand2, uint32_t operand3, OperandScale operand_scale, | 41 uint32_t operand2, uint32_t operand3, |
42 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 42 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
43 | 43 |
44 void WriteJump(Bytecode bytecode, BytecodeLabel* label, | 44 void WriteJump(Bytecode bytecode, BytecodeLabel* label, |
45 OperandScale operand_scale, | 45 |
46 const BytecodeSourceInfo& info = BytecodeSourceInfo()); | 46 const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
47 | 47 |
48 BytecodeArrayWriter* writer() { return &bytecode_array_writer_; } | 48 BytecodeArrayWriter* writer() { return &bytecode_array_writer_; } |
49 ZoneVector<unsigned char>* bytecodes() { return writer()->bytecodes(); } | 49 ZoneVector<unsigned char>* bytecodes() { return writer()->bytecodes(); } |
50 SourcePositionTableBuilder* source_position_table_builder() { | 50 SourcePositionTableBuilder* source_position_table_builder() { |
51 return writer()->source_position_table_builder(); | 51 return writer()->source_position_table_builder(); |
52 } | 52 } |
53 int max_register_count() { return writer()->max_register_count(); } | 53 int max_register_count() { return writer()->max_register_count(); } |
54 | 54 |
55 private: | 55 private: |
56 ConstantArrayBuilder constant_array_builder_; | 56 ConstantArrayBuilder constant_array_builder_; |
57 BytecodeArrayWriter bytecode_array_writer_; | 57 BytecodeArrayWriter bytecode_array_writer_; |
58 }; | 58 }; |
59 | 59 |
60 void BytecodeArrayWriterUnittest::Write(BytecodeNode* node, | 60 void BytecodeArrayWriterUnittest::Write(BytecodeNode* node, |
61 const BytecodeSourceInfo& info) { | 61 const BytecodeSourceInfo& info) { |
62 if (info.is_valid()) { | 62 if (info.is_valid()) { |
63 node->source_info().Update(info); | 63 node->source_info().Update(info); |
64 } | 64 } |
65 writer()->Write(node); | 65 writer()->Write(node); |
66 } | 66 } |
67 | 67 |
68 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, | 68 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, |
69 const BytecodeSourceInfo& info) { | 69 const BytecodeSourceInfo& info) { |
70 BytecodeNode node(bytecode); | 70 BytecodeNode node(bytecode); |
71 Write(&node, info); | 71 Write(&node, info); |
72 } | 72 } |
73 | 73 |
74 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, | 74 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
75 OperandScale operand_scale, | |
76 const BytecodeSourceInfo& info) { | 75 const BytecodeSourceInfo& info) { |
77 BytecodeNode node(bytecode, operand0, operand_scale); | 76 BytecodeNode node(bytecode, operand0); |
78 Write(&node, info); | 77 Write(&node, info); |
79 } | 78 } |
80 | 79 |
81 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, | 80 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
82 uint32_t operand1, | 81 uint32_t operand1, |
83 OperandScale operand_scale, | |
84 const BytecodeSourceInfo& info) { | 82 const BytecodeSourceInfo& info) { |
85 BytecodeNode node(bytecode, operand0, operand1, operand_scale); | 83 BytecodeNode node(bytecode, operand0, operand1); |
86 Write(&node, info); | 84 Write(&node, info); |
87 } | 85 } |
88 | 86 |
89 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, | 87 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
90 uint32_t operand1, uint32_t operand2, | 88 uint32_t operand1, uint32_t operand2, |
91 OperandScale operand_scale, | |
92 const BytecodeSourceInfo& info) { | 89 const BytecodeSourceInfo& info) { |
93 BytecodeNode node(bytecode, operand0, operand1, operand2, operand_scale); | 90 BytecodeNode node(bytecode, operand0, operand1, operand2); |
94 Write(&node, info); | 91 Write(&node, info); |
95 } | 92 } |
96 | 93 |
97 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, | 94 void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
98 uint32_t operand1, uint32_t operand2, | 95 uint32_t operand1, uint32_t operand2, |
99 uint32_t operand3, | 96 uint32_t operand3, |
100 OperandScale operand_scale, | |
101 const BytecodeSourceInfo& info) { | 97 const BytecodeSourceInfo& info) { |
102 BytecodeNode node(bytecode, operand0, operand1, operand2, operand3, | 98 BytecodeNode node(bytecode, operand0, operand1, operand2, operand3); |
103 operand_scale); | |
104 Write(&node, info); | 99 Write(&node, info); |
105 } | 100 } |
106 | 101 |
107 void BytecodeArrayWriterUnittest::WriteJump(Bytecode bytecode, | 102 void BytecodeArrayWriterUnittest::WriteJump(Bytecode bytecode, |
108 BytecodeLabel* label, | 103 BytecodeLabel* label, |
109 OperandScale operand_scale, | |
110 const BytecodeSourceInfo& info) { | 104 const BytecodeSourceInfo& info) { |
111 BytecodeNode node(bytecode, 0, operand_scale); | 105 BytecodeNode node(bytecode, 0); |
112 if (info.is_valid()) { | 106 if (info.is_valid()) { |
113 node.source_info().Update(info); | 107 node.source_info().Update(info); |
114 } | 108 } |
115 writer()->WriteJump(&node, label); | 109 writer()->WriteJump(&node, label); |
116 } | 110 } |
117 | 111 |
118 TEST_F(BytecodeArrayWriterUnittest, SimpleExample) { | 112 TEST_F(BytecodeArrayWriterUnittest, SimpleExample) { |
119 CHECK_EQ(bytecodes()->size(), 0); | 113 CHECK_EQ(bytecodes()->size(), 0); |
120 | 114 |
121 Write(Bytecode::kStackCheck, {10, false}); | 115 Write(Bytecode::kStackCheck, {10, false}); |
122 CHECK_EQ(bytecodes()->size(), 1); | 116 CHECK_EQ(bytecodes()->size(), 1); |
123 CHECK_EQ(max_register_count(), 0); | 117 CHECK_EQ(max_register_count(), 0); |
124 | 118 |
125 Write(Bytecode::kLdaSmi, 0xff, OperandScale::kSingle, {55, true}); | 119 Write(Bytecode::kLdaSmi, 127, {55, true}); |
126 CHECK_EQ(bytecodes()->size(), 3); | 120 CHECK_EQ(bytecodes()->size(), 3); |
127 CHECK_EQ(max_register_count(), 0); | 121 CHECK_EQ(max_register_count(), 0); |
128 | 122 |
129 Write(Bytecode::kLdar, Register(1).ToOperand(), OperandScale::kDouble); | 123 Write(Bytecode::kLdar, Register(200).ToOperand()); |
130 CHECK_EQ(bytecodes()->size(), 7); | 124 CHECK_EQ(bytecodes()->size(), 7); |
131 CHECK_EQ(max_register_count(), 2); | 125 CHECK_EQ(max_register_count(), 201); |
132 | 126 |
133 Write(Bytecode::kReturn, {70, true}); | 127 Write(Bytecode::kReturn, {70, true}); |
134 CHECK_EQ(bytecodes()->size(), 8); | 128 CHECK_EQ(bytecodes()->size(), 8); |
135 CHECK_EQ(max_register_count(), 2); | 129 CHECK_EQ(max_register_count(), 201); |
136 | 130 |
137 static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(0xff), B(Wide), | 131 static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(127), B(Wide), |
138 B(Ldar), R16(1), B(Return)}; | 132 B(Ldar), R16(200), B(Return)}; |
139 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); | 133 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); |
140 for (size_t i = 0; i < arraysize(bytes); ++i) { | 134 for (size_t i = 0; i < arraysize(bytes); ++i) { |
141 CHECK_EQ(bytecodes()->at(i), bytes[i]); | 135 CHECK_EQ(bytecodes()->at(i), bytes[i]); |
142 } | 136 } |
143 | 137 |
144 writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array()); | 138 writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array()); |
145 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); | 139 CHECK_EQ(bytecodes()->size(), arraysize(bytes)); |
146 | 140 |
147 PositionTableEntry expected_positions[] = { | 141 PositionTableEntry expected_positions[] = { |
148 {0, 10, false}, {1, 55, true}, {7, 70, true}}; | 142 {0, 10, false}, {1, 55, true}, {7, 70, true}}; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 }; | 184 }; |
191 | 185 |
192 static const PositionTableEntry expected_positions[] = { | 186 static const PositionTableEntry expected_positions[] = { |
193 {0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true}, | 187 {0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true}, |
194 {17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}}; | 188 {17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}}; |
195 | 189 |
196 BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3; | 190 BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3; |
197 | 191 |
198 #define R(i) static_cast<uint32_t>(Register(i).ToOperand()) | 192 #define R(i) static_cast<uint32_t>(Register(i).ToOperand()) |
199 Write(Bytecode::kStackCheck, {30, false}); | 193 Write(Bytecode::kStackCheck, {30, false}); |
200 Write(Bytecode::kLdaConstant, U8(0), OperandScale::kSingle, {42, true}); | 194 Write(Bytecode::kLdaConstant, U8(0), {42, true}); |
201 CHECK_EQ(max_register_count(), 0); | 195 CHECK_EQ(max_register_count(), 0); |
202 Write(Bytecode::kStar, R(1), OperandScale::kSingle, {42, false}); | 196 Write(Bytecode::kStar, R(1), {42, false}); |
203 CHECK_EQ(max_register_count(), 2); | 197 CHECK_EQ(max_register_count(), 2); |
204 WriteJump(Bytecode::kJumpIfUndefined, &jump_end_1, OperandScale::kSingle, | 198 WriteJump(Bytecode::kJumpIfUndefined, &jump_end_1, {68, true}); |
205 {68, true}); | 199 WriteJump(Bytecode::kJumpIfNull, &jump_end_2); |
206 WriteJump(Bytecode::kJumpIfNull, &jump_end_2, OperandScale::kSingle); | |
207 Write(Bytecode::kToObject); | 200 Write(Bytecode::kToObject); |
208 CHECK_EQ(max_register_count(), 2); | 201 CHECK_EQ(max_register_count(), 2); |
209 Write(Bytecode::kStar, R(3), OperandScale::kSingle); | 202 Write(Bytecode::kStar, R(3)); |
210 CHECK_EQ(max_register_count(), 4); | 203 CHECK_EQ(max_register_count(), 4); |
211 Write(Bytecode::kForInPrepare, R(4), OperandScale::kSingle); | 204 Write(Bytecode::kForInPrepare, R(4)); |
212 CHECK_EQ(max_register_count(), 7); | 205 CHECK_EQ(max_register_count(), 7); |
213 Write(Bytecode::kLdaZero); | 206 Write(Bytecode::kLdaZero); |
214 CHECK_EQ(max_register_count(), 7); | 207 CHECK_EQ(max_register_count(), 7); |
215 Write(Bytecode::kStar, R(7), OperandScale::kSingle); | 208 Write(Bytecode::kStar, R(7)); |
216 CHECK_EQ(max_register_count(), 8); | 209 CHECK_EQ(max_register_count(), 8); |
217 writer()->BindLabel(&back_jump); | 210 writer()->BindLabel(&back_jump); |
218 Write(Bytecode::kForInDone, R(7), R(6), OperandScale::kSingle, {63, true}); | 211 Write(Bytecode::kForInDone, R(7), R(6), {63, true}); |
219 CHECK_EQ(max_register_count(), 8); | 212 CHECK_EQ(max_register_count(), 8); |
220 WriteJump(Bytecode::kJumpIfTrue, &jump_end_3, OperandScale::kSingle); | 213 WriteJump(Bytecode::kJumpIfTrue, &jump_end_3); |
221 Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1), OperandScale::kSingle); | 214 Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1)); |
222 WriteJump(Bytecode::kJumpIfUndefined, &jump_for_in, OperandScale::kSingle); | 215 WriteJump(Bytecode::kJumpIfUndefined, &jump_for_in); |
223 Write(Bytecode::kStar, R(0), OperandScale::kSingle); | 216 Write(Bytecode::kStar, R(0)); |
224 Write(Bytecode::kStackCheck, {54, false}); | 217 Write(Bytecode::kStackCheck, {54, false}); |
225 Write(Bytecode::kLdar, R(0), OperandScale::kSingle); | 218 Write(Bytecode::kLdar, R(0)); |
226 Write(Bytecode::kStar, R(2), OperandScale::kSingle); | 219 Write(Bytecode::kStar, R(2)); |
227 Write(Bytecode::kReturn, {85, true}); | 220 Write(Bytecode::kReturn, {85, true}); |
228 writer()->BindLabel(&jump_for_in); | 221 writer()->BindLabel(&jump_for_in); |
229 Write(Bytecode::kForInStep, R(7), OperandScale::kSingle); | 222 Write(Bytecode::kForInStep, R(7)); |
230 Write(Bytecode::kStar, R(7), OperandScale::kSingle); | 223 Write(Bytecode::kStar, R(7)); |
231 WriteJump(Bytecode::kJump, &back_jump, OperandScale::kSingle); | 224 WriteJump(Bytecode::kJump, &back_jump); |
232 writer()->BindLabel(&jump_end_1); | 225 writer()->BindLabel(&jump_end_1); |
233 writer()->BindLabel(&jump_end_2); | 226 writer()->BindLabel(&jump_end_2); |
234 writer()->BindLabel(&jump_end_3); | 227 writer()->BindLabel(&jump_end_3); |
235 Write(Bytecode::kLdaUndefined); | 228 Write(Bytecode::kLdaUndefined); |
236 Write(Bytecode::kReturn, {85, true}); | 229 Write(Bytecode::kReturn, {85, true}); |
237 CHECK_EQ(max_register_count(), 8); | 230 CHECK_EQ(max_register_count(), 8); |
238 #undef R | 231 #undef R |
239 | 232 |
240 CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes)); | 233 CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes)); |
241 for (size_t i = 0; i < arraysize(expected_bytes); ++i) { | 234 for (size_t i = 0; i < arraysize(expected_bytes); ++i) { |
(...skipping 10 matching lines...) Expand all Loading... |
252 CHECK_EQ(source_iterator.source_position(), expected.source_position); | 245 CHECK_EQ(source_iterator.source_position(), expected.source_position); |
253 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); | 246 CHECK_EQ(source_iterator.is_statement(), expected.is_statement); |
254 source_iterator.Advance(); | 247 source_iterator.Advance(); |
255 } | 248 } |
256 CHECK(source_iterator.done()); | 249 CHECK(source_iterator.done()); |
257 } | 250 } |
258 | 251 |
259 } // namespace interpreter | 252 } // namespace interpreter |
260 } // namespace internal | 253 } // namespace internal |
261 } // namespace v8 | 254 } // namespace v8 |
OLD | NEW |