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 "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/compiler.h" | 7 #include "src/compiler.h" |
8 #include "src/interpreter/bytecode-array-iterator.h" | 8 #include "src/interpreter/bytecode-array-iterator.h" |
9 #include "src/interpreter/bytecode-generator.h" | 9 #include "src/interpreter/bytecode-generator.h" |
10 #include "src/interpreter/interpreter.h" | 10 #include "src/interpreter/interpreter.h" |
(...skipping 4323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4334 }}, | 4334 }}, |
4335 }; | 4335 }; |
4336 | 4336 |
4337 for (size_t i = 0; i < arraysize(snippets); i++) { | 4337 for (size_t i = 0; i < arraysize(snippets); i++) { |
4338 Handle<BytecodeArray> bytecode_array = | 4338 Handle<BytecodeArray> bytecode_array = |
4339 helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet); | 4339 helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet); |
4340 CheckBytecodeArrayEqual(snippets[i], bytecode_array); | 4340 CheckBytecodeArrayEqual(snippets[i], bytecode_array); |
4341 } | 4341 } |
4342 } | 4342 } |
4343 | 4343 |
| 4344 |
| 4345 TEST(Switch) { |
| 4346 InitializedHandleScope handle_scope; |
| 4347 BytecodeGeneratorHelper helper; |
| 4348 |
| 4349 ExpectedSnippet<int> snippets[] = { |
| 4350 {"var a = 1;\n" |
| 4351 "switch(a) {\n" |
| 4352 " case 1: return 2;\n" |
| 4353 " case 2: return 3;\n" |
| 4354 "}\n", |
| 4355 2 * kPointerSize, |
| 4356 1, |
| 4357 30, |
| 4358 { |
| 4359 B(LdaSmi8), U8(1), // |
| 4360 B(Star), R(1), // The tag variable is allocated as a |
| 4361 B(Ldar), R(1), // local by the parser, hence this |
| 4362 B(Star), R(0), // strange shuffling. |
| 4363 B(LdaSmi8), U8(1), // |
| 4364 B(TestEqualStrict), R(0), // |
| 4365 B(JumpIfTrue), U8(10), // |
| 4366 B(LdaSmi8), U8(2), // |
| 4367 B(TestEqualStrict), R(0), // |
| 4368 B(JumpIfTrue), U8(7), // |
| 4369 B(Jump), U8(8), // |
| 4370 B(LdaSmi8), U8(2), // |
| 4371 B(Return), // |
| 4372 B(LdaSmi8), U8(3), // |
| 4373 B(Return), // |
| 4374 B(LdaUndefined), // |
| 4375 B(Return), // |
| 4376 }}, |
| 4377 {"var a = 1;\n" |
| 4378 "switch(a) {\n" |
| 4379 " case 1: a = 2; break;\n" |
| 4380 " case 2: a = 3; break;\n" |
| 4381 "}\n", |
| 4382 2 * kPointerSize, |
| 4383 1, |
| 4384 36, |
| 4385 { |
| 4386 B(LdaSmi8), U8(1), // |
| 4387 B(Star), R(1), // |
| 4388 B(Ldar), R(1), // |
| 4389 B(Star), R(0), // |
| 4390 B(LdaSmi8), U8(1), // |
| 4391 B(TestEqualStrict), R(0), // |
| 4392 B(JumpIfTrue), U8(10), // |
| 4393 B(LdaSmi8), U8(2), // |
| 4394 B(TestEqualStrict), R(0), // |
| 4395 B(JumpIfTrue), U8(10), // |
| 4396 B(Jump), U8(14), // |
| 4397 B(LdaSmi8), U8(2), // |
| 4398 B(Star), R(1), // |
| 4399 B(Jump), U8(8), // |
| 4400 B(LdaSmi8), U8(3), // |
| 4401 B(Star), R(1), // |
| 4402 B(Jump), U8(2), // |
| 4403 B(LdaUndefined), // |
| 4404 B(Return), // |
| 4405 }}, |
| 4406 {"var a = 1;\n" |
| 4407 "switch(a) {\n" |
| 4408 " case 1: a = 2; // fall-through\n" |
| 4409 " case 2: a = 3; break;\n" |
| 4410 "}\n", |
| 4411 2 * kPointerSize, |
| 4412 1, |
| 4413 34, |
| 4414 { |
| 4415 B(LdaSmi8), U8(1), // |
| 4416 B(Star), R(1), // |
| 4417 B(Ldar), R(1), // |
| 4418 B(Star), R(0), // |
| 4419 B(LdaSmi8), U8(1), // |
| 4420 B(TestEqualStrict), R(0), // |
| 4421 B(JumpIfTrue), U8(10), // |
| 4422 B(LdaSmi8), U8(2), // |
| 4423 B(TestEqualStrict), R(0), // |
| 4424 B(JumpIfTrue), U8(8), // |
| 4425 B(Jump), U8(12), // |
| 4426 B(LdaSmi8), U8(2), // |
| 4427 B(Star), R(1), // |
| 4428 B(LdaSmi8), U8(3), // |
| 4429 B(Star), R(1), // |
| 4430 B(Jump), U8(2), // |
| 4431 B(LdaUndefined), // |
| 4432 B(Return), // |
| 4433 }}, |
| 4434 {"var a = 1;\n" |
| 4435 "switch(a) {\n" |
| 4436 " case 2: break;\n" |
| 4437 " case 3: break;\n" |
| 4438 " default: a = 1; break;\n" |
| 4439 "}\n", |
| 4440 2 * kPointerSize, |
| 4441 1, |
| 4442 34, |
| 4443 { |
| 4444 B(LdaSmi8), U8(1), // |
| 4445 B(Star), R(1), // |
| 4446 B(Ldar), R(1), // |
| 4447 B(Star), R(0), // |
| 4448 B(LdaSmi8), U8(2), // |
| 4449 B(TestEqualStrict), R(0), // |
| 4450 B(JumpIfTrue), U8(10), // |
| 4451 B(LdaSmi8), U8(3), // |
| 4452 B(TestEqualStrict), R(0), // |
| 4453 B(JumpIfTrue), U8(6), // |
| 4454 B(Jump), U8(6), // |
| 4455 B(Jump), U8(10), // |
| 4456 B(Jump), U8(8), // |
| 4457 B(LdaSmi8), U8(1), // |
| 4458 B(Star), R(1), // |
| 4459 B(Jump), U8(2), // |
| 4460 B(LdaUndefined), // |
| 4461 B(Return), // |
| 4462 }}, |
| 4463 {"var a = 1;\n" |
| 4464 "switch(typeof(a)) {\n" |
| 4465 " case 2: a = 1; break;\n" |
| 4466 " case 3: a = 2; break;\n" |
| 4467 " default: a = 3; break;\n" |
| 4468 "}\n", |
| 4469 2 * kPointerSize, |
| 4470 1, |
| 4471 43, |
| 4472 { |
| 4473 B(LdaSmi8), U8(1), // |
| 4474 B(Star), R(1), // |
| 4475 B(Ldar), R(1), // |
| 4476 B(TypeOf), // |
| 4477 B(Star), R(0), // |
| 4478 B(LdaSmi8), U8(2), // |
| 4479 B(TestEqualStrict), R(0), // |
| 4480 B(JumpIfTrue), U8(10), // |
| 4481 B(LdaSmi8), U8(3), // |
| 4482 B(TestEqualStrict), R(0), // |
| 4483 B(JumpIfTrue), U8(10), // |
| 4484 B(Jump), U8(14), // |
| 4485 B(LdaSmi8), U8(1), // |
| 4486 B(Star), R(1), // |
| 4487 B(Jump), U8(14), // |
| 4488 B(LdaSmi8), U8(2), // |
| 4489 B(Star), R(1), // |
| 4490 B(Jump), U8(8), // |
| 4491 B(LdaSmi8), U8(3), // |
| 4492 B(Star), R(1), // |
| 4493 B(Jump), U8(2), // |
| 4494 B(LdaUndefined), // |
| 4495 B(Return), // |
| 4496 }}, |
| 4497 {"var a = 1;\n" |
| 4498 "switch(a) {\n" |
| 4499 " case typeof(a): a = 1; break;\n" |
| 4500 " default: a = 2; break;\n" |
| 4501 "}\n", |
| 4502 2 * kPointerSize, |
| 4503 1, |
| 4504 31, |
| 4505 { |
| 4506 B(LdaSmi8), U8(1), // |
| 4507 B(Star), R(1), // |
| 4508 B(Ldar), R(1), // |
| 4509 B(Star), R(0), // |
| 4510 B(Ldar), R(1), // |
| 4511 B(TypeOf), // |
| 4512 B(TestEqualStrict), R(0), // |
| 4513 B(JumpIfTrue), U8(4), // |
| 4514 B(Jump), U8(8), // |
| 4515 B(LdaSmi8), U8(1), // |
| 4516 B(Star), R(1), // |
| 4517 B(Jump), U8(8), // |
| 4518 B(LdaSmi8), U8(2), // |
| 4519 B(Star), R(1), // |
| 4520 B(Jump), U8(2), // |
| 4521 B(LdaUndefined), // |
| 4522 B(Return), // |
| 4523 }}, |
| 4524 {"var a = 1;\n" |
| 4525 "switch(a) {\n" |
| 4526 " case 1:\n" REPEAT_64(SPACE, " a = 2;") |
| 4527 "break;\n" |
| 4528 " case 2: a = 3; break;" |
| 4529 "}\n", |
| 4530 2 * kPointerSize, |
| 4531 1, |
| 4532 288, |
| 4533 { |
| 4534 B(LdaSmi8), U8(1), // |
| 4535 B(Star), R(1), // |
| 4536 B(Ldar), R(1), // |
| 4537 B(Star), R(0), // |
| 4538 B(LdaSmi8), U8(1), // |
| 4539 B(TestEqualStrict), R(0), // |
| 4540 B(JumpIfTrue), U8(10), // |
| 4541 B(LdaSmi8), U8(2), // |
| 4542 B(TestEqualStrict), R(0), // |
| 4543 B(JumpIfTrueConstant), U8(0), // |
| 4544 B(JumpConstant), U8(1), // |
| 4545 REPEAT_64(COMMA, // |
| 4546 B(LdaSmi8), U8(2), // |
| 4547 B(Star), R(1)), // |
| 4548 B(Jump), U8(8), // |
| 4549 B(LdaSmi8), U8(3), // |
| 4550 B(Star), R(1), // |
| 4551 B(Jump), U8(2), // |
| 4552 B(LdaUndefined), // |
| 4553 B(Return), // |
| 4554 }, |
| 4555 2, |
| 4556 {262, 266}}, |
| 4557 {"var a = 1;\n" |
| 4558 "switch(a) {\n" |
| 4559 " case 1: \n" |
| 4560 " switch(a + 1) {\n" |
| 4561 " case 2 : a = 1; break;\n" |
| 4562 " default : a = 2; break;\n" |
| 4563 " } // fall-through\n" |
| 4564 " case 2: a = 3;\n" |
| 4565 "}\n", |
| 4566 3 * kPointerSize, |
| 4567 1, |
| 4568 54, |
| 4569 { |
| 4570 B(LdaSmi8), U8(1), // |
| 4571 B(Star), R(2), // |
| 4572 B(Ldar), R(2), // |
| 4573 B(Star), R(0), // |
| 4574 B(LdaSmi8), U8(1), // |
| 4575 B(TestEqualStrict), R(0), // |
| 4576 B(JumpIfTrue), U8(10), // |
| 4577 B(LdaSmi8), U8(2), // |
| 4578 B(TestEqualStrict), R(0), // |
| 4579 B(JumpIfTrue), U8(30), // |
| 4580 B(Jump), U8(32), // |
| 4581 B(LdaSmi8), U8(1), // |
| 4582 B(Add), R(2), // |
| 4583 B(Star), R(1), // |
| 4584 B(LdaSmi8), U8(2), // |
| 4585 B(TestEqualStrict), R(1), // |
| 4586 B(JumpIfTrue), U8(4), // |
| 4587 B(Jump), U8(8), // |
| 4588 B(LdaSmi8), U8(1), // |
| 4589 B(Star), R(2), // |
| 4590 B(Jump), U8(8), // |
| 4591 B(LdaSmi8), U8(2), // |
| 4592 B(Star), R(2), // |
| 4593 B(Jump), U8(2), // |
| 4594 B(LdaSmi8), U8(3), // |
| 4595 B(Star), R(2), // |
| 4596 B(LdaUndefined), // |
| 4597 B(Return), // |
| 4598 }}, |
| 4599 }; |
| 4600 |
| 4601 for (size_t i = 0; i < arraysize(snippets); i++) { |
| 4602 Handle<BytecodeArray> bytecode_array = |
| 4603 helper.MakeBytecodeForFunctionBody(snippets[i].code_snippet); |
| 4604 CheckBytecodeArrayEqual(snippets[i], bytecode_array); |
| 4605 } |
| 4606 } |
| 4607 |
4344 } // namespace interpreter | 4608 } // namespace interpreter |
4345 } // namespace internal | 4609 } // namespace internal |
4346 } // namespace v8 | 4610 } // namespace v8 |
OLD | NEW |