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"
|
2020-09-30 17:40:39 +00:00
|
|
|
#include "src/objects/js-function.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 {
|
|
|
|
|
2020-07-10 13:52:47 +00:00
|
|
|
template <class T>
|
|
|
|
using TVariable = TypedCodeAssemblerVariable<T>;
|
2017-07-18 13:45:08 +00:00
|
|
|
|
2019-11-27 17:32:26 +00:00
|
|
|
TNode<Smi> SmiTag(CodeAssembler* m, Node* value) {
|
2016-06-17 13:19:58 +00:00
|
|
|
int32_t constant_value;
|
2019-09-10 01:19:59 +00:00
|
|
|
if (m->ToInt32Constant(value, &constant_value) &&
|
2016-06-17 13:19:58 +00:00
|
|
|
Smi::IsValid(constant_value)) {
|
2019-09-10 01:19:59 +00:00
|
|
|
return m->SmiConstant(Smi::FromInt(constant_value));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
2019-11-27 17:32:26 +00:00
|
|
|
return m->BitcastWordToTaggedSigned(
|
|
|
|
m->WordShl(value, m->IntPtrConstant(kSmiShiftSize + kSmiTagSize)));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Node* UndefinedConstant(CodeAssembler* m) {
|
|
|
|
return m->LoadRoot(RootIndex::kUndefinedValue);
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Node* LoadObjectField(CodeAssembler* m, Node* object, int offset,
|
2019-05-10 10:10:34 +00:00
|
|
|
MachineType type = MachineType::AnyTagged()) {
|
2019-09-10 01:19:59 +00:00
|
|
|
return m->Load(type, object, m->IntPtrConstant(offset - kHeapObjectTag));
|
2016-06-17 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Node* LoadMap(CodeAssembler* m, 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());
|
2019-09-10 01:19:59 +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()));
|
2020-02-14 15:32:05 +00:00
|
|
|
TNode<Smi> 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()));
|
2020-02-14 15:32:05 +00:00
|
|
|
TNode<Smi> 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()));
|
2020-02-14 15:32:05 +00:00
|
|
|
TNode<Smi> a = SmiTag(&m, m.Int32Constant(2));
|
|
|
|
TNode<Smi> 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()));
|
2020-02-14 15:32:05 +00:00
|
|
|
TNode<Smi> a = SmiTag(&m, m.Int32Constant(2));
|
|
|
|
TNode<Smi> 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-09-10 01:19:59 +00:00
|
|
|
Handle<JSFunction> CreateSumAllArgumentsFunction(FunctionTester* ft) {
|
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"
|
|
|
|
"})";
|
2019-09-10 01:19:59 +00:00
|
|
|
return ft->NewFunction(source);
|
2016-06-17 13:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction0Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 1;
|
2020-07-15 11:03:22 +00:00
|
|
|
const int kContextOffset = kNumParams + 3;
|
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams + 1); // Include receiver.
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
2020-09-30 17:40:39 +00:00
|
|
|
auto function = m.Parameter<JSFunction>(1);
|
|
|
|
auto context = m.Parameter<Context>(kContextOffset);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
2020-09-30 17:40:39 +00:00
|
|
|
auto receiver = SmiTag(&m, m.Int32Constant(42));
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
2019-11-27 17:32:26 +00:00
|
|
|
TNode<Object> result = m.CallJS(callable, context, function, receiver);
|
2016-06-17 13:49:26 +00:00
|
|
|
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
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
|
2016-06-17 13:49:26 +00:00
|
|
|
MaybeHandle<Object> result = ft.Call(sum);
|
|
|
|
CHECK_EQ(Smi::FromInt(42), *result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction1Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
2020-07-15 11:03:22 +00:00
|
|
|
const int kNumParams = 1;
|
|
|
|
const int kContextOffset = kNumParams + 3;
|
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams + 1); // Include receiver.
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
2020-09-30 17:40:39 +00:00
|
|
|
auto function = m.Parameter<JSFunction>(1);
|
|
|
|
auto context = m.Parameter<Context>(kContextOffset);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Node* receiver = SmiTag(&m, m.Int32Constant(42));
|
|
|
|
Node* a = SmiTag(&m, m.Int32Constant(13));
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
2019-11-27 17:32:26 +00:00
|
|
|
TNode<Object> result = m.CallJS(callable, context, function, receiver, a);
|
2016-06-17 13:49:26 +00:00
|
|
|
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
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
|
2016-06-17 13:49:26 +00:00
|
|
|
MaybeHandle<Object> result = ft.Call(sum);
|
|
|
|
CHECK_EQ(Smi::FromInt(55), *result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SimpleCallJSFunction2Arg) {
|
|
|
|
Isolate* isolate(CcTest::InitIsolateOnce());
|
|
|
|
const int kNumParams = 2;
|
2020-07-15 11:03:22 +00:00
|
|
|
const int kContextOffset = kNumParams + 3;
|
|
|
|
CodeAssemblerTester asm_tester(isolate, kNumParams + 1); // Include receiver.
|
2017-07-18 13:45:08 +00:00
|
|
|
CodeAssembler m(asm_tester.state());
|
2016-06-17 13:49:26 +00:00
|
|
|
{
|
2020-09-30 17:40:39 +00:00
|
|
|
auto function = m.Parameter<JSFunction>(1);
|
|
|
|
auto context = m.Parameter<Context>(kContextOffset);
|
2016-06-17 13:49:26 +00:00
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Node* receiver = SmiTag(&m, m.Int32Constant(42));
|
|
|
|
Node* a = SmiTag(&m, m.Int32Constant(13));
|
|
|
|
Node* b = SmiTag(&m, m.Int32Constant(153));
|
2016-06-17 13:49:26 +00:00
|
|
|
|
|
|
|
Callable callable = CodeFactory::Call(isolate);
|
2019-11-27 17:32:26 +00:00
|
|
|
TNode<Object> result =
|
|
|
|
m.CallJS(callable, context, function, receiver, a, b);
|
2016-06-17 13:49:26 +00:00
|
|
|
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
|
|
|
|
2019-09-10 01:19:59 +00:00
|
|
|
Handle<JSFunction> sum = CreateSumAllArgumentsFunction(&ft);
|
2016-06-17 13:49:26 +00:00
|
|
|
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());
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Int32T> var1(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp;
|
2016-06-17 13:19:58 +00:00
|
|
|
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());
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Int32T> var1(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp;
|
2016-06-17 13:19:58 +00:00
|
|
|
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);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp2;
|
2016-06-17 13:19:58 +00:00
|
|
|
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());
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Int32T> var1(&m);
|
|
|
|
TVariable<Int32T> var2(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), merge(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp;
|
|
|
|
var2 = temp;
|
2016-06-17 13:19:58 +00:00
|
|
|
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);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp2;
|
2016-06-17 13:19:58 +00:00
|
|
|
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());
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Int32T> var1(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), merge(&m, &var1), end(&m);
|
2019-08-22 09:56:32 +00:00
|
|
|
TNode<Int32T> temp = m.Int32Constant(0);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp;
|
2016-06-17 13:19:58 +00:00
|
|
|
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);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp2;
|
2016-06-17 13:19:58 +00:00
|
|
|
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());
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Smi> var1(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), default_label(&m);
|
|
|
|
CodeAssemblerLabel* 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);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = 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);
|
2020-07-10 13:52:47 +00:00
|
|
|
var1 = temp2;
|
2017-08-30 20:28:04 +00:00
|
|
|
m.Goto(&default_label);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Bind(&default_label);
|
2020-07-10 13:52:47 +00:00
|
|
|
CHECK_EQ(IrOpcode::kPhi, (*var1.value()).opcode());
|
|
|
|
CHECK_EQ(2, (*var1.value()).op()->ValueInputCount());
|
2017-08-30 20:28:04 +00:00
|
|
|
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());
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel 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());
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel l1(&m), l2(&m), l3(&m), default_label(&m);
|
|
|
|
CodeAssemblerLabel* 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
|
|
|
|
2019-09-10 01:19:59 +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
|
|
|
|
2019-09-10 01:19:59 +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());
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel block1(&m, CodeAssemblerLabel::kDeferred);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Goto(&block1);
|
|
|
|
m.Bind(&block1);
|
|
|
|
{
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Map> var_object(&m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel loop(&m, &var_object);
|
2020-07-10 13:52:47 +00:00
|
|
|
var_object = m.CAST(LoadMap(&m, m.SmiConstant(0)));
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Goto(&loop);
|
|
|
|
m.Bind(&loop);
|
|
|
|
{
|
2020-07-10 13:52:47 +00:00
|
|
|
TNode<Map> map = m.CAST(LoadMap(&m, var_object.value()));
|
|
|
|
var_object = map;
|
2016-06-17 13:19:58 +00:00
|
|
|
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());
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel block1(&m);
|
|
|
|
CodeAssemblerLabel block2(&m);
|
|
|
|
CodeAssemblerLabel block3(&m);
|
|
|
|
CodeAssemblerLabel block4(&m);
|
2020-09-30 17:40:39 +00:00
|
|
|
m.Branch(m.WordEqual(m.UncheckedParameter<IntPtrT>(0), m.IntPtrConstant(0)),
|
2017-08-03 14:27:11 +00:00
|
|
|
&block1, &block4);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Bind(&block4);
|
|
|
|
{
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<IntPtrT> var_object(&m);
|
2020-09-30 17:40:39 +00:00
|
|
|
m.Branch(m.WordEqual(m.UncheckedParameter<IntPtrT>(0), m.IntPtrConstant(0)),
|
2017-08-03 14:27:11 +00:00
|
|
|
&block2, &block3);
|
2016-06-17 13:19:58 +00:00
|
|
|
|
|
|
|
m.Bind(&block2);
|
2020-07-10 13:52:47 +00:00
|
|
|
var_object = m.IntPtrConstant(55);
|
2016-06-17 13:19:58 +00:00
|
|
|
m.Goto(&block1);
|
|
|
|
|
|
|
|
m.Bind(&block3);
|
2020-07-10 13:52:47 +00:00
|
|
|
var_object = m.IntPtrConstant(66);
|
2016-06-17 13:19:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
2020-07-10 13:52:47 +00:00
|
|
|
TVariable<Object> var(m.SmiConstant(0), &m);
|
2019-09-05 22:10:56 +00:00
|
|
|
CodeAssemblerLabel exception(&m, {&var}, CodeAssemblerLabel::kDeferred);
|
2018-11-13 14:01:35 +00:00
|
|
|
{
|
2020-02-18 10:58:49 +00:00
|
|
|
ScopedExceptionHandler 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
|