Chromium Code Reviews| 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 <utility> | |
| 6 | |
| 7 #include "src/v8.h" | |
| 8 | |
| 9 #include "src/compiler/pipeline.h" | |
| 10 #include "src/execution.h" | |
| 11 #include "src/handles.h" | |
| 12 #include "src/interpreter/bytecode-array-builder.h" | |
| 13 #include "src/interpreter/interpreter.h" | |
| 14 #include "src/parser.h" | |
| 15 #include "test/cctest/cctest.h" | |
| 16 | |
| 17 namespace v8 { | |
| 18 namespace internal { | |
| 19 namespace compiler { | |
| 20 | |
| 21 | |
| 22 static const char* kFunctionName = "f"; | |
| 23 | |
| 24 | |
| 25 static MaybeHandle<Object> CallFunction(Isolate* isolate, | |
| 26 Handle<JSFunction> function) { | |
| 27 return Execution::Call(isolate, function, | |
| 28 isolate->factory()->undefined_value(), 0, nullptr, | |
| 29 false); | |
| 30 } | |
| 31 | |
| 32 | |
| 33 template <class... A> | |
| 34 static MaybeHandle<Object> CallFunction(Isolate* isolate, | |
| 35 Handle<JSFunction> function, | |
| 36 A... args) { | |
| 37 Handle<Object> argv[] = {args...}; | |
| 38 return Execution::Call(isolate, function, | |
| 39 isolate->factory()->undefined_value(), sizeof...(args), | |
| 40 argv, false); | |
| 41 } | |
| 42 | |
| 43 | |
| 44 template <class... A> | |
| 45 class BytecodeGraphCallable { | |
| 46 public: | |
| 47 BytecodeGraphCallable(Isolate* isolate, Handle<JSFunction> function) | |
| 48 : isolate_(isolate), function_(function) {} | |
| 49 virtual ~BytecodeGraphCallable() {} | |
| 50 | |
| 51 MaybeHandle<Object> operator()(A... args) { | |
| 52 return CallFunction(isolate_, function_, args...); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 Isolate* isolate_; | |
| 57 Handle<JSFunction> function_; | |
| 58 }; | |
| 59 | |
| 60 | |
| 61 class BytecodeGraphTester { | |
| 62 public: | |
| 63 BytecodeGraphTester(Isolate* isolate, Zone* zone, const char* script) | |
| 64 : isolate_(isolate), zone_(zone), script_(script) { | |
| 65 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.
| |
| 66 i::FLAG_ignition_filter = StrDup(kFunctionName); | |
| 67 i::FLAG_always_opt = false; | |
| 68 // Ensure handler table is generated. | |
| 69 isolate->interpreter()->Initialize(); | |
| 70 } | |
| 71 virtual ~BytecodeGraphTester() {} | |
| 72 | |
| 73 template <class... A> | |
| 74 BytecodeGraphCallable<A...> GetCallable() { | |
| 75 return BytecodeGraphCallable<A...>(isolate_, GetFunction()); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 Isolate* isolate_; | |
| 80 Zone* zone_; | |
| 81 const char* script_; | |
| 82 | |
| 83 Handle<JSFunction> GetFunction() { | |
| 84 CompileRun(script_); | |
| 85 Local<Function> api_function = | |
| 86 Local<Function>::Cast(CcTest::global()->Get(v8_str(kFunctionName))); | |
| 87 Handle<JSFunction> function = v8::Utils::OpenHandle(*api_function); | |
| 88 CHECK(function->shared()->HasBytecodeArray()); | |
| 89 | |
| 90 ParseInfo parse_info(zone_, function); | |
| 91 | |
| 92 CompilationInfo compilation_info(&parse_info); | |
| 93 compilation_info.SetOptimizing(BailoutId::None(), Handle<Code>()); | |
| 94 Parser parser(&parse_info); | |
| 95 CHECK(parser.Parse(&parse_info)); | |
| 96 compiler::Pipeline pipeline(&compilation_info); | |
| 97 Handle<Code> code = pipeline.GenerateCode(); | |
| 98 function->ReplaceCode(*code); | |
| 99 | |
| 100 return function; | |
| 101 } | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(BytecodeGraphTester); | |
| 104 }; | |
| 105 | |
| 106 } // namespace compiler | |
| 107 } // namespace internal | |
| 108 } // namespace v8 | |
| 109 | |
| 110 | |
| 111 using namespace v8::internal; | |
| 112 using namespace v8::internal::compiler; | |
| 113 | |
| 114 | |
| 115 struct ExpectedSnippet { | |
| 116 const char* code_snippet; | |
| 117 Handle<Object> return_val; | |
| 118 }; | |
| 119 | |
| 120 | |
| 121 TEST(BytecodeGraphBuilderReturnStatements) { | |
| 122 HandleAndZoneScope handles; | |
| 123 Isolate* isolate = handles.main_isolate(); | |
| 124 Factory* factory = isolate->factory(); | |
| 125 | |
| 126 ExpectedSnippet snippets[] = { | |
| 127 {"return;", factory->undefined_value()}, | |
| 128 {"return null;", factory->null_value()}, | |
| 129 {"return true;", factory->true_value()}, | |
| 130 {"return false;", factory->false_value()}, | |
| 131 {"return 0;", Handle<Object>(Smi::FromInt(0), isolate)}, | |
| 132 {"return +1;", Handle<Object>(Smi::FromInt(1), isolate)}, | |
| 133 {"return -1;", Handle<Object>(Smi::FromInt(-1), isolate)}, | |
| 134 {"return +127;", Handle<Object>(Smi::FromInt(127), isolate)}, | |
| 135 {"return -128;", Handle<Object>(Smi::FromInt(-128), isolate)}, | |
| 136 }; | |
| 137 | |
| 138 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); | |
| 139 for (size_t i = 0; i < num_snippets; i++) { | |
| 140 ScopedVector<char> script(1024); | |
| 141 SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, | |
| 142 snippets[i].code_snippet, kFunctionName); | |
| 143 | |
| 144 BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), | |
| 145 script.start()); | |
| 146 auto callable = tester.GetCallable<>(); | |
| 147 Handle<Object> return_val = callable().ToHandleChecked(); | |
| 148 CHECK(return_val.is_identical_to(snippets[i].return_val)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 | |
| 153 TEST(BytecodeGraphBuilderPrimitiveExpressions) { | |
| 154 HandleAndZoneScope handles; | |
| 155 Isolate* isolate = handles.main_isolate(); | |
| 156 | |
| 157 ExpectedSnippet snippets[] = { | |
| 158 {"return 1 + 1;", Handle<Object>(Smi::FromInt(2), isolate)}, | |
| 159 {"return 20 - 30;", Handle<Object>(Smi::FromInt(-10), isolate)}, | |
| 160 {"return 4 * 100;", Handle<Object>(Smi::FromInt(400), isolate)}, | |
| 161 {"return 100 / 5;", Handle<Object>(Smi::FromInt(20), isolate)}, | |
| 162 {"return 25 % 7;", Handle<Object>(Smi::FromInt(4), isolate)}, | |
| 163 }; | |
| 164 | |
| 165 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); | |
| 166 for (size_t i = 0; i < num_snippets; i++) { | |
| 167 ScopedVector<char> script(1024); | |
| 168 SNPrintF(script, "function %s() { %s }\n%s();", kFunctionName, | |
| 169 snippets[i].code_snippet, kFunctionName); | |
| 170 | |
| 171 BytecodeGraphTester tester(handles.main_isolate(), handles.main_zone(), | |
| 172 script.start()); | |
| 173 auto callable = tester.GetCallable<>(); | |
| 174 Handle<Object> return_val = callable().ToHandleChecked(); | |
| 175 CHECK(return_val.is_identical_to(snippets[i].return_val)); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 | |
| 180 template <typename P, typename R> | |
| 181 void TwoParameterTest(Isolate* isolate, Zone* zone, const char* code_snippet, | |
| 182 std::pair<const char*, P> param1, | |
| 183 std::pair<const char*, P> param2, R expected_result) { | |
| 184 v8::base::SmartArrayPointer<char> param1_value_string = | |
| 185 Object::ToString(isolate, param1.second).ToHandleChecked()->ToCString(); | |
| 186 v8::base::SmartArrayPointer<char> param2_value_string = | |
| 187 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
| |
| 188 | |
| 189 ScopedVector<char> script(1024); | |
| 190 SNPrintF(script, "function %s(%s, %s) { %s }\n%s(%s, %s);", kFunctionName, | |
| 191 param1.first, param2.first, code_snippet, kFunctionName, | |
| 192 param1_value_string.get(), param2_value_string.get()); | |
| 193 | |
| 194 BytecodeGraphTester tester(isolate, zone, script.start()); | |
| 195 auto callable = tester.GetCallable<Handle<Object>, Handle<Object>>(); | |
| 196 Handle<Object> actual_result = | |
| 197 callable(param1.second, param2.second).ToHandleChecked(); | |
| 198 CHECK(actual_result->SameValue(*expected_result)); | |
| 199 } | |
| 200 | |
| 201 | |
| 202 TEST(BytecodeGraphBuilderTwoParameterTests) { | |
| 203 HandleAndZoneScope handles; | |
| 204 Isolate* isolate = handles.main_isolate(); | |
| 205 Zone* zone = handles.main_zone(); | |
| 206 Factory* factory = isolate->factory(); | |
| 207 | |
| 208 // Integers | |
| 209 TwoParameterTest(isolate, zone, "return p1 + p2; ", | |
| 210 std::make_pair("p1", factory->NewNumberFromInt(3)), | |
| 211 std::make_pair("p2", factory->NewNumberFromInt(-73)), | |
| 212 factory->NewNumberFromInt(-70)); | |
| 213 TwoParameterTest(isolate, zone, "return p1 + p2 + 3;", | |
| 214 std::make_pair("p1", factory->NewNumberFromInt(10372)), | |
| 215 std::make_pair("p2", factory->NewNumberFromInt(-2)), | |
| 216 factory->NewNumberFromInt(10373)); | |
| 217 TwoParameterTest(isolate, zone, "return p1 - p2;", | |
| 218 std::make_pair("p1", factory->NewNumberFromInt(1000)), | |
| 219 std::make_pair("p2", factory->NewNumberFromInt(-100)), | |
| 220 factory->NewNumberFromInt(1100)); | |
| 221 TwoParameterTest(isolate, zone, "return p1 * p2;", | |
| 222 std::make_pair("p1", factory->NewNumberFromInt(1000)), | |
| 223 std::make_pair("p2", factory->NewNumberFromInt(-100)), | |
| 224 factory->NewHeapNumber(-100000)); | |
| 225 TwoParameterTest(isolate, zone, "return p1 / p2;", | |
| 226 std::make_pair("p1", factory->NewNumberFromInt(1000)), | |
| 227 std::make_pair("p2", factory->NewNumberFromInt(-100)), | |
| 228 factory->NewHeapNumber(-10)); | |
| 229 TwoParameterTest(isolate, zone, "return p1 % p2;", | |
| 230 std::make_pair("p1", factory->NewNumberFromInt(373)), | |
| 231 std::make_pair("p2", factory->NewNumberFromInt(16)), | |
| 232 factory->NewHeapNumber(5)); | |
| 233 | |
| 234 // Doubles | |
| 235 TwoParameterTest(isolate, zone, "return p1 + p2;", | |
| 236 std::make_pair("p1", factory->NewNumber(3.333)), | |
| 237 std::make_pair("p2", factory->NewNumber(6.666)), | |
| 238 factory->NewNumber(9.999)); | |
| 239 TwoParameterTest(isolate, zone, "return p1 - p2;", | |
| 240 std::make_pair("p1", factory->NewNumber(3.333)), | |
| 241 std::make_pair("p2", factory->NewNumber(6.666)), | |
| 242 factory->NewNumber(-3.333)); | |
| 243 TwoParameterTest(isolate, zone, "return p1 * p2;", | |
| 244 std::make_pair("p1", factory->NewNumber(3.333)), | |
| 245 std::make_pair("p2", factory->NewNumber(6.666)), | |
| 246 factory->NewNumber(3.333 * 6.666)); | |
| 247 TwoParameterTest(isolate, zone, "return p1 / p2;", | |
| 248 std::make_pair("p1", factory->NewNumber(9)), | |
| 249 std::make_pair("p2", factory->NewNumber(4)), | |
| 250 factory->NewNumber(2.25)); | |
| 251 | |
| 252 // Strings | |
| 253 TwoParameterTest( | |
| 254 isolate, zone, "return p1 + p2;", | |
| 255 std::make_pair("p1", factory->NewStringFromStaticChars("'abc'")), | |
| 256 std::make_pair("p2", factory->NewStringFromStaticChars("'def'")), | |
| 257 factory->NewStringFromStaticChars("'abc''def'")); | |
| 258 } | |
|
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
| |
| OLD | NEW |