2015-08-18 13:46:43 +00:00
|
|
|
// Copyright 2015 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/compiler.h"
|
2015-09-24 11:48:22 +00:00
|
|
|
#include "src/interpreter/bytecode-array-iterator.h"
|
2015-08-18 13:46:43 +00:00
|
|
|
#include "src/interpreter/bytecode-generator.h"
|
|
|
|
#include "src/interpreter/interpreter.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
2015-10-01 13:48:05 +00:00
|
|
|
#include "test/cctest/test-feedback-vector.h"
|
2015-08-18 13:46:43 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
class BytecodeGeneratorHelper {
|
|
|
|
public:
|
2015-08-27 10:32:26 +00:00
|
|
|
const char* kFunctionName = "f";
|
2015-08-18 13:46:43 +00:00
|
|
|
|
2015-09-28 18:05:56 +00:00
|
|
|
static const int kLastParamIndex =
|
2015-09-02 13:03:06 +00:00
|
|
|
-InterpreterFrameConstants::kLastParamFromRegisterPointer / kPointerSize;
|
|
|
|
|
2015-08-18 13:46:43 +00:00
|
|
|
BytecodeGeneratorHelper() {
|
2015-09-09 15:46:04 +00:00
|
|
|
i::FLAG_vector_stores = true;
|
2015-08-18 13:46:43 +00:00
|
|
|
i::FLAG_ignition = true;
|
2015-09-10 16:21:34 +00:00
|
|
|
i::FLAG_ignition_filter = StrDup(kFunctionName);
|
|
|
|
i::FLAG_always_opt = false;
|
2015-08-18 13:46:43 +00:00
|
|
|
CcTest::i_isolate()->interpreter()->Initialize();
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:48:05 +00:00
|
|
|
Isolate* isolate() { return CcTest::i_isolate(); }
|
2015-09-02 13:03:06 +00:00
|
|
|
Factory* factory() { return CcTest::i_isolate()->factory(); }
|
|
|
|
|
|
|
|
|
2015-08-18 13:46:43 +00:00
|
|
|
Handle<BytecodeArray> MakeBytecode(const char* script,
|
|
|
|
const char* function_name) {
|
|
|
|
CompileRun(script);
|
|
|
|
Local<Function> function =
|
|
|
|
Local<Function>::Cast(CcTest::global()->Get(v8_str(function_name)));
|
|
|
|
i::Handle<i::JSFunction> js_function = v8::Utils::OpenHandle(*function);
|
|
|
|
return handle(js_function->shared()->bytecode_array(), CcTest::i_isolate());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<BytecodeArray> MakeBytecodeForFunctionBody(const char* body) {
|
|
|
|
ScopedVector<char> program(1024);
|
|
|
|
SNPrintF(program, "function %s() { %s }\n%s();", kFunctionName, body,
|
|
|
|
kFunctionName);
|
|
|
|
return MakeBytecode(program.start(), kFunctionName);
|
|
|
|
}
|
2015-08-27 10:32:26 +00:00
|
|
|
|
|
|
|
Handle<BytecodeArray> MakeBytecodeForFunction(const char* function) {
|
|
|
|
ScopedVector<char> program(1024);
|
|
|
|
SNPrintF(program, "%s\n%s();", function, kFunctionName);
|
|
|
|
return MakeBytecode(program.start(), kFunctionName);
|
|
|
|
}
|
2015-08-18 13:46:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
// Helper macros for handcrafting bytecode sequences.
|
|
|
|
#define B(x) static_cast<uint8_t>(Bytecode::k##x)
|
|
|
|
#define U8(x) static_cast<uint8_t>((x) & 0xff)
|
|
|
|
#define R(x) static_cast<uint8_t>(-(x) & 0xff)
|
|
|
|
#define _ static_cast<uint8_t>(0x5a)
|
|
|
|
|
|
|
|
|
2015-08-18 13:46:43 +00:00
|
|
|
// Structure for containing expected bytecode snippets.
|
2015-08-28 15:40:52 +00:00
|
|
|
template<typename T>
|
2015-08-18 13:46:43 +00:00
|
|
|
struct ExpectedSnippet {
|
2015-09-02 13:03:06 +00:00
|
|
|
const char* code_snippet;
|
2015-08-18 13:46:43 +00:00
|
|
|
int frame_size;
|
2015-08-27 10:32:26 +00:00
|
|
|
int parameter_count;
|
2015-08-18 13:46:43 +00:00
|
|
|
int bytecode_length;
|
2015-09-24 15:20:47 +00:00
|
|
|
const uint8_t bytecode[512];
|
2015-08-28 15:40:52 +00:00
|
|
|
int constant_count;
|
2015-09-24 15:20:47 +00:00
|
|
|
T constants[4];
|
2015-08-18 13:46:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
static void CheckConstant(int expected, Object* actual) {
|
|
|
|
CHECK_EQ(expected, Smi::cast(actual)->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckConstant(double expected, Object* actual) {
|
|
|
|
CHECK_EQ(expected, HeapNumber::cast(actual)->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckConstant(const char* expected, Object* actual) {
|
|
|
|
Handle<String> expected_string =
|
|
|
|
CcTest::i_isolate()->factory()->NewStringFromAsciiChecked(expected);
|
|
|
|
CHECK(String::cast(actual)->Equals(*expected_string));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
static void CheckConstant(Handle<Object> expected, Object* actual) {
|
|
|
|
CHECK(actual == *expected || expected->StrictEquals(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
template <typename T>
|
|
|
|
static void CheckBytecodeArrayEqual(struct ExpectedSnippet<T> expected,
|
|
|
|
Handle<BytecodeArray> actual,
|
|
|
|
bool has_unknown = false) {
|
|
|
|
CHECK_EQ(actual->frame_size(), expected.frame_size);
|
|
|
|
CHECK_EQ(actual->parameter_count(), expected.parameter_count);
|
|
|
|
CHECK_EQ(actual->length(), expected.bytecode_length);
|
|
|
|
if (expected.constant_count == 0) {
|
|
|
|
CHECK_EQ(actual->constant_pool(), CcTest::heap()->empty_fixed_array());
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(actual->constant_pool()->length(), expected.constant_count);
|
|
|
|
for (int i = 0; i < expected.constant_count; i++) {
|
|
|
|
CheckConstant(expected.constants[i], actual->constant_pool()->get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeArrayIterator iterator(actual);
|
|
|
|
int i = 0;
|
|
|
|
while (!iterator.done()) {
|
|
|
|
int bytecode_index = i++;
|
|
|
|
Bytecode bytecode = iterator.current_bytecode();
|
|
|
|
if (Bytecodes::ToByte(bytecode) != expected.bytecode[bytecode_index]) {
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << "Check failed: expected bytecode [" << bytecode_index
|
|
|
|
<< "] to be " << Bytecodes::ToString(static_cast<Bytecode>(
|
|
|
|
expected.bytecode[bytecode_index]))
|
|
|
|
<< " but got " << Bytecodes::ToString(bytecode);
|
|
|
|
FATAL(stream.str().c_str());
|
|
|
|
}
|
|
|
|
for (int j = 0; j < Bytecodes::NumberOfOperands(bytecode); ++j, ++i) {
|
|
|
|
uint8_t raw_operand =
|
|
|
|
iterator.GetRawOperand(j, Bytecodes::GetOperandType(bytecode, j));
|
|
|
|
if (has_unknown) {
|
|
|
|
// Check actual bytecode array doesn't have the same byte as the
|
|
|
|
// one we use to specify an unknown byte.
|
|
|
|
CHECK_NE(raw_operand, _);
|
|
|
|
if (expected.bytecode[i] == _) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (raw_operand != expected.bytecode[i]) {
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << "Check failed: expected operand [" << j << "] for bytecode ["
|
|
|
|
<< bytecode_index << "] to be "
|
|
|
|
<< static_cast<unsigned int>(expected.bytecode[i]) << " but got "
|
|
|
|
<< static_cast<unsigned int>(raw_operand);
|
|
|
|
FATAL(stream.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iterator.Advance();
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 13:46:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(PrimitiveReturnStatements) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
ExpectedSnippet<int> snippets[] = {
|
2015-09-08 15:02:44 +00:00
|
|
|
{"", 0, 1, 2, {B(LdaUndefined), B(Return)}, 0},
|
2015-08-28 15:40:52 +00:00
|
|
|
{"return;", 0, 1, 2, {B(LdaUndefined), B(Return)}, 0},
|
|
|
|
{"return null;", 0, 1, 2, {B(LdaNull), B(Return)}, 0},
|
|
|
|
{"return true;", 0, 1, 2, {B(LdaTrue), B(Return)}, 0},
|
|
|
|
{"return false;", 0, 1, 2, {B(LdaFalse), B(Return)}, 0},
|
|
|
|
{"return 0;", 0, 1, 2, {B(LdaZero), B(Return)}, 0},
|
|
|
|
{"return +1;", 0, 1, 3, {B(LdaSmi8), U8(1), B(Return)}, 0},
|
|
|
|
{"return -1;", 0, 1, 3, {B(LdaSmi8), U8(-1), B(Return)}, 0},
|
|
|
|
{"return +127;", 0, 1, 3, {B(LdaSmi8), U8(127), B(Return)}, 0},
|
|
|
|
{"return -128;", 0, 1, 3, {B(LdaSmi8), U8(-128), B(Return)}, 0},
|
2015-08-18 13:46:43 +00:00
|
|
|
};
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-02 13:03:06 +00:00
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-18 13:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PrimitiveExpressions) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
ExpectedSnippet<int> snippets[] = {
|
2015-08-18 13:46:43 +00:00
|
|
|
{"var x = 0; return x;",
|
|
|
|
kPointerSize,
|
2015-08-27 10:32:26 +00:00
|
|
|
1,
|
2015-08-18 13:46:43 +00:00
|
|
|
6,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Return) //
|
2015-08-28 15:40:52 +00:00
|
|
|
},
|
|
|
|
0
|
|
|
|
},
|
2015-08-18 13:46:43 +00:00
|
|
|
{"var x = 0; return x + 3;",
|
|
|
|
2 * kPointerSize,
|
2015-08-27 10:32:26 +00:00
|
|
|
1,
|
2015-08-18 13:46:43 +00:00
|
|
|
12,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), // Easy to spot r1 not really needed here.
|
|
|
|
B(Star), R(1), // Dead store.
|
|
|
|
B(LdaSmi8), U8(3), //
|
|
|
|
B(Add), R(1), //
|
|
|
|
B(Return) //
|
2015-08-28 15:40:52 +00:00
|
|
|
},
|
|
|
|
0
|
|
|
|
}};
|
2015-08-18 13:46:43 +00:00
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-02 13:03:06 +00:00
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-27 10:32:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Parameters) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
2015-09-24 11:48:22 +00:00
|
|
|
ExpectedSnippet<int> snippets[] = {
|
2015-08-27 10:32:26 +00:00
|
|
|
{"function f() { return this; }",
|
2015-09-02 13:03:06 +00:00
|
|
|
0, 1, 3, {B(Ldar), R(helper.kLastParamIndex), B(Return)}, 0},
|
2015-08-27 10:32:26 +00:00
|
|
|
{"function f(arg1) { return arg1; }",
|
2015-09-02 13:03:06 +00:00
|
|
|
0, 2, 3, {B(Ldar), R(helper.kLastParamIndex), B(Return)}, 0},
|
2015-08-27 10:32:26 +00:00
|
|
|
{"function f(arg1) { return this; }",
|
2015-09-02 13:03:06 +00:00
|
|
|
0, 2, 3, {B(Ldar), R(helper.kLastParamIndex - 1), B(Return)}, 0},
|
2015-08-27 10:32:26 +00:00
|
|
|
{"function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return arg4; }",
|
2015-09-02 13:03:06 +00:00
|
|
|
0, 8, 3, {B(Ldar), R(helper.kLastParamIndex - 3), B(Return)}, 0},
|
2015-08-27 10:32:26 +00:00
|
|
|
{"function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return this; }",
|
2015-09-02 13:03:06 +00:00
|
|
|
0, 8, 3, {B(Ldar), R(helper.kLastParamIndex - 7), B(Return)}, 0}
|
2015-08-27 10:32:26 +00:00
|
|
|
};
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-02 13:03:06 +00:00
|
|
|
helper.MakeBytecodeForFunction(snippets[i].code_snippet);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-18 13:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 15:40:52 +00:00
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
TEST(IntegerConstants) {
|
2015-08-28 15:40:52 +00:00
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
ExpectedSnippet<int> snippets[] = {
|
|
|
|
{"return 12345678;",
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
{12345678}},
|
|
|
|
{"var a = 1234; return 5678;",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
{1234, 5678}},
|
|
|
|
{"var a = 1234; return 1234;",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
{1234}}};
|
2015-08-28 15:40:52 +00:00
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-28 15:40:52 +00:00
|
|
|
}
|
2015-09-24 15:20:47 +00:00
|
|
|
}
|
2015-08-28 15:40:52 +00:00
|
|
|
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
TEST(HeapNumberConstants) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
ExpectedSnippet<double> snippets[] = {
|
|
|
|
{"return 1.2;",
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
{1.2}},
|
|
|
|
{"var a = 1.2; return 2.6;",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
{1.2, 2.6}},
|
|
|
|
{"var a = 3.14; return 3.14;",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
{3.14, 3.14}}};
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-28 15:40:52 +00:00
|
|
|
}
|
2015-09-24 15:20:47 +00:00
|
|
|
}
|
2015-08-28 15:40:52 +00:00
|
|
|
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
TEST(StringConstants) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"return \"This is a string\";",
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
{"This is a string"}},
|
|
|
|
{"var a = \"First string\"; return \"Second string\";",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
{"First string", "Second string"}},
|
|
|
|
{"var a = \"Same string\"; return \"Same string\";",
|
|
|
|
1 * kPointerSize,
|
|
|
|
1,
|
|
|
|
7,
|
|
|
|
{
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
{"Same string"}}};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-08-28 15:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-02 13:03:06 +00:00
|
|
|
TEST(PropertyLoads) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
2015-10-01 13:48:05 +00:00
|
|
|
Zone zone;
|
|
|
|
|
|
|
|
FeedbackVectorSpec feedback_spec(&zone);
|
|
|
|
FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
|
|
|
|
FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
|
2015-09-02 13:03:06 +00:00
|
|
|
|
|
|
|
Handle<i::TypeFeedbackVector> vector =
|
2015-10-01 13:48:05 +00:00
|
|
|
i::TypeFeedbackVector::New(helper.isolate(), &feedback_spec);
|
2015-09-02 13:03:06 +00:00
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"function f(a) { return a.name; }\nf({name : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
1 * kPointerSize,
|
|
|
|
2,
|
|
|
|
10,
|
2015-09-02 13:03:06 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(0), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(Return) //
|
2015-09-02 13:03:06 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"name"}},
|
2015-09-02 13:03:06 +00:00
|
|
|
{"function f(a) { return a[\"key\"]; }\nf({key : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
1 * kPointerSize,
|
|
|
|
2,
|
|
|
|
10,
|
2015-09-02 13:03:06 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(0), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(Return) //
|
2015-09-02 13:03:06 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"key"}},
|
2015-09-02 13:03:06 +00:00
|
|
|
{"function f(a) { return a[100]; }\nf({100 : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
1 * kPointerSize,
|
|
|
|
2,
|
|
|
|
10,
|
2015-09-02 13:03:06 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaSmi8), U8(100), //
|
|
|
|
B(KeyedLoadIC), R(0), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(Return) //
|
2015-09-24 15:20:47 +00:00
|
|
|
},
|
|
|
|
0},
|
2015-09-02 13:03:06 +00:00
|
|
|
{"function f(a, b) { return a[b]; }\nf({arg : \"test\"}, \"arg\")",
|
2015-09-24 15:20:47 +00:00
|
|
|
1 * kPointerSize,
|
|
|
|
3,
|
|
|
|
10,
|
2015-09-02 13:03:06 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex - 1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(KeyedLoadIC), R(0), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(Return) //
|
2015-09-24 15:20:47 +00:00
|
|
|
},
|
|
|
|
0},
|
2015-09-02 13:03:06 +00:00
|
|
|
{"function f(a) { var b = a.name; return a[-124]; }\n"
|
|
|
|
"f({\"-124\" : \"test\", name : 123 })",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
2,
|
|
|
|
21,
|
2015-09-02 13:03:06 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(1), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(-124), //
|
|
|
|
B(KeyedLoadIC), R(1), U8(vector->GetIndex(slot2)), //
|
|
|
|
B(Return) //
|
2015-09-02 13:03:06 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"name"}}};
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-28 18:05:56 +00:00
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-09-02 13:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 15:46:04 +00:00
|
|
|
|
|
|
|
TEST(PropertyStores) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
2015-10-01 13:48:05 +00:00
|
|
|
Zone zone;
|
|
|
|
|
|
|
|
FeedbackVectorSpec feedback_spec(&zone);
|
|
|
|
FeedbackVectorSlot slot1 = feedback_spec.AddStoreICSlot();
|
|
|
|
FeedbackVectorSlot slot2 = feedback_spec.AddStoreICSlot();
|
2015-09-09 15:46:04 +00:00
|
|
|
|
|
|
|
Handle<i::TypeFeedbackVector> vector =
|
2015-10-01 13:48:05 +00:00
|
|
|
i::TypeFeedbackVector::New(helper.isolate(), &feedback_spec);
|
2015-09-09 15:46:04 +00:00
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"function f(a) { a.name = \"val\"; }\nf({name : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
2,
|
|
|
|
16,
|
2015-09-09 15:46:04 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(StoreIC), R(0), R(1), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return) //
|
2015-09-09 15:46:04 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
2,
|
|
|
|
{"name", "val"}},
|
2015-09-09 15:46:04 +00:00
|
|
|
{"function f(a) { a[\"key\"] = \"val\"; }\nf({key : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
2,
|
|
|
|
16,
|
2015-09-09 15:46:04 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(StoreIC), R(0), R(1), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return) //
|
2015-09-09 15:46:04 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
2,
|
|
|
|
{"key", "val"}},
|
2015-09-09 15:46:04 +00:00
|
|
|
{"function f(a) { a[100] = \"val\"; }\nf({100 : \"test\"})",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
2,
|
|
|
|
16,
|
2015-09-09 15:46:04 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaSmi8), U8(100), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(KeyedStoreIC), R(0), R(1), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return) //
|
2015-09-09 15:46:04 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"val"}},
|
2015-09-09 15:46:04 +00:00
|
|
|
{"function f(a, b) { a[b] = \"val\"; }\nf({arg : \"test\"}, \"arg\")",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
3,
|
|
|
|
16,
|
2015-09-09 15:46:04 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex - 1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(KeyedStoreIC), R(0), R(1), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return) //
|
2015-09-09 15:46:04 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"val"}},
|
2015-09-09 15:46:04 +00:00
|
|
|
{"function f(a) { a.name = a[-124]; }\n"
|
|
|
|
"f({\"-124\" : \"test\", name : 123 })",
|
2015-09-24 15:20:47 +00:00
|
|
|
3 * kPointerSize,
|
|
|
|
2,
|
|
|
|
23,
|
2015-09-09 15:46:04 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(-124), //
|
|
|
|
B(KeyedLoadIC), R(2), U8(vector->GetIndex(slot1)), //
|
|
|
|
B(StoreIC), R(0), R(1), U8(vector->GetIndex(slot2)), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return) //
|
2015-09-09 15:46:04 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"name"}}};
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-28 18:05:56 +00:00
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
2015-09-09 15:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 10:05:18 +00:00
|
|
|
|
|
|
|
#define FUNC_ARG "new (function Obj() { this.func = function() { return; }})()"
|
|
|
|
|
|
|
|
|
|
|
|
TEST(PropertyCall) {
|
|
|
|
InitializedHandleScope handle_scope;
|
2015-09-24 15:20:47 +00:00
|
|
|
BytecodeGeneratorHelper helper; //
|
2015-10-01 13:48:05 +00:00
|
|
|
Zone zone;
|
|
|
|
|
|
|
|
FeedbackVectorSpec feedback_spec(&zone);
|
|
|
|
FeedbackVectorSlot slot1 = feedback_spec.AddLoadICSlot();
|
|
|
|
FeedbackVectorSlot slot2 = feedback_spec.AddLoadICSlot();
|
|
|
|
USE(slot1);
|
2015-09-14 10:05:18 +00:00
|
|
|
|
|
|
|
Handle<i::TypeFeedbackVector> vector =
|
2015-10-01 13:48:05 +00:00
|
|
|
i::TypeFeedbackVector::New(helper.isolate(), &feedback_spec);
|
2015-09-14 10:05:18 +00:00
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"function f(a) { return a.func(); }\nf(" FUNC_ARG ")",
|
2015-09-24 15:20:47 +00:00
|
|
|
2 * kPointerSize,
|
|
|
|
2,
|
|
|
|
16,
|
2015-09-14 10:05:18 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(1), U8(vector->GetIndex(slot2)), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Call), R(0), R(1), U8(0), //
|
|
|
|
B(Return) //
|
2015-09-14 10:05:18 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"func"}},
|
2015-09-14 10:05:18 +00:00
|
|
|
{"function f(a, b, c) { return a.func(b, c); }\nf(" FUNC_ARG ", 1, 2)",
|
2015-09-24 15:20:47 +00:00
|
|
|
4 * kPointerSize,
|
|
|
|
4,
|
|
|
|
24,
|
2015-09-14 10:05:18 +00:00
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex - 2), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(1), U8(vector->GetIndex(slot2)), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex - 1), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(3), //
|
|
|
|
B(Call), R(0), R(1), U8(2), //
|
|
|
|
B(Return) //
|
2015-09-14 10:05:18 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"func"}},
|
|
|
|
{"function f(a, b) { return a.func(b + b, b); }\nf(" FUNC_ARG ", 1)",
|
|
|
|
4 * kPointerSize,
|
|
|
|
3,
|
|
|
|
30,
|
|
|
|
{
|
2015-10-01 13:48:05 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex - 1), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(LoadIC), R(1), U8(vector->GetIndex(slot2)), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Add), R(2), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(3), //
|
|
|
|
B(Call), R(0), R(1), U8(2), //
|
|
|
|
B(Return) //
|
2015-09-14 10:05:18 +00:00
|
|
|
},
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{"func"}}};
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-28 18:05:56 +00:00
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
|
2015-09-24 11:48:22 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LoadGlobal) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"var a = 1;\nfunction f() { return a; }\nf()",
|
|
|
|
0, 1, 3,
|
|
|
|
{
|
|
|
|
B(LdaGlobal), _,
|
|
|
|
B(Return)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"function t() { }\nfunction f() { return t; }\nf()",
|
|
|
|
0, 1, 3,
|
|
|
|
{
|
|
|
|
B(LdaGlobal), _,
|
|
|
|
B(Return)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, "f");
|
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CallGlobal) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
ExpectedSnippet<const char*> snippets[] = {
|
|
|
|
{"function t() { }\nfunction f() { return t(); }\nf()",
|
|
|
|
2 * kPointerSize, 1, 12,
|
|
|
|
{
|
|
|
|
B(LdaUndefined),
|
|
|
|
B(Star), R(1),
|
|
|
|
B(LdaGlobal), _,
|
|
|
|
B(Star), R(0),
|
|
|
|
B(Call), R(0), R(1), U8(0),
|
|
|
|
B(Return)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"function t(a, b, c) { }\nfunction f() { return t(1, 2, 3); }\nf()",
|
|
|
|
5 * kPointerSize, 1, 24,
|
|
|
|
{
|
|
|
|
B(LdaUndefined),
|
|
|
|
B(Star), R(1),
|
|
|
|
B(LdaGlobal), _,
|
|
|
|
B(Star), R(0),
|
|
|
|
B(LdaSmi8), U8(1),
|
|
|
|
B(Star), R(2),
|
|
|
|
B(LdaSmi8), U8(2),
|
|
|
|
B(Star), R(3),
|
|
|
|
B(LdaSmi8), U8(3),
|
|
|
|
B(Star), R(4),
|
|
|
|
B(Call), R(0), R(1), U8(3),
|
|
|
|
B(Return)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
2015-09-24 11:48:22 +00:00
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, "f");
|
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array, true);
|
2015-09-14 10:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
TEST(IfConditions) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
Handle<Object> unused = helper.factory()->undefined_value();
|
|
|
|
|
|
|
|
ExpectedSnippet<Handle<Object>> snippets[] = {
|
2015-09-28 18:05:56 +00:00
|
|
|
{"function f() { if (0) { return 1; } else { return -1; } } f()",
|
2015-09-24 15:20:47 +00:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
14,
|
|
|
|
{B(LdaZero), //
|
|
|
|
B(ToBoolean), //
|
|
|
|
B(JumpIfFalse), U8(7), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Jump), U8(5), //
|
2015-09-24 15:20:47 +00:00
|
|
|
B(LdaSmi8), U8(-1), //
|
|
|
|
B(Return), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
|
|
|
0,
|
|
|
|
{unused, unused, unused, unused}},
|
2015-09-28 18:05:56 +00:00
|
|
|
{"function f() { if ('lucky') { return 1; } else { return -1; } } f();",
|
2015-09-24 15:20:47 +00:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
15,
|
|
|
|
{B(LdaConstant), U8(0), //
|
|
|
|
B(ToBoolean), //
|
|
|
|
B(JumpIfFalse), U8(7), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Jump), U8(5), //
|
2015-09-24 15:20:47 +00:00
|
|
|
B(LdaSmi8), U8(-1), //
|
|
|
|
B(Return), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
|
|
|
1,
|
2015-09-28 18:05:56 +00:00
|
|
|
{helper.factory()->NewStringFromStaticChars("lucky"), unused, unused,
|
|
|
|
unused}},
|
|
|
|
{"function f() { if (false) { return 1; } else { return -1; } } f();",
|
2015-09-24 15:20:47 +00:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
13,
|
|
|
|
{B(LdaFalse), //
|
|
|
|
B(JumpIfFalse), U8(7), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Jump), U8(5), //
|
2015-09-24 15:20:47 +00:00
|
|
|
B(LdaSmi8), U8(-1), //
|
|
|
|
B(Return), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
|
|
|
0,
|
|
|
|
{unused, unused, unused, unused}},
|
2015-09-28 18:05:56 +00:00
|
|
|
{"function f(a) { if (a <= 0) { return 200; } else { return -200; } }"
|
|
|
|
"f(99);",
|
2015-09-24 15:20:47 +00:00
|
|
|
kPointerSize,
|
|
|
|
2,
|
|
|
|
19,
|
2015-10-01 13:33:10 +00:00
|
|
|
{B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaZero), //
|
|
|
|
B(TestLessThanOrEqual), R(0), //
|
|
|
|
B(JumpIfFalse), U8(7), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Jump), U8(5), //
|
|
|
|
B(LdaConstant), U8(1), //
|
|
|
|
B(Return), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
2015-09-24 15:20:47 +00:00
|
|
|
2,
|
|
|
|
{helper.factory()->NewNumberFromInt(200),
|
|
|
|
helper.factory()->NewNumberFromInt(-200), unused, unused}},
|
2015-09-28 18:05:56 +00:00
|
|
|
{"function f(a, b) { if (a in b) { return 200; } }"
|
|
|
|
"f('prop', { prop: 'yes'});",
|
2015-09-24 15:20:47 +00:00
|
|
|
kPointerSize,
|
|
|
|
3,
|
2015-10-01 15:04:09 +00:00
|
|
|
15,
|
2015-10-01 13:33:10 +00:00
|
|
|
{B(Ldar), R(helper.kLastParamIndex - 1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), //
|
|
|
|
B(TestIn), R(0), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(JumpIfFalse), U8(5), //
|
2015-10-01 13:33:10 +00:00
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
2015-09-24 15:20:47 +00:00
|
|
|
1,
|
|
|
|
{helper.factory()->NewNumberFromInt(200), unused, unused, unused}},
|
|
|
|
{"function f(z) { var a = 0; var b = 0; if (a === 0.01) { "
|
|
|
|
#define X "b = a; a = b; "
|
|
|
|
X X X X X X X X X X X X X X X X X X X X X X X X
|
|
|
|
#undef X
|
2015-09-28 18:05:56 +00:00
|
|
|
" return 200; } else { return -200; } } f(0.001)",
|
2015-09-24 15:20:47 +00:00
|
|
|
3 * kPointerSize,
|
|
|
|
2,
|
|
|
|
218,
|
|
|
|
{B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaConstant), U8(0), //
|
|
|
|
B(TestEqualStrict), R(2), //
|
|
|
|
B(JumpIfFalseConstant), U8(2), //
|
|
|
|
#define X B(Ldar), R(0), B(Star), R(1), B(Ldar), R(1), B(Star), R(0),
|
|
|
|
X X X X X X X X X X X X X X X X X X X X X X X X
|
|
|
|
#undef X
|
2015-10-01 15:04:09 +00:00
|
|
|
B(LdaConstant), U8(1), //
|
2015-09-24 15:20:47 +00:00
|
|
|
B(Return), //
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Jump), U8(5), //
|
2015-09-24 15:20:47 +00:00
|
|
|
B(LdaConstant), U8(3), //
|
|
|
|
B(Return), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return)}, //
|
|
|
|
4,
|
|
|
|
{helper.factory()->NewHeapNumber(0.01),
|
|
|
|
helper.factory()->NewNumberFromInt(200),
|
|
|
|
helper.factory()->NewNumberFromInt(199),
|
2015-09-28 18:05:56 +00:00
|
|
|
helper.factory()->NewNumberFromInt(-200)}},
|
|
|
|
{"function f(a, b) {\n"
|
|
|
|
" if (a == b) { return 1; }\n"
|
|
|
|
" if (a === b) { return 1; }\n"
|
|
|
|
" if (a < b) { return 1; }\n"
|
|
|
|
" if (a > b) { return 1; }\n"
|
|
|
|
" if (a <= b) { return 1; }\n"
|
|
|
|
" if (a >= b) { return 1; }\n"
|
|
|
|
" if (a in b) { return 1; }\n"
|
|
|
|
" if (a instanceof b) { return 1; }\n"
|
|
|
|
" /* if (a != b) { return 1; } */" // TODO(oth) Ast visitor yields
|
|
|
|
" /* if (a !== b) { return 1; } */" // UNARY NOT, rather than !=/!==.
|
|
|
|
" return 0;\n"
|
|
|
|
"} f(1, 1);",
|
|
|
|
kPointerSize,
|
|
|
|
3,
|
2015-10-01 15:04:09 +00:00
|
|
|
106,
|
2015-09-28 18:05:56 +00:00
|
|
|
{
|
2015-10-01 15:04:09 +00:00
|
|
|
#define IF_CONDITION_RETURN(condition) \
|
2015-10-01 13:33:10 +00:00
|
|
|
B(Ldar), R(helper.kLastParamIndex - 1), \
|
|
|
|
B(Star), R(0), \
|
|
|
|
B(Ldar), R(helper.kLastParamIndex), \
|
|
|
|
B(condition), R(0), \
|
2015-10-01 15:04:09 +00:00
|
|
|
B(JumpIfFalse), U8(5), \
|
2015-10-01 13:33:10 +00:00
|
|
|
B(LdaSmi8), U8(1), \
|
2015-10-01 15:04:09 +00:00
|
|
|
B(Return),
|
2015-09-28 18:05:56 +00:00
|
|
|
IF_CONDITION_RETURN(TestEqual) //
|
|
|
|
IF_CONDITION_RETURN(TestEqualStrict) //
|
|
|
|
IF_CONDITION_RETURN(TestLessThan) //
|
|
|
|
IF_CONDITION_RETURN(TestGreaterThan) //
|
|
|
|
IF_CONDITION_RETURN(TestLessThanOrEqual) //
|
|
|
|
IF_CONDITION_RETURN(TestGreaterThanOrEqual) //
|
|
|
|
IF_CONDITION_RETURN(TestIn) //
|
|
|
|
IF_CONDITION_RETURN(TestInstanceOf) //
|
|
|
|
#undef IF_CONDITION_RETURN
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Return)}, //
|
|
|
|
0,
|
|
|
|
{unused, unused, unused, unused}},
|
|
|
|
};
|
2015-09-24 15:20:47 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
|
|
|
Handle<BytecodeArray> bytecode_array =
|
2015-09-28 18:05:56 +00:00
|
|
|
helper.MakeBytecode(snippets[i].code_snippet, helper.kFunctionName);
|
2015-09-24 15:20:47 +00:00
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-01 15:04:09 +00:00
|
|
|
TEST(BasicLoops) {
|
|
|
|
InitializedHandleScope handle_scope;
|
|
|
|
BytecodeGeneratorHelper helper;
|
|
|
|
|
|
|
|
ExpectedSnippet<int> snippets[] = {
|
|
|
|
{"var x = 0;"
|
|
|
|
"var y = 1;"
|
|
|
|
"while (x < 10) {"
|
|
|
|
" y = y * 10;"
|
|
|
|
" x = x + 1;"
|
|
|
|
"}"
|
|
|
|
"return y;",
|
|
|
|
3 * kPointerSize,
|
|
|
|
1,
|
|
|
|
42,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Jump), U8(22), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(10), //
|
|
|
|
B(Mul), R(2), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(2), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(10), //
|
|
|
|
B(TestLessThan), R(2), //
|
|
|
|
B(JumpIfTrue), U8(-28), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Return), //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
{"var i = 0;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (i < 0) continue;"
|
|
|
|
" if (i == 3) break;"
|
|
|
|
" if (i == 4) break;"
|
|
|
|
" if (i == 10) continue;"
|
|
|
|
" if (i == 5) break;"
|
|
|
|
" i = i + 1;"
|
|
|
|
"}"
|
|
|
|
"return i;",
|
|
|
|
2 * kPointerSize,
|
|
|
|
1,
|
|
|
|
80,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Jump), U8(71), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaZero), //
|
|
|
|
B(TestLessThan), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(60), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(3), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(51), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(4), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(39), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(10), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(24), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(5), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(15), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaTrue), //
|
|
|
|
B(JumpIfTrue), U8(-70), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
{"var x = 0; var y = 1;"
|
|
|
|
"do {"
|
|
|
|
" y = y * 10;"
|
|
|
|
" if (x == 5) break;"
|
|
|
|
" if (x == 6) continue;"
|
|
|
|
" x = x + 1;"
|
|
|
|
"} while (x < 10);"
|
|
|
|
"return y;",
|
|
|
|
3 * kPointerSize,
|
|
|
|
1,
|
|
|
|
64,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(10), //
|
|
|
|
B(Mul), R(2), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(5), //
|
|
|
|
B(TestEqual), R(2), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(34), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(6), //
|
|
|
|
B(TestEqual), R(2), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(12), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(2), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(10), //
|
|
|
|
B(TestLessThan), R(2), //
|
|
|
|
B(JumpIfTrue), U8(-52), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Return) //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
{"var x = 0; "
|
|
|
|
"for(;;) {"
|
|
|
|
" if (x == 1) break;"
|
|
|
|
" x = x + 1;"
|
|
|
|
"}",
|
|
|
|
2 * kPointerSize,
|
|
|
|
1,
|
|
|
|
29,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), //
|
|
|
|
U8(1), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(14), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Jump), U8(-22), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return), //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
{"var u = 0;"
|
|
|
|
"for(var i = 0; i < 100; i = i + 1) {"
|
|
|
|
" u = u + 1;"
|
|
|
|
" continue;"
|
|
|
|
"}",
|
|
|
|
3 * kPointerSize,
|
|
|
|
1,
|
|
|
|
42,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Jump), U8(24), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(2), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Jump), U8(2), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(2), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(Ldar), R(1), //
|
|
|
|
B(Star), R(2), //
|
|
|
|
B(LdaSmi8), U8(100), //
|
|
|
|
B(TestLessThan), R(2), //
|
|
|
|
B(JumpIfTrue), U8(-30), //
|
|
|
|
B(LdaUndefined), //
|
|
|
|
B(Return), //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
{"var i = 0;"
|
|
|
|
"while(true) {"
|
|
|
|
" while (i < 3) {"
|
|
|
|
" if (i == 2) break;"
|
|
|
|
" i = i + 1;"
|
|
|
|
" }"
|
|
|
|
" i = i + 1;"
|
|
|
|
" break;"
|
|
|
|
"}"
|
|
|
|
"return i;",
|
|
|
|
2 * kPointerSize,
|
|
|
|
1,
|
|
|
|
57,
|
|
|
|
{
|
|
|
|
B(LdaZero), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Jump), U8(48), //
|
|
|
|
B(Jump), U8(24), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(2), //
|
|
|
|
B(TestEqual), R(1), //
|
|
|
|
B(JumpIfFalse), U8(4), //
|
|
|
|
B(Jump), U8(22), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(3), //
|
|
|
|
B(TestLessThan), R(1), //
|
|
|
|
B(JumpIfTrue), U8(-30), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Star), R(1), //
|
|
|
|
B(LdaSmi8), U8(1), //
|
|
|
|
B(Add), R(1), //
|
|
|
|
B(Star), R(0), //
|
|
|
|
B(Jump), U8(5), //
|
|
|
|
B(LdaTrue), //
|
|
|
|
B(JumpIfTrue), U8(-47), //
|
|
|
|
B(Ldar), R(0), //
|
|
|
|
B(Return), //
|
|
|
|
},
|
|
|
|
0},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(snippets); i++) {
|
|
|
|
Handle<BytecodeArray> bytecode_array =
|
|
|
|
helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet);
|
|
|
|
CheckBytecodeArrayEqual(snippets[i], bytecode_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 13:46:43 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace v8
|