| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/execution.h" | 7 #include "src/execution.h" |
| 8 #include "src/handles.h" | 8 #include "src/handles.h" |
| 9 #include "src/interpreter/bytecode-array-builder.h" | 9 #include "src/interpreter/bytecode-array-builder.h" |
| 10 #include "src/interpreter/interpreter.h" | 10 #include "src/interpreter/interpreter.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 Isolate* isolate_; | 49 Isolate* isolate_; |
| 50 Handle<JSFunction> function_; | 50 Handle<JSFunction> function_; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 | 53 |
| 54 class InterpreterTester { | 54 class InterpreterTester { |
| 55 public: | 55 public: |
| 56 InterpreterTester(Isolate* isolate, Handle<BytecodeArray> bytecode) | 56 InterpreterTester( |
| 57 : isolate_(isolate), bytecode_(bytecode) { | 57 Isolate* isolate, Handle<BytecodeArray> bytecode, |
| 58 Handle<TypeFeedbackVector> feedback_vector = Handle<TypeFeedbackVector>()) |
| 59 : isolate_(isolate), |
| 60 bytecode_(bytecode), |
| 61 feedback_vector_(feedback_vector) { |
| 58 i::FLAG_ignition = true; | 62 i::FLAG_ignition = true; |
| 59 // Ensure handler table is generated. | 63 // Ensure handler table is generated. |
| 60 isolate->interpreter()->Initialize(); | 64 isolate->interpreter()->Initialize(); |
| 61 } | 65 } |
| 62 virtual ~InterpreterTester() {} | 66 virtual ~InterpreterTester() {} |
| 63 | 67 |
| 64 template <class... A> | 68 template <class... A> |
| 65 InterpreterCallable<A...> GetCallable() { | 69 InterpreterCallable<A...> GetCallable() { |
| 66 return InterpreterCallable<A...>(isolate_, GetBytecodeFunction<A...>()); | 70 return InterpreterCallable<A...>(isolate_, GetBytecodeFunction<A...>()); |
| 67 } | 71 } |
| 68 | 72 |
| 73 Handle<Object> NewObject(const char* script) { |
| 74 return v8::Utils::OpenHandle(*CompileRun(script)); |
| 75 } |
| 76 |
| 69 private: | 77 private: |
| 70 Isolate* isolate_; | 78 Isolate* isolate_; |
| 71 Handle<BytecodeArray> bytecode_; | 79 Handle<BytecodeArray> bytecode_; |
| 80 Handle<TypeFeedbackVector> feedback_vector_; |
| 72 | 81 |
| 73 template <class... A> | 82 template <class... A> |
| 74 Handle<JSFunction> GetBytecodeFunction() { | 83 Handle<JSFunction> GetBytecodeFunction() { |
| 75 int arg_count = sizeof...(A); | 84 int arg_count = sizeof...(A); |
| 76 std::string function_text("(function("); | 85 std::string function_text("(function("); |
| 77 for (int i = 0; i < arg_count; i++) { | 86 for (int i = 0; i < arg_count; i++) { |
| 78 function_text += i == 0 ? "a" : ", a"; | 87 function_text += i == 0 ? "a" : ", a"; |
| 79 } | 88 } |
| 80 function_text += "){})"; | 89 function_text += "){})"; |
| 81 | 90 |
| 82 Handle<JSFunction> function = v8::Utils::OpenHandle( | 91 Handle<JSFunction> function = v8::Utils::OpenHandle( |
| 83 *v8::Handle<v8::Function>::Cast(CompileRun(function_text.c_str()))); | 92 *v8::Handle<v8::Function>::Cast(CompileRun(function_text.c_str()))); |
| 84 function->ReplaceCode(*isolate_->builtins()->InterpreterEntryTrampoline()); | 93 function->ReplaceCode(*isolate_->builtins()->InterpreterEntryTrampoline()); |
| 85 function->shared()->set_function_data(*bytecode_); | 94 function->shared()->set_function_data(*bytecode_); |
| 95 if (*feedback_vector_) { |
| 96 function->shared()->set_feedback_vector(*feedback_vector_); |
| 97 } |
| 86 return function; | 98 return function; |
| 87 } | 99 } |
| 88 | 100 |
| 89 DISALLOW_COPY_AND_ASSIGN(InterpreterTester); | 101 DISALLOW_COPY_AND_ASSIGN(InterpreterTester); |
| 90 }; | 102 }; |
| 91 | 103 |
| 92 } // namespace interpreter | 104 } // namespace interpreter |
| 93 } // namespace internal | 105 } // namespace internal |
| 94 } // namespace v8 | 106 } // namespace v8 |
| 95 | 107 |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 InterpreterTester tester(handles.main_isolate(), bytecode_array); | 405 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 394 auto callable = tester.GetCallable<>(); | 406 auto callable = tester.GetCallable<>(); |
| 395 Handle<Object> return_val = callable().ToHandleChecked(); | 407 Handle<Object> return_val = callable().ToHandleChecked(); |
| 396 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(21)); | 408 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(21)); |
| 397 } | 409 } |
| 398 | 410 |
| 399 | 411 |
| 400 TEST(InterpreterParameter1) { | 412 TEST(InterpreterParameter1) { |
| 401 HandleAndZoneScope handles; | 413 HandleAndZoneScope handles; |
| 402 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); | 414 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 403 builder.set_locals_count(1); | 415 builder.set_locals_count(0); |
| 404 builder.set_parameter_count(1); | 416 builder.set_parameter_count(1); |
| 405 builder.LoadAccumulatorWithRegister(builder.Parameter(0)).Return(); | 417 builder.LoadAccumulatorWithRegister(builder.Parameter(0)).Return(); |
| 406 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); | 418 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 407 | 419 |
| 408 InterpreterTester tester(handles.main_isolate(), bytecode_array); | 420 InterpreterTester tester(handles.main_isolate(), bytecode_array); |
| 409 auto callable = tester.GetCallable<Handle<Object>>(); | 421 auto callable = tester.GetCallable<Handle<Object>>(); |
| 410 | 422 |
| 411 // Check for heap objects. | 423 // Check for heap objects. |
| 412 Handle<Object> true_value = handles.main_isolate()->factory()->true_value(); | 424 Handle<Object> true_value = handles.main_isolate()->factory()->true_value(); |
| 413 Handle<Object> return_val = callable(true_value).ToHandleChecked(); | 425 Handle<Object> return_val = callable(true_value).ToHandleChecked(); |
| 414 CHECK(return_val.is_identical_to(true_value)); | 426 CHECK(return_val.is_identical_to(true_value)); |
| 415 | 427 |
| 416 // Check for Smis. | 428 // Check for Smis. |
| 417 return_val = callable(Handle<Smi>(Smi::FromInt(3), handles.main_isolate())) | 429 return_val = callable(Handle<Smi>(Smi::FromInt(3), handles.main_isolate())) |
| 418 .ToHandleChecked(); | 430 .ToHandleChecked(); |
| 419 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(3)); | 431 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(3)); |
| 420 } | 432 } |
| 421 | 433 |
| 422 | 434 |
| 423 TEST(InterpreterParameter8) { | 435 TEST(InterpreterParameter8) { |
| 424 HandleAndZoneScope handles; | 436 HandleAndZoneScope handles; |
| 425 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); | 437 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 426 builder.set_locals_count(1); | 438 builder.set_locals_count(0); |
| 427 builder.set_parameter_count(8); | 439 builder.set_parameter_count(8); |
| 428 builder.LoadAccumulatorWithRegister(builder.Parameter(0)) | 440 builder.LoadAccumulatorWithRegister(builder.Parameter(0)) |
| 429 .BinaryOperation(Token::Value::ADD, builder.Parameter(1)) | 441 .BinaryOperation(Token::Value::ADD, builder.Parameter(1)) |
| 430 .BinaryOperation(Token::Value::ADD, builder.Parameter(2)) | 442 .BinaryOperation(Token::Value::ADD, builder.Parameter(2)) |
| 431 .BinaryOperation(Token::Value::ADD, builder.Parameter(3)) | 443 .BinaryOperation(Token::Value::ADD, builder.Parameter(3)) |
| 432 .BinaryOperation(Token::Value::ADD, builder.Parameter(4)) | 444 .BinaryOperation(Token::Value::ADD, builder.Parameter(4)) |
| 433 .BinaryOperation(Token::Value::ADD, builder.Parameter(5)) | 445 .BinaryOperation(Token::Value::ADD, builder.Parameter(5)) |
| 434 .BinaryOperation(Token::Value::ADD, builder.Parameter(6)) | 446 .BinaryOperation(Token::Value::ADD, builder.Parameter(6)) |
| 435 .BinaryOperation(Token::Value::ADD, builder.Parameter(7)) | 447 .BinaryOperation(Token::Value::ADD, builder.Parameter(7)) |
| 436 .Return(); | 448 .Return(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 447 Handle<Smi> arg5 = Handle<Smi>(Smi::FromInt(5), handles.main_isolate()); | 459 Handle<Smi> arg5 = Handle<Smi>(Smi::FromInt(5), handles.main_isolate()); |
| 448 Handle<Smi> arg6 = Handle<Smi>(Smi::FromInt(6), handles.main_isolate()); | 460 Handle<Smi> arg6 = Handle<Smi>(Smi::FromInt(6), handles.main_isolate()); |
| 449 Handle<Smi> arg7 = Handle<Smi>(Smi::FromInt(7), handles.main_isolate()); | 461 Handle<Smi> arg7 = Handle<Smi>(Smi::FromInt(7), handles.main_isolate()); |
| 450 Handle<Smi> arg8 = Handle<Smi>(Smi::FromInt(8), handles.main_isolate()); | 462 Handle<Smi> arg8 = Handle<Smi>(Smi::FromInt(8), handles.main_isolate()); |
| 451 // Check for Smis. | 463 // Check for Smis. |
| 452 Handle<Object> return_val = | 464 Handle<Object> return_val = |
| 453 callable(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) | 465 callable(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) |
| 454 .ToHandleChecked(); | 466 .ToHandleChecked(); |
| 455 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(36)); | 467 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(36)); |
| 456 } | 468 } |
| 469 |
| 470 |
| 471 TEST(InterpreterLoadNamedProperty) { |
| 472 HandleAndZoneScope handles; |
| 473 i::Isolate* isolate = handles.main_isolate(); |
| 474 i::Factory* factory = isolate->factory(); |
| 475 |
| 476 i::Code::Kind ic_kinds[] = { i::Code::LOAD_IC }; |
| 477 i::FeedbackVectorSpec feedback_spec(0, 1, ic_kinds); |
| 478 Handle<i::TypeFeedbackVector> vector = |
| 479 factory->NewTypeFeedbackVector(&feedback_spec); |
| 480 |
| 481 Handle<i::String> name = factory->NewStringFromAsciiChecked("val"); |
| 482 name = factory->string_table()->LookupString(isolate, name); |
| 483 |
| 484 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 485 builder.set_locals_count(1); |
| 486 builder.set_parameter_count(1); |
| 487 builder.LoadLiteral(name) |
| 488 .StoreAccumulatorInRegister(Register(0)) |
| 489 .LoadAccumulatorWithRegister(builder.Parameter(0)) |
| 490 .LoadNamedProperty(Register(0), vector->first_ic_slot_index(), i::SLOPPY) |
| 491 .Return(); |
| 492 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 493 |
| 494 InterpreterTester tester(handles.main_isolate(), bytecode_array, vector); |
| 495 auto callable = tester.GetCallable<Handle<Object>>(); |
| 496 |
| 497 Handle<Object> object = tester.NewObject("({ val : 123 })"); |
| 498 // Test IC miss. |
| 499 Handle<Object> return_val = callable(object).ToHandleChecked(); |
| 500 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(123)); |
| 501 |
| 502 // Test transition to monomorphic IC. |
| 503 return_val = callable(object).ToHandleChecked(); |
| 504 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(123)); |
| 505 |
| 506 // Test transition to polymorphic IC. |
| 507 Handle<Object> object2 = tester.NewObject("({ val : 456, other : 123 })"); |
| 508 return_val = callable(object2).ToHandleChecked(); |
| 509 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(456)); |
| 510 |
| 511 // Test transition to megamorphic IC. |
| 512 Handle<Object> object3 = tester.NewObject("({ val : 789, val2 : 123 })"); |
| 513 callable(object3).ToHandleChecked(); |
| 514 Handle<Object> object4 = tester.NewObject("({ val : 789, val3 : 123 })"); |
| 515 callable(object4).ToHandleChecked(); |
| 516 Handle<Object> object5 = tester.NewObject("({ val : 789, val4 : 123 })"); |
| 517 return_val = callable(object5).ToHandleChecked(); |
| 518 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(789)); |
| 519 } |
| 520 |
| 521 |
| 522 TEST(InterpreterLoadKeyedProperty) { |
| 523 HandleAndZoneScope handles; |
| 524 i::Isolate* isolate = handles.main_isolate(); |
| 525 i::Factory* factory = isolate->factory(); |
| 526 |
| 527 i::Code::Kind ic_kinds[] = { i::Code::KEYED_LOAD_IC }; |
| 528 i::FeedbackVectorSpec feedback_spec(0, 1, ic_kinds); |
| 529 Handle<i::TypeFeedbackVector> vector = |
| 530 factory->NewTypeFeedbackVector(&feedback_spec); |
| 531 |
| 532 Handle<i::String> key = factory->NewStringFromAsciiChecked("key"); |
| 533 key = factory->string_table()->LookupString(isolate, key); |
| 534 |
| 535 BytecodeArrayBuilder builder(handles.main_isolate(), handles.main_zone()); |
| 536 builder.set_locals_count(1); |
| 537 builder.set_parameter_count(1); |
| 538 builder.LoadLiteral(key) |
| 539 .StoreAccumulatorInRegister(Register(0)) |
| 540 .LoadAccumulatorWithRegister(builder.Parameter(0)) |
| 541 .LoadKeyedProperty(Register(0), vector->first_ic_slot_index(), i::SLOPPY) |
| 542 .Return(); |
| 543 Handle<BytecodeArray> bytecode_array = builder.ToBytecodeArray(); |
| 544 |
| 545 InterpreterTester tester(handles.main_isolate(), bytecode_array, vector); |
| 546 auto callable = tester.GetCallable<Handle<Object>>(); |
| 547 |
| 548 Handle<Object> object = tester.NewObject("({ key : 123 })"); |
| 549 // Test IC miss. |
| 550 Handle<Object> return_val = callable(object).ToHandleChecked(); |
| 551 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(123)); |
| 552 |
| 553 // Test transition to monomorphic IC. |
| 554 return_val = callable(object).ToHandleChecked(); |
| 555 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(123)); |
| 556 |
| 557 // Test transition to megamorphic IC. |
| 558 Handle<Object> object3 = tester.NewObject("({ key : 789, val2 : 123 })"); |
| 559 return_val = callable(object3).ToHandleChecked(); |
| 560 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(789)); |
| 561 } |
| OLD | NEW |