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 <iostream> |
| 6 |
| 7 #include "src/compiler/bytecode-graph-builder.h" |
| 8 #include "src/compiler/common-operator.h" |
| 9 #include "src/compiler/graph-visualizer.h" |
| 10 #include "src/compiler/instruction.h" |
| 11 #include "src/compiler/instruction-selector.h" |
| 12 #include "src/compiler/js-graph.h" |
| 13 #include "src/compiler/js-operator.h" |
| 14 #include "src/interpreter/bytecodes.h" |
| 15 #include "test/cctest/cctest.h" |
| 16 |
| 17 namespace v8 { |
| 18 namespace internal { |
| 19 namespace compiler { |
| 20 |
| 21 |
| 22 struct BytecodeSnippet { |
| 23 const char* body; |
| 24 int frame_size; |
| 25 int parameter_count; |
| 26 int bytecode_length; |
| 27 const uint8_t bytecode[16]; |
| 28 }; |
| 29 |
| 30 |
| 31 // Helper macros for handcrafting bytecode sequences. |
| 32 #define B(x) static_cast<uint8_t>(interpreter::Bytecode::k##x) |
| 33 #define U8(x) static_cast<uint8_t>(x & 0xff) |
| 34 #define R(x) static_cast<uint8_t>(-x & 0xff) |
| 35 |
| 36 |
| 37 static const int kLastParamIndex = |
| 38 -InterpreterFrameConstants::kLastParamFromRegisterPointer / kPointerSize; |
| 39 |
| 40 |
| 41 const BytecodeSnippet kBytecodeSnippets[] = { |
| 42 {"var x = 0; return x;", |
| 43 kPointerSize, |
| 44 0, |
| 45 6, |
| 46 { |
| 47 B(LdaZero), // |
| 48 B(Star), R(0), // |
| 49 B(Ldar), R(0), // |
| 50 B(Return) // |
| 51 }}, |
| 52 {"var x = 0; return x + 3;", |
| 53 2 * kPointerSize, |
| 54 0, |
| 55 12, |
| 56 { |
| 57 B(LdaZero), // |
| 58 B(Star), R(0), // |
| 59 B(Ldar), R(0), // Easy to spot r1 not really needed here. |
| 60 B(Star), R(1), // Dead store. |
| 61 B(LdaSmi8), U8(3), // |
| 62 B(Add), R(1), // |
| 63 B(Return) // |
| 64 }}, |
| 65 {"function f(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { return arg4; }", |
| 66 0, |
| 67 8, |
| 68 3, |
| 69 { |
| 70 B(Ldar), // |
| 71 R(kLastParamIndex - 3), // |
| 72 B(Return) // |
| 73 }}, |
| 74 }; |
| 75 |
| 76 |
| 77 TEST(BytecodeGraphBuilder) { |
| 78 i::FLAG_ignition = true; |
| 79 i::FLAG_always_opt = true; |
| 80 |
| 81 HandleAndZoneScope test_scope; |
| 82 |
| 83 size_t snippet_count = |
| 84 sizeof(kBytecodeSnippets) / sizeof(kBytecodeSnippets[0]); |
| 85 for (size_t i = 0; i < snippet_count; i++) { |
| 86 Graph* graph = new (test_scope.main_zone()) Graph(test_scope.main_zone()); |
| 87 MachineOperatorBuilder* machine = |
| 88 new (test_scope.main_zone()) MachineOperatorBuilder( |
| 89 test_scope.main_zone(), kMachPtr, |
| 90 InstructionSelector::SupportedMachineOperatorFlags()); |
| 91 CommonOperatorBuilder* common = new (test_scope.main_zone()) |
| 92 CommonOperatorBuilder(test_scope.main_zone()); |
| 93 JSOperatorBuilder* javascript = |
| 94 new (test_scope.main_zone()) JSOperatorBuilder(test_scope.main_zone()); |
| 95 JSGraph* jsgraph = new (test_scope.main_zone()) |
| 96 JSGraph(test_scope.main_isolate(), graph, common, javascript, machine); |
| 97 |
| 98 const BytecodeSnippet& snippet = kBytecodeSnippets[i]; |
| 99 Handle<FixedArray> constant_pool = |
| 100 test_scope.main_isolate()->factory()->NewFixedArray(0); |
| 101 Handle<BytecodeArray> bytecode_array = |
| 102 test_scope.main_isolate()->factory()->NewBytecodeArray( |
| 103 snippet.bytecode_length, snippet.bytecode, snippet.frame_size, |
| 104 snippet.parameter_count, constant_pool); |
| 105 |
| 106 BytecodeGraphBuilder builder(test_scope.main_zone(), bytecode_array, |
| 107 jsgraph); |
| 108 builder.CreateGraphForTest(); |
| 109 std::cout << AsDOT(*builder.graph()) << std::endl; |
| 110 } |
| 111 } |
| 112 |
| 113 |
| 114 } // namespace compiler |
| 115 } // namespace internal |
| 116 } // namespace v8 |
OLD | NEW |