1 // Copyright 2016 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/interpreter/bytecode-pipeline.h" 8 #include "src/interpreter/bytecode-register-allocator.h" 9 #include "src/isolate.h" 10 #include "test/unittests/test-utils.h" 11 12 namespace v8 { 13 namespace internal { 14 namespace interpreter { 15 16 using BytecodeNodeTest = TestWithIsolateAndZone; 17 18 TEST(BytecodeSourceInfo, Operations) { 19 BytecodeSourceInfo x(0, true); 20 CHECK_EQ(x.source_position(), 0); 21 CHECK_EQ(x.is_statement(), true); 22 CHECK_EQ(x.is_valid(), true); 23 x.set_invalid(); 24 CHECK_EQ(x.is_statement(), false); 25 CHECK_EQ(x.is_valid(), false); 26 27 x.MakeStatementPosition(1); 28 BytecodeSourceInfo y(1, true); 29 CHECK(x == y); 30 CHECK(!(x != y)); 31 32 x.set_invalid(); 33 CHECK(!(x == y)); 34 CHECK(x != y); 35 36 y.MakeStatementPosition(1); 37 CHECK_EQ(y.source_position(), 1); 38 CHECK_EQ(y.is_statement(), true); 39 40 y.MakeStatementPosition(2); 41 CHECK_EQ(y.source_position(), 2); 42 CHECK_EQ(y.is_statement(), true); 43 44 y.set_invalid(); 45 y.MakeExpressionPosition(3); 46 CHECK_EQ(y.source_position(), 3); 47 CHECK_EQ(y.is_statement(), false); 48 49 y.MakeStatementPosition(3); 50 CHECK_EQ(y.source_position(), 3); 51 CHECK_EQ(y.is_statement(), true); 52 } 53 54 TEST_F(BytecodeNodeTest, Constructor0) { 55 BytecodeNode node; 56 CHECK_EQ(node.bytecode(), Bytecode::kIllegal); 57 CHECK(!node.source_info().is_valid()); 58 } 59 60 TEST_F(BytecodeNodeTest, Constructor1) { 61 BytecodeNode node(Bytecode::kLdaZero); 62 CHECK_EQ(node.bytecode(), Bytecode::kLdaZero); 63 CHECK_EQ(node.operand_count(), 0); 64 CHECK(!node.source_info().is_valid()); 65 } 66 67 TEST_F(BytecodeNodeTest, Constructor2) { 68 uint32_t operands[] = {0x11}; 69 BytecodeNode node(Bytecode::kJumpIfTrue, operands[0]); 70 CHECK_EQ(node.bytecode(), Bytecode::kJumpIfTrue); 71 CHECK_EQ(node.operand_count(), 1); 72 CHECK_EQ(node.operand(0), operands[0]); 73 CHECK(!node.source_info().is_valid()); 74 } 75 76 TEST_F(BytecodeNodeTest, Constructor3) { 77 uint32_t operands[] = {0x11}; 78 BytecodeNode node(Bytecode::kLdaGlobal, operands[0]); 79 CHECK_EQ(node.bytecode(), Bytecode::kLdaGlobal); 80 CHECK_EQ(node.operand_count(), 1); 81 CHECK_EQ(node.operand(0), operands[0]); 82 CHECK(!node.source_info().is_valid()); 83 } 84 85 TEST_F(BytecodeNodeTest, Constructor4) { 86 uint32_t operands[] = {0x11, 0x22, 0x33}; 87 BytecodeNode node(Bytecode::kLdaNamedProperty, operands[0], operands[1], 88 operands[2]); 89 CHECK_EQ(node.operand_count(), 3); 90 CHECK_EQ(node.bytecode(), Bytecode::kLdaNamedProperty); 91 CHECK_EQ(node.operand(0), operands[0]); 92 CHECK_EQ(node.operand(1), operands[1]); 93 CHECK_EQ(node.operand(2), operands[2]); 94 CHECK(!node.source_info().is_valid()); 95 } 96 97 TEST_F(BytecodeNodeTest, Constructor5) { 98 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 99 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 100 operands[3]); 101 CHECK_EQ(node.operand_count(), 4); 102 CHECK_EQ(node.bytecode(), Bytecode::kForInNext); 103 CHECK_EQ(node.operand(0), operands[0]); 104 CHECK_EQ(node.operand(1), operands[1]); 105 CHECK_EQ(node.operand(2), operands[2]); 106 CHECK_EQ(node.operand(3), operands[3]); 107 CHECK(!node.source_info().is_valid()); 108 } 109 110 TEST_F(BytecodeNodeTest, Equality) { 111 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 112 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 113 operands[3]); 114 CHECK_EQ(node, node); 115 BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], 116 operands[2], operands[3]); 117 CHECK_EQ(node, other); 118 } 119 120 TEST_F(BytecodeNodeTest, EqualityWithSourceInfo) { 121 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 122 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 123 operands[3]); 124 node.source_info().MakeStatementPosition(3); 125 CHECK_EQ(node, node); 126 BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], 127 operands[2], operands[3]); 128 other.source_info().MakeStatementPosition(3); 129 CHECK_EQ(node, other); 130 } 131 132 TEST_F(BytecodeNodeTest, NoEqualityWithDifferentSourceInfo) { 133 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 134 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 135 operands[3]); 136 node.source_info().MakeStatementPosition(3); 137 BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], 138 operands[2], operands[3]); 139 CHECK_NE(node, other); 140 } 141 142 TEST_F(BytecodeNodeTest, Clone) { 143 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 144 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 145 operands[3]); 146 BytecodeNode clone; 147 clone.Clone(&node); 148 CHECK_EQ(clone, node); 149 } 150 151 TEST_F(BytecodeNodeTest, SetBytecode0) { 152 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 153 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 154 operands[3]); 155 BytecodeSourceInfo source_info(77, false); 156 node.source_info().Clone(source_info); 157 CHECK_EQ(node.source_info(), source_info); 158 159 BytecodeNode clone; 160 clone.Clone(&node); 161 clone.set_bytecode(Bytecode::kNop); 162 CHECK_EQ(clone.bytecode(), Bytecode::kNop); 163 CHECK_EQ(clone.operand_count(), 0); 164 CHECK_EQ(clone.source_info(), source_info); 165 } 166 167 TEST_F(BytecodeNodeTest, SetBytecode1) { 168 uint32_t operands[] = {0x71, 0xa5, 0x5a, 0xfc}; 169 BytecodeNode node(Bytecode::kForInNext, operands[0], operands[1], operands[2], 170 operands[3]); 171 BytecodeSourceInfo source_info(77, false); 172 node.source_info().Clone(source_info); 173 174 BytecodeNode clone; 175 clone.Clone(&node); 176 clone.set_bytecode(Bytecode::kJump, 0x01aabbcc); 177 CHECK_EQ(clone.bytecode(), Bytecode::kJump); 178 CHECK_EQ(clone.operand_count(), 1); 179 CHECK_EQ(clone.operand(0), 0x01aabbcc); 180 CHECK_EQ(clone.source_info(), source_info); 181 } 182 183 } // namespace interpreter 184 } // namespace internal 185 } // namespace v8 186