Line data Source code
1 : // Copyright 2018 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 <fstream>
6 : #include <iostream>
7 :
8 : #include "src/interpreter/bytecodes.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 : namespace interpreter {
13 :
14 540 : void WriteBytecode(std::ofstream& out, Bytecode bytecode,
15 : OperandScale operand_scale, int* count, int offset_table[],
16 : int table_index) {
17 : DCHECK_NOT_NULL(count);
18 540 : if (Bytecodes::BytecodeHasHandler(bytecode, operand_scale)) {
19 948 : out << " \\\n V(" << Bytecodes::ToString(bytecode, operand_scale, "")
20 474 : << "Handler, interpreter::OperandScale::k" << operand_scale
21 948 : << ", interpreter::Bytecode::k" << Bytecodes::ToString(bytecode) << ")";
22 474 : offset_table[table_index] = *count;
23 474 : (*count)++;
24 : } else {
25 66 : offset_table[table_index] = -1;
26 : }
27 540 : }
28 :
29 1 : void WriteHeader(const char* header_filename) {
30 2 : std::ofstream out(header_filename);
31 :
32 : out << "// Automatically generated from interpreter/bytecodes.h\n"
33 : << "// The following list macro is used to populate the builtins list\n"
34 : << "// with the bytecode handlers\n\n"
35 : << "#ifndef V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n"
36 : << "#define V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n\n"
37 : << "namespace v8 {\n"
38 : << "namespace internal {\n\n"
39 1 : << "#define BUILTIN_LIST_BYTECODE_HANDLERS(V)";
40 :
41 : constexpr int kTableSize =
42 : BytecodeOperands::kOperandScaleCount * Bytecodes::kBytecodeCount;
43 : int offset_table[kTableSize];
44 1 : int count = 0;
45 : int index = 0;
46 :
47 : #define ADD_BYTECODES(Name, ...) \
48 : WriteBytecode(out, Bytecode::k##Name, operand_scale, &count, offset_table, \
49 : index++);
50 : OperandScale operand_scale = OperandScale::kSingle;
51 1 : BYTECODE_LIST(ADD_BYTECODES)
52 1 : int single_count = count;
53 : operand_scale = OperandScale::kDouble;
54 1 : BYTECODE_LIST(ADD_BYTECODES)
55 1 : int wide_count = count - single_count;
56 : operand_scale = OperandScale::kQuadruple;
57 1 : BYTECODE_LIST(ADD_BYTECODES)
58 : #undef ADD_BYTECODES
59 1 : int extra_wide_count = count - wide_count - single_count;
60 1 : CHECK_GT(single_count, wide_count);
61 1 : CHECK_EQ(single_count, Bytecodes::kBytecodeCount);
62 1 : CHECK_EQ(wide_count, extra_wide_count);
63 1 : out << "\n\nconst int kNumberOfBytecodeHandlers = " << single_count << ";\n"
64 1 : << "const int kNumberOfWideBytecodeHandlers = " << wide_count << ";\n\n"
65 : << "// Mapping from (Bytecode + OperandScaleAsIndex * |Bytecodes|) to\n"
66 : << "// a dense form with all the illegal Bytecode/OperandScale\n"
67 : << "// combinations removed. Used to index into the builtins table.\n"
68 1 : << "constexpr int kBytecodeToBuiltinsMapping[" << kTableSize << "] = {\n"
69 1 : << " ";
70 :
71 1081 : for (int i = 0; i < kTableSize; ++i) {
72 540 : if (i == single_count || i == 2 * single_count) {
73 2 : out << "\n ";
74 : }
75 540 : out << offset_table[i] << ", ";
76 : }
77 :
78 : out << "};\n\n"
79 : << "} // namespace internal\n"
80 : << "} // namespace v8\n"
81 1 : << "#endif // V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n";
82 1 : }
83 :
84 : } // namespace interpreter
85 : } // namespace internal
86 : } // namespace v8
87 :
88 1 : int main(int argc, const char* argv[]) {
89 1 : if (argc != 2) {
90 0 : std::cerr << "Usage: " << argv[0] << " <output filename>\n";
91 0 : std::exit(1);
92 : }
93 :
94 1 : v8::internal::interpreter::WriteHeader(argv[1]);
95 :
96 : return 0;
97 2 : }
|