V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
generate-bytecodes-builtins-list.cc
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 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  if (Bytecodes::BytecodeHasHandler(bytecode, operand_scale)) {
19  out << " \\\n V(" << Bytecodes::ToString(bytecode, operand_scale, "")
20  << "Handler, interpreter::OperandScale::k" << operand_scale
21  << ", interpreter::Bytecode::k" << Bytecodes::ToString(bytecode) << ")";
22  offset_table[table_index] = *count;
23  (*count)++;
24  } else {
25  offset_table[table_index] = -1;
26  }
27 }
28 
29 void WriteHeader(const char* header_filename) {
30  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  << "#define BUILTIN_LIST_BYTECODE_HANDLERS(V)";
40 
41  constexpr int kTableSize =
42  BytecodeOperands::kOperandScaleCount * Bytecodes::kBytecodeCount;
43  int offset_table[kTableSize];
44  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  BYTECODE_LIST(ADD_BYTECODES)
52  int single_count = count;
53  operand_scale = OperandScale::kDouble;
54  BYTECODE_LIST(ADD_BYTECODES)
55  int wide_count = count - single_count;
56  operand_scale = OperandScale::kQuadruple;
57  BYTECODE_LIST(ADD_BYTECODES)
58 #undef ADD_BYTECODES
59  int extra_wide_count = count - wide_count - single_count;
60  CHECK_GT(single_count, wide_count);
61  CHECK_EQ(single_count, Bytecodes::kBytecodeCount);
62  CHECK_EQ(wide_count, extra_wide_count);
63  out << "\n\nconst int kNumberOfBytecodeHandlers = " << single_count << ";\n"
64  << "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  << "constexpr int kBytecodeToBuiltinsMapping[" << kTableSize << "] = {\n"
69  << " ";
70 
71  for (int i = 0; i < kTableSize; ++i) {
72  if (i == single_count || i == 2 * single_count) {
73  out << "\n ";
74  }
75  out << offset_table[i] << ", ";
76  }
77 
78  out << "};\n\n"
79  << "} // namespace internal\n"
80  << "} // namespace v8\n"
81  << "#endif // V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n";
82 }
83 
84 } // namespace interpreter
85 } // namespace internal
86 } // namespace v8
87 
88 int main(int argc, const char* argv[]) {
89  if (argc != 2) {
90  std::cerr << "Usage: " << argv[0] << " <output filename>\n";
91  std::exit(1);
92  }
93 
94  v8::internal::interpreter::WriteHeader(argv[1]);
95 
96  return 0;
97 }
Definition: libplatform.h:13