| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/interpreter/bytecode-array-builder.h" | 7 #include "src/interpreter/bytecode-array-builder.h" |
| 8 #include "test/cctest/cctest.h" | 8 #include "test/unittests/test-utils.h" |
| 9 | 9 |
| 10 using namespace v8::internal; | 10 namespace v8 { |
| 11 using namespace v8::internal::interpreter; | 11 namespace internal { |
| 12 namespace interpreter { |
| 12 | 13 |
| 13 TEST(AllBytecodesGenerated) { | 14 class BytecodeArrayBuilderTest : public TestWithIsolate { |
| 14 InitializedHandleScope handle_scope; | 15 public: |
| 15 BytecodeArrayBuilder builder(handle_scope.main_isolate()); | 16 BytecodeArrayBuilderTest() {} |
| 17 ~BytecodeArrayBuilderTest() override {} |
| 18 }; |
| 19 |
| 20 |
| 21 TEST_F(BytecodeArrayBuilderTest, AllBytecodesGenerated) { |
| 22 BytecodeArrayBuilder builder(isolate()); |
| 16 | 23 |
| 17 builder.set_locals_count(1); | 24 builder.set_locals_count(1); |
| 18 CHECK_EQ(builder.locals_count(), 1); | 25 CHECK_EQ(builder.locals_count(), 1); |
| 19 | 26 |
| 20 // Emit constant loads. | 27 // Emit constant loads. |
| 21 builder.LoadLiteral(Smi::FromInt(0)) | 28 builder.LoadLiteral(Smi::FromInt(0)) |
| 22 .LoadLiteral(Smi::FromInt(8)) | 29 .LoadLiteral(Smi::FromInt(8)) |
| 23 .LoadUndefined() | 30 .LoadUndefined() |
| 24 .LoadNull() | 31 .LoadNull() |
| 25 .LoadTheHole() | 32 .LoadTheHole() |
| 26 .LoadTrue() | 33 .LoadTrue() |
| 27 .LoadFalse(); | 34 .LoadFalse(); |
| 28 | 35 |
| 29 // Emit accumulator transfers. | 36 // Emit accumulator transfers. |
| 30 builder.LoadAccumulatorWithRegister(0).StoreAccumulatorInRegister(0); | 37 Register reg(0); |
| 38 builder.LoadAccumulatorWithRegister(reg).StoreAccumulatorInRegister(reg); |
| 31 | 39 |
| 32 // Emit binary operators invocations. | 40 // Emit binary operators invocations. |
| 33 builder.BinaryOperation(Token::Value::ADD, 0) | 41 builder.BinaryOperation(Token::Value::ADD, reg) |
| 34 .BinaryOperation(Token::Value::SUB, 0) | 42 .BinaryOperation(Token::Value::SUB, reg) |
| 35 .BinaryOperation(Token::Value::MUL, 0) | 43 .BinaryOperation(Token::Value::MUL, reg) |
| 36 .BinaryOperation(Token::Value::DIV, 0); | 44 .BinaryOperation(Token::Value::DIV, reg); |
| 37 | 45 |
| 38 // Emit control flow. Return must be the last instruction. | 46 // Emit control flow. Return must be the last instruction. |
| 39 builder.Return(); | 47 builder.Return(); |
| 40 | 48 |
| 41 // Generate BytecodeArray. | 49 // Generate BytecodeArray. |
| 42 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); | 50 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); |
| 43 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize); | 51 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize); |
| 44 | 52 |
| 45 // Build scorecard of bytecodes encountered in the BytecodeArray. | 53 // Build scorecard of bytecodes encountered in the BytecodeArray. |
| 46 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1); | 54 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 59 CHECK_EQ(scorecard[Bytecodes::ToByte(final_bytecode)], 1); | 67 CHECK_EQ(scorecard[Bytecodes::ToByte(final_bytecode)], 1); |
| 60 | 68 |
| 61 #define CHECK_BYTECODE_PRESENT(Name, ...) \ | 69 #define CHECK_BYTECODE_PRESENT(Name, ...) \ |
| 62 /* Check Bytecode is marked in scorecard */ \ | 70 /* Check Bytecode is marked in scorecard */ \ |
| 63 CHECK_GE(scorecard[Bytecodes::ToByte(Bytecode::k##Name)], 1); | 71 CHECK_GE(scorecard[Bytecodes::ToByte(Bytecode::k##Name)], 1); |
| 64 BYTECODE_LIST(CHECK_BYTECODE_PRESENT) | 72 BYTECODE_LIST(CHECK_BYTECODE_PRESENT) |
| 65 #undef CHECK_BYTECODE_PRESENT | 73 #undef CHECK_BYTECODE_PRESENT |
| 66 } | 74 } |
| 67 | 75 |
| 68 | 76 |
| 69 TEST(FrameSizesLookGood) { | 77 TEST_F(BytecodeArrayBuilderTest, FrameSizesLookGood) { |
| 70 for (int locals = 0; locals < 5; locals++) { | 78 for (int locals = 0; locals < 5; locals++) { |
| 71 for (int temps = 0; temps < 3; temps++) { | 79 for (int temps = 0; temps < 3; temps++) { |
| 72 InitializedHandleScope handle_scope; | 80 BytecodeArrayBuilder builder(isolate()); |
| 73 BytecodeArrayBuilder builder(handle_scope.main_isolate()); | |
| 74 builder.set_locals_count(locals); | 81 builder.set_locals_count(locals); |
| 75 builder.Return(); | 82 builder.Return(); |
| 76 | 83 |
| 77 TemporaryRegisterScope temporaries(&builder); | 84 TemporaryRegisterScope temporaries(&builder); |
| 78 for (int i = 0; i < temps; i++) { | 85 for (int i = 0; i < temps; i++) { |
| 79 temporaries.NewRegister(); | 86 temporaries.NewRegister(); |
| 80 } | 87 } |
| 81 | 88 |
| 82 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); | 89 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); |
| 83 int total_registers = locals + temps; | 90 int total_registers = locals + temps; |
| 84 CHECK_EQ(the_array->frame_size(), total_registers * kPointerSize); | 91 CHECK_EQ(the_array->frame_size(), total_registers * kPointerSize); |
| 85 } | 92 } |
| 86 } | 93 } |
| 87 } | 94 } |
| 88 | 95 |
| 89 | 96 |
| 90 TEST(TemporariesRecycled) { | 97 TEST_F(BytecodeArrayBuilderTest, TemporariesRecycled) { |
| 91 InitializedHandleScope handle_scope; | 98 BytecodeArrayBuilder builder(isolate()); |
| 92 BytecodeArrayBuilder builder(handle_scope.main_isolate()); | |
| 93 builder.set_locals_count(0); | 99 builder.set_locals_count(0); |
| 94 builder.Return(); | 100 builder.Return(); |
| 95 | 101 |
| 96 int first; | 102 int first; |
| 97 { | 103 { |
| 98 TemporaryRegisterScope temporaries(&builder); | 104 TemporaryRegisterScope temporaries(&builder); |
| 99 first = temporaries.NewRegister(); | 105 first = temporaries.NewRegister().index(); |
| 100 temporaries.NewRegister(); | 106 temporaries.NewRegister(); |
| 101 temporaries.NewRegister(); | 107 temporaries.NewRegister(); |
| 102 temporaries.NewRegister(); | 108 temporaries.NewRegister(); |
| 103 } | 109 } |
| 104 | 110 |
| 105 int second; | 111 int second; |
| 106 { | 112 { |
| 107 TemporaryRegisterScope temporaries(&builder); | 113 TemporaryRegisterScope temporaries(&builder); |
| 108 second = temporaries.NewRegister(); | 114 second = temporaries.NewRegister().index(); |
| 109 } | 115 } |
| 110 | 116 |
| 111 CHECK_EQ(first, second); | 117 CHECK_EQ(first, second); |
| 112 } | 118 } |
| 119 |
| 120 } // namespace interpreter |
| 121 } // namespace internal |
| 122 } // namespace v8 |
| OLD | NEW |