Chromium Code Reviews| Index: test/cctest/compiler/test-run-bytecode-graph-builder.cc |
| diff --git a/test/cctest/compiler/test-run-bytecode-graph-builder.cc b/test/cctest/compiler/test-run-bytecode-graph-builder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d07c9ceb4d21ff6cf62eb02e0b7d484780fa87d4 |
| --- /dev/null |
| +++ b/test/cctest/compiler/test-run-bytecode-graph-builder.cc |
| @@ -0,0 +1,258 @@ |
| +// 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 <utility> |
| + |
| +#include "src/v8.h" |
| + |
| +#include "src/compiler/pipeline.h" |
| +#include "src/execution.h" |
| +#include "src/handles.h" |
| +#include "src/interpreter/bytecode-array-builder.h" |
| +#include "src/interpreter/interpreter.h" |
| +#include "src/parser.h" |
| +#include "test/cctest/cctest.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| +namespace compiler { |
| + |
| + |
| +static const char* kFunctionName = "f"; |
| + |
| + |
| +static MaybeHandle<Object> CallFunction(Isolate* isolate, |
| + Handle<JSFunction> function) { |
| + return Execution::Call(isolate, function, |
| + isolate->factory()->undefined_value(), 0, nullptr, |
| + false); |
| +} |
| + |
| + |
| +template <class... A> |
| +static MaybeHandle<Object> CallFunction(Isolate* isolate, |
| + Handle<JSFunction> function, |
| + A... args) { |
| + Handle<Object> argv[] = {args...}; |
| + return Execution::Call(isolate, function, |
| + isolate->factory()->undefined_value(), sizeof...(args), |
| + argv, false); |
| +} |
| + |
| + |
| +template <class... A> |
| +class BytecodeGraphCallable { |
| + public: |
| + BytecodeGraphCallable(Isolate* isolate, Handle<JSFunction> function) |
| + : isolate_(isolate), function_(function) {} |
| + virtual ~BytecodeGraphCallable() {} |
| + |
| + MaybeHandle<Object> operator()(A... args) { |
| + return CallFunction(isolate_, function_, args...); |
| + } |
| + |
| + private: |
| + Isolate* isolate_; |
| + Handle<JSFunction> function_; |
| +}; |
| + |
| + |
| +class BytecodeGraphTester { |
| + public: |
| + BytecodeGraphTester(Isolate* isolate, Zone* zone, const char* script) |
| + : isolate_(isolate), zone_(zone), script_(script) { |
| + i::FLAG_ignition = true; |
|
rmcilroy
2015/09/10 10:19:43
need to also add i::FLAG_vector_stores now.
oth
2015/09/10 13:59:06
Done.
|
| + i::FLAG_ignition_filter = StrDup(kFunctionName); |
| + i::FLAG_always_opt = false; |
| + // Ensure handler table is generated. |
| + isolate->interpreter()->Initialize(); |
| + } |
| + virtual ~BytecodeGraphTester() {} |
| + |
| + template <class... A> |
| + BytecodeGraphCallable<A...> GetCallable() { |
| + return BytecodeGraphCallable<A...>(isolate_, GetFunction()); |
| + } |
| + |
| + private: |
| + Isolate* isolate_; |
| + Zone* zone_; |
| + const char* script_; |
| + |
| + Handle<JSFunction> GetFunction() { |
| + CompileRun(script_); |
| + Local<Function> api_function = |
| + Local<Function>::Cast(CcTest::global()->Get(v8_str(kFunctionName))); |
| + Handle<JSFunction> function = v8::Utils::OpenHandle(*api_function); |
| + CHECK(function->shared()->HasBytecodeArray()); |
| + |
| + ParseInfo parse_info(zone_, function); |
| + |
| + CompilationInfo compilation_info(&parse_info); |
| + compilation_info.SetOptimizing(BailoutId::None(), Handle<Code>()); |
| + Parser parser(&parse_info); |
| + CHECK(parser.Parse(&parse_info)); |
| + compiler::Pipeline pipeline(&compilation_info); |
| + Handle<Code> code = pipeline.GenerateCode(); |
| + function->ReplaceCode(*code); |
| + |
| + return function; |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BytecodeGraphTester); |
| +}; |
| + |
| +} // namespace compiler |
| +} // namespace internal |
| +} // namespace v8 |
| + |
| + |
| +using namespace v8::internal; |
| +using namespace v8::internal::compiler; |
| + |
| + |
| +struct ExpectedSnippet { |
| + const char* code_snippet; |
| + Handle<Object> return_val; |
| +}; |
| + |
| + |
| +TEST(BytecodeGraphBuilderReturnStatements) { |
| + HandleAndZoneScope handles; |
| + Isolate* isolate = handles.main_isolate(); |
| + Factory* factory = isolate->factory(); |
| + |
| + ExpectedSnippet snippets[] = { |
| + {"return;", factory->undefined_value()}, |
| + {"return null;", factory->null_value()}, |
| + {"return true;", factory->true_value()}, |
| + {"return false;", factory->false_value()}, |
| + {"return 0;", Handle<Object>(Smi::FromInt(0), isolate)}, |
| + {"return +1;", Handle<Object>(Smi::FromInt(1), isolate)}, |
| + {"return -1;", Handle<Object>(Smi::FromInt(-1), isolate)}, |
| + {"return +127;", Handle<Object>(Smi::FromInt(127), isolate)}, |
| + {"return -128;", Handle<Object>(Smi::FromInt(-128), isolate)}, |
| + }; |
| + |
| + size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| + for (size_t i = 0; i < num_snippets; i++) { |
| + ScopedVector<char> script(1024); |
| + SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, |
| + snippets[i].code_snippet, kFunctionName); |
| + |
| + BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), |
| + script.start()); |
| + auto callable = tester.GetCallable<>(); |
| + Handle<Object> return_val = callable().ToHandleChecked(); |
| + CHECK(return_val.is_identical_to(snippets[i].return_val)); |
| + } |
| +} |
| + |
| + |
| +TEST(BytecodeGraphBuilderPrimitiveExpressions) { |
| + HandleAndZoneScope handles; |
| + Isolate* isolate = handles.main_isolate(); |
| + |
| + ExpectedSnippet snippets[] = { |
| + {"return 1 + 1;", Handle<Object>(Smi::FromInt(2), isolate)}, |
| + {"return 20 - 30;", Handle<Object>(Smi::FromInt(-10), isolate)}, |
| + {"return 4 * 100;", Handle<Object>(Smi::FromInt(400), isolate)}, |
| + {"return 100 / 5;", Handle<Object>(Smi::FromInt(20), isolate)}, |
| + {"return 25 % 7;", Handle<Object>(Smi::FromInt(4), isolate)}, |
| + }; |
| + |
| + size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| + for (size_t i = 0; i < num_snippets; i++) { |
| + ScopedVector<char> script(1024); |
| + SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, |
| + snippets[i].code_snippet, kFunctionName); |
| + |
| + BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), |
| + script.start()); |
| + auto callable = tester.GetCallable<>(); |
| + Handle<Object> return_val = callable().ToHandleChecked(); |
| + CHECK(return_val.is_identical_to(snippets[i].return_val)); |
| + } |
| +} |
| + |
| + |
| +template <typename P, typename R> |
| +void TwoParameterTest(Isolate* isolate, Zone* zone, const char* code_snippet, |
| + std::pair<const char*, P> param1, |
| + std::pair<const char*, P> param2, R expected_result) { |
| + v8::base::SmartArrayPointer<char> param1_value_string = |
| + Object::ToString(isolate, param1.second).ToHandleChecked()->ToCString(); |
| + v8::base::SmartArrayPointer<char> param2_value_string = |
| + Object::ToString(isolate, param2.second).ToHandleChecked()->ToCString(); |
|
rmcilroy
2015/09/10 10:19:43
This seems overly complicated. You don't actually
oth
2015/09/10 13:59:06
Agree, the missing knowledge was the existence of
|
| + |
| + ScopedVector<char> script(1024); |
| + SNPrintF(script, "function %s(%s, %s) { %s }\n%s(%s, %s);", kFunctionName, |
| + param1.first, param2.first, code_snippet, kFunctionName, |
| + param1_value_string.get(), param2_value_string.get()); |
| + |
| + BytecodeGraphTester tester(isolate, zone, script.start()); |
| + auto callable = tester.GetCallable<Handle<Object>, Handle<Object>>(); |
| + Handle<Object> actual_result = |
| + callable(param1.second, param2.second).ToHandleChecked(); |
| + CHECK(actual_result->SameValue(*expected_result)); |
| +} |
| + |
| + |
| +TEST(BytecodeGraphBuilderTwoParameterTests) { |
| + HandleAndZoneScope handles; |
| + Isolate* isolate = handles.main_isolate(); |
| + Zone* zone = handles.main_zone(); |
| + Factory* factory = isolate->factory(); |
| + |
| + // Integers |
| + TwoParameterTest(isolate, zone, "return p1 + p2; ", |
| + std::make_pair("p1", factory->NewNumberFromInt(3)), |
| + std::make_pair("p2", factory->NewNumberFromInt(-73)), |
| + factory->NewNumberFromInt(-70)); |
| + TwoParameterTest(isolate, zone, "return p1 + p2 + 3;", |
| + std::make_pair("p1", factory->NewNumberFromInt(10372)), |
| + std::make_pair("p2", factory->NewNumberFromInt(-2)), |
| + factory->NewNumberFromInt(10373)); |
| + TwoParameterTest(isolate, zone, "return p1 - p2;", |
| + std::make_pair("p1", factory->NewNumberFromInt(1000)), |
| + std::make_pair("p2", factory->NewNumberFromInt(-100)), |
| + factory->NewNumberFromInt(1100)); |
| + TwoParameterTest(isolate, zone, "return p1 * p2;", |
| + std::make_pair("p1", factory->NewNumberFromInt(1000)), |
| + std::make_pair("p2", factory->NewNumberFromInt(-100)), |
| + factory->NewHeapNumber(-100000)); |
| + TwoParameterTest(isolate, zone, "return p1 / p2;", |
| + std::make_pair("p1", factory->NewNumberFromInt(1000)), |
| + std::make_pair("p2", factory->NewNumberFromInt(-100)), |
| + factory->NewHeapNumber(-10)); |
| + TwoParameterTest(isolate, zone, "return p1 % p2;", |
| + std::make_pair("p1", factory->NewNumberFromInt(373)), |
| + std::make_pair("p2", factory->NewNumberFromInt(16)), |
| + factory->NewHeapNumber(5)); |
| + |
| + // Doubles |
| + TwoParameterTest(isolate, zone, "return p1 + p2;", |
| + std::make_pair("p1", factory->NewNumber(3.333)), |
| + std::make_pair("p2", factory->NewNumber(6.666)), |
| + factory->NewNumber(9.999)); |
| + TwoParameterTest(isolate, zone, "return p1 - p2;", |
| + std::make_pair("p1", factory->NewNumber(3.333)), |
| + std::make_pair("p2", factory->NewNumber(6.666)), |
| + factory->NewNumber(-3.333)); |
| + TwoParameterTest(isolate, zone, "return p1 * p2;", |
| + std::make_pair("p1", factory->NewNumber(3.333)), |
| + std::make_pair("p2", factory->NewNumber(6.666)), |
| + factory->NewNumber(3.333 * 6.666)); |
| + TwoParameterTest(isolate, zone, "return p1 / p2;", |
| + std::make_pair("p1", factory->NewNumber(9)), |
| + std::make_pair("p2", factory->NewNumber(4)), |
| + factory->NewNumber(2.25)); |
| + |
| + // Strings |
| + TwoParameterTest( |
| + isolate, zone, "return p1 + p2;", |
| + std::make_pair("p1", factory->NewStringFromStaticChars("'abc'")), |
| + std::make_pair("p2", factory->NewStringFromStaticChars("'def'")), |
| + factory->NewStringFromStaticChars("'abc''def'")); |
| +} |
|
rmcilroy
2015/09/10 10:19:43
nit - could you add a TODO to add a test for const
oth
2015/09/10 13:59:06
Added double and string snippets to BytecodeGraphB
|