2016-05-12 19:18:07 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/v8.h"
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/factory.h"
|
2016-05-12 19:18:07 +00:00
|
|
|
#include "src/interpreter/bytecode-array-writer.h"
|
2016-06-03 14:52:59 +00:00
|
|
|
#include "src/interpreter/bytecode-label.h"
|
|
|
|
#include "src/interpreter/constant-array-builder.h"
|
2016-05-12 19:18:07 +00:00
|
|
|
#include "src/interpreter/source-position-table.h"
|
|
|
|
#include "src/isolate.h"
|
|
|
|
#include "src/utils.h"
|
|
|
|
#include "test/unittests/interpreter/bytecode-utils.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
class BytecodeArrayWriterUnittest : public TestWithIsolateAndZone {
|
|
|
|
public:
|
|
|
|
BytecodeArrayWriterUnittest()
|
2016-06-03 14:52:59 +00:00
|
|
|
: constant_array_builder_(isolate(), zone()),
|
|
|
|
bytecode_array_writer_(isolate(), zone(), &constant_array_builder_) {}
|
2016-05-12 19:18:07 +00:00
|
|
|
~BytecodeArrayWriterUnittest() override {}
|
|
|
|
|
|
|
|
void Write(BytecodeNode* node, const BytecodeSourceInfo& info);
|
|
|
|
void Write(Bytecode bytecode,
|
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
2016-06-10 10:34:50 +00:00
|
|
|
void Write(Bytecode bytecode, uint32_t operand0,
|
2016-05-12 19:18:07 +00:00
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
|
|
|
void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
2016-06-10 10:34:50 +00:00
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
|
|
|
void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
2016-06-10 10:34:50 +00:00
|
|
|
uint32_t operand2,
|
2016-05-12 19:18:07 +00:00
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
|
|
|
void Write(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
2016-06-10 10:34:50 +00:00
|
|
|
uint32_t operand2, uint32_t operand3,
|
2016-05-12 19:18:07 +00:00
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
void WriteJump(Bytecode bytecode, BytecodeLabel* label,
|
2016-06-10 10:34:50 +00:00
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
const BytecodeSourceInfo& info = BytecodeSourceInfo());
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
BytecodeArrayWriter* writer() { return &bytecode_array_writer_; }
|
2016-06-03 14:52:59 +00:00
|
|
|
ZoneVector<unsigned char>* bytecodes() { return writer()->bytecodes(); }
|
|
|
|
SourcePositionTableBuilder* source_position_table_builder() {
|
|
|
|
return writer()->source_position_table_builder();
|
|
|
|
}
|
|
|
|
int max_register_count() { return writer()->max_register_count(); }
|
2016-05-12 19:18:07 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-03 14:52:59 +00:00
|
|
|
ConstantArrayBuilder constant_array_builder_;
|
2016-05-12 19:18:07 +00:00
|
|
|
BytecodeArrayWriter bytecode_array_writer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(BytecodeNode* node,
|
|
|
|
const BytecodeSourceInfo& info) {
|
|
|
|
if (info.is_valid()) {
|
|
|
|
node->source_info().Update(info);
|
|
|
|
}
|
|
|
|
writer()->Write(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(Bytecode bytecode,
|
|
|
|
const BytecodeSourceInfo& info) {
|
|
|
|
BytecodeNode node(bytecode);
|
|
|
|
Write(&node, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
|
|
|
|
const BytecodeSourceInfo& info) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode node(bytecode, operand0);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(&node, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
|
|
|
|
uint32_t operand1,
|
|
|
|
const BytecodeSourceInfo& info) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode node(bytecode, operand0, operand1);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(&node, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
|
|
|
|
uint32_t operand1, uint32_t operand2,
|
|
|
|
const BytecodeSourceInfo& info) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode node(bytecode, operand0, operand1, operand2);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(&node, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeArrayWriterUnittest::Write(Bytecode bytecode, uint32_t operand0,
|
|
|
|
uint32_t operand1, uint32_t operand2,
|
|
|
|
uint32_t operand3,
|
|
|
|
const BytecodeSourceInfo& info) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode node(bytecode, operand0, operand1, operand2, operand3);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(&node, info);
|
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
void BytecodeArrayWriterUnittest::WriteJump(Bytecode bytecode,
|
|
|
|
BytecodeLabel* label,
|
|
|
|
const BytecodeSourceInfo& info) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode node(bytecode, 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
if (info.is_valid()) {
|
|
|
|
node.source_info().Update(info);
|
|
|
|
}
|
|
|
|
writer()->WriteJump(&node, label);
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
TEST_F(BytecodeArrayWriterUnittest, SimpleExample) {
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), 0);
|
2016-05-12 19:18:07 +00:00
|
|
|
|
|
|
|
Write(Bytecode::kStackCheck, {10, false});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), 1);
|
|
|
|
CHECK_EQ(max_register_count(), 0);
|
2016-05-12 19:18:07 +00:00
|
|
|
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kLdaSmi, 127, {55, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), 3);
|
|
|
|
CHECK_EQ(max_register_count(), 0);
|
2016-05-12 19:18:07 +00:00
|
|
|
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kLdar, Register(200).ToOperand());
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), 7);
|
2016-06-10 10:34:50 +00:00
|
|
|
CHECK_EQ(max_register_count(), 201);
|
2016-05-12 19:18:07 +00:00
|
|
|
|
|
|
|
Write(Bytecode::kReturn, {70, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), 8);
|
2016-06-10 10:34:50 +00:00
|
|
|
CHECK_EQ(max_register_count(), 201);
|
2016-05-12 19:18:07 +00:00
|
|
|
|
2016-06-10 10:34:50 +00:00
|
|
|
static const uint8_t bytes[] = {B(StackCheck), B(LdaSmi), U8(127), B(Wide),
|
|
|
|
B(Ldar), R16(200), B(Return)};
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), arraysize(bytes));
|
2016-05-12 19:18:07 +00:00
|
|
|
for (size_t i = 0; i < arraysize(bytes); ++i) {
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->at(i), bytes[i]);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
writer()->ToBytecodeArray(0, 0, factory()->empty_fixed_array());
|
|
|
|
CHECK_EQ(bytecodes()->size(), arraysize(bytes));
|
2016-05-12 19:18:07 +00:00
|
|
|
|
|
|
|
PositionTableEntry expected_positions[] = {
|
|
|
|
{0, 10, false}, {1, 55, true}, {7, 70, true}};
|
|
|
|
Handle<ByteArray> source_positions =
|
2016-06-03 14:52:59 +00:00
|
|
|
source_position_table_builder()->ToSourcePositionTable();
|
2016-05-12 19:18:07 +00:00
|
|
|
SourcePositionTableIterator source_iterator(*source_positions);
|
|
|
|
for (size_t i = 0; i < arraysize(expected_positions); ++i) {
|
|
|
|
const PositionTableEntry& expected = expected_positions[i];
|
|
|
|
CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset);
|
|
|
|
CHECK_EQ(source_iterator.source_position(), expected.source_position);
|
|
|
|
CHECK_EQ(source_iterator.is_statement(), expected.is_statement);
|
|
|
|
source_iterator.Advance();
|
|
|
|
}
|
|
|
|
CHECK(source_iterator.done());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodeArrayWriterUnittest, ComplexExample) {
|
|
|
|
static const uint8_t expected_bytes[] = {
|
|
|
|
// clang-format off
|
|
|
|
/* 0 30 E> */ B(StackCheck),
|
|
|
|
/* 1 42 S> */ B(LdaConstant), U8(0),
|
|
|
|
/* 3 42 E> */ B(Star), R8(1),
|
|
|
|
/* 5 68 S> */ B(JumpIfUndefined), U8(38),
|
|
|
|
/* 7 */ B(JumpIfNull), U8(36),
|
|
|
|
/* 9 */ B(ToObject),
|
|
|
|
/* 10 */ B(Star), R8(3),
|
|
|
|
/* 12 */ B(ForInPrepare), R8(4),
|
|
|
|
/* 14 */ B(LdaZero),
|
|
|
|
/* 15 */ B(Star), R8(7),
|
|
|
|
/* 17 63 S> */ B(ForInDone), R8(7), R8(6),
|
|
|
|
/* 20 */ B(JumpIfTrue), U8(23),
|
|
|
|
/* 22 */ B(ForInNext), R8(3), R8(7), R8(4), U8(1),
|
|
|
|
/* 27 */ B(JumpIfUndefined), U8(10),
|
|
|
|
/* 29 */ B(Star), R8(0),
|
|
|
|
/* 31 54 E> */ B(StackCheck),
|
|
|
|
/* 32 */ B(Ldar), R8(0),
|
|
|
|
/* 34 */ B(Star), R8(2),
|
|
|
|
/* 36 85 S> */ B(Return),
|
|
|
|
/* 37 */ B(ForInStep), R8(7),
|
|
|
|
/* 39 */ B(Star), R8(7),
|
|
|
|
/* 41 */ B(Jump), U8(-24),
|
|
|
|
/* 43 */ B(LdaUndefined),
|
|
|
|
/* 44 85 S> */ B(Return),
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
|
|
|
|
static const PositionTableEntry expected_positions[] = {
|
|
|
|
{0, 30, false}, {1, 42, true}, {3, 42, false}, {5, 68, true},
|
|
|
|
{17, 63, true}, {31, 54, false}, {36, 85, true}, {44, 85, true}};
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
BytecodeLabel back_jump, jump_for_in, jump_end_1, jump_end_2, jump_end_3;
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
#define R(i) static_cast<uint32_t>(Register(i).ToOperand())
|
|
|
|
Write(Bytecode::kStackCheck, {30, false});
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kLdaConstant, U8(0), {42, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 0);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kStar, R(1), {42, false});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 2);
|
2016-06-10 10:34:50 +00:00
|
|
|
WriteJump(Bytecode::kJumpIfUndefined, &jump_end_1, {68, true});
|
|
|
|
WriteJump(Bytecode::kJumpIfNull, &jump_end_2);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(Bytecode::kToObject);
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 2);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kStar, R(3));
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 4);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kForInPrepare, R(4));
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 7);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(Bytecode::kLdaZero);
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 7);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kStar, R(7));
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 8);
|
|
|
|
writer()->BindLabel(&back_jump);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kForInDone, R(7), R(6), {63, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 8);
|
2016-06-10 10:34:50 +00:00
|
|
|
WriteJump(Bytecode::kJumpIfTrue, &jump_end_3);
|
|
|
|
Write(Bytecode::kForInNext, R(3), R(7), R(4), U8(1));
|
|
|
|
WriteJump(Bytecode::kJumpIfUndefined, &jump_for_in);
|
|
|
|
Write(Bytecode::kStar, R(0));
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(Bytecode::kStackCheck, {54, false});
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kLdar, R(0));
|
|
|
|
Write(Bytecode::kStar, R(2));
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(Bytecode::kReturn, {85, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
writer()->BindLabel(&jump_for_in);
|
2016-06-10 10:34:50 +00:00
|
|
|
Write(Bytecode::kForInStep, R(7));
|
|
|
|
Write(Bytecode::kStar, R(7));
|
|
|
|
WriteJump(Bytecode::kJump, &back_jump);
|
2016-06-03 14:52:59 +00:00
|
|
|
writer()->BindLabel(&jump_end_1);
|
|
|
|
writer()->BindLabel(&jump_end_2);
|
|
|
|
writer()->BindLabel(&jump_end_3);
|
2016-05-12 19:18:07 +00:00
|
|
|
Write(Bytecode::kLdaUndefined);
|
|
|
|
Write(Bytecode::kReturn, {85, true});
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(max_register_count(), 8);
|
2016-05-12 19:18:07 +00:00
|
|
|
#undef R
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(bytecodes()->size(), arraysize(expected_bytes));
|
2016-05-12 19:18:07 +00:00
|
|
|
for (size_t i = 0; i < arraysize(expected_bytes); ++i) {
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(static_cast<int>(bytecodes()->at(i)),
|
2016-05-12 19:18:07 +00:00
|
|
|
static_cast<int>(expected_bytes[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<ByteArray> source_positions =
|
2016-06-03 14:52:59 +00:00
|
|
|
source_position_table_builder()->ToSourcePositionTable();
|
2016-05-12 19:18:07 +00:00
|
|
|
SourcePositionTableIterator source_iterator(*source_positions);
|
|
|
|
for (size_t i = 0; i < arraysize(expected_positions); ++i) {
|
|
|
|
const PositionTableEntry& expected = expected_positions[i];
|
|
|
|
CHECK_EQ(source_iterator.bytecode_offset(), expected.bytecode_offset);
|
|
|
|
CHECK_EQ(source_iterator.source_position(), expected.source_position);
|
|
|
|
CHECK_EQ(source_iterator.is_statement(), expected.is_statement);
|
|
|
|
source_iterator.Advance();
|
|
|
|
}
|
|
|
|
CHECK(source_iterator.done());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|