Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/unittests/interpreter/bytecode-array-builder-unittest.cc

Issue 1343363002: [Interpreter] Basic flow control. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Clarify comment and diff reduction. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "src/interpreter/bytecode-array-iterator.h"
8 #include "test/unittests/test-utils.h" 9 #include "test/unittests/test-utils.h"
9 10
10 namespace v8 { 11 namespace v8 {
11 namespace internal { 12 namespace internal {
12 namespace interpreter { 13 namespace interpreter {
13 14
14 class BytecodeArrayBuilderTest : public TestWithIsolateAndZone { 15 class BytecodeArrayBuilderTest : public TestWithIsolateAndZone {
15 public: 16 public:
16 BytecodeArrayBuilderTest() {} 17 BytecodeArrayBuilderTest() {}
17 ~BytecodeArrayBuilderTest() override {} 18 ~BytecodeArrayBuilderTest() override {}
(...skipping 30 matching lines...) Expand all
48 // Call operations. 49 // Call operations.
49 builder.Call(reg, reg, 0); 50 builder.Call(reg, reg, 0);
50 51
51 // Emit binary operators invocations. 52 // Emit binary operators invocations.
52 builder.BinaryOperation(Token::Value::ADD, reg) 53 builder.BinaryOperation(Token::Value::ADD, reg)
53 .BinaryOperation(Token::Value::SUB, reg) 54 .BinaryOperation(Token::Value::SUB, reg)
54 .BinaryOperation(Token::Value::MUL, reg) 55 .BinaryOperation(Token::Value::MUL, reg)
55 .BinaryOperation(Token::Value::DIV, reg) 56 .BinaryOperation(Token::Value::DIV, reg)
56 .BinaryOperation(Token::Value::MOD, reg); 57 .BinaryOperation(Token::Value::MOD, reg);
57 58
59 // Emit test operators invocations.
60 builder.CompareOperation(Token::Value::EQ, reg)
61 .CompareOperation(Token::Value::NE, reg)
62 .CompareOperation(Token::Value::EQ_STRICT, reg)
63 .CompareOperation(Token::Value::NE_STRICT, reg)
64 .CompareOperation(Token::Value::LT, reg)
65 .CompareOperation(Token::Value::GT, reg)
66 .CompareOperation(Token::Value::LTE, reg)
67 .CompareOperation(Token::Value::GTE, reg)
68 .CompareOperation(Token::Value::INSTANCEOF, reg)
69 .CompareOperation(Token::Value::IN, reg);
70
58 // Emit control flow. Return must be the last instruction. 71 // Emit control flow. Return must be the last instruction.
72 BytecodeLabel start;
73 builder.Bind(&start);
74 // Short jumps with Imm8 operands
75 builder.Jump(&start).JumpIfTrue(&start).JumpIfFalse(&start);
76 // Insert dummy ops to force longer jumps
77 for (int i = 0; i < 128; i++) {
78 builder.LoadTrue();
79 }
80 // Longer jumps requiring Constant operand
81 builder.Jump(&start).JumpIfTrue(&start).JumpIfFalse(&start);
59 builder.Return(); 82 builder.Return();
60 83
61 // Generate BytecodeArray. 84 // Generate BytecodeArray.
62 Handle<BytecodeArray> the_array = builder.ToBytecodeArray(); 85 Handle<BytecodeArray> the_array = builder.ToBytecodeArray();
63 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize); 86 CHECK_EQ(the_array->frame_size(), builder.locals_count() * kPointerSize);
64 87
65 // Build scorecard of bytecodes encountered in the BytecodeArray. 88 // Build scorecard of bytecodes encountered in the BytecodeArray.
66 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1); 89 std::vector<int> scorecard(Bytecodes::ToByte(Bytecode::kLast) + 1);
67 Bytecode final_bytecode = Bytecode::kLdaZero; 90 Bytecode final_bytecode = Bytecode::kLdaZero;
68 for (int i = 0; i < the_array->length(); i++) { 91 for (int i = 0; i < the_array->length(); i++) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 .LoadLiteral(large_smi) 196 .LoadLiteral(large_smi)
174 .LoadLiteral(heap_num_1) 197 .LoadLiteral(heap_num_1)
175 .LoadLiteral(heap_num_1) 198 .LoadLiteral(heap_num_1)
176 .LoadLiteral(heap_num_2_copy); 199 .LoadLiteral(heap_num_2_copy);
177 200
178 Handle<BytecodeArray> array = builder.ToBytecodeArray(); 201 Handle<BytecodeArray> array = builder.ToBytecodeArray();
179 // Should only have one entry for each identical constant. 202 // Should only have one entry for each identical constant.
180 CHECK_EQ(array->constant_pool()->length(), 3); 203 CHECK_EQ(array->constant_pool()->length(), 3);
181 } 204 }
182 205
206
207 TEST_F(BytecodeArrayBuilderTest, ForwardJumps) {
208 static const int kFarJumpDistance = 256;
209
210 BytecodeArrayBuilder builder(isolate(), zone());
211 builder.set_parameter_count(0);
212 builder.set_locals_count(0);
213
214 BytecodeLabel far0, far1, far2;
215 BytecodeLabel near0, near1, near2;
216
217 builder.Jump(&near0)
218 .JumpIfTrue(&near1)
219 .JumpIfFalse(&near2)
220 .Bind(&near0)
221 .Bind(&near1)
222 .Bind(&near2)
223 .Jump(&far0)
224 .JumpIfTrue(&far1)
225 .JumpIfFalse(&far2);
226 for (int i = 0; i < kFarJumpDistance - 6; i++) {
227 builder.LoadUndefined();
228 }
229 builder.Bind(&far0).Bind(&far1).Bind(&far2);
230 builder.Return();
231
232 Handle<BytecodeArray> array = builder.ToBytecodeArray();
233 DCHECK_EQ(array->length(), 12 + kFarJumpDistance - 6 + 1);
234
235 BytecodeArrayIterator iterator(array);
236 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
237 CHECK_EQ(iterator.GetSmi8Operand(0), 6);
238 iterator.Advance();
239
240 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueSmi8);
241 CHECK_EQ(iterator.GetSmi8Operand(0), 4);
242 iterator.Advance();
243
244 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseSmi8);
245 CHECK_EQ(iterator.GetSmi8Operand(0), 2);
246 iterator.Advance();
247
248 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpConstant);
249 CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
250 Smi::FromInt(kFarJumpDistance));
251 CHECK_EQ(
252 array->get(iterator.current_offset() +
253 Smi::cast(*iterator.GetConstantForIndexOperand(0))->value()),
254 Bytecodes::ToByte(Bytecode::kReturn));
255 iterator.Advance();
256
257 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueConstant);
258 CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
259 Smi::FromInt(kFarJumpDistance - 2));
260 CHECK_EQ(
261 array->get(iterator.current_offset() +
262 Smi::cast(*iterator.GetConstantForIndexOperand(0))->value()),
263 Bytecodes::ToByte(Bytecode::kReturn));
264 iterator.Advance();
265
266 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseConstant);
267 CHECK_EQ(*iterator.GetConstantForIndexOperand(0),
268 Smi::FromInt(kFarJumpDistance - 4));
269 CHECK_EQ(
270 array->get(iterator.current_offset() +
271 Smi::cast(*iterator.GetConstantForIndexOperand(0))->value()),
272 Bytecodes::ToByte(Bytecode::kReturn));
273 iterator.Advance();
274 }
275
276
277 TEST_F(BytecodeArrayBuilderTest, BackwardJumps) {
278 BytecodeArrayBuilder builder(isolate(), zone());
279 builder.set_parameter_count(0);
280 builder.set_locals_count(0);
281
282 BytecodeLabel label0, label1, label2;
283 builder.Bind(&label0)
284 .Jump(&label0)
285 .Bind(&label1)
286 .JumpIfTrue(&label1)
287 .Bind(&label2)
288 .JumpIfFalse(&label2);
289 for (int i = 0; i < 64; i++) {
290 builder.Jump(&label2);
291 }
292 builder.JumpIfFalse(&label2);
293 builder.JumpIfTrue(&label1);
294 builder.Jump(&label0);
295 builder.Return();
296
297 Handle<BytecodeArray> array = builder.ToBytecodeArray();
298 BytecodeArrayIterator iterator(array);
299 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
300 CHECK_EQ(iterator.GetSmi8Operand(0), 0);
301 iterator.Advance();
302 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueSmi8);
303 CHECK_EQ(iterator.GetSmi8Operand(0), 0);
304 iterator.Advance();
305 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseSmi8);
306 CHECK_EQ(iterator.GetSmi8Operand(0), 0);
307 iterator.Advance();
308 for (int i = 0; i < 64; i++) {
309 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
310 CHECK_EQ(iterator.GetSmi8Operand(0), -i * 2 - 2);
311 iterator.Advance();
312 }
313 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfFalseConstant);
314 CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -130);
315 iterator.Advance();
316 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpIfTrueConstant);
317 CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -134);
318 iterator.Advance();
319 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpConstant);
320 CHECK_EQ(Smi::cast(*iterator.GetConstantForIndexOperand(0))->value(), -138);
321 iterator.Advance();
322 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
323 iterator.Advance();
324 CHECK(iterator.done());
325 }
326
327
328 TEST_F(BytecodeArrayBuilderTest, LabelReuse) {
329 BytecodeArrayBuilder builder(isolate(), zone());
330 builder.set_parameter_count(0);
331 builder.set_locals_count(0);
332
333 // Labels can only have 1 forward reference, but
334 // can be referred to mulitple times once bound.
335 BytecodeLabel label;
336
337 builder.Jump(&label).Bind(&label).Jump(&label).Jump(&label).Return();
338
339 Handle<BytecodeArray> array = builder.ToBytecodeArray();
340 BytecodeArrayIterator iterator(array);
341 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
342 CHECK_EQ(iterator.GetSmi8Operand(0), 2);
343 iterator.Advance();
344 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
345 CHECK_EQ(iterator.GetSmi8Operand(0), 0);
346 iterator.Advance();
347 CHECK_EQ(iterator.current_bytecode(), Bytecode::kJumpSmi8);
348 CHECK_EQ(iterator.GetSmi8Operand(0), -2);
349 iterator.Advance();
350 CHECK_EQ(iterator.current_bytecode(), Bytecode::kReturn);
351 iterator.Advance();
352 CHECK(iterator.done());
353 }
354
355
183 } // namespace interpreter 356 } // namespace interpreter
184 } // namespace internal 357 } // namespace internal
185 } // namespace v8 358 } // namespace v8
OLDNEW
« test/cctest/interpreter/test-interpreter.cc ('K') | « test/cctest/interpreter/test-interpreter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698