Chromium Code Reviews| 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; | |
|
rmcilroy
2015/08/18 10:31:31
nit - remove the "i::FLAG_print_bytecode = false"?
oth
2015/08/18 10:56:11
Done. It was an explicit clue on debugging problem
| |
| 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[16]; | |
| 52 }; | |
| 53 | |
|
rmcilroy
2015/08/18 10:31:31
nit - extra newline
oth
2015/08/18 10:56:11
Done.
| |
| 54 #define B(x) static_cast<uint8_t>(Bytecode::k##x) | |
| 55 #define U8(x) static_cast<uint8_t>(x & 0xff) | |
| 56 #define R(x) static_cast<uint8_t>(-x & 0xff) | |
| 57 | |
| 58 | |
| 59 TEST(PrimitiveReturnStatements) { | |
| 60 InitializedHandleScope handle_scope; | |
| 61 BytecodeGeneratorHelper helper; | |
| 62 | |
| 63 ExpectedSnippet snippets[] = { | |
| 64 {"return;", 0, 2, {B(LdaUndefined), B(Return)}}, | |
| 65 {"return null;", 0, 2, {B(LdaNull), B(Return)}}, | |
| 66 {"return true;", 0, 2, {B(LdaTrue), B(Return)}}, | |
| 67 {"return false;", 0, 2, {B(LdaFalse), B(Return)}}, | |
| 68 {"return 0;", 0, 2, {B(LdaZero), B(Return)}}, | |
| 69 {"return +1;", 0, 3, {B(LdaSmi8), U8(1), B(Return)}}, | |
| 70 {"return -1;", 0, 3, {B(LdaSmi8), U8(-1), B(Return)}}, | |
| 71 {"return +127;", 0, 3, {B(LdaSmi8), U8(127), B(Return)}}, | |
| 72 {"return -128;", 0, 3, {B(LdaSmi8), U8(-128), B(Return)}}, | |
| 73 }; | |
| 74 | |
| 75 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); | |
| 76 for (size_t i = 0; i < num_snippets; i++) { | |
| 77 Handle<BytecodeArray> ba = | |
| 78 helper.MakeBytecodeForFunctionBody(snippets[i].body); | |
| 79 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); | |
| 80 CHECK_EQ(ba->length(), snippets[i].bytecode_length); | |
| 81 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, | |
| 82 ba->length())); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 | |
| 87 TEST(PrimitiveExpressions) { | |
| 88 InitializedHandleScope handle_scope; | |
| 89 BytecodeGeneratorHelper helper; | |
| 90 | |
| 91 ExpectedSnippet snippets[] = { | |
| 92 {"var x = 0; return x;", | |
| 93 kPointerSize, | |
| 94 6, | |
| 95 { | |
| 96 B(LdaZero), // | |
| 97 B(Star), R(0), // | |
| 98 B(Ldar), R(0), // | |
| 99 B(Return) // | |
| 100 }}, | |
| 101 {"var x = 0; return x + 3;", | |
| 102 2 * kPointerSize, | |
| 103 12, | |
| 104 { | |
| 105 B(LdaZero), // | |
| 106 B(Star), R(0), // | |
| 107 B(Ldar), R(0), // Easy to spot r1 not really needed here. | |
| 108 B(Star), R(1), // Dead store. | |
| 109 B(LdaSmi8), U8(3), // | |
| 110 B(Add), R(1), // | |
| 111 B(Return) // | |
| 112 }}}; | |
|
rmcilroy
2015/08/18 10:31:31
indentation seems a little unusual here - is "git
oth
2015/08/18 10:56:11
Yes.
| |
| 113 | |
| 114 size_t num_snippets = sizeof(snippets) / sizeof(snippets[0]); | |
| 115 for (size_t i = 0; i < num_snippets; i++) { | |
| 116 Handle<BytecodeArray> ba = | |
| 117 helper.MakeBytecodeForFunctionBody(snippets[i].body); | |
| 118 CHECK_EQ(ba->frame_size(), snippets[i].frame_size); | |
| 119 CHECK_EQ(ba->length(), snippets[i].bytecode_length); | |
| 120 CHECK(!memcmp(ba->GetFirstBytecodeAddress(), snippets[i].bytecode, | |
| 121 ba->length())); | |
| 122 } | |
| 123 } | |
|
rmcilroy
2015/08/18 10:31:31
nit - extra newline below
oth
2015/08/18 10:56:11
Done.
| |
| 124 } | |
|
rmcilroy
2015/08/18 10:31:31
nit - comments on closing namespace brackets.
oth
2015/08/18 10:56:11
Done.
| |
| 125 } | |
| 126 } | |
| OLD | NEW |