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 <iostream> | 5 #include <iostream> |
6 | 6 |
7 #include "src/compiler/bytecode-graph-builder.h" | 7 #include "src/compiler/bytecode-graph-builder.h" |
8 #include "src/compiler/common-operator.h" | 8 #include "src/compiler/common-operator.h" |
9 #include "src/compiler/graph-visualizer.h" | 9 #include "src/compiler/graph-visualizer.h" |
10 #include "src/compiler/instruction.h" | 10 #include "src/compiler/instruction.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 Matcher<Node*> BytecodeGraphBuilderTest::IsTrueConstant() { | 94 Matcher<Node*> BytecodeGraphBuilderTest::IsTrueConstant() { |
95 return IsHeapConstant(factory()->true_value()); | 95 return IsHeapConstant(factory()->true_value()); |
96 } | 96 } |
97 | 97 |
98 | 98 |
99 TEST_F(BytecodeGraphBuilderTest, ReturnUndefined) { | 99 TEST_F(BytecodeGraphBuilderTest, ReturnUndefined) { |
100 array_builder()->set_locals_count(0); | 100 array_builder()->set_locals_count(0); |
| 101 array_builder()->set_context_count(0); |
101 array_builder()->set_parameter_count(1); | 102 array_builder()->set_parameter_count(1); |
102 array_builder()->LoadUndefined().Return(); | 103 array_builder()->LoadUndefined().Return(); |
103 | 104 |
104 Graph* graph = GetCompletedGraph(); | 105 Graph* graph = GetCompletedGraph(); |
105 Node* end = graph->end(); | 106 Node* end = graph->end(); |
106 EXPECT_EQ(1, end->InputCount()); | 107 EXPECT_EQ(1, end->InputCount()); |
107 Node* ret = end->InputAt(0); | 108 Node* ret = end->InputAt(0); |
108 Node* effect = graph->start(); | 109 Node* effect = graph->start(); |
109 Node* control = graph->start(); | 110 Node* control = graph->start(); |
110 EXPECT_THAT(ret, IsReturn(IsUndefinedConstant(), effect, control)); | 111 EXPECT_THAT(ret, IsReturn(IsUndefinedConstant(), effect, control)); |
111 } | 112 } |
112 | 113 |
113 | 114 |
114 TEST_F(BytecodeGraphBuilderTest, ReturnNull) { | 115 TEST_F(BytecodeGraphBuilderTest, ReturnNull) { |
115 array_builder()->set_locals_count(0); | 116 array_builder()->set_locals_count(0); |
| 117 array_builder()->set_context_count(0); |
116 array_builder()->set_parameter_count(1); | 118 array_builder()->set_parameter_count(1); |
117 array_builder()->LoadNull().Return(); | 119 array_builder()->LoadNull().Return(); |
118 | 120 |
119 Graph* graph = GetCompletedGraph(); | 121 Graph* graph = GetCompletedGraph(); |
120 Node* end = graph->end(); | 122 Node* end = graph->end(); |
121 EXPECT_EQ(1, end->InputCount()); | 123 EXPECT_EQ(1, end->InputCount()); |
122 Node* ret = end->InputAt(0); | 124 Node* ret = end->InputAt(0); |
123 EXPECT_THAT(ret, IsReturn(IsNullConstant(), graph->start(), graph->start())); | 125 EXPECT_THAT(ret, IsReturn(IsNullConstant(), graph->start(), graph->start())); |
124 } | 126 } |
125 | 127 |
126 | 128 |
127 TEST_F(BytecodeGraphBuilderTest, ReturnTheHole) { | 129 TEST_F(BytecodeGraphBuilderTest, ReturnTheHole) { |
128 array_builder()->set_locals_count(0); | 130 array_builder()->set_locals_count(0); |
| 131 array_builder()->set_context_count(0); |
129 array_builder()->set_parameter_count(1); | 132 array_builder()->set_parameter_count(1); |
130 array_builder()->LoadTheHole().Return(); | 133 array_builder()->LoadTheHole().Return(); |
131 | 134 |
132 Graph* graph = GetCompletedGraph(); | 135 Graph* graph = GetCompletedGraph(); |
133 Node* end = graph->end(); | 136 Node* end = graph->end(); |
134 EXPECT_EQ(1, end->InputCount()); | 137 EXPECT_EQ(1, end->InputCount()); |
135 Node* ret = end->InputAt(0); | 138 Node* ret = end->InputAt(0); |
136 Node* effect = graph->start(); | 139 Node* effect = graph->start(); |
137 Node* control = graph->start(); | 140 Node* control = graph->start(); |
138 EXPECT_THAT(ret, IsReturn(IsTheHoleConstant(), effect, control)); | 141 EXPECT_THAT(ret, IsReturn(IsTheHoleConstant(), effect, control)); |
139 } | 142 } |
140 | 143 |
141 | 144 |
142 TEST_F(BytecodeGraphBuilderTest, ReturnTrue) { | 145 TEST_F(BytecodeGraphBuilderTest, ReturnTrue) { |
143 array_builder()->set_locals_count(0); | 146 array_builder()->set_locals_count(0); |
| 147 array_builder()->set_context_count(0); |
144 array_builder()->set_parameter_count(1); | 148 array_builder()->set_parameter_count(1); |
145 array_builder()->LoadTrue().Return(); | 149 array_builder()->LoadTrue().Return(); |
146 | 150 |
147 Graph* graph = GetCompletedGraph(); | 151 Graph* graph = GetCompletedGraph(); |
148 Node* end = graph->end(); | 152 Node* end = graph->end(); |
149 EXPECT_EQ(1, end->InputCount()); | 153 EXPECT_EQ(1, end->InputCount()); |
150 Node* ret = end->InputAt(0); | 154 Node* ret = end->InputAt(0); |
151 Node* effect = graph->start(); | 155 Node* effect = graph->start(); |
152 Node* control = graph->start(); | 156 Node* control = graph->start(); |
153 EXPECT_THAT(ret, IsReturn(IsTrueConstant(), effect, control)); | 157 EXPECT_THAT(ret, IsReturn(IsTrueConstant(), effect, control)); |
154 } | 158 } |
155 | 159 |
156 | 160 |
157 TEST_F(BytecodeGraphBuilderTest, ReturnFalse) { | 161 TEST_F(BytecodeGraphBuilderTest, ReturnFalse) { |
158 array_builder()->set_locals_count(0); | 162 array_builder()->set_locals_count(0); |
| 163 array_builder()->set_context_count(0); |
159 array_builder()->set_parameter_count(1); | 164 array_builder()->set_parameter_count(1); |
160 array_builder()->LoadFalse().Return(); | 165 array_builder()->LoadFalse().Return(); |
161 | 166 |
162 Graph* graph = GetCompletedGraph(); | 167 Graph* graph = GetCompletedGraph(); |
163 Node* end = graph->end(); | 168 Node* end = graph->end(); |
164 EXPECT_EQ(1, end->InputCount()); | 169 EXPECT_EQ(1, end->InputCount()); |
165 Node* ret = end->InputAt(0); | 170 Node* ret = end->InputAt(0); |
166 Node* effect = graph->start(); | 171 Node* effect = graph->start(); |
167 Node* control = graph->start(); | 172 Node* control = graph->start(); |
168 EXPECT_THAT(ret, IsReturn(IsFalseConstant(), effect, control)); | 173 EXPECT_THAT(ret, IsReturn(IsFalseConstant(), effect, control)); |
169 } | 174 } |
170 | 175 |
171 | 176 |
172 TEST_F(BytecodeGraphBuilderTest, ReturnInt8) { | 177 TEST_F(BytecodeGraphBuilderTest, ReturnInt8) { |
173 static const int kValue = 3; | 178 static const int kValue = 3; |
174 array_builder()->set_locals_count(0); | 179 array_builder()->set_locals_count(0); |
| 180 array_builder()->set_context_count(0); |
175 array_builder()->set_parameter_count(1); | 181 array_builder()->set_parameter_count(1); |
176 array_builder()->LoadLiteral(Smi::FromInt(kValue)).Return(); | 182 array_builder()->LoadLiteral(Smi::FromInt(kValue)).Return(); |
177 | 183 |
178 Graph* graph = GetCompletedGraph(); | 184 Graph* graph = GetCompletedGraph(); |
179 Node* end = graph->end(); | 185 Node* end = graph->end(); |
180 EXPECT_EQ(1, end->InputCount()); | 186 EXPECT_EQ(1, end->InputCount()); |
181 Node* ret = end->InputAt(0); | 187 Node* ret = end->InputAt(0); |
182 Node* effect = graph->start(); | 188 Node* effect = graph->start(); |
183 Node* control = graph->start(); | 189 Node* control = graph->start(); |
184 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); | 190 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); |
185 } | 191 } |
186 | 192 |
187 | 193 |
188 TEST_F(BytecodeGraphBuilderTest, ReturnDouble) { | 194 TEST_F(BytecodeGraphBuilderTest, ReturnDouble) { |
189 const double kValue = 0.123456789; | 195 const double kValue = 0.123456789; |
190 array_builder()->set_locals_count(0); | 196 array_builder()->set_locals_count(0); |
| 197 array_builder()->set_context_count(0); |
191 array_builder()->set_parameter_count(1); | 198 array_builder()->set_parameter_count(1); |
192 array_builder()->LoadLiteral(factory()->NewHeapNumber(kValue)); | 199 array_builder()->LoadLiteral(factory()->NewHeapNumber(kValue)); |
193 array_builder()->Return(); | 200 array_builder()->Return(); |
194 | 201 |
195 Graph* graph = GetCompletedGraph(); | 202 Graph* graph = GetCompletedGraph(); |
196 Node* end = graph->end(); | 203 Node* end = graph->end(); |
197 EXPECT_EQ(1, end->InputCount()); | 204 EXPECT_EQ(1, end->InputCount()); |
198 Node* ret = end->InputAt(0); | 205 Node* ret = end->InputAt(0); |
199 Node* effect = graph->start(); | 206 Node* effect = graph->start(); |
200 Node* control = graph->start(); | 207 Node* control = graph->start(); |
201 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); | 208 EXPECT_THAT(ret, IsReturn(IsNumberConstant(kValue), effect, control)); |
202 } | 209 } |
203 | 210 |
204 | 211 |
205 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithParameters) { | 212 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithParameters) { |
206 array_builder()->set_locals_count(1); | 213 array_builder()->set_locals_count(1); |
| 214 array_builder()->set_context_count(0); |
207 array_builder()->set_parameter_count(3); | 215 array_builder()->set_parameter_count(3); |
208 array_builder() | 216 array_builder() |
209 ->LoadAccumulatorWithRegister(array_builder()->Parameter(1)) | 217 ->LoadAccumulatorWithRegister(array_builder()->Parameter(1)) |
210 .BinaryOperation(Token::Value::ADD, array_builder()->Parameter(2), | 218 .BinaryOperation(Token::Value::ADD, array_builder()->Parameter(2), |
211 Strength::WEAK) | 219 Strength::WEAK) |
212 .StoreAccumulatorInRegister(interpreter::Register(0)) | 220 .StoreAccumulatorInRegister(interpreter::Register(0)) |
213 .Return(); | 221 .Return(); |
214 | 222 |
215 Graph* graph = GetCompletedGraph(); | 223 Graph* graph = GetCompletedGraph(); |
216 Node* end = graph->end(); | 224 Node* end = graph->end(); |
217 EXPECT_EQ(1, end->InputCount()); | 225 EXPECT_EQ(1, end->InputCount()); |
218 Node* ret = end->InputAt(0); | 226 Node* ret = end->InputAt(0); |
219 // NB binary operation is <reg> <op> <acc>. The register represents | 227 // NB binary operation is <reg> <op> <acc>. The register represents |
220 // the left-hand side, which is why parameters appear in opposite | 228 // the left-hand side, which is why parameters appear in opposite |
221 // order to construction via the builder. | 229 // order to construction via the builder. |
222 EXPECT_THAT(ret, IsReturn(IsJSAdd(IsParameter(2), IsParameter(1)), _, _)); | 230 EXPECT_THAT(ret, IsReturn(IsJSAdd(IsParameter(2), IsParameter(1)), _, _)); |
223 } | 231 } |
224 | 232 |
225 | 233 |
226 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { | 234 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { |
227 static const int kLeft = -655371; | 235 static const int kLeft = -655371; |
228 static const int kRight = +2000000; | 236 static const int kRight = +2000000; |
229 array_builder()->set_locals_count(1); | 237 array_builder()->set_locals_count(1); |
| 238 array_builder()->set_context_count(0); |
230 array_builder()->set_parameter_count(1); | 239 array_builder()->set_parameter_count(1); |
231 array_builder() | 240 array_builder() |
232 ->LoadLiteral(Smi::FromInt(kLeft)) | 241 ->LoadLiteral(Smi::FromInt(kLeft)) |
233 .StoreAccumulatorInRegister(interpreter::Register(0)) | 242 .StoreAccumulatorInRegister(interpreter::Register(0)) |
234 .LoadLiteral(Smi::FromInt(kRight)) | 243 .LoadLiteral(Smi::FromInt(kRight)) |
235 .BinaryOperation(Token::Value::ADD, interpreter::Register(0), | 244 .BinaryOperation(Token::Value::ADD, interpreter::Register(0), |
236 Strength::WEAK) | 245 Strength::WEAK) |
237 .Return(); | 246 .Return(); |
238 | 247 |
239 Graph* graph = GetCompletedGraph(); | 248 Graph* graph = GetCompletedGraph(); |
240 Node* end = graph->end(); | 249 Node* end = graph->end(); |
241 EXPECT_EQ(1, end->InputCount()); | 250 EXPECT_EQ(1, end->InputCount()); |
242 Node* ret = end->InputAt(0); | 251 Node* ret = end->InputAt(0); |
243 EXPECT_THAT( | 252 EXPECT_THAT( |
244 ret, IsReturn(IsJSAdd(IsNumberConstant(kLeft), IsNumberConstant(kRight)), | 253 ret, IsReturn(IsJSAdd(IsNumberConstant(kLeft), IsNumberConstant(kRight)), |
245 _, _)); | 254 _, _)); |
246 } | 255 } |
247 | 256 |
248 } // namespace compiler | 257 } // namespace compiler |
249 } // namespace internal | 258 } // namespace internal |
250 } // namespace v8 | 259 } // namespace v8 |
OLD | NEW |