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 <iostream> |
| 6 |
| 7 #include "src/compiler/bytecode-graph-builder.h" |
| 8 #include "src/compiler/common-operator.h" |
| 9 #include "src/compiler/graph-visualizer.h" |
| 10 #include "src/compiler/instruction.h" |
| 11 #include "src/compiler/instruction-selector.h" |
| 12 #include "src/compiler/js-graph.h" |
| 13 #include "src/compiler/js-operator.h" |
| 14 #include "src/interpreter/bytecode-array-builder.h" |
| 15 #include "src/parser.h" |
| 16 #include "test/unittests/compiler/compiler-test-utils.h" |
| 17 #include "test/unittests/compiler/graph-unittest.h" |
| 18 #include "test/unittests/compiler/node-test-utils.h" |
| 19 #include "test/unittests/test-utils.h" |
| 20 |
| 21 using ::testing::_; |
| 22 |
| 23 namespace v8 { |
| 24 namespace internal { |
| 25 namespace compiler { |
| 26 |
| 27 class BytecodeGraphBuilderTest : public TestWithIsolateAndZone { |
| 28 public: |
| 29 BytecodeGraphBuilderTest() : array_builder_(isolate(), zone()) {} |
| 30 |
| 31 Graph* GetCompletedGraph(); |
| 32 |
| 33 Matcher<Node*> IsUndefinedConstant(); |
| 34 Matcher<Node*> IsNullConstant(); |
| 35 Matcher<Node*> IsTheHoleConstant(); |
| 36 Matcher<Node*> IsFalseConstant(); |
| 37 Matcher<Node*> IsTrueConstant(); |
| 38 |
| 39 interpreter::BytecodeArrayBuilder* array_builder() { return &array_builder_; } |
| 40 |
| 41 private: |
| 42 interpreter::BytecodeArrayBuilder array_builder_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(BytecodeGraphBuilderTest); |
| 45 }; |
| 46 |
| 47 |
| 48 Graph* BytecodeGraphBuilderTest::GetCompletedGraph() { |
| 49 MachineOperatorBuilder* machine = new (zone()) MachineOperatorBuilder( |
| 50 zone(), kMachPtr, InstructionSelector::SupportedMachineOperatorFlags()); |
| 51 CommonOperatorBuilder* common = new (zone()) CommonOperatorBuilder(zone()); |
| 52 JSOperatorBuilder* javascript = new (zone()) JSOperatorBuilder(zone()); |
| 53 Graph* graph = new (zone()) Graph(zone()); |
| 54 JSGraph* jsgraph = |
| 55 new (zone()) JSGraph(isolate(), graph, common, javascript, machine); |
| 56 |
| 57 Handle<String> name = factory()->NewStringFromStaticChars("test"); |
| 58 Handle<SharedFunctionInfo> shared_info = |
| 59 factory()->NewSharedFunctionInfo(name, MaybeHandle<Code>()); |
| 60 |
| 61 ParseInfo parse_info(zone(), shared_info); |
| 62 CompilationInfo info(&parse_info); |
| 63 Handle<BytecodeArray> bytecode_array = array_builder()->ToBytecodeArray(); |
| 64 info.shared_info()->set_function_data(*bytecode_array); |
| 65 |
| 66 BytecodeGraphBuilder graph_builder(zone(), &info, jsgraph); |
| 67 graph_builder.CreateGraph(); |
| 68 return graph; |
| 69 } |
| 70 |
| 71 |
| 72 Matcher<Node*> BytecodeGraphBuilderTest::IsUndefinedConstant() { |
| 73 return IsHeapConstant(factory()->undefined_value()); |
| 74 } |
| 75 |
| 76 |
| 77 Matcher<Node*> BytecodeGraphBuilderTest::IsNullConstant() { |
| 78 return IsHeapConstant(factory()->null_value()); |
| 79 } |
| 80 |
| 81 |
| 82 Matcher<Node*> BytecodeGraphBuilderTest::IsTheHoleConstant() { |
| 83 return IsHeapConstant(factory()->the_hole_value()); |
| 84 } |
| 85 |
| 86 |
| 87 Matcher<Node*> BytecodeGraphBuilderTest::IsFalseConstant() { |
| 88 return IsHeapConstant(factory()->false_value()); |
| 89 } |
| 90 |
| 91 |
| 92 Matcher<Node*> BytecodeGraphBuilderTest::IsTrueConstant() { |
| 93 return IsHeapConstant(factory()->true_value()); |
| 94 } |
| 95 |
| 96 |
| 97 TEST_F(BytecodeGraphBuilderTest, ReturnUndefined) { |
| 98 array_builder()->set_locals_count(0); |
| 99 array_builder()->set_parameter_count(1); |
| 100 array_builder()->LoadUndefined().Return(); |
| 101 |
| 102 Graph* graph = GetCompletedGraph(); |
| 103 Node* end = graph->end(); |
| 104 EXPECT_EQ(1, end->InputCount()); |
| 105 Node* ret = end->InputAt(0); |
| 106 Node* effect = graph->start(); |
| 107 Node* control = graph->start(); |
| 108 EXPECT_THAT(ret, IsReturn(IsUndefinedConstant(), effect, control)); |
| 109 } |
| 110 |
| 111 |
| 112 TEST_F(BytecodeGraphBuilderTest, ReturnNull) { |
| 113 array_builder()->set_locals_count(0); |
| 114 array_builder()->set_parameter_count(1); |
| 115 array_builder()->LoadNull().Return(); |
| 116 |
| 117 Graph* graph = GetCompletedGraph(); |
| 118 Node* end = graph->end(); |
| 119 EXPECT_EQ(1, end->InputCount()); |
| 120 Node* ret = end->InputAt(0); |
| 121 EXPECT_THAT(ret, IsReturn(IsNullConstant(), graph->start(), graph->start())); |
| 122 } |
| 123 |
| 124 |
| 125 TEST_F(BytecodeGraphBuilderTest, ReturnTheHole) { |
| 126 array_builder()->set_locals_count(0); |
| 127 array_builder()->set_parameter_count(1); |
| 128 array_builder()->LoadTheHole().Return(); |
| 129 |
| 130 Graph* graph = GetCompletedGraph(); |
| 131 Node* end = graph->end(); |
| 132 EXPECT_EQ(1, end->InputCount()); |
| 133 Node* ret = end->InputAt(0); |
| 134 Node* effect = graph->start(); |
| 135 Node* control = graph->start(); |
| 136 EXPECT_THAT(ret, IsReturn(IsTheHoleConstant(), effect, control)); |
| 137 } |
| 138 |
| 139 |
| 140 TEST_F(BytecodeGraphBuilderTest, ReturnTrue) { |
| 141 array_builder()->set_locals_count(0); |
| 142 array_builder()->set_parameter_count(1); |
| 143 array_builder()->LoadTrue().Return(); |
| 144 |
| 145 Graph* graph = GetCompletedGraph(); |
| 146 Node* end = graph->end(); |
| 147 EXPECT_EQ(1, end->InputCount()); |
| 148 Node* ret = end->InputAt(0); |
| 149 Node* effect = graph->start(); |
| 150 Node* control = graph->start(); |
| 151 EXPECT_THAT(ret, IsReturn(IsTrueConstant(), effect, control)); |
| 152 } |
| 153 |
| 154 |
| 155 TEST_F(BytecodeGraphBuilderTest, ReturnFalse) { |
| 156 array_builder()->set_locals_count(0); |
| 157 array_builder()->set_parameter_count(1); |
| 158 array_builder()->LoadFalse().Return(); |
| 159 |
| 160 Graph* graph = GetCompletedGraph(); |
| 161 Node* end = graph->end(); |
| 162 EXPECT_EQ(1, end->InputCount()); |
| 163 Node* ret = end->InputAt(0); |
| 164 Node* effect = graph->start(); |
| 165 Node* control = graph->start(); |
| 166 EXPECT_THAT(ret, IsReturn(IsFalseConstant(), effect, control)); |
| 167 } |
| 168 |
| 169 |
| 170 TEST_F(BytecodeGraphBuilderTest, ReturnInt8) { |
| 171 static const int kValue = 3; |
| 172 array_builder()->set_locals_count(0); |
| 173 array_builder()->set_parameter_count(1); |
| 174 array_builder()->LoadLiteral(Smi::FromInt(kValue)).Return(); |
| 175 |
| 176 Graph* graph = GetCompletedGraph(); |
| 177 Node* end = graph->end(); |
| 178 EXPECT_EQ(1, end->InputCount()); |
| 179 Node* ret = end->InputAt(0); |
| 180 Node* effect = graph->start(); |
| 181 Node* control = graph->start(); |
| 182 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); |
| 183 } |
| 184 |
| 185 |
| 186 TEST_F(BytecodeGraphBuilderTest, ReturnDouble) { |
| 187 const double kValue = 0.123456789; |
| 188 array_builder()->set_locals_count(0); |
| 189 array_builder()->set_parameter_count(1); |
| 190 array_builder()->LoadLiteral(factory()->NewHeapNumber(kValue)); |
| 191 array_builder()->Return(); |
| 192 |
| 193 Graph* graph = GetCompletedGraph(); |
| 194 Node* end = graph->end(); |
| 195 EXPECT_EQ(1, end->InputCount()); |
| 196 Node* ret = end->InputAt(0); |
| 197 Node* effect = graph->start(); |
| 198 Node* control = graph->start(); |
| 199 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); |
| 200 } |
| 201 |
| 202 |
| 203 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithParameters) { |
| 204 array_builder()->set_locals_count(1); |
| 205 array_builder()->set_parameter_count(3); |
| 206 array_builder() |
| 207 ->LoadAccumulatorWithRegister(array_builder()->Parameter(1)) |
| 208 .BinaryOperation(Token::Value::ADD, array_builder()->Parameter(2)) |
| 209 .StoreAccumulatorInRegister(interpreter::Register(0)) |
| 210 .Return(); |
| 211 |
| 212 Graph* graph = GetCompletedGraph(); |
| 213 Node* end = graph->end(); |
| 214 EXPECT_EQ(1, end->InputCount()); |
| 215 Node* ret = end->InputAt(0); |
| 216 // NB binary operation is <reg> <op> <acc>. The register represents |
| 217 // the left-hand side, which is why parameters appear in opposite |
| 218 // order to construction via the builder. |
| 219 EXPECT_THAT(ret, IsReturn(IsJSAdd(IsParameter(2), IsParameter(1)), _, _)); |
| 220 } |
| 221 |
| 222 |
| 223 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { |
| 224 static const int kLeft = -655371; |
| 225 static const int kRight = +2000000; |
| 226 array_builder()->set_locals_count(1); |
| 227 array_builder()->set_parameter_count(1); |
| 228 array_builder() |
| 229 ->LoadLiteral(Smi::FromInt(kLeft)) |
| 230 .StoreAccumulatorInRegister(interpreter::Register(0)) |
| 231 .LoadLiteral(Smi::FromInt(kRight)) |
| 232 .BinaryOperation(Token::Value::ADD, interpreter::Register(0)) |
| 233 .Return(); |
| 234 |
| 235 Graph* graph = GetCompletedGraph(); |
| 236 Node* end = graph->end(); |
| 237 EXPECT_EQ(1, end->InputCount()); |
| 238 Node* ret = end->InputAt(0); |
| 239 EXPECT_THAT( |
| 240 ret, IsReturn(IsJSAdd(IsNumberConstant(kLeft), IsNumberConstant(kRight)), |
| 241 _, _)); |
| 242 } |
| 243 |
| 244 } // namespace compiler |
| 245 } // namespace internal |
| 246 } // namespace v8 |
OLD | NEW |