blob: 90a91cee9fa2a044696f7dadf3df1fa989e1a622 [file] [log] [blame]
Ben Murdochc5610432016-08-08 18:44:38 +01001// 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
3// found in the LICENSE file.
4
5#include "src/v8.h"
6
Ben Murdoch61f157c2016-09-16 13:49:30 +01007#include "src/api.h"
8#include "src/factory.h"
Ben Murdochc5610432016-08-08 18:44:38 +01009#include "src/interpreter/bytecode-array-writer.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010010#include "src/interpreter/bytecode-label.h"
11#include "src/interpreter/constant-array-builder.h"
Ben Murdochc5610432016-08-08 18:44:38 +010012#include "src/interpreter/source-position-table.h"
13#include "src/isolate.h"
14#include "src/utils.h"
15#include "test/unittests/interpreter/bytecode-utils.h"
16#include "test/unittests/test-utils.h"
17
18namespace v8 {
19namespace internal {
20namespace interpreter {
21
22class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone {
23 public:
24 BytecodeArrayWriterUnittest()
Ben Murdoch61f157c2016-09-16 13:49:30 +010025 : constant_array_builder_(isolate(), zone()),
26 bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {}
Ben Murdochc5610432016-08-08 18:44:38 +010027 ~BytecodeArrayWriterUnittest() override {}
28
29 void Write(BytecodeNode* node, const BytecodeSourceInfo& info);
30 void Write(Bytecode bytecode,
31 const BytecodeSourceInfo& info = BytecodeSourceInfo());
Ben Murdoch61f157c2016-09-16 13:49:30 +010032 void Write(Bytecode bytecode, uint32_t operand0,
Ben Murdochc5610432016-08-08 18:44:38 +010033 const BytecodeSourceInfo& info = BytecodeSourceInfo());
34 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
Ben Murdoch61f157c2016-09-16 13:49:30 +010035
Ben Murdochc5610432016-08-08 18:44:38 +010036 const BytecodeSourceInfo& info = BytecodeSourceInfo());
37 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
Ben Murdoch61f157c2016-09-16 13:49:30 +010038 uint32_t operand2,
Ben Murdochc5610432016-08-08 18:44:38 +010039 const BytecodeSourceInfo& info = BytecodeSourceInfo());
40 void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
Ben Murdoch61f157c2016-09-16 13:49:30 +010041 uint32_t operand2, uint32_t operand3,
Ben Murdochc5610432016-08-08 18:44:38 +010042 const BytecodeSourceInfo& info = BytecodeSourceInfo());
43
Ben Murdoch61f157c2016-09-16 13:49:30 +010044 void WriteJump(Bytecode bytecode, BytecodeLabel* label,
45
46 const BytecodeSourceInfo& info = BytecodeSourceInfo());
47
Ben Murdochc5610432016-08-08 18:44:38 +010048 BytecodeArrayWriter* writer() { return &bytecode_array_writer_; }
Ben Murdoch61f157c2016-09-16 13:49:30 +010049 ZoneVector<unsigned char>* bytecodes() { return writer()->bytecodes(); }
50 SourcePositionTableBuilder* source_position_table_builder() {
51 return writer()->source_position_table_builder();
52 }
53 int max_register_count() { return writer()->max_register_count(); }
Ben Murdochc5610432016-08-08 18:44:38 +010054
55 private:
Ben Murdoch61f157c2016-09-16 13:49:30 +010056 ConstantArrayBuilder constant_array_builder_;
Ben Murdochc5610432016-08-08 18:44:38 +010057 BytecodeArrayWriter bytecode_array_writer_;
58};
59
60void BytecodeArrayWriterUnittest::Write(BytecodeNode* node,
61 const BytecodeSourceInfo& info) {
62 if (info.is_valid()) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010063 node->source_info().Clone(info);
Ben Murdochc5610432016-08-08 18:44:38 +010064 }
65 writer()->Write(node);
66}
67
68void BytecodeArrayWriterUnittest::Write(Bytecode bytecode,
69 const BytecodeSourceInfo& info) {
70 BytecodeNode node(bytecode);
71 Write(&node, info);
72}
73
74void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
Ben Murdochc5610432016-08-08 18:44:38 +010075 const BytecodeSourceInfo& info) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010076 BytecodeNode node(bytecode, operand0);
Ben Murdochc5610432016-08-08 18:44:38 +010077 Write(&node, info);
78}
79
80void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
81 uint32_t operand1,
Ben Murdochc5610432016-08-08 18:44:38 +010082 const BytecodeSourceInfo& info) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010083 BytecodeNode node(bytecode, operand0, operand1);
Ben Murdochc5610432016-08-08 18:44:38 +010084 Write(&node, info);
85}
86
87void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
88 uint32_t operand1, uint32_t operand2,
Ben Murdochc5610432016-08-08 18:44:38 +010089 const BytecodeSourceInfo& info) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010090 BytecodeNode node(bytecode, operand0, operand1, operand2);
Ben Murdochc5610432016-08-08 18:44:38 +010091 Write(&node, info);
92}
93
94void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
95 uint32_t operand1, uint32_t operand2,
96 uint32_t operand3,
Ben Murdochc5610432016-08-08 18:44:38 +010097 const BytecodeSourceInfo& info) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010098 BytecodeNode node(bytecode, operand0, operand1, operand2, operand3);
Ben Murdochc5610432016-08-08 18:44:38 +010099 Write(&node, info);
100}
101
Ben Murdoch61f157c2016-09-16 13:49:30 +0100102void BytecodeArrayWriterUnittest::WriteJump(Bytecode bytecode,
103 BytecodeLabel* label,
104 const BytecodeSourceInfo& info) {
105 BytecodeNode node(bytecode, 0);
106 if (info.is_valid()) {
107 node.source_info().Clone(info);
108 }
109 writer()->WriteJump(&node, label);
110}
111
Ben Murdochc5610432016-08-08 18:44:38 +0100112TEST_F(BytecodeArrayWriterUnittest, SimpleExample) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100113 CHECK_EQ(bytecodes()->size(), 0);
Ben Murdochc5610432016-08-08 18:44:38 +0100114
115 Write(Bytecode::kStackCheck, {10, false});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100116 CHECK_EQ(bytecodes()->size(), 1);
117 CHECK_EQ(max_register_count(), 0);
Ben Murdochc5610432016-08-08 18:44:38 +0100118
Ben Murdoch61f157c2016-09-16 13:49:30 +0100119 Write(Bytecode::kLdaSmi, 127, {55, true});
120 CHECK_EQ(bytecodes()->size(), 3);
121 CHECK_EQ(max_register_count(), 0);
Ben Murdochc5610432016-08-08 18:44:38 +0100122
Ben Murdoch61f157c2016-09-16 13:49:30 +0100123 Write(Bytecode::kLdar, Register(200).ToOperand());
124 CHECK_EQ(bytecodes()->size(), 7);
125 CHECK_EQ(max_register_count(), 201);
Ben Murdochc5610432016-08-08 18:44:38 +0100126
127 Write(Bytecode::kReturn, {70, true});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100128 CHECK_EQ(bytecodes()->size(), 8);
129 CHECK_EQ(max_register_count(), 201);
Ben Murdochc5610432016-08-08 18:44:38 +0100130
Ben Murdoch61f157c2016-09-16 13:49:30 +0100131 static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(127), B(Wide),
132 B(Ldar), R16(200), B(Return)};
133 CHECK_EQ(bytecodes()->size(), arraysize(bytes));
Ben Murdochc5610432016-08-08 18:44:38 +0100134 for (size_t i = 0; i < arraysize(bytes); ++i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100135 CHECK_EQ(bytecodes()->at(i), bytes[i]);
Ben Murdochc5610432016-08-08 18:44:38 +0100136 }
137
Ben Murdoch61f157c2016-09-16 13:49:30 +0100138 writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array());
139 CHECK_EQ(bytecodes()->size(), arraysize(bytes));
Ben Murdochc5610432016-08-08 18:44:38 +0100140
141 PositionTableEntry expected_positions[] = {
142 {0, 10, false}, {1, 55, true}, {7, 70, true}};
143 Handle<ByteArray> source_positions =
Ben Murdoch61f157c2016-09-16 13:49:30 +0100144 source_position_table_builder()->ToSourcePositionTable();
Ben Murdochc5610432016-08-08 18:44:38 +0100145 SourcePositionTableIterator source_iterator(*source_positions);
146 for (size_t i = 0; i < arraysize(expected_positions); ++i) {
147 const PositionTableEntry& expected = expected_positions[i];
148 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset);
149 CHECK_EQ(source_iterator.source_position(), expected.source_position);
150 CHECK_EQ(source_iterator.is_statement(), expected.is_statement);
151 source_iterator.Advance();
152 }
153 CHECK(source_iterator.done());
154}
155
156TEST_F(BytecodeArrayWriterUnittest, ComplexExample) {
157 static const uint8_t expected_bytes[] = {
158 // clang-format off
159 /* 0 30 E> */ B(StackCheck),
160 /* 1 42 S> */ B(LdaConstant), U8(0),
161 /* 3 42 E> */ B(Star), R8(1),
162 /* 5 68 S> */ B(JumpIfUndefined), U8(38),
163 /* 7 */ B(JumpIfNull), U8(36),
164 /* 9 */ B(ToObject),
165 /* 10 */ B(Star), R8(3),
166 /* 12 */ B(ForInPrepare), R8(4),
167 /* 14 */ B(LdaZero),
168 /* 15 */ B(Star), R8(7),
169 /* 17 63 S> */ B(ForInDone), R8(7), R8(6),
170 /* 20 */ B(JumpIfTrue), U8(23),
171 /* 22 */ B(ForInNext), R8(3), R8(7), R8(4), U8(1),
172 /* 27 */ B(JumpIfUndefined), U8(10),
173 /* 29 */ B(Star), R8(0),
174 /* 31 54 E> */ B(StackCheck),
175 /* 32 */ B(Ldar), R8(0),
176 /* 34 */ B(Star), R8(2),
177 /* 36 85 S> */ B(Return),
178 /* 37 */ B(ForInStep), R8(7),
179 /* 39 */ B(Star), R8(7),
180 /* 41 */ B(Jump), U8(-24),
181 /* 43 */ B(LdaUndefined),
182 /* 44 85 S> */ B(Return),
183 // clang-format on
184 };
185
186 static const PositionTableEntry expected_positions[] = {
187 {0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true},
188 {17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}};
189
Ben Murdoch61f157c2016-09-16 13:49:30 +0100190 BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3;
191
Ben Murdochc5610432016-08-08 18:44:38 +0100192#define R(i) static_cast<uint32_t>(Register(i).ToOperand())
193 Write(Bytecode::kStackCheck, {30, false});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100194 Write(Bytecode::kLdaConstant, U8(0), {42, true});
195 CHECK_EQ(max_register_count(), 0);
196 Write(Bytecode::kStar, R(1), {42, false});
197 CHECK_EQ(max_register_count(), 2);
198 WriteJump(Bytecode::kJumpIfUndefined, &jump_end_1, {68, true});
199 WriteJump(Bytecode::kJumpIfNull, &jump_end_2);
Ben Murdochc5610432016-08-08 18:44:38 +0100200 Write(Bytecode::kToObject);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100201 CHECK_EQ(max_register_count(), 2);
202 Write(Bytecode::kStar, R(3));
203 CHECK_EQ(max_register_count(), 4);
204 Write(Bytecode::kForInPrepare, R(4));
205 CHECK_EQ(max_register_count(), 7);
Ben Murdochc5610432016-08-08 18:44:38 +0100206 Write(Bytecode::kLdaZero);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100207 CHECK_EQ(max_register_count(), 7);
208 Write(Bytecode::kStar, R(7));
209 CHECK_EQ(max_register_count(), 8);
210 writer()->BindLabel(&back_jump);
211 Write(Bytecode::kForInDone, R(7), R(6), {63, true});
212 CHECK_EQ(max_register_count(), 8);
213 WriteJump(Bytecode::kJumpIfTrue, &jump_end_3);
214 Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1));
215 WriteJump(Bytecode::kJumpIfUndefined, &jump_for_in);
216 Write(Bytecode::kStar, R(0));
Ben Murdochc5610432016-08-08 18:44:38 +0100217 Write(Bytecode::kStackCheck, {54, false});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100218 Write(Bytecode::kLdar, R(0));
219 Write(Bytecode::kStar, R(2));
Ben Murdochc5610432016-08-08 18:44:38 +0100220 Write(Bytecode::kReturn, {85, true});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100221 writer()->BindLabel(&jump_for_in);
222 Write(Bytecode::kForInStep, R(7));
223 Write(Bytecode::kStar, R(7));
224 WriteJump(Bytecode::kJump, &back_jump);
225 writer()->BindLabel(&jump_end_1);
226 writer()->BindLabel(&jump_end_2);
227 writer()->BindLabel(&jump_end_3);
Ben Murdochc5610432016-08-08 18:44:38 +0100228 Write(Bytecode::kLdaUndefined);
229 Write(Bytecode::kReturn, {85, true});
Ben Murdoch61f157c2016-09-16 13:49:30 +0100230 CHECK_EQ(max_register_count(), 8);
Ben Murdochc5610432016-08-08 18:44:38 +0100231#undef R
232
Ben Murdoch61f157c2016-09-16 13:49:30 +0100233 CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes));
Ben Murdochc5610432016-08-08 18:44:38 +0100234 for (size_t i = 0; i < arraysize(expected_bytes); ++i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100235 CHECK_EQ(static_cast<int>(bytecodes()->at(i)),
Ben Murdochc5610432016-08-08 18:44:38 +0100236 static_cast<int>(expected_bytes[i]));
237 }
238
239 Handle<ByteArray> source_positions =
Ben Murdoch61f157c2016-09-16 13:49:30 +0100240 source_position_table_builder()->ToSourcePositionTable();
Ben Murdochc5610432016-08-08 18:44:38 +0100241 SourcePositionTableIterator source_iterator(*source_positions);
242 for (size_t i = 0; i < arraysize(expected_positions); ++i) {
243 const PositionTableEntry& expected = expected_positions[i];
244 CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset);
245 CHECK_EQ(source_iterator.source_position(), expected.source_position);
246 CHECK_EQ(source_iterator.is_statement(), expected.is_statement);
247 source_iterator.Advance();
248 }
249 CHECK(source_iterator.done());
250}
251
252} // namespace interpreter
253} // namespace internal
254} // namespace v8