Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/v8.h" |
| 6 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 7 | #include "src/api.h" |
| 8 | #include "src/factory.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 9 | #include "src/interpreter/bytecode-array-writer.h" |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 10 | #include "src/interpreter/bytecode-label.h" |
| 11 | #include "src/interpreter/constant-array-builder.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 12 | #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 | |
| 18 | namespace v8 { |
| 19 | namespace internal { |
| 20 | namespace interpreter { |
| 21 | |
| 22 | class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone { |
| 23 | public: |
| 24 | BytecodeArrayWriterUnittest() |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 25 | : constant_array_builder_(isolate(), zone()), |
| 26 | bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {} |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 27 | ~BytecodeArrayWriterUnittest() override {} |
| 28 | |
| 29 | void Write(BytecodeNode* node, const BytecodeSourceInfo& info); |
| 30 | void Write(Bytecode bytecode, |
| 31 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 32 | void Write(Bytecode bytecode, uint32_t operand0, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 33 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 34 | void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 35 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 36 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 37 | void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 38 | uint32_t operand2, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 39 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 40 | void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 41 | uint32_t operand2, uint32_t operand3, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 42 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 43 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 44 | void WriteJump(Bytecode bytecode, BytecodeLabel* label, |
| 45 | |
| 46 | const BytecodeSourceInfo& info = BytecodeSourceInfo()); |
| 47 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 48 | BytecodeArrayWriter* writer() { return &bytecode_array_writer_; } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 49 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 54 | |
| 55 | private: |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 56 | ConstantArrayBuilder constant_array_builder_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 57 | BytecodeArrayWriter bytecode_array_writer_; |
| 58 | }; |
| 59 | |
| 60 | void BytecodeArrayWriterUnittest::Write(BytecodeNode* node, |
| 61 | const BytecodeSourceInfo& info) { |
| 62 | if (info.is_valid()) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 63 | node->source_info().Clone(info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 64 | } |
| 65 | writer()->Write(node); |
| 66 | } |
| 67 | |
| 68 | void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, |
| 69 | const BytecodeSourceInfo& info) { |
| 70 | BytecodeNode node(bytecode); |
| 71 | Write(&node, info); |
| 72 | } |
| 73 | |
| 74 | void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 75 | const BytecodeSourceInfo& info) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 76 | BytecodeNode node(bytecode, operand0); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 77 | Write(&node, info); |
| 78 | } |
| 79 | |
| 80 | void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
| 81 | uint32_t operand1, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 82 | const BytecodeSourceInfo& info) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 83 | BytecodeNode node(bytecode, operand0, operand1); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 84 | Write(&node, info); |
| 85 | } |
| 86 | |
| 87 | void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
| 88 | uint32_t operand1, uint32_t operand2, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 89 | const BytecodeSourceInfo& info) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 90 | BytecodeNode node(bytecode, operand0, operand1, operand2); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 91 | Write(&node, info); |
| 92 | } |
| 93 | |
| 94 | void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0, |
| 95 | uint32_t operand1, uint32_t operand2, |
| 96 | uint32_t operand3, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 97 | const BytecodeSourceInfo& info) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 98 | BytecodeNode node(bytecode, operand0, operand1, operand2, operand3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 99 | Write(&node, info); |
| 100 | } |
| 101 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 102 | void 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 112 | TEST_F(BytecodeArrayWriterUnittest, SimpleExample) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 113 | CHECK_EQ(bytecodes()->size(), 0); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 114 | |
| 115 | Write(Bytecode::kStackCheck, {10, false}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 116 | CHECK_EQ(bytecodes()->size(), 1); |
| 117 | CHECK_EQ(max_register_count(), 0); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 118 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 119 | Write(Bytecode::kLdaSmi, 127, {55, true}); |
| 120 | CHECK_EQ(bytecodes()->size(), 3); |
| 121 | CHECK_EQ(max_register_count(), 0); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 122 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 123 | Write(Bytecode::kLdar, Register(200).ToOperand()); |
| 124 | CHECK_EQ(bytecodes()->size(), 7); |
| 125 | CHECK_EQ(max_register_count(), 201); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 126 | |
| 127 | Write(Bytecode::kReturn, {70, true}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 128 | CHECK_EQ(bytecodes()->size(), 8); |
| 129 | CHECK_EQ(max_register_count(), 201); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 130 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 131 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 134 | for (size_t i = 0; i < arraysize(bytes); ++i) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 135 | CHECK_EQ(bytecodes()->at(i), bytes[i]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 138 | writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array()); |
| 139 | CHECK_EQ(bytecodes()->size(), arraysize(bytes)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 140 | |
| 141 | PositionTableEntry expected_positions[] = { |
| 142 | {0, 10, false}, {1, 55, true}, {7, 70, true}}; |
| 143 | Handle<ByteArray> source_positions = |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 144 | source_position_table_builder()->ToSourcePositionTable(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 145 | 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 | |
| 156 | TEST_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 Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 190 | BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3; |
| 191 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 192 | #define R(i) static_cast<uint32_t>(Register(i).ToOperand()) |
| 193 | Write(Bytecode::kStackCheck, {30, false}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 194 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 200 | Write(Bytecode::kToObject); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 201 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 206 | Write(Bytecode::kLdaZero); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 207 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 217 | Write(Bytecode::kStackCheck, {54, false}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 218 | Write(Bytecode::kLdar, R(0)); |
| 219 | Write(Bytecode::kStar, R(2)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 220 | Write(Bytecode::kReturn, {85, true}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 221 | 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 228 | Write(Bytecode::kLdaUndefined); |
| 229 | Write(Bytecode::kReturn, {85, true}); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 230 | CHECK_EQ(max_register_count(), 8); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 231 | #undef R |
| 232 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 233 | CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 234 | for (size_t i = 0; i < arraysize(expected_bytes); ++i) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 235 | CHECK_EQ(static_cast<int>(bytecodes()->at(i)), |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 236 | static_cast<int>(expected_bytes[i])); |
| 237 | } |
| 238 | |
| 239 | Handle<ByteArray> source_positions = |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 240 | source_position_table_builder()->ToSourcePositionTable(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 241 | 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 |