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 "src/v8.h" |
| 6 |
| 7 #include "src/compiler.h" |
| 8 #include "src/interpreter/bytecode-generator.h" |
| 9 #include "src/interpreter/interpreter.h" |
| 10 #include "test/cctest/cctest.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 namespace interpreter { |
| 15 |
| 16 class BytecodeGeneratorHelper { |
| 17 public: |
| 18 const char* kFunctionName = "my_function"; |
| 19 |
| 20 BytecodeGeneratorHelper() { |
| 21 i::FLAG_ignition = true; |
| 22 i::FLAG_ignition_filter = kFunctionName; |
| 23 i::FLAG_print_bytecode = false; |
| 24 CcTest::i_isolate()->interpreter()->Initialize(); |
| 25 } |
| 26 |
| 27 |
| 28 Handle<BytecodeArray> MakeBytecode(const char* script, |
| 29 const char* function_name) { |
| 30 CompileRun(script); |
| 31 Local<Function> function = |
| 32 Local<Function>::Cast(CcTest::global()->Get(v8_str(function_name))); |
| 33 i::Handle<i::JSFunction> js_function = v8::Utils::OpenHandle(*function); |
| 34 return handle(js_function->shared()->bytecode_array(), CcTest::i_isolate()); |
| 35 } |
| 36 |
| 37 |
| 38 Handle<BytecodeArray> MakeBytecodeForFunctionBody(const char* body) { |
| 39 ScopedVector<char> program(1024); |
| 40 SNPrintF(program, "function %s() { %s }\n%s();", kFunctionName, body, |
| 41 kFunctionName); |
| 42 return MakeBytecode(program.start(), kFunctionName); |
| 43 } |
| 44 }; |
| 45 |
| 46 |
| 47 struct ExpectedSnippet { |
| 48 const char* body; |
| 49 int frame_size; |
| 50 int bytecode_length; |
| 51 const uint8_t bytecode[4]; |
| 52 }; |
| 53 |
| 54 |
| 55 TEST(PrimitiveReturnStatements) { |
| 56 InitializedHandleScope handle_scope; |
| 57 BytecodeGeneratorHelper helper; |
| 58 |
| 59 #define B(x) static_cast<uint8_t>(Bytecode::k##x) |
| 60 #define U8(x) static_cast<uint8_t>(x & 0xff) |
| 61 |
| 62 ExpectedSnippet snippets[] = { |
| 63 {"return;", 0, 2, {B(LdaUndefined), B(Return)}}, |
| 64 {"return null;", 0, 2, {B(LdaNull), B(Return)}}, |
| 65 {"return true;", 0, 2, {B(LdaTrue), B(Return)}}, |
| 66 {"return false;", 0, 2, {B(LdaFalse), B(Return)}}, |
| 67 {"return 0;", 0, 2, {B(LdaZero), B(Return)}}, |
| 68 {"return +1;", 0, 3, {B(LdaSmi8), U8(1), B(Return)}}, |
| 69 {"return -1;", 0, 3, {B(LdaSmi8), U8(-1), B(Return)}}, |
| 70 {"return +127;", 0, 3, {B(LdaSmi8), U8(127), B(Return)}}, |
| 71 {"return -128;", 0, 3, {B(LdaSmi8), U8(-128), B(Return)}}, |
| 72 }; |
| 73 |
| 74 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 75 for (size_t i = 0; i < num_snippets; i++) { |
| 76 Handle<BytecodeArray> ba = |
| 77 helper.MakeBytecodeForFunctionBody(snippets[i].body); |
| 78 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); |
| 79 CHECK_EQ(ba->length(), snippets[i].bytecode_length); |
| 80 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, |
| 81 ba->length())); |
| 82 } |
| 83 } |
| 84 } |
| 85 } |
| 86 } |
OLD | NEW |