2015-08-03 10:42:16 +00:00
|
|
|
// Copyright 2014 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"
|
|
|
|
|
|
|
|
#include "src/interpreter/bytecode-array-builder.h"
|
2015-09-24 15:20:47 +00:00
|
|
|
#include "src/interpreter/bytecode-array-iterator.h"
|
2015-08-13 11:27:54 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
2015-08-03 10:42:16 +00:00
|
|
|
|
2015-08-13 11:27:54 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
2015-08-03 10:42:16 +00:00
|
|
|
|
2015-08-28 15:40:52 +00:00
|
|
|
class BytecodeArrayBuilderTest : public TestWithIsolateAndZone {
|
2015-08-13 11:27:54 +00:00
|
|
|
public:
|
|
|
|
BytecodeArrayBuilderTest() {}
|
|
|
|
~BytecodeArrayBuilderTest() override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, AllBytecodesGenerated) {
|
2015-08-28 15:40:52 +00:00
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
2015-08-03 10:42:16 +00:00
|
|
|
|
2016-01-04 17:38:17 +00:00
|
|
|
builder.set_locals_count(200);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(1);
|
2015-08-27 10:32:26 +00:00
|
|
|
builder.set_parameter_count(0);
|
2016-01-04 17:38:17 +00:00
|
|
|
CHECK_EQ(builder.locals_count(), 200);
|
2015-10-16 15:29:07 +00:00
|
|
|
CHECK_EQ(builder.context_count(), 1);
|
2016-01-04 17:38:17 +00:00
|
|
|
CHECK_EQ(builder.fixed_register_count(), 201);
|
2015-08-03 10:42:16 +00:00
|
|
|
|
|
|
|
// Emit constant loads.
|
|
|
|
builder.LoadLiteral(Smi::FromInt(0))
|
|
|
|
.LoadLiteral(Smi::FromInt(8))
|
2015-08-28 15:40:52 +00:00
|
|
|
.LoadLiteral(Smi::FromInt(10000000))
|
2015-08-03 10:42:16 +00:00
|
|
|
.LoadUndefined()
|
|
|
|
.LoadNull()
|
|
|
|
.LoadTheHole()
|
|
|
|
.LoadTrue()
|
|
|
|
.LoadFalse();
|
|
|
|
|
2015-11-13 14:14:57 +00:00
|
|
|
// Emit accumulator transfers. Stores followed by loads to the same register
|
|
|
|
// are not generated. Hence, a dummy instruction in between.
|
2015-08-13 11:27:54 +00:00
|
|
|
Register reg(0);
|
2015-11-13 14:14:57 +00:00
|
|
|
builder.LoadAccumulatorWithRegister(reg)
|
|
|
|
.LoadNull()
|
|
|
|
.StoreAccumulatorInRegister(reg);
|
2015-08-03 10:42:16 +00:00
|
|
|
|
2015-11-20 11:17:10 +00:00
|
|
|
// Emit register-register transfer.
|
|
|
|
Register other(1);
|
|
|
|
builder.MoveRegister(reg, other);
|
|
|
|
|
2016-01-04 17:38:17 +00:00
|
|
|
// Emit register-register exchanges.
|
|
|
|
Register wide(150);
|
|
|
|
builder.ExchangeRegisters(reg, wide);
|
|
|
|
builder.ExchangeRegisters(wide, reg);
|
|
|
|
Register wider(151);
|
|
|
|
builder.ExchangeRegisters(wide, wider);
|
|
|
|
|
2015-10-22 14:55:32 +00:00
|
|
|
// Emit global load / store operations.
|
2015-12-23 09:34:00 +00:00
|
|
|
Factory* factory = isolate()->factory();
|
|
|
|
Handle<String> name = factory->NewStringFromStaticChars("var_name");
|
|
|
|
builder.LoadGlobal(name, 1, LanguageMode::SLOPPY,
|
2015-11-03 17:03:27 +00:00
|
|
|
TypeofMode::NOT_INSIDE_TYPEOF)
|
2015-12-23 09:34:00 +00:00
|
|
|
.LoadGlobal(name, 1, LanguageMode::STRICT, TypeofMode::NOT_INSIDE_TYPEOF)
|
|
|
|
.LoadGlobal(name, 1, LanguageMode::SLOPPY, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.LoadGlobal(name, 1, LanguageMode::STRICT, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.StoreGlobal(name, 1, LanguageMode::SLOPPY)
|
|
|
|
.StoreGlobal(name, 1, LanguageMode::STRICT);
|
2015-10-30 11:17:07 +00:00
|
|
|
|
2015-10-13 13:09:48 +00:00
|
|
|
// Emit context operations.
|
|
|
|
builder.PushContext(reg);
|
|
|
|
builder.PopContext(reg);
|
2015-10-07 21:18:58 +00:00
|
|
|
builder.LoadContextSlot(reg, 1);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.StoreContextSlot(reg, 1);
|
2015-09-24 11:48:22 +00:00
|
|
|
|
2015-09-09 15:46:04 +00:00
|
|
|
// Emit load / store property operations.
|
2015-12-23 09:34:00 +00:00
|
|
|
builder.LoadNamedProperty(reg, name, 0, LanguageMode::SLOPPY)
|
2015-09-09 15:46:04 +00:00
|
|
|
.LoadKeyedProperty(reg, 0, LanguageMode::SLOPPY)
|
2015-12-23 09:34:00 +00:00
|
|
|
.StoreNamedProperty(reg, name, 0, LanguageMode::SLOPPY)
|
2015-10-07 07:54:07 +00:00
|
|
|
.StoreKeyedProperty(reg, reg, 0, LanguageMode::SLOPPY)
|
2015-12-23 09:34:00 +00:00
|
|
|
.LoadNamedProperty(reg, name, 0, LanguageMode::STRICT)
|
2015-10-07 07:54:07 +00:00
|
|
|
.LoadKeyedProperty(reg, 0, LanguageMode::STRICT)
|
2015-12-23 09:34:00 +00:00
|
|
|
.StoreNamedProperty(reg, name, 0, LanguageMode::STRICT)
|
2015-10-14 13:19:49 +00:00
|
|
|
.StoreKeyedProperty(reg, reg, 0, LanguageMode::STRICT);
|
2015-09-02 13:03:06 +00:00
|
|
|
|
2015-12-16 17:24:20 +00:00
|
|
|
// Emit load / store lookup slots.
|
|
|
|
builder.LoadLookupSlot(name, TypeofMode::NOT_INSIDE_TYPEOF)
|
|
|
|
.LoadLookupSlot(name, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.StoreLookupSlot(name, LanguageMode::SLOPPY)
|
|
|
|
.StoreLookupSlot(name, LanguageMode::STRICT);
|
|
|
|
|
|
|
|
// Emit closure operations.
|
2015-11-26 14:33:06 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_info = factory->NewSharedFunctionInfo(
|
|
|
|
factory->NewStringFromStaticChars("function_a"), MaybeHandle<Code>(),
|
|
|
|
false);
|
|
|
|
builder.CreateClosure(shared_info, NOT_TENURED);
|
2015-10-13 09:39:55 +00:00
|
|
|
|
2015-10-22 21:41:47 +00:00
|
|
|
// Emit argument creation operations.
|
|
|
|
builder.CreateArguments(CreateArgumentsType::kMappedArguments)
|
|
|
|
.CreateArguments(CreateArgumentsType::kUnmappedArguments);
|
|
|
|
|
2015-12-09 11:53:07 +00:00
|
|
|
// Emit literal creation operations.
|
|
|
|
builder.CreateRegExpLiteral(factory->NewStringFromStaticChars("a"), 0, 0)
|
|
|
|
.CreateArrayLiteral(factory->NewFixedArray(1), 0, 0)
|
|
|
|
.CreateObjectLiteral(factory->NewFixedArray(1), 0, 0);
|
2015-10-13 14:00:40 +00:00
|
|
|
|
2015-09-14 10:05:18 +00:00
|
|
|
// Call operations.
|
2015-11-17 12:18:25 +00:00
|
|
|
builder.Call(reg, reg, 0, 0)
|
|
|
|
.Call(reg, reg, 0, 1024)
|
2015-11-04 09:21:26 +00:00
|
|
|
.CallRuntime(Runtime::kIsArray, reg, 1)
|
|
|
|
.CallJSRuntime(Context::SPREAD_ITERABLE_INDEX, reg, 1);
|
2015-09-14 10:05:18 +00:00
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
// Emit binary operator invocations.
|
2015-10-07 07:54:07 +00:00
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::SUB, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::MUL, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::DIV, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::MOD, reg, Strength::WEAK);
|
2015-08-03 10:42:16 +00:00
|
|
|
|
2015-10-12 13:35:57 +00:00
|
|
|
// Emit bitwise operator invocations
|
|
|
|
builder.BinaryOperation(Token::Value::BIT_OR, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::BIT_XOR, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::BIT_AND, reg, Strength::WEAK);
|
|
|
|
|
2015-10-12 10:45:19 +00:00
|
|
|
// Emit shift operator invocations
|
|
|
|
builder.BinaryOperation(Token::Value::SHL, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::SAR, reg, Strength::WEAK)
|
|
|
|
.BinaryOperation(Token::Value::SHR, reg, Strength::WEAK);
|
|
|
|
|
2015-10-22 20:40:20 +00:00
|
|
|
// Emit count operatior invocations
|
|
|
|
builder.CountOperation(Token::Value::ADD, Strength::WEAK)
|
|
|
|
.CountOperation(Token::Value::SUB, Strength::WEAK);
|
|
|
|
|
2015-10-06 14:15:09 +00:00
|
|
|
// Emit unary operator invocations.
|
|
|
|
builder.LogicalNot().TypeOf();
|
|
|
|
|
2015-10-28 09:49:20 +00:00
|
|
|
// Emit delete
|
2015-12-23 09:11:13 +00:00
|
|
|
builder.Delete(reg, LanguageMode::SLOPPY)
|
|
|
|
.Delete(reg, LanguageMode::STRICT)
|
|
|
|
.DeleteLookupSlot();
|
2015-10-22 20:40:20 +00:00
|
|
|
|
2015-10-15 16:46:16 +00:00
|
|
|
// Emit new.
|
|
|
|
builder.New(reg, reg, 0);
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
// Emit test operator invocations.
|
2015-10-07 07:54:07 +00:00
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::NE, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::EQ_STRICT, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::NE_STRICT, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::LT, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::GT, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::LTE, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::GTE, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::INSTANCEOF, reg, Strength::WEAK)
|
|
|
|
.CompareOperation(Token::Value::IN, reg, Strength::WEAK);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
// Emit cast operator invocations.
|
2015-10-22 20:40:20 +00:00
|
|
|
builder.CastAccumulatorToNumber()
|
2015-10-29 12:06:00 +00:00
|
|
|
.CastAccumulatorToJSObject()
|
2015-10-14 10:10:01 +00:00
|
|
|
.CastAccumulatorToName();
|
2015-09-24 15:20:47 +00:00
|
|
|
|
2015-08-03 10:42:16 +00:00
|
|
|
// Emit control flow. Return must be the last instruction.
|
2015-09-24 15:20:47 +00:00
|
|
|
BytecodeLabel start;
|
|
|
|
builder.Bind(&start);
|
|
|
|
// Short jumps with Imm8 operands
|
2015-10-15 09:11:40 +00:00
|
|
|
builder.Jump(&start)
|
2015-10-29 12:06:00 +00:00
|
|
|
.JumpIfNull(&start)
|
|
|
|
.JumpIfUndefined(&start);
|
2015-10-30 16:48:18 +00:00
|
|
|
// Perform an operation that returns boolean value to
|
|
|
|
// generate JumpIfTrue/False
|
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
|
|
|
// Perform an operation that returns a non-boolean operation to
|
|
|
|
// generate JumpIfToBooleanTrue/False.
|
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
2015-09-24 15:20:47 +00:00
|
|
|
// Insert dummy ops to force longer jumps
|
|
|
|
for (int i = 0; i < 128; i++) {
|
|
|
|
builder.LoadTrue();
|
|
|
|
}
|
|
|
|
// Longer jumps requiring Constant operand
|
2015-10-15 09:11:40 +00:00
|
|
|
builder.Jump(&start)
|
2015-10-29 12:06:00 +00:00
|
|
|
.JumpIfNull(&start)
|
|
|
|
.JumpIfUndefined(&start);
|
2015-10-30 16:48:18 +00:00
|
|
|
// Perform an operation that returns boolean value to
|
|
|
|
// generate JumpIfTrue/False
|
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
|
|
|
// Perform an operation that returns a non-boolean operation to
|
|
|
|
// generate JumpIfToBooleanTrue/False.
|
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
2015-10-19 10:59:00 +00:00
|
|
|
|
2015-11-03 11:27:54 +00:00
|
|
|
// Emit throw in it's own basic block so that the rest of the code isn't
|
|
|
|
// omitted due to being dead.
|
|
|
|
BytecodeLabel after_throw;
|
|
|
|
builder.Jump(&after_throw)
|
|
|
|
.Throw()
|
|
|
|
.Bind(&after_throw);
|
2015-10-19 10:59:00 +00:00
|
|
|
|
2015-12-21 13:30:47 +00:00
|
|
|
builder.ForInPrepare(reg, reg, reg)
|
|
|
|
.ForInDone(reg, reg)
|
|
|
|
.ForInNext(reg, reg, reg, reg)
|
|
|
|
.ForInStep(reg);
|
2015-10-29 12:06:00 +00:00
|
|
|
|
2015-10-30 11:17:07 +00:00
|
|
|
// Wide constant pool loads
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
// Emit junk in constant pool to force wide constant pool index.
|
2015-12-23 09:34:00 +00:00
|
|
|
builder.LoadLiteral(factory->NewNumber(2.5321 + i));
|
2015-10-30 11:17:07 +00:00
|
|
|
}
|
|
|
|
builder.LoadLiteral(Smi::FromInt(20000000));
|
2015-12-23 09:34:00 +00:00
|
|
|
Handle<String> wide_name = factory->NewStringFromStaticChars("var_wide_name");
|
|
|
|
|
|
|
|
// Emit wide global load / store operations.
|
|
|
|
builder.LoadGlobal(name, 1024, LanguageMode::SLOPPY,
|
|
|
|
TypeofMode::NOT_INSIDE_TYPEOF)
|
|
|
|
.LoadGlobal(wide_name, 1, LanguageMode::STRICT,
|
|
|
|
TypeofMode::NOT_INSIDE_TYPEOF)
|
|
|
|
.LoadGlobal(name, 1024, LanguageMode::SLOPPY, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.LoadGlobal(wide_name, 1, LanguageMode::STRICT, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.StoreGlobal(name, 1024, LanguageMode::SLOPPY)
|
|
|
|
.StoreGlobal(wide_name, 1, LanguageMode::STRICT);
|
|
|
|
|
|
|
|
// Emit wide load / store property operations.
|
|
|
|
builder.LoadNamedProperty(reg, wide_name, 0, LanguageMode::SLOPPY)
|
|
|
|
.LoadKeyedProperty(reg, 2056, LanguageMode::SLOPPY)
|
|
|
|
.StoreNamedProperty(reg, wide_name, 0, LanguageMode::SLOPPY)
|
|
|
|
.StoreKeyedProperty(reg, reg, 2056, LanguageMode::SLOPPY)
|
|
|
|
.LoadNamedProperty(reg, wide_name, 0, LanguageMode::STRICT)
|
|
|
|
.LoadKeyedProperty(reg, 2056, LanguageMode::STRICT)
|
|
|
|
.StoreNamedProperty(reg, wide_name, 0, LanguageMode::STRICT)
|
|
|
|
.StoreKeyedProperty(reg, reg, 2056, LanguageMode::STRICT);
|
2015-10-30 11:17:07 +00:00
|
|
|
|
2016-01-05 11:36:26 +00:00
|
|
|
// Emit wide load / store lookup slots.
|
|
|
|
builder.LoadLookupSlot(wide_name, TypeofMode::NOT_INSIDE_TYPEOF)
|
|
|
|
.LoadLookupSlot(wide_name, TypeofMode::INSIDE_TYPEOF)
|
|
|
|
.StoreLookupSlot(wide_name, LanguageMode::SLOPPY)
|
|
|
|
.StoreLookupSlot(wide_name, LanguageMode::STRICT);
|
|
|
|
|
2015-11-26 14:33:06 +00:00
|
|
|
// CreateClosureWide
|
|
|
|
Handle<SharedFunctionInfo> shared_info2 = factory->NewSharedFunctionInfo(
|
|
|
|
factory->NewStringFromStaticChars("function_b"), MaybeHandle<Code>(),
|
|
|
|
false);
|
|
|
|
builder.CreateClosure(shared_info2, NOT_TENURED);
|
|
|
|
|
2015-12-09 11:53:07 +00:00
|
|
|
// Emit wide variant of literal creation operations.
|
|
|
|
builder.CreateRegExpLiteral(factory->NewStringFromStaticChars("wide_literal"),
|
|
|
|
0, 0)
|
|
|
|
.CreateArrayLiteral(factory->NewFixedArray(2), 0, 0)
|
|
|
|
.CreateObjectLiteral(factory->NewFixedArray(2), 0, 0);
|
|
|
|
|
2016-01-05 19:08:11 +00:00
|
|
|
// Longer jumps requiring ConstantWide operand
|
|
|
|
builder.Jump(&start).JumpIfNull(&start).JumpIfUndefined(&start);
|
|
|
|
// Perform an operation that returns boolean value to
|
|
|
|
// generate JumpIfTrue/False
|
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
|
|
|
// Perform an operation that returns a non-boolean operation to
|
|
|
|
// generate JumpIfToBooleanTrue/False.
|
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&start)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&start);
|
|
|
|
|
2015-08-03 10:42:16 +00:00
|
|
|
builder.Return();
|
|
|
|
|
|
|
|
// Generate BytecodeArray.
|
|
|
|
Handle<BytecodeArray> the_array = builder.ToBytecodeArray();
|
2015-10-16 15:29:07 +00:00
|
|
|
CHECK_EQ(the_array->frame_size(),
|
|
|
|
builder.fixed_register_count() * kPointerSize);
|
2015-08-03 10:42:16 +00:00
|
|
|
|
|
|
|
// Build scorecard of bytecodes encountered in the BytecodeArray.
|
|
|
|
std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1);
|
|
|
|
Bytecode final_bytecode = Bytecode::kLdaZero;
|
2015-10-01 17:22:58 +00:00
|
|
|
int i = 0;
|
|
|
|
while (i < the_array->length()) {
|
2015-08-03 10:42:16 +00:00
|
|
|
uint8_t code = the_array->get(i);
|
|
|
|
scorecard[code] += 1;
|
|
|
|
final_bytecode = Bytecodes::FromByte(code);
|
2015-10-01 17:22:58 +00:00
|
|
|
i += Bytecodes::Size(Bytecodes::FromByte(code));
|
2015-08-03 10:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check return occurs at the end and only once in the BytecodeArray.
|
|
|
|
CHECK_EQ(final_bytecode, Bytecode::kReturn);
|
|
|
|
CHECK_EQ(scorecard[Bytecodes::ToByte(final_bytecode)], 1);
|
|
|
|
|
|
|
|
#define CHECK_BYTECODE_PRESENT(Name, ...) \
|
|
|
|
/* Check Bytecode is marked in scorecard */ \
|
|
|
|
CHECK_GE(scorecard[Bytecodes::ToByte(Bytecode::k##Name)], 1);
|
|
|
|
BYTECODE_LIST(CHECK_BYTECODE_PRESENT)
|
|
|
|
#undef CHECK_BYTECODE_PRESENT
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-13 11:27:54 +00:00
|
|
|
TEST_F(BytecodeArrayBuilderTest, FrameSizesLookGood) {
|
2015-08-03 20:38:57 +00:00
|
|
|
for (int locals = 0; locals < 5; locals++) {
|
2015-10-16 15:29:07 +00:00
|
|
|
for (int contexts = 0; contexts < 4; contexts++) {
|
|
|
|
for (int temps = 0; temps < 3; temps++) {
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
|
|
|
builder.set_locals_count(locals);
|
|
|
|
builder.set_context_count(contexts);
|
|
|
|
|
|
|
|
TemporaryRegisterScope temporaries(&builder);
|
|
|
|
for (int i = 0; i < temps; i++) {
|
2015-10-21 15:28:55 +00:00
|
|
|
builder.StoreAccumulatorInRegister(temporaries.NewRegister());
|
2015-10-16 15:29:07 +00:00
|
|
|
}
|
2015-10-21 15:28:55 +00:00
|
|
|
builder.Return();
|
2015-10-16 15:29:07 +00:00
|
|
|
|
|
|
|
Handle<BytecodeArray> the_array = builder.ToBytecodeArray();
|
|
|
|
int total_registers = locals + contexts + temps;
|
|
|
|
CHECK_EQ(the_array->frame_size(), total_registers * kPointerSize);
|
2015-08-03 10:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-13 11:27:54 +00:00
|
|
|
TEST_F(BytecodeArrayBuilderTest, TemporariesRecycled) {
|
2015-08-28 15:40:52 +00:00
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
2015-08-27 10:32:26 +00:00
|
|
|
builder.set_parameter_count(0);
|
2015-08-03 10:42:16 +00:00
|
|
|
builder.set_locals_count(0);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-08-03 10:42:16 +00:00
|
|
|
builder.Return();
|
|
|
|
|
|
|
|
int first;
|
|
|
|
{
|
|
|
|
TemporaryRegisterScope temporaries(&builder);
|
2015-08-13 11:27:54 +00:00
|
|
|
first = temporaries.NewRegister().index();
|
2015-08-03 10:42:16 +00:00
|
|
|
temporaries.NewRegister();
|
|
|
|
temporaries.NewRegister();
|
|
|
|
temporaries.NewRegister();
|
|
|
|
}
|
|
|
|
|
|
|
|
int second;
|
|
|
|
{
|
|
|
|
TemporaryRegisterScope temporaries(&builder);
|
2015-08-13 11:27:54 +00:00
|
|
|
second = temporaries.NewRegister().index();
|
2015-08-03 10:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(first, second);
|
|
|
|
}
|
2015-08-13 11:27:54 +00:00
|
|
|
|
2015-08-13 17:11:25 +00:00
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, RegisterValues) {
|
|
|
|
int index = 1;
|
|
|
|
uint8_t operand = static_cast<uint8_t>(-index);
|
|
|
|
|
|
|
|
Register the_register(index);
|
|
|
|
CHECK_EQ(the_register.index(), index);
|
|
|
|
|
|
|
|
int actual_operand = the_register.ToOperand();
|
|
|
|
CHECK_EQ(actual_operand, operand);
|
|
|
|
|
|
|
|
int actual_index = Register::FromOperand(actual_operand).index();
|
|
|
|
CHECK_EQ(actual_index, index);
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:32:26 +00:00
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, Parameters) {
|
2015-08-28 15:40:52 +00:00
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
2015-08-27 10:32:26 +00:00
|
|
|
builder.set_parameter_count(10);
|
|
|
|
builder.set_locals_count(0);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-08-27 10:32:26 +00:00
|
|
|
|
|
|
|
Register param0(builder.Parameter(0));
|
|
|
|
Register param9(builder.Parameter(9));
|
|
|
|
CHECK_EQ(param9.index() - param0.index(), 9);
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:40:52 +00:00
|
|
|
|
2015-10-21 15:28:55 +00:00
|
|
|
TEST_F(BytecodeArrayBuilderTest, RegisterType) {
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(10);
|
|
|
|
builder.set_locals_count(3);
|
|
|
|
builder.set_context_count(0);
|
|
|
|
|
|
|
|
TemporaryRegisterScope temporary_register_scope(&builder);
|
|
|
|
Register temp0 = temporary_register_scope.NewRegister();
|
|
|
|
Register param0(builder.Parameter(0));
|
|
|
|
Register param9(builder.Parameter(9));
|
|
|
|
Register temp1 = temporary_register_scope.NewRegister();
|
|
|
|
Register reg0(0);
|
|
|
|
Register reg1(1);
|
|
|
|
Register reg2(2);
|
|
|
|
Register temp2 = temporary_register_scope.NewRegister();
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(temp0), false);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(temp1), false);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(temp2), false);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(param0), true);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(param9), true);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(reg0), true);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(reg1), true);
|
|
|
|
CHECK_EQ(builder.RegisterIsParameterOrLocal(reg2), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-28 15:40:52 +00:00
|
|
|
TEST_F(BytecodeArrayBuilderTest, Constants) {
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
|
|
|
builder.set_locals_count(0);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-08-28 15:40:52 +00:00
|
|
|
|
|
|
|
Factory* factory = isolate()->factory();
|
|
|
|
Handle<HeapObject> heap_num_1 = factory->NewHeapNumber(3.14);
|
|
|
|
Handle<HeapObject> heap_num_2 = factory->NewHeapNumber(5.2);
|
|
|
|
Handle<Object> large_smi(Smi::FromInt(0x12345678), isolate());
|
|
|
|
Handle<HeapObject> heap_num_2_copy(*heap_num_2);
|
|
|
|
builder.LoadLiteral(heap_num_1)
|
|
|
|
.LoadLiteral(heap_num_2)
|
|
|
|
.LoadLiteral(large_smi)
|
|
|
|
.LoadLiteral(heap_num_1)
|
|
|
|
.LoadLiteral(heap_num_1)
|
|
|
|
.LoadLiteral(heap_num_2_copy);
|
|
|
|
|
|
|
|
Handle<BytecodeArray> array = builder.ToBytecodeArray();
|
|
|
|
// Should only have one entry for each identical constant.
|
|
|
|
CHECK_EQ(array->constant_pool()->length(), 3);
|
|
|
|
}
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, ForwardJumps) {
|
|
|
|
static const int kFarJumpDistance = 256;
|
|
|
|
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
2015-10-30 16:48:18 +00:00
|
|
|
builder.set_locals_count(1);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
2015-10-30 16:48:18 +00:00
|
|
|
Register reg(0);
|
|
|
|
BytecodeLabel far0, far1, far2, far3, far4;
|
|
|
|
BytecodeLabel near0, near1, near2, near3, near4;
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
builder.Jump(&near0)
|
2015-10-30 16:48:18 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
2015-09-24 15:20:47 +00:00
|
|
|
.JumpIfTrue(&near1)
|
2015-10-30 16:48:18 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
2015-09-24 15:20:47 +00:00
|
|
|
.JumpIfFalse(&near2)
|
2015-10-30 16:48:18 +00:00
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&near3)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&near4)
|
2015-09-24 15:20:47 +00:00
|
|
|
.Bind(&near0)
|
|
|
|
.Bind(&near1)
|
|
|
|
.Bind(&near2)
|
2015-10-30 16:48:18 +00:00
|
|
|
.Bind(&near3)
|
|
|
|
.Bind(&near4)
|
2015-09-24 15:20:47 +00:00
|
|
|
.Jump(&far0)
|
2015-10-30 16:48:18 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
2015-09-24 15:20:47 +00:00
|
|
|
.JumpIfTrue(&far1)
|
2015-10-30 16:48:18 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&far2)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&far3)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&far4);
|
|
|
|
for (int i = 0; i < kFarJumpDistance - 18; i++) {
|
2015-09-24 15:20:47 +00:00
|
|
|
builder.LoadUndefined();
|
|
|
|
}
|
2015-10-30 16:48:18 +00:00
|
|
|
builder.Bind(&far0).Bind(&far1).Bind(&far2).Bind(&far3).Bind(&far4);
|
2015-09-24 15:20:47 +00:00
|
|
|
builder.Return();
|
|
|
|
|
|
|
|
Handle<BytecodeArray> array = builder.ToBytecodeArray();
|
2015-10-30 16:48:18 +00:00
|
|
|
DCHECK_EQ(array->length(), 36 + kFarJumpDistance - 18 + 1);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
BytecodeArrayIterator iterator(array);
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-30 16:48:18 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 18);
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore compare operation.
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrue);
|
2015-10-30 16:48:18 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 14);
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore compare operation.
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalse);
|
2015-10-30 16:48:18 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 10);
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore add operation.
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanTrue);
|
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 6);
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore add operation.
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanFalse);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
2015-10-30 16:48:18 +00:00
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpConstant);
|
|
|
|
CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
|
|
|
|
Smi::FromInt(kFarJumpDistance));
|
2015-10-30 16:48:18 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore compare operation.
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueConstant);
|
|
|
|
CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
|
2015-10-30 16:48:18 +00:00
|
|
|
Smi::FromInt(kFarJumpDistance - 4));
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore compare operation.
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseConstant);
|
|
|
|
CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
|
2015-10-30 16:48:18 +00:00
|
|
|
Smi::FromInt(kFarJumpDistance - 8));
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore add operation.
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanTrueConstant);
|
|
|
|
CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
|
|
|
|
Smi::FromInt(kFarJumpDistance - 12));
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
// Ignore add operation.
|
|
|
|
iterator.Advance();
|
|
|
|
|
|
|
|
CHECK_EQ(iterator.current_bytecode(),
|
|
|
|
Bytecode::kJumpIfToBooleanFalseConstant);
|
|
|
|
CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
|
|
|
|
Smi::FromInt(kFarJumpDistance - 16));
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, BackwardJumps) {
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
2015-10-30 16:48:18 +00:00
|
|
|
builder.set_locals_count(1);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-10-30 16:48:18 +00:00
|
|
|
Register reg(0);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
2015-10-30 16:48:18 +00:00
|
|
|
BytecodeLabel label0, label1, label2, label3, label4;
|
2015-09-24 15:20:47 +00:00
|
|
|
builder.Bind(&label0)
|
|
|
|
.Jump(&label0)
|
|
|
|
.Bind(&label1)
|
2015-10-30 16:48:18 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
2015-11-03 11:27:54 +00:00
|
|
|
.JumpIfTrue(&label1)
|
2015-09-24 15:20:47 +00:00
|
|
|
.Bind(&label2)
|
2015-11-03 11:27:54 +00:00
|
|
|
.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
2015-10-30 16:48:18 +00:00
|
|
|
.JumpIfFalse(&label2)
|
|
|
|
.Bind(&label3)
|
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
2015-11-03 11:27:54 +00:00
|
|
|
.JumpIfTrue(&label3)
|
2015-10-30 16:48:18 +00:00
|
|
|
.Bind(&label4)
|
2015-11-03 11:27:54 +00:00
|
|
|
.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
2015-10-30 16:48:18 +00:00
|
|
|
.JumpIfFalse(&label4);
|
2015-11-03 11:27:54 +00:00
|
|
|
for (int i = 0; i < 63; i++) {
|
2015-10-30 16:48:18 +00:00
|
|
|
builder.Jump(&label4);
|
2015-09-24 15:20:47 +00:00
|
|
|
}
|
2015-10-30 16:48:18 +00:00
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&label4);
|
|
|
|
builder.BinaryOperation(Token::Value::ADD, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&label3);
|
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfFalse(&label2);
|
|
|
|
builder.CompareOperation(Token::Value::EQ, reg, Strength::WEAK)
|
|
|
|
.JumpIfTrue(&label1);
|
2015-09-24 15:20:47 +00:00
|
|
|
builder.Jump(&label0);
|
|
|
|
builder.Return();
|
|
|
|
|
|
|
|
Handle<BytecodeArray> array = builder.ToBytecodeArray();
|
|
|
|
BytecodeArrayIterator iterator(array);
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 0);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
2015-10-30 16:48:18 +00:00
|
|
|
// Ignore compare operation.
|
|
|
|
iterator.Advance();
|
2015-09-24 15:20:47 +00:00
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrue);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
2015-10-30 16:48:18 +00:00
|
|
|
// Ignore compare operation.
|
|
|
|
iterator.Advance();
|
2015-09-24 15:20:47 +00:00
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalse);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
2015-10-30 16:48:18 +00:00
|
|
|
// Ignore binary operation.
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanTrue);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-10-30 16:48:18 +00:00
|
|
|
iterator.Advance();
|
|
|
|
// Ignore binary operation.
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanFalse);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-10-30 16:48:18 +00:00
|
|
|
iterator.Advance();
|
2015-11-03 11:27:54 +00:00
|
|
|
for (int i = 0; i < 63; i++) {
|
2015-09-24 15:20:47 +00:00
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -i * 2 - 4);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
}
|
2015-10-30 16:48:18 +00:00
|
|
|
// Ignore binary operation.
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(),
|
|
|
|
Bytecode::kJumpIfToBooleanFalseConstant);
|
|
|
|
CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -132);
|
|
|
|
iterator.Advance();
|
|
|
|
// Ignore binary operation.
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfToBooleanTrueConstant);
|
|
|
|
CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -140);
|
|
|
|
iterator.Advance();
|
|
|
|
// Ignore compare operation.
|
|
|
|
iterator.Advance();
|
2015-09-24 15:20:47 +00:00
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseConstant);
|
2015-10-30 16:48:18 +00:00
|
|
|
CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -148);
|
|
|
|
iterator.Advance();
|
|
|
|
// Ignore compare operation.
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueConstant);
|
2015-10-30 16:48:18 +00:00
|
|
|
CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -156);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpConstant);
|
2015-11-03 11:27:54 +00:00
|
|
|
CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -160);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK(iterator.done());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, LabelReuse) {
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
|
|
|
builder.set_locals_count(0);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
// Labels can only have 1 forward reference, but
|
|
|
|
// can be referred to mulitple times once bound.
|
|
|
|
BytecodeLabel label;
|
|
|
|
|
|
|
|
builder.Jump(&label).Bind(&label).Jump(&label).Jump(&label).Return();
|
|
|
|
|
|
|
|
Handle<BytecodeArray> array = builder.ToBytecodeArray();
|
|
|
|
BytecodeArrayIterator iterator(array);
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 0);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK(iterator.done());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(BytecodeArrayBuilderTest, LabelAddressReuse) {
|
|
|
|
static const int kRepeats = 3;
|
|
|
|
|
|
|
|
BytecodeArrayBuilder builder(isolate(), zone());
|
|
|
|
builder.set_parameter_count(0);
|
|
|
|
builder.set_locals_count(0);
|
2015-10-16 15:29:07 +00:00
|
|
|
builder.set_context_count(0);
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kRepeats; i++) {
|
|
|
|
BytecodeLabel label;
|
|
|
|
builder.Jump(&label).Bind(&label).Jump(&label).Jump(&label);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.Return();
|
|
|
|
|
|
|
|
Handle<BytecodeArray> array = builder.ToBytecodeArray();
|
|
|
|
BytecodeArrayIterator iterator(array);
|
|
|
|
for (int i = 0; i < kRepeats; i++) {
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), 0);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kJump);
|
2015-10-01 17:22:58 +00:00
|
|
|
CHECK_EQ(iterator.GetImmediateOperand(0), -2);
|
2015-09-24 15:20:47 +00:00
|
|
|
iterator.Advance();
|
|
|
|
}
|
|
|
|
CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
|
|
|
|
iterator.Advance();
|
|
|
|
CHECK(iterator.done());
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:08:11 +00:00
|
|
|
|
2015-08-13 11:27:54 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|