Chromium Code Reviews| Index: test/unittests/compiler/bytecode-graph-builder-unittest.cc |
| diff --git a/test/unittests/compiler/bytecode-graph-builder-unittest.cc b/test/unittests/compiler/bytecode-graph-builder-unittest.cc |
| index 27ff4ca359a25a327d6518f192d780fffb164927..c131912d61a01aa7988e54f970babc8e4c8bb248 100644 |
| --- a/test/unittests/compiler/bytecode-graph-builder-unittest.cc |
| +++ b/test/unittests/compiler/bytecode-graph-builder-unittest.cc |
| @@ -11,7 +11,9 @@ |
| #include "src/compiler/instruction-selector.h" |
| #include "src/compiler/js-graph.h" |
| #include "src/compiler/js-operator.h" |
| +#include "src/compiler/linkage.h" |
| #include "src/interpreter/bytecode-array-builder.h" |
| +#include "src/interpreter/interpreter.h" |
| #include "src/parser.h" |
| #include "test/unittests/compiler/compiler-test-utils.h" |
| #include "test/unittests/compiler/graph-unittest.h" |
| @@ -24,17 +26,162 @@ namespace v8 { |
| namespace internal { |
| namespace compiler { |
| +#define SPACE() |
| + |
| +#define REPEAT_2(SEP, ...) __VA_ARGS__ SEP() __VA_ARGS__ |
| +#define REPEAT_4(SEP, ...) \ |
| + REPEAT_2(SEP, __VA_ARGS__) SEP() REPEAT_2(SEP, __VA_ARGS__) |
| +#define REPEAT_8(SEP, ...) \ |
| + REPEAT_4(SEP, __VA_ARGS__) SEP() REPEAT_4(SEP, __VA_ARGS__) |
| +#define REPEAT_16(SEP, ...) \ |
| + REPEAT_8(SEP, __VA_ARGS__) SEP() REPEAT_8(SEP, __VA_ARGS__) |
| +#define REPEAT_32(SEP, ...) \ |
| + REPEAT_16(SEP, __VA_ARGS__) SEP() REPEAT_16(SEP, __VA_ARGS__) |
| +#define REPEAT_64(SEP, ...) \ |
| + REPEAT_32(SEP, __VA_ARGS__) SEP() REPEAT_32(SEP, __VA_ARGS__) |
| +#define REPEAT_128(SEP, ...) \ |
| + REPEAT_64(SEP, __VA_ARGS__) SEP() REPEAT_64(SEP, __VA_ARGS__) |
| +#define REPEAT_256(SEP, ...) \ |
| + REPEAT_128(SEP, __VA_ARGS__) SEP() REPEAT_128(SEP, __VA_ARGS__) |
| + |
| +#define REPEAT_127(SEP, ...) \ |
| + REPEAT_64(SEP, __VA_ARGS__) \ |
| + SEP() \ |
| + REPEAT_32(SEP, __VA_ARGS__) \ |
| + SEP() \ |
| + REPEAT_16(SEP, __VA_ARGS__) \ |
| + SEP() \ |
| + REPEAT_8(SEP, __VA_ARGS__) \ |
| + SEP() \ |
| + REPEAT_4(SEP, __VA_ARGS__) SEP() REPEAT_2(SEP, __VA_ARGS__) SEP() __VA_ARGS__ |
| + |
| +static const char* kFunctionName = "f"; |
| + |
| +class GraphGeneratorHelper { |
| + public: |
| + GraphGeneratorHelper(Isolate* isolate, Zone* zone, const char* script, |
| + const char* function_name = kFunctionName) |
|
rmcilroy
2015/11/10 18:00:15
Do you think you'll ever need the function_name pa
mythria
2015/11/11 13:39:24
Done.
|
| + : GraphGeneratorHelper(isolate, zone, script, |
| + MaybeHandle<BytecodeArray>(), function_name) {} |
| + |
| + GraphGeneratorHelper(Isolate* isolate, Zone* zone, |
| + MaybeHandle<BytecodeArray> bytecode, |
| + const char* function_name = kFunctionName) |
|
rmcilroy
2015/11/10 18:00:15
No need for the function_name argument here - it'l
mythria
2015/11/11 13:39:24
Done.
|
| + : GraphGeneratorHelper(isolate, zone, nullptr, bytecode, function_name) {} |
| + |
| + Graph* GetCompletedGraph() { |
| + ParseInfo parse_info = GetParseInfo(); |
|
rmcilroy
2015/11/10 18:00:15
I'd just inline all of GetParseInfo() here and inc
mythria
2015/11/11 13:39:24
I did this more to avoid using a new to create Com
|
| + CompilationInfo info(&parse_info); |
| + if (!bytecode_.is_null()) { |
| + info.shared_info()->set_function_data(*bytecode_.ToHandleChecked()); |
| + } |
| + |
| + MachineOperatorBuilder* machine = new (zone_) MachineOperatorBuilder( |
| + zone_, kMachPtr, InstructionSelector::SupportedMachineOperatorFlags()); |
| + CommonOperatorBuilder* common = new (zone_) CommonOperatorBuilder(zone_); |
| + JSOperatorBuilder* javascript = new (zone_) JSOperatorBuilder(zone_); |
| + Graph* graph = new (zone_) Graph(zone_); |
| + JSGraph* jsgraph = new (zone_) |
| + JSGraph(isolate_, graph, common, javascript, nullptr, machine); |
| + |
| + BytecodeGraphBuilder graph_builder(zone_, &info, jsgraph); |
| + graph_builder.CreateGraph(); |
| + return graph; |
| + } |
| + |
| + static Handle<String> GetName(Isolate* isolate, const char* name) { |
| + Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(name); |
| + return isolate->factory()->string_table()->LookupString(isolate, result); |
| + } |
| + |
| + private: |
| + Isolate* isolate_; |
| + Zone* zone_; |
| + const char* script_; |
| + MaybeHandle<BytecodeArray> bytecode_; |
| + const char* function_name_; |
|
rmcilroy
2015/11/10 18:00:15
Fields go below private functions: https://google.
mythria
2015/11/11 13:39:24
Done.
|
| + |
| + GraphGeneratorHelper(Isolate* isolate, Zone* zone, const char* script, |
| + MaybeHandle<BytecodeArray> bytecode, |
| + const char* function_name) |
| + : isolate_(isolate), |
| + zone_(zone), |
| + script_(script), |
| + bytecode_(bytecode), |
| + function_name_(function_name) { |
| + i::FLAG_ignition = true; |
| + i::FLAG_always_opt = false; |
| + i::FLAG_vector_stores = true; |
| + // Set ignition filter flag via SetFlagsFromString to avoid double-free |
| + // (or potential leak with StrDup() based on ownership confusion). |
| + ScopedVector<char> ignition_filter(64); |
| + SNPrintF(ignition_filter, "--ignition-filter=%s", function_name_); |
| + FlagList::SetFlagsFromString(ignition_filter.start(), |
| + ignition_filter.length()); |
| + // Ensure handler table is generated. |
| + isolate_->interpreter()->Initialize(); |
| + } |
| + |
| + ParseInfo GetParseInfo() { |
| + if (script_) { |
| + v8::Context::New(v8::Isolate::GetCurrent())->Enter(); |
| + v8::Local<v8::Context> context = |
| + v8::Isolate::GetCurrent()->GetCurrentContext(); |
| + CompileRun(script_); |
| + |
| + Local<Function> api_function = |
| + Local<Function>::Cast(context->Global() |
| + ->Get(context, v8_str(function_name_)) |
| + .ToLocalChecked()); |
| + Handle<JSFunction> function = |
| + Handle<JSFunction>::cast(v8::Utils::OpenHandle(*api_function)); |
| + CHECK(function->shared()->HasBytecodeArray()); |
| + |
| + context->Exit(); |
| + return ParseInfo(zone_, function); |
| + } else { |
|
rmcilroy
2015/11/10 18:00:15
DCHECK(!bytecode_.is_null());
mythria
2015/11/11 13:39:25
Done.
|
| + Handle<String> name = |
| + isolate_->factory()->NewStringFromAsciiChecked(function_name_); |
| + std::string script_str("function " + std::string(function_name_) + |
| + "() {}"); |
| + Handle<String> script = |
| + isolate_->factory()->NewStringFromAsciiChecked(script_str.c_str()); |
| + Handle<SharedFunctionInfo> shared_info = |
| + isolate_->factory()->NewSharedFunctionInfo(name, MaybeHandle<Code>()); |
| + shared_info->set_script(*isolate_->factory()->NewScript(script)); |
| + return ParseInfo(zone_, shared_info); |
| + } |
| + } |
| + |
| + static v8::Local<v8::String> v8_str(const char* string) { |
| + v8::Local<v8::String> v8_string = |
| + v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), string, |
| + v8::NewStringType::kNormal) |
| + .ToLocalChecked(); |
| + return v8_string; |
| + } |
| + |
| + static Local<Value> CompileRun(const char* source) { |
| + v8::Local<v8::Context> context = |
| + v8::Isolate::GetCurrent()->GetCurrentContext(); |
| + v8::Local<v8::Script> compiled_script = |
| + (v8::Script::Compile(context, v8_str(source)).ToLocalChecked()); |
| + return compiled_script->Run(context).ToLocalChecked(); |
| + } |
| +}; |
| + |
| + |
| class BytecodeGraphBuilderTest : public TestWithIsolateAndZone { |
| public: |
| BytecodeGraphBuilderTest() : array_builder_(isolate(), zone()) {} |
| - Graph* GetCompletedGraph(); |
| - |
| Matcher<Node*> IsUndefinedConstant(); |
| Matcher<Node*> IsNullConstant(); |
| Matcher<Node*> IsTheHoleConstant(); |
| Matcher<Node*> IsFalseConstant(); |
| Matcher<Node*> IsTrueConstant(); |
| + Matcher<Node*> IsIntPtrConstant(int value); |
| + Matcher<Node*> IsFeedbackVector(Node* effect, Node* control); |
| interpreter::BytecodeArrayBuilder* array_builder() { return &array_builder_; } |
| @@ -45,32 +192,6 @@ class BytecodeGraphBuilderTest : public TestWithIsolateAndZone { |
| }; |
| -Graph* BytecodeGraphBuilderTest::GetCompletedGraph() { |
| - MachineOperatorBuilder* machine = new (zone()) MachineOperatorBuilder( |
| - zone(), kMachPtr, InstructionSelector::SupportedMachineOperatorFlags()); |
| - CommonOperatorBuilder* common = new (zone()) CommonOperatorBuilder(zone()); |
| - JSOperatorBuilder* javascript = new (zone()) JSOperatorBuilder(zone()); |
| - Graph* graph = new (zone()) Graph(zone()); |
| - JSGraph* jsgraph = new (zone()) |
| - JSGraph(isolate(), graph, common, javascript, nullptr, machine); |
| - |
| - Handle<String> name = factory()->NewStringFromStaticChars("test"); |
| - Handle<String> script = factory()->NewStringFromStaticChars("test() {}"); |
| - Handle<SharedFunctionInfo> shared_info = |
| - factory()->NewSharedFunctionInfo(name, MaybeHandle<Code>()); |
| - shared_info->set_script(*factory()->NewScript(script)); |
| - |
| - ParseInfo parse_info(zone(), shared_info); |
| - CompilationInfo info(&parse_info); |
| - Handle<BytecodeArray> bytecode_array = array_builder()->ToBytecodeArray(); |
| - info.shared_info()->set_function_data(*bytecode_array); |
| - |
| - BytecodeGraphBuilder graph_builder(zone(), &info, jsgraph); |
| - graph_builder.CreateGraph(); |
| - return graph; |
| -} |
| - |
| - |
| Matcher<Node*> BytecodeGraphBuilderTest::IsUndefinedConstant() { |
| return IsHeapConstant(factory()->undefined_value()); |
| } |
| @@ -96,13 +217,37 @@ Matcher<Node*> BytecodeGraphBuilderTest::IsTrueConstant() { |
| } |
| +Matcher<Node*> BytecodeGraphBuilderTest::IsIntPtrConstant(int value) { |
| + if (kPointerSize == 8) { |
| + return IsInt64Constant(value); |
| + } else { |
| + return IsInt32Constant(value); |
| + } |
| +} |
| + |
| + |
| +Matcher<Node*> BytecodeGraphBuilderTest::IsFeedbackVector(Node* effect, |
| + Node* control) { |
| + int offset = SharedFunctionInfo::kFeedbackVectorOffset - kHeapObjectTag; |
| + int offset1 = JSFunction::kSharedFunctionInfoOffset - kHeapObjectTag; |
| + |
| + return IsLoad(kMachAnyTagged, |
| + IsLoad(kMachAnyTagged, |
| + IsParameter(Linkage::kJSFunctionCallClosureParamIndex), |
| + IsIntPtrConstant(offset1), effect, control), |
| + IsIntPtrConstant(offset), effect, control); |
| +} |
| + |
| + |
| TEST_F(BytecodeGraphBuilderTest, ReturnUndefined) { |
| array_builder()->set_locals_count(0); |
| array_builder()->set_context_count(0); |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadUndefined().Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -118,7 +263,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnNull) { |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadNull().Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -132,7 +279,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnTheHole) { |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadTheHole().Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -148,7 +297,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnTrue) { |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadTrue().Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -164,7 +315,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnFalse) { |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadFalse().Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -181,7 +334,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnInt8) { |
| array_builder()->set_parameter_count(1); |
| array_builder()->LoadLiteral(Smi::FromInt(kValue)).Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -199,7 +354,9 @@ TEST_F(BytecodeGraphBuilderTest, ReturnDouble) { |
| array_builder()->LoadLiteral(factory()->NewHeapNumber(kValue)); |
| array_builder()->Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -220,7 +377,9 @@ TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithParameters) { |
| .StoreAccumulatorInRegister(interpreter::Register(0)) |
| .Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -245,7 +404,9 @@ TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { |
| Strength::WEAK) |
| .Return(); |
| - Graph* graph = GetCompletedGraph(); |
| + GraphGeneratorHelper helper(isolate(), zone(), |
| + array_builder()->ToBytecodeArray()); |
| + Graph* graph = helper.GetCompletedGraph(); |
| Node* end = graph->end(); |
| EXPECT_EQ(1, end->InputCount()); |
| Node* ret = end->InputAt(0); |
| @@ -254,6 +415,83 @@ TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { |
| _, _)); |
| } |
| + |
| +TEST_F(BytecodeGraphBuilderTest, NamedLoadSloppy) { |
| + const char* code_snippet = "function f(p1) {return p1.val;}; f({val:10});"; |
| + GraphGeneratorHelper helper(isolate(), zone(), code_snippet); |
| + Graph* graph = helper.GetCompletedGraph(); |
| + |
| + Node* ret = graph->end()->InputAt(0); |
| + Node* start = graph->start(); |
| + |
| + Handle<Name> name = GraphGeneratorHelper::GetName(isolate(), "val"); |
| + Matcher<Node*> feedback_vector_matcher = IsFeedbackVector(start, start); |
| + Matcher<Node*> load_named_matcher = IsJSLoadNamed( |
| + name, IsParameter(1), feedback_vector_matcher, start, start); |
| + |
| + EXPECT_THAT(ret, IsReturn(load_named_matcher, _, _)); |
| +} |
| + |
| + |
| +TEST_F(BytecodeGraphBuilderTest, NamedLoadStrict) { |
| + const char* code_snippet = |
| + "function f(p1) {'use strict'; return p1.val;}; f({val:10});"; |
| + GraphGeneratorHelper helper(isolate(), zone(), code_snippet); |
| + Graph* graph = helper.GetCompletedGraph(); |
| + |
| + Node* ret = graph->end()->InputAt(0); |
| + Node* start = graph->start(); |
| + |
| + Handle<Name> name = GraphGeneratorHelper::GetName(isolate(), "val"); |
| + Matcher<Node*> feedback_vector_matcher = IsFeedbackVector(start, start); |
| + Matcher<Node*> load_named_matcher = IsJSLoadNamed( |
| + name, IsParameter(1), feedback_vector_matcher, start, start); |
| + |
| + EXPECT_THAT(ret, IsReturn(load_named_matcher, _, _)); |
| +} |
| + |
| + |
| +TEST_F(BytecodeGraphBuilderTest, NamedLoadSloppyWide) { |
| + const char code_snippet[] = "function f(p1) {var b; " |
| + REPEAT_127(SPACE, " b = p1.name; ") |
|
rmcilroy
2015/11/10 18:00:15
nit - for clarity maybe make this val_prev or some
mythria
2015/11/11 13:39:24
Done.
|
| + "return p1.val;}; f({val:10, name:'abc'});"; |
| + GraphGeneratorHelper helper(isolate(), zone(), code_snippet); |
| + Graph* graph = helper.GetCompletedGraph(); |
| + |
| + Node* ret = graph->end()->InputAt(0); |
| + Node* start = graph->start(); |
| + |
| + Handle<Name> name = GraphGeneratorHelper::GetName(isolate(), "val"); |
| + Matcher<Node*> feedback_vector_matcher = IsFeedbackVector(start, start); |
| + Matcher<Node*> effect = IsJSLoadNamed( |
|
rmcilroy
2015/11/10 18:00:15
nit - call this preceeding_load or similar for cla
mythria
2015/11/11 13:39:24
Done.
|
| + GraphGeneratorHelper::GetName(isolate(), "name"), _, _, _, _); |
| + Matcher<Node*> load_named_matcher_wide = IsJSLoadNamed( |
| + name, IsParameter(1), feedback_vector_matcher, effect, IsIfSuccess(_)); |
| + |
| + EXPECT_THAT(ret, IsReturn(load_named_matcher_wide, _, _)); |
| +} |
| + |
| + |
| +TEST_F(BytecodeGraphBuilderTest, NamedLoadStrictWide) { |
| + const char code_snippet[] = "function f(p1) {'use strict'; var b;" |
| + REPEAT_127(SPACE, " b = p1.name; ") |
|
rmcilroy
2015/11/10 18:00:15
ditto
mythria
2015/11/11 13:39:24
Done.
|
| + "return p1.val;}; f({val:10, name:'abc'});"; |
| + GraphGeneratorHelper helper(isolate(), zone(), code_snippet); |
| + Graph* graph = helper.GetCompletedGraph(); |
| + |
| + Node* ret = graph->end()->InputAt(0); |
| + Node* start = graph->start(); |
| + |
| + Handle<Name> name = GraphGeneratorHelper::GetName(isolate(), "val"); |
| + Matcher<Node*> feedback_vector_matcher = IsFeedbackVector(start, start); |
| + Matcher<Node*> effect = IsJSLoadNamed( |
|
rmcilroy
2015/11/10 18:00:15
ditto
mythria
2015/11/11 13:39:24
Done.
|
| + GraphGeneratorHelper::GetName(isolate(), "name"), _, _, _, _); |
| + Matcher<Node*> load_named_matcher_wide = IsJSLoadNamed( |
| + name, IsParameter(1), feedback_vector_matcher, effect, IsIfSuccess(_)); |
| + |
| + EXPECT_THAT(ret, IsReturn(load_named_matcher_wide, _, _)); |
| +} |
| + |
| } // namespace compiler |
| } // namespace internal |
| } // namespace v8 |