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..22acdaeeb4f0455b0e554289840019550e0161fe |
--- /dev/null |
+++ b/test/cctest/compiler/test-run-bytecode-graph-builder.cc |
@@ -0,0 +1,175 @@ |
+// 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/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), |
Michael Starzinger
2015/09/03 12:19:40
Wow, "sizeof...", C++ never seizes to surprise me.
|
+ 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; |
+ i::FLAG_ignition_filter = kFunctionName; |
Michael Starzinger
2015/09/03 12:19:40
This assignment could potentially trigger double-f
oth
2015/09/04 11:06:36
Thanks, we had another instance of this in the int
|
+ 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)); |
+ } |
+} |