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 const BytecodeSnippet kBytecodeSnippets[] = { |
| 38 {"var x = 0; return x;", |
| 39 kPointerSize, |
| 40 0, |
| 41 6, |
| 42 { |
| 43 B(LdaZero), // |
| 44 B(Star), R(0), // |
| 45 B(Ldar), R(0), // |
| 46 B(Return) // |
| 47 }}, |
| 48 {"var x = 0; return x + 3;", |
| 49 2 * kPointerSize, |
| 50 0, |
| 51 12, |
| 52 { |
| 53 B(LdaZero), // |
| 54 B(Star), R(0), // |
| 55 B(Ldar), R(0), // Easy to spot r1 not really needed here. |
| 56 B(Star), R(1), // Dead store. |
| 57 B(LdaSmi8), U8(3), // |
| 58 B(Add), R(1), // |
| 59 B(Return) // |
| 60 }}, |
| 61 }; |
| 62 |
| 63 |
| 64 TEST(BytecodeGraphBuilder) { |
| 65 HandleAndZoneScope test_scope; |
| 66 |
| 67 size_t snippet_count = |
| 68 sizeof(kBytecodeSnippets) / sizeof(kBytecodeSnippets[0]); |
| 69 for (size_t i = 0; i < snippet_count; i++) { |
| 70 Graph* graph = new (test_scope.main_zone()) Graph(test_scope.main_zone()); |
| 71 MachineOperatorBuilder* machine = |
| 72 new (test_scope.main_zone()) MachineOperatorBuilder( |
| 73 test_scope.main_zone(), kMachPtr, |
| 74 InstructionSelector::SupportedMachineOperatorFlags()); |
| 75 CommonOperatorBuilder* common = new (test_scope.main_zone()) |
| 76 CommonOperatorBuilder(test_scope.main_zone()); |
| 77 JSOperatorBuilder* javascript = |
| 78 new (test_scope.main_zone()) JSOperatorBuilder(test_scope.main_zone()); |
| 79 JSGraph* jsgraph = new (test_scope.main_zone()) |
| 80 JSGraph(test_scope.main_isolate(), graph, common, javascript, machine); |
| 81 |
| 82 const BytecodeSnippet& snippet = kBytecodeSnippets[i]; |
| 83 Handle<FixedArray> constant_pool = |
| 84 test_scope.main_isolate()->factory()->NewFixedArray(0); |
| 85 Handle<BytecodeArray> bytecode_array = |
| 86 test_scope.main_isolate()->factory()->NewBytecodeArray( |
| 87 snippet.bytecode_length, snippet.bytecode, snippet.frame_size, |
| 88 snippet.parameter_count, constant_pool); |
| 89 |
| 90 BytecodeGraphBuilder builder(test_scope.main_zone(), bytecode_array, |
| 91 jsgraph); |
| 92 builder.CreateGraphForTest(); |
| 93 std::cout << AsDOT(*builder.graph()) << std::endl; |
| 94 } |
| 95 } |
| 96 |
| 97 |
| 98 } // namespace compiler |
| 99 } // namespace internal |
| 100 } // namespace v8 |
OLD | NEW |