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 CcTest::i_isolate()->interpreter()->Initialize(); |
| 24 } |
| 25 |
| 26 |
| 27 Handle<BytecodeArray> MakeBytecode(const char* script, |
| 28 const char* function_name) { |
| 29 CompileRun(script); |
| 30 Local<Function> function = |
| 31 Local<Function>::Cast(CcTest::global()->Get(v8_str(function_name))); |
| 32 i::Handle<i::JSFunction> js_function = v8::Utils::OpenHandle(*function); |
| 33 return handle(js_function->shared()->bytecode_array(), CcTest::i_isolate()); |
| 34 } |
| 35 |
| 36 |
| 37 Handle<BytecodeArray> MakeBytecodeForFunctionBody(const char* body) { |
| 38 ScopedVector<char> program(1024); |
| 39 SNPrintF(program, "function %s() { %s }\n%s();", kFunctionName, body, |
| 40 kFunctionName); |
| 41 return MakeBytecode(program.start(), kFunctionName); |
| 42 } |
| 43 }; |
| 44 |
| 45 |
| 46 // Structure for containing expected bytecode snippets. |
| 47 struct ExpectedSnippet { |
| 48 const char* body; |
| 49 int frame_size; |
| 50 int bytecode_length; |
| 51 const uint8_t bytecode[16]; |
| 52 }; |
| 53 |
| 54 |
| 55 // Helper macros for handcrafting bytecode sequences. |
| 56 #define B(x) static_cast<uint8_t>(Bytecode::k##x) |
| 57 #define U8(x) static_cast<uint8_t>(x & 0xff) |
| 58 #define R(x) static_cast<uint8_t>(-x & 0xff) |
| 59 |
| 60 |
| 61 TEST(PrimitiveReturnStatements) { |
| 62 InitializedHandleScope handle_scope; |
| 63 BytecodeGeneratorHelper helper; |
| 64 |
| 65 ExpectedSnippet snippets[] = { |
| 66 {"return;", 0, 2, {B(LdaUndefined), B(Return)}}, |
| 67 {"return null;", 0, 2, {B(LdaNull), B(Return)}}, |
| 68 {"return true;", 0, 2, {B(LdaTrue), B(Return)}}, |
| 69 {"return false;", 0, 2, {B(LdaFalse), B(Return)}}, |
| 70 {"return 0;", 0, 2, {B(LdaZero), B(Return)}}, |
| 71 {"return +1;", 0, 3, {B(LdaSmi8), U8(1), B(Return)}}, |
| 72 {"return -1;", 0, 3, {B(LdaSmi8), U8(-1), B(Return)}}, |
| 73 {"return +127;", 0, 3, {B(LdaSmi8), U8(127), B(Return)}}, |
| 74 {"return -128;", 0, 3, {B(LdaSmi8), U8(-128), B(Return)}}, |
| 75 }; |
| 76 |
| 77 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 78 for (size_t i = 0; i < num_snippets; i++) { |
| 79 Handle<BytecodeArray> ba = |
| 80 helper.MakeBytecodeForFunctionBody(snippets[i].body); |
| 81 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); |
| 82 CHECK_EQ(ba->length(), snippets[i].bytecode_length); |
| 83 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, |
| 84 ba->length())); |
| 85 } |
| 86 } |
| 87 |
| 88 |
| 89 TEST(PrimitiveExpressions) { |
| 90 InitializedHandleScope handle_scope; |
| 91 BytecodeGeneratorHelper helper; |
| 92 |
| 93 ExpectedSnippet snippets[] = { |
| 94 {"var x = 0; return x;", |
| 95 kPointerSize, |
| 96 6, |
| 97 { |
| 98 B(LdaZero), // |
| 99 B(Star), R(0), // |
| 100 B(Ldar), R(0), // |
| 101 B(Return) // |
| 102 }}, |
| 103 {"var x = 0; return x + 3;", |
| 104 2 * kPointerSize, |
| 105 12, |
| 106 { |
| 107 B(LdaZero), // |
| 108 B(Star), R(0), // |
| 109 B(Ldar), R(0), // Easy to spot r1 not really needed here. |
| 110 B(Star), R(1), // Dead store. |
| 111 B(LdaSmi8), U8(3), // |
| 112 B(Add), R(1), // |
| 113 B(Return) // |
| 114 }}}; |
| 115 |
| 116 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); |
| 117 for (size_t i = 0; i < num_snippets; i++) { |
| 118 Handle<BytecodeArray> ba = |
| 119 helper.MakeBytecodeForFunctionBody(snippets[i].body); |
| 120 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); |
| 121 CHECK_EQ(ba->length(), snippets[i].bytecode_length); |
| 122 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, |
| 123 ba->length())); |
| 124 } |
| 125 } |
| 126 |
| 127 } // namespace interpreter |
| 128 } // namespace internal |
| 129 } // namespance v8 |
OLD | NEW |