Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 27 | x.MakeStatementPosition(1); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 36 | y.MakeStatementPosition(1); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 37 | CHECK_EQ(y.source_position(), 1); |
| 38 | CHECK_EQ(y.is_statement(), true); |
| 39 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 40 | y.MakeStatementPosition(2); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 41 | CHECK_EQ(y.source_position(), 2); |
| 42 | CHECK_EQ(y.is_statement(), true); |
| 43 | |
| 44 | y.set_invalid(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 45 | y.MakeExpressionPosition(3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 46 | CHECK_EQ(y.source_position(), 3); |
| 47 | CHECK_EQ(y.is_statement(), false); |
| 48 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 49 | y.MakeStatementPosition(3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 64 | CHECK(!node.source_info().is_valid()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | TEST_F(BytecodeNodeTest, Constructor2) { |
| 68 | uint32_t operands[] = {0x11}; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 69 | BytecodeNode node(Bytecode::kJumpIfTrue, operands[0]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 70 | CHECK_EQ(node.bytecode(), Bytecode::kJumpIfTrue); |
| 71 | CHECK_EQ(node.operand_count(), 1); |
| 72 | CHECK_EQ(node.operand(0), operands[0]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 73 | CHECK(!node.source_info().is_valid()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST_F(BytecodeNodeTest, Constructor3) { |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 77 | uint32_t operands[] = {0x11}; |
| 78 | BytecodeNode node(Bytecode::kLdaGlobal, operands[0]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 79 | CHECK_EQ(node.bytecode(), Bytecode::kLdaGlobal); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 80 | CHECK_EQ(node.operand_count(), 1); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 81 | CHECK_EQ(node.operand(0), operands[0]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 82 | CHECK(!node.source_info().is_valid()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_F(BytecodeNodeTest, Constructor4) { |
| 86 | uint32_t operands[] = {0x11, 0x22, 0x33}; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 87 | BytecodeNode node(Bytecode::kLdaNamedProperty, operands[0], operands[1], |
| 88 | operands[2]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 89 | CHECK_EQ(node.operand_count(), 3); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 90 | CHECK_EQ(node.bytecode(), Bytecode::kLdaNamedProperty); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 91 | CHECK_EQ(node.operand(0), operands[0]); |
| 92 | CHECK_EQ(node.operand(1), operands[1]); |
| 93 | CHECK_EQ(node.operand(2), operands[2]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 94 | CHECK(!node.source_info().is_valid()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 100 | operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 107 | CHECK(!node.source_info().is_valid()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 113 | operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 114 | CHECK_EQ(node, node); |
| 115 | BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 116 | operands[2], operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 123 | operands[3]); |
| 124 | node.source_info().MakeStatementPosition(3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 125 | CHECK_EQ(node, node); |
| 126 | BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 127 | operands[2], operands[3]); |
| 128 | other.source_info().MakeStatementPosition(3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 135 | operands[3]); |
| 136 | node.source_info().MakeStatementPosition(3); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 137 | BytecodeNode other(Bytecode::kForInNext, operands[0], operands[1], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 138 | operands[2], operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 145 | operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 154 | operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 155 | BytecodeSourceInfo source_info(77, false); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 156 | node.source_info().Clone(source_info); |
| 157 | CHECK_EQ(node.source_info(), source_info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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], |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 170 | operands[3]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 171 | BytecodeSourceInfo source_info(77, false); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 172 | node.source_info().Clone(source_info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 173 | |
| 174 | BytecodeNode clone; |
| 175 | clone.Clone(&node); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 176 | clone.set_bytecode(Bytecode::kJump, 0x01aabbcc); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 177 | CHECK_EQ(clone.bytecode(), Bytecode::kJump); |
| 178 | CHECK_EQ(clone.operand_count(), 1); |
| 179 | CHECK_EQ(clone.operand(0), 0x01aabbcc); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 180 | CHECK_EQ(clone.source_info(), source_info); |
| 181 | } |
| 182 | |
| 183 | } // namespace interpreter |
| 184 | } // namespace internal |
| 185 | } // namespace v8 |