2016-06-17 13:19:58 +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.
|
|
|
|
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/code-factory.h"
|
2016-06-17 13:19:58 +00:00
|
|
|
#include "src/compiler/code-assembler.h"
|
2017-08-30 20:28:04 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
|
|
|
#include "src/compiler/opcodes.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2018-12-17 17:01:48 +00:00
|
|
|
#include "src/objects/heap-number-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2016-06-17 13:19:58 +00:00
|
|
|
#include "test/cctest/compiler/code-assembler-tester.h"
|
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-05-27 11:31:49 +00:00
|
|
|
using Label = CodeAssemblerLabel;
|
|
|
|
using Variable = CodeAssemblerVariable;
|
2017-07-18 13:45:08 +00:00
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Node* SmiTag(CodeAssembler& m, // NOLINT(runtime/references)
|
|
|
|
Node* value) {
|
2016-06-17 13:19:58 +00:00
|
|
|
int32_t constant_value;
|
2019-07-25 18:53:58 +00:00
|
|
|
if (m.ToInt32Constant(value, &constant_value) &&
|
2016-06-17 13:19:58 +00:00
|
|
|
Smi::IsValid(constant_value)) {
|
|
|
|
return m.SmiConstant(Smi::FromInt(constant_value));
|
|
|
|
}
|
2016-08-12 12:57:31 +00:00
|
|
|
return m.WordShl(value, m.IntPtrConstant(kSmiShiftSize + kSmiTagSize));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Node* UndefinedConstant(CodeAssembler& m) { // NOLINT(runtime/references)
|
2018-09-20 10:14:46 +00:00
|
|
|
return m.LoadRoot(RootIndex::kUndefinedValue);
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Node* SmiFromInt32(CodeAssembler& m, // NOLINT(runtime/references)
|
|
|
|
Node* value) {
|
2016-12-15 11:45:18 +00:00
|
|
|
value = m.ChangeInt32ToIntPtr(value);
|
|
|
|
return m.BitcastWordToTaggedSigned(
|
|
|
|
m.WordShl(value, kSmiShiftSize + kSmiTagSize));
|
|
|
|
}
|
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Node* LoadObjectField(CodeAssembler& m, // NOLINT(runtime/references)
|
|
|
|
Node* object, int offset,
|
2019-05-10 10:10:34 +00:00
|
|
|
MachineType type = MachineType::AnyTagged()) {
|
|
|
|
return m.Load(type, object, m.IntPtrConstant(offset - kHeapObjectTag));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Node* LoadMap(CodeAssembler& m, // NOLINT(runtime/references)
|
|
|
|
Node* object) {
|
2016-12-15 11:45:18 +00:00
|
|
|
return LoadObjectField(m, object, JSObject::kMapOffset);
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:19:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SimpleSmiReturn) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Return(SmiTag(m, m.Int32Constant(37)));
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
|
|
|
CHECK_EQ(37, ft.CallChecked<Smi>()->value());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleIntPtrReturn) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
int test;
|
2016-12-15 11:45:18 +00:00
|
|
|
m.Return(m.BitcastWordToTagged(
|
|
|
|
m.IntPtrConstant(reinterpret_cast<intptr_t>(&test))));
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
2016-06-17 13:19:58 +00:00
|
|
|
MaybeHandle<Object> result = ft.Call();
|
2018-12-25 00:19:47 +00:00
|
|
|
CHECK_EQ(reinterpret_cast<Address>(&test), result.ToHandleChecked()->ptr());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleDoubleReturn) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Return(m.NumberConstant(0.5));
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
|
|
|
CHECK_EQ(0.5, ft.CallChecked<HeapNumber>()->value());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallRuntime1Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2016-06-17 13:19:58 +00:00
|
|
|
Node* b = SmiTag(m, m.Int32Constant(0));
|
2018-04-17 00:59:05 +00:00
|
|
|
m.Return(m.CallRuntime(Runtime::kIsSmi, context, b));
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
2018-04-17 00:59:05 +00:00
|
|
|
CHECK(ft.CallChecked<Oddball>().is_identical_to(
|
|
|
|
isolate->factory()->true_value()));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleTailCallRuntime1Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2016-06-17 13:19:58 +00:00
|
|
|
Node* b = SmiTag(m, m.Int32Constant(0));
|
2018-04-17 00:59:05 +00:00
|
|
|
m.TailCallRuntime(Runtime::kIsSmi, context, b);
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
2018-04-17 00:59:05 +00:00
|
|
|
CHECK(ft.CallChecked<Oddball>().is_identical_to(
|
|
|
|
isolate->factory()->true_value()));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallRuntime2Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2016-06-17 13:19:58 +00:00
|
|
|
Node* a = SmiTag(m, m.Int32Constant(2));
|
|
|
|
Node* b = SmiTag(m, m.Int32Constant(4));
|
2016-06-28 10:23:58 +00:00
|
|
|
m.Return(m.CallRuntime(Runtime::kAdd, context, a, b));
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
|
|
|
CHECK_EQ(6, ft.CallChecked<Smi>()->value());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleTailCallRuntime2Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2016-06-17 13:19:58 +00:00
|
|
|
Node* a = SmiTag(m, m.Int32Constant(2));
|
|
|
|
Node* b = SmiTag(m, m.Int32Constant(4));
|
2016-06-28 10:23:58 +00:00
|
|
|
m.TailCallRuntime(Runtime::kAdd, context, a, b);
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode());
|
|
|
|
CHECK_EQ(6, ft.CallChecked<Smi>()->value());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 13:49:26 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-07-08 08:42:48 +00:00
|
|
|
Handle<JSFunction> CreateSumAllArgumentsFunction(
|
|
|
|
FunctionTester& ft) { // NOLINT(runtime/references)
|
2016-06-17 13:49:26 +00:00
|
|
|
const char* source =
|
|
|
|
"(function() {\n"
|
|
|
|
" var sum = 0 + this;\n"
|
|
|
|
" for (var i = 0; i < arguments.length; i++) {\n"
|
|
|
|
" sum += arguments[i];\n"
|
|
|
|
" }\n"
|
|
|
|
" return sum;\n"
|
|
|
|
"})";
|
|
|
|
return ft.NewFunction(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction0Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 1;
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
|
|
|
Node* function = m.Parameter(0);
|
|
|
|
Node* context = m.Parameter(kNumParams + 2);
|
|
|
|
|
|
|
|
Node* receiver = SmiTag(m, m.Int32Constant(42));
|
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
|
|
|
Node* result = m.CallJS(callable, context, function, receiver);
|
|
|
|
m.Return(result);
|
|
|
|
}
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft);
|
|
|
|
MaybeHandle<Object> result = ft.Call(sum);
|
|
|
|
CHECK_EQ(Smi::FromInt(42), *result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction1Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 2;
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
|
|
|
Node* function = m.Parameter(0);
|
|
|
|
Node* context = m.Parameter(1);
|
|
|
|
|
|
|
|
Node* receiver = SmiTag(m, m.Int32Constant(42));
|
|
|
|
Node* a = SmiTag(m, m.Int32Constant(13));
|
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
|
|
|
Node* result = m.CallJS(callable, context, function, receiver, a);
|
|
|
|
m.Return(result);
|
|
|
|
}
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft);
|
|
|
|
MaybeHandle<Object> result = ft.Call(sum);
|
|
|
|
CHECK_EQ(Smi::FromInt(55), *result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction2Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 2;
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
|
|
|
Node* function = m.Parameter(0);
|
|
|
|
Node* context = m.Parameter(1);
|
|
|
|
|
|
|
|
Node* receiver = SmiTag(m, m.Int32Constant(42));
|
|
|
|
Node* a = SmiTag(m, m.Int32Constant(13));
|
|
|
|
Node* b = SmiTag(m, m.Int32Constant(153));
|
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
|
|
|
Node* result = m.CallJS(callable, context, function, receiver, a, b);
|
|
|
|
m.Return(result);
|
|
|
|
}
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(ft);
|
|
|
|
MaybeHandle<Object> result = ft.Call(sum);
|
|
|
|
CHECK_EQ(Smi::FromInt(208), *result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:19:58 +00:00
|
|
|
TEST(VariableMerge1) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Variable var1(&m, MachineRepresentation::kTagged);
|
|
|
|
Label l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp);
|
|
|
|
m.Branch(m.Int32Constant(1), &l1, &l2);
|
|
|
|
m.Bind(&l1);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&l2);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&merge);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VariableMerge2) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Variable var1(&m, MachineRepresentation::kTagged);
|
|
|
|
Label l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp);
|
|
|
|
m.Branch(m.Int32Constant(1), &l1, &l2);
|
|
|
|
m.Bind(&l1);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&l2);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp2 = m.Int32Constant(2);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp2);
|
|
|
|
CHECK_EQ(var1.value(), temp2);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&merge);
|
|
|
|
CHECK_NE(var1.value(), temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VariableMerge3) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Variable var1(&m, MachineRepresentation::kTagged);
|
|
|
|
Variable var2(&m, MachineRepresentation::kTagged);
|
|
|
|
Label l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp);
|
|
|
|
var2.Bind(temp);
|
|
|
|
m.Branch(m.Int32Constant(1), &l1, &l2);
|
|
|
|
m.Bind(&l1);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&l2);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp2 = m.Int32Constant(2);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp2);
|
|
|
|
CHECK_EQ(var1.value(), temp2);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&merge);
|
|
|
|
CHECK_NE(var1.value(), temp);
|
|
|
|
CHECK_NE(var1.value(), temp2);
|
|
|
|
CHECK_EQ(var2.value(), temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VariableMergeBindFirst) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Variable var1(&m, MachineRepresentation::kTagged);
|
|
|
|
Label l1(&m), l2(&m), merge(&m, &var1), end(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp);
|
|
|
|
m.Branch(m.Int32Constant(1), &l1, &l2);
|
|
|
|
m.Bind(&l1);
|
|
|
|
CHECK_EQ(var1.value(), temp);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&merge);
|
|
|
|
CHECK(var1.value() != temp);
|
2017-09-25 09:45:55 +00:00
|
|
|
CHECK_NOT_NULL(var1.value());
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Goto(&end);
|
|
|
|
m.Bind(&l2);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp2 = m.Int32Constant(2);
|
2016-06-17 13:19:58 +00:00
|
|
|
var1.Bind(temp2);
|
|
|
|
CHECK_EQ(var1.value(), temp2);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&end);
|
|
|
|
CHECK(var1.value() != temp);
|
2017-09-25 09:45:55 +00:00
|
|
|
CHECK_NOT_NULL(var1.value());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VariableMergeSwitch) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Variable var1(&m, MachineRepresentation::kTagged);
|
|
|
|
Label l1(&m), l2(&m), default_label(&m);
|
|
|
|
Label* labels[] = {&l1, &l2};
|
2016-06-17 13:19:58 +00:00
|
|
|
int32_t values[] = {1, 2};
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Smi> temp1 = m.SmiConstant(0);
|
2017-08-30 20:28:04 +00:00
|
|
|
var1.Bind(temp1);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Switch(m.Int32Constant(2), &default_label, values, labels, 2);
|
|
|
|
m.Bind(&l1);
|
2017-08-30 20:28:04 +00:00
|
|
|
CHECK_EQ(temp1, var1.value());
|
|
|
|
m.Return(temp1);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Bind(&l2);
|
2017-08-30 20:28:04 +00:00
|
|
|
CHECK_EQ(temp1, var1.value());
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Smi> temp2 = m.SmiConstant(7);
|
2017-08-30 20:28:04 +00:00
|
|
|
var1.Bind(temp2);
|
|
|
|
m.Goto(&default_label);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Bind(&default_label);
|
2017-08-30 20:28:04 +00:00
|
|
|
CHECK_EQ(IrOpcode::kPhi, var1.value()->opcode());
|
|
|
|
CHECK_EQ(2, var1.value()->op()->ValueInputCount());
|
|
|
|
CHECK_EQ(temp1, NodeProperties::GetValueInput(var1.value(), 0));
|
|
|
|
CHECK_EQ(temp2, NodeProperties::GetValueInput(var1.value(), 1));
|
|
|
|
m.Return(temp1);
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SplitEdgeBranchMerge) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Label l1(&m), merge(&m);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Branch(m.Int32Constant(1), &l1, &merge);
|
|
|
|
m.Bind(&l1);
|
|
|
|
m.Goto(&merge);
|
|
|
|
m.Bind(&merge);
|
2017-07-18 13:45:08 +00:00
|
|
|
USE(asm_tester.GenerateCode());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SplitEdgeSwitchMerge) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
Label l1(&m), l2(&m), l3(&m), default_label(&m);
|
|
|
|
Label* labels[] = {&l1, &l2};
|
2016-06-17 13:19:58 +00:00
|
|
|
int32_t values[] = {1, 2};
|
|
|
|
m.Branch(m.Int32Constant(1), &l3, &l1);
|
|
|
|
m.Bind(&l3);
|
|
|
|
m.Switch(m.Int32Constant(2), &default_label, values, labels, 2);
|
|
|
|
m.Bind(&l1);
|
|
|
|
m.Goto(&l2);
|
|
|
|
m.Bind(&l2);
|
|
|
|
m.Goto(&default_label);
|
|
|
|
m.Bind(&default_label);
|
2017-07-18 13:45:08 +00:00
|
|
|
USE(asm_tester.GenerateCode());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestToConstant) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
int32_t value32;
|
|
|
|
int64_t value64;
|
|
|
|
Node* a = m.Int32Constant(5);
|
2019-07-25 18:53:58 +00:00
|
|
|
CHECK(m.ToInt32Constant(a, &value32));
|
|
|
|
CHECK(m.ToInt64Constant(a, &value64));
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
a = m.Int64Constant(static_cast<int64_t>(1) << 32);
|
2019-07-25 18:53:58 +00:00
|
|
|
CHECK(!m.ToInt32Constant(a, &value32));
|
|
|
|
CHECK(m.ToInt64Constant(a, &value64));
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
a = m.Int64Constant(13);
|
2019-07-25 18:53:58 +00:00
|
|
|
CHECK(m.ToInt32Constant(a, &value32));
|
|
|
|
CHECK(m.ToInt64Constant(a, &value64));
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
a = UndefinedConstant(m);
|
2019-07-25 18:53:58 +00:00
|
|
|
CHECK(!m.ToInt32Constant(a, &value32));
|
|
|
|
CHECK(!m.ToInt64Constant(a, &value64));
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
a = UndefinedConstant(m);
|
2019-07-25 18:53:58 +00:00
|
|
|
CHECK(!m.ToInt32Constant(a, &value32));
|
|
|
|
CHECK(!m.ToInt64Constant(a, &value64));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DeferredCodePhiHints) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
Label block1(&m, Label::kDeferred);
|
|
|
|
m.Goto(&block1);
|
|
|
|
m.Bind(&block1);
|
|
|
|
{
|
|
|
|
Variable var_object(&m, MachineRepresentation::kTagged);
|
|
|
|
Label loop(&m, &var_object);
|
2016-12-15 11:45:18 +00:00
|
|
|
var_object.Bind(m.SmiConstant(0));
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Goto(&loop);
|
|
|
|
m.Bind(&loop);
|
|
|
|
{
|
2016-12-15 11:45:18 +00:00
|
|
|
Node* map = LoadMap(m, var_object.value());
|
2016-06-17 13:19:58 +00:00
|
|
|
var_object.Bind(map);
|
|
|
|
m.Goto(&loop);
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 13:45:08 +00:00
|
|
|
CHECK(!asm_tester.GenerateCode().is_null());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestOutOfScopeVariable) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:19:58 +00:00
|
|
|
Label block1(&m);
|
|
|
|
Label block2(&m);
|
|
|
|
Label block3(&m);
|
|
|
|
Label block4(&m);
|
2017-08-03 14:27:11 +00:00
|
|
|
m.Branch(m.WordEqual(m.UncheckedCast<IntPtrT>(m.Parameter(0)),
|
|
|
|
m.IntPtrConstant(0)),
|
|
|
|
&block1, &block4);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Bind(&block4);
|
|
|
|
{
|
|
|
|
Variable var_object(&m, MachineRepresentation::kTagged);
|
2017-08-03 14:27:11 +00:00
|
|
|
m.Branch(m.WordEqual(m.UncheckedCast<IntPtrT>(m.Parameter(0)),
|
|
|
|
m.IntPtrConstant(0)),
|
|
|
|
&block2, &block3);
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
m.Bind(&block2);
|
|
|
|
var_object.Bind(m.IntPtrConstant(55));
|
|
|
|
m.Goto(&block1);
|
|
|
|
|
|
|
|
m.Bind(&block3);
|
|
|
|
var_object.Bind(m.IntPtrConstant(66));
|
|
|
|
m.Goto(&block1);
|
|
|
|
}
|
|
|
|
m.Bind(&block1);
|
2017-07-18 13:45:08 +00:00
|
|
|
CHECK(!asm_tester.GenerateCode().is_null());
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 11:45:18 +00:00
|
|
|
TEST(GotoIfException) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
|
|
|
|
const int kNumParams = 1;
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-12-15 11:45:18 +00:00
|
|
|
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
|
|
|
TNode<Symbol> to_string_tag =
|
2016-12-15 11:45:18 +00:00
|
|
|
m.HeapConstant(isolate->factory()->to_string_tag_symbol());
|
|
|
|
Variable exception(&m, MachineRepresentation::kTagged);
|
|
|
|
|
|
|
|
Label exception_handler(&m);
|
2017-05-31 12:40:13 +00:00
|
|
|
Callable to_string = Builtins::CallableFor(isolate, Builtins::kToString);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Object> string = m.CallStub(to_string, context, to_string_tag);
|
2016-12-15 11:45:18 +00:00
|
|
|
m.GotoIfException(string, &exception_handler, &exception);
|
|
|
|
m.Return(string);
|
|
|
|
|
|
|
|
m.Bind(&exception_handler);
|
|
|
|
m.Return(exception.value());
|
|
|
|
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
2016-12-15 11:45:18 +00:00
|
|
|
Handle<Object> result = ft.Call().ToHandleChecked();
|
|
|
|
|
|
|
|
// Should be a TypeError.
|
|
|
|
CHECK(result->IsJSObject());
|
|
|
|
|
|
|
|
Handle<Object> constructor =
|
2018-07-13 09:17:37 +00:00
|
|
|
Object::GetPropertyOrElement(isolate, result,
|
2016-12-15 11:45:18 +00:00
|
|
|
isolate->factory()->constructor_string())
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(constructor->SameValue(*isolate->type_error_function()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GotoIfExceptionMultiple) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
|
|
|
|
const int kNumParams = 4; // receiver, first, second, third
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-12-15 11:45:18 +00:00
|
|
|
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2016-12-15 11:45:18 +00:00
|
|
|
Node* first_value = m.Parameter(0);
|
|
|
|
Node* second_value = m.Parameter(1);
|
|
|
|
Node* third_value = m.Parameter(2);
|
|
|
|
|
|
|
|
Label exception_handler1(&m);
|
|
|
|
Label exception_handler2(&m);
|
|
|
|
Label exception_handler3(&m);
|
|
|
|
Variable return_value(&m, MachineRepresentation::kWord32);
|
|
|
|
Variable error(&m, MachineRepresentation::kTagged);
|
|
|
|
|
|
|
|
return_value.Bind(m.Int32Constant(0));
|
|
|
|
|
|
|
|
// try { return ToString(param1) } catch (e) { ... }
|
2017-05-31 12:40:13 +00:00
|
|
|
Callable to_string = Builtins::CallableFor(isolate, Builtins::kToString);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Object> string = m.CallStub(to_string, context, first_value);
|
2016-12-15 11:45:18 +00:00
|
|
|
m.GotoIfException(string, &exception_handler1, &error);
|
|
|
|
m.Return(string);
|
|
|
|
|
|
|
|
// try { ToString(param2); return 7 } catch (e) { ... }
|
|
|
|
m.Bind(&exception_handler1);
|
|
|
|
return_value.Bind(m.Int32Constant(7));
|
|
|
|
error.Bind(UndefinedConstant(m));
|
|
|
|
string = m.CallStub(to_string, context, second_value);
|
|
|
|
m.GotoIfException(string, &exception_handler2, &error);
|
2018-02-23 13:46:00 +00:00
|
|
|
m.Return(SmiFromInt32(m, return_value.value()));
|
2016-12-15 11:45:18 +00:00
|
|
|
|
|
|
|
// try { ToString(param3); return 7 & ~2; } catch (e) { return e; }
|
|
|
|
m.Bind(&exception_handler2);
|
|
|
|
// Return returnValue & ~2
|
|
|
|
error.Bind(UndefinedConstant(m));
|
|
|
|
string = m.CallStub(to_string, context, third_value);
|
|
|
|
m.GotoIfException(string, &exception_handler3, &error);
|
2018-02-23 13:46:00 +00:00
|
|
|
m.Return(SmiFromInt32(
|
2016-12-15 11:45:18 +00:00
|
|
|
m, m.Word32And(return_value.value(),
|
|
|
|
m.Word32Xor(m.Int32Constant(2), m.Int32Constant(-1)))));
|
|
|
|
|
|
|
|
m.Bind(&exception_handler3);
|
|
|
|
m.Return(error.value());
|
|
|
|
|
2017-07-18 13:45:08 +00:00
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
2016-12-15 11:45:18 +00:00
|
|
|
|
|
|
|
Handle<Object> result;
|
|
|
|
// First handler does not throw, returns result of first value.
|
|
|
|
result = ft.Call(isolate->factory()->undefined_value(),
|
|
|
|
isolate->factory()->to_string_tag_symbol())
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(String::cast(*result).IsOneByteEqualTo(OneByteVector("undefined")));
|
|
|
|
|
|
|
|
// First handler returns a number.
|
|
|
|
result = ft.Call(isolate->factory()->to_string_tag_symbol(),
|
|
|
|
isolate->factory()->undefined_value())
|
|
|
|
.ToHandleChecked();
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(7, Smi::ToInt(*result));
|
2016-12-15 11:45:18 +00:00
|
|
|
|
|
|
|
// First handler throws, second handler returns a number.
|
|
|
|
result = ft.Call(isolate->factory()->to_string_tag_symbol(),
|
|
|
|
isolate->factory()->to_primitive_symbol())
|
|
|
|
.ToHandleChecked();
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(7 & ~2, Smi::ToInt(*result));
|
2016-12-15 11:45:18 +00:00
|
|
|
|
|
|
|
// First handler throws, second handler throws, third handler returns thrown
|
|
|
|
// value.
|
|
|
|
result = ft.Call(isolate->factory()->to_string_tag_symbol(),
|
|
|
|
isolate->factory()->to_primitive_symbol(),
|
|
|
|
isolate->factory()->unscopables_symbol())
|
|
|
|
.ToHandleChecked();
|
|
|
|
|
|
|
|
// Should be a TypeError.
|
|
|
|
CHECK(result->IsJSObject());
|
|
|
|
|
|
|
|
Handle<Object> constructor =
|
2018-07-13 09:17:37 +00:00
|
|
|
Object::GetPropertyOrElement(isolate, result,
|
2016-12-15 11:45:18 +00:00
|
|
|
isolate->factory()->constructor_string())
|
|
|
|
.ToHandleChecked();
|
|
|
|
CHECK(constructor->SameValue(*isolate->type_error_function()));
|
|
|
|
}
|
|
|
|
|
2018-11-13 14:01:35 +00:00
|
|
|
TEST(ExceptionHandler) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 0;
|
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
|
|
|
|
CodeAssembler::TVariable<Object> var(m.SmiConstant(0), &m);
|
|
|
|
Label exception(&m, {&var}, Label::kDeferred);
|
|
|
|
{
|
|
|
|
CodeAssemblerScopedExceptionHandler handler(&m, &exception, &var);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Context> context =
|
|
|
|
m.HeapConstant(Handle<Context>(isolate->native_context()));
|
2018-11-13 14:01:35 +00:00
|
|
|
m.CallRuntime(Runtime::kThrow, context, m.SmiConstant(2));
|
|
|
|
}
|
|
|
|
m.Return(m.SmiConstant(1));
|
|
|
|
|
|
|
|
m.Bind(&exception);
|
|
|
|
m.Return(var.value());
|
|
|
|
|
|
|
|
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
|
|
|
|
CHECK_EQ(2, ft.CallChecked<Smi>()->value());
|
|
|
|
}
|
|
|
|
|
2018-12-13 19:30:56 +00:00
|
|
|
TEST(TestCodeAssemblerCodeComment) {
|
|
|
|
i::FLAG_code_comments = true;
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 0;
|
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
|
|
|
|
m.Comment("Comment1");
|
|
|
|
m.Return(m.SmiConstant(1));
|
|
|
|
|
|
|
|
Handle<Code> code = asm_tester.GenerateCode();
|
|
|
|
CHECK_NE(code->code_comments(), kNullAddress);
|
2019-04-10 11:55:01 +00:00
|
|
|
CodeCommentsIterator it(code->code_comments(), code->code_comments_size());
|
2018-12-13 19:30:56 +00:00
|
|
|
CHECK(it.HasCurrent());
|
|
|
|
bool found_comment = false;
|
|
|
|
while (it.HasCurrent()) {
|
|
|
|
if (strcmp(it.GetComment(), "Comment1") == 0) found_comment = true;
|
|
|
|
it.Next();
|
|
|
|
}
|
|
|
|
CHECK(found_comment);
|
|
|
|
}
|
|
|
|
|
2019-05-21 14:59:31 +00:00
|
|
|
TEST(StaticAssert) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
CodeAssemblerTester asm_tester(isolate);
|
|
|
|
CodeAssembler m(asm_tester.state());
|
|
|
|
m.StaticAssert(m.ReinterpretCast<BoolT>(m.Int32Constant(1)));
|
|
|
|
USE(asm_tester.GenerateCode());
|
|
|
|
}
|
|
|
|
|
2016-06-17 13:19:58 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|