OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/compiler/pipeline.h" |
| 8 #include "src/execution.h" |
| 9 #include "src/handles.h" |
| 10 #include "src/interpreter/bytecode-array-builder.h" |
| 11 #include "src/interpreter/interpreter.h" |
| 12 #include "src/parser.h" |
| 13 #include "test/cctest/cctest.h" |
| 14 |
| 15 namespace v8 { |
| 16 namespace internal { |
| 17 namespace compiler { |
| 18 |
| 19 |
| 20 static const char* kFunctionName = "f"; |
| 21 |
| 22 |
| 23 static MaybeHandle<Object> CallFunction(Isolate* isolate, |
| 24 Handle<JSFunction> function) { |
| 25 return Execution::Call(isolate, function, |
| 26 isolate->factory()->undefined_value(), 0, nullptr, |
| 27 false); |
| 28 } |
| 29 |
| 30 |
| 31 template <class... A> |
| 32 static MaybeHandle<Object> CallFunction(Isolate* isolate, |
| 33 Handle<JSFunction> function, |
| 34 A... args) { |
| 35 Handle<Object> argv[] = {args...}; |
| 36 return Execution::Call(isolate, function, |
| 37 isolate->factory()->undefined_value(), sizeof...(args), |
| 38 argv, false); |
| 39 } |
| 40 |
| 41 |
| 42 template <class... A> |
| 43 class BytecodeGraphCallable { |
| 44 public: |
| 45 BytecodeGraphCallable(Isolate* isolate, Handle<JSFunction> function) |
| 46 : isolate_(isolate), function_(function) {} |
| 47 virtual ~BytecodeGraphCallable() {} |
| 48 |
| 49 MaybeHandle<Object> operator()(A... args) { |
| 50 return CallFunction(isolate_, function_, args...); |
| 51 } |
| 52 |
| 53 private: |
| 54 Isolate* isolate_; |
| 55 Handle<JSFunction> function_; |
| 56 }; |
| 57 |
| 58 |
| 59 class BytecodeGraphTester { |
| 60 public: |
| 61 BytecodeGraphTester(Isolate* isolate, Zone* zone, const char* script) |
| 62 : isolate_(isolate), zone_(zone), script_(script) { |
| 63 i::FLAG_ignition = true; |
| 64 i::FLAG_ignition_filter = StrDup(kFunctionName); |
| 65 i::FLAG_always_opt = false; |
| 66 // Ensure handler table is generated. |
| 67 isolate->interpreter()->Initialize(); |
| 68 } |
| 69 virtual ~BytecodeGraphTester() {} |
| 70 |
| 71 template <class... A> |
| 72 BytecodeGraphCallable<A...> GetCallable() { |
| 73 return BytecodeGraphCallable<A...>(isolate_, GetFunction()); |
| 74 } |
| 75 |
| 76 private: |
| 77 Isolate* isolate_; |
| 78 Zone* zone_; |
| 79 const char* script_; |
| 80 |
| 81 Handle<JSFunction> GetFunction() { |
| 82 CompileRun(script_); |
| 83 Local<Function> api_function = |
| 84 Local<Function>::Cast(CcTest::global()->Get(v8_str(kFunctionName))); |
| 85 Handle<JSFunction> function = v8::Utils::OpenHandle(*api_function); |
| 86 CHECK(function->shared()->HasBytecodeArray()); |
| 87 |
| 88 ParseInfo parse_info(zone_, function); |
| 89 |
| 90 CompilationInfo compilation_info(&parse_info); |
| 91 compilation_info.SetOptimizing(BailoutId::None(), Handle<Code>()); |
| 92 Parser parser(&parse_info); |
| 93 CHECK(parser.Parse(&parse_info)); |
| 94 compiler::Pipeline pipeline(&compilation_info); |
| 95 Handle<Code> code = pipeline.GenerateCode(); |
| 96 function->ReplaceCode(*code); |
| 97 |
| 98 return function; |
| 99 } |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(BytecodeGraphTester); |
| 102 }; |
| 103 |
| 104 } // namespace compiler |
| 105 } // namespace internal |
| 106 } // namespace v8 |
| 107 |
| 108 |
| 109 using namespace v8::internal; |
| 110 using namespace v8::internal::compiler; |
| 111 |
| 112 |
| 113 struct ExpectedSnippet { |
| 114 const char* code_snippet; |
| 115 Handle<Object> return_val; |
| 116 }; |
| 117 |
| 118 |
| 119 TEST(BytecodeGraphBuilderReturnStatements) { |
| 120 HandleAndZoneScope handles; |
| 121 Isolate* isolate = handles.main_isolate(); |
| 122 Factory* factory = isolate->factory(); |
| 123 |
| 124 ExpectedSnippet snippets[] = { |
| 125 {"return;", factory->undefined_value()}, |
| 126 {"return null;", factory->null_value()}, |
| 127 {"return true;", factory->true_value()}, |
| 128 {"return false;", factory->false_value()}, |
| 129 {"return 0;", Handle<Object>(Smi::FromInt(0), isolate)}, |
| 130 {"return +1;", Handle<Object>(Smi::FromInt(1), isolate)}, |
| 131 {"return -1;", Handle<Object>(Smi::FromInt(-1), isolate)}, |
| 132 {"return +127;", Handle<Object>(Smi::FromInt(127), isolate)}, |
| 133 {"return -128;", Handle<Object>(Smi::FromInt(-128), isolate)}, |
| 134 }; |
| 135 |
| 136 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 137 for (size_t i = 0; i < num_snippets; i++) { |
| 138 ScopedVector<char> script(1024); |
| 139 SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, |
| 140 snippets[i].code_snippet, kFunctionName); |
| 141 |
| 142 BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), |
| 143 script.start()); |
| 144 auto callable = tester.GetCallable<>(); |
| 145 Handle<Object> return_val = callable().ToHandleChecked(); |
| 146 CHECK(return_val.is_identical_to(snippets[i].return_val)); |
| 147 } |
| 148 } |
| 149 |
| 150 |
| 151 TEST(BytecodeGraphBuilderPrimitiveExpressions) { |
| 152 HandleAndZoneScope handles; |
| 153 Isolate* isolate = handles.main_isolate(); |
| 154 |
| 155 ExpectedSnippet snippets[] = { |
| 156 {"return 1 + 1;", Handle<Object>(Smi::FromInt(2), isolate)}, |
| 157 {"return 20 - 30;", Handle<Object>(Smi::FromInt(-10), isolate)}, |
| 158 {"return 4 * 100;", Handle<Object>(Smi::FromInt(400), isolate)}, |
| 159 {"return 100 / 5;", Handle<Object>(Smi::FromInt(20), isolate)}, |
| 160 {"return 25 % 7;", Handle<Object>(Smi::FromInt(4), isolate)}, |
| 161 }; |
| 162 |
| 163 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 164 for (size_t i = 0; i < num_snippets; i++) { |
| 165 ScopedVector<char> script(1024); |
| 166 SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, |
| 167 snippets[i].code_snippet, kFunctionName); |
| 168 |
| 169 BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), |
| 170 script.start()); |
| 171 auto callable = tester.GetCallable<>(); |
| 172 Handle<Object> return_val = callable().ToHandleChecked(); |
| 173 CHECK(return_val.is_identical_to(snippets[i].return_val)); |
| 174 } |
| 175 } |
OLD | NEW |