generate-bytecodes-builtins-list.cc 3.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <fstream>
#include <iostream>

#include "src/interpreter/bytecodes.h"

namespace v8 {
namespace internal {
namespace interpreter {

void WriteBytecode(std::ofstream& out, Bytecode bytecode,
15 16 17
                   OperandScale operand_scale, int* count, int offset_table[],
                   int table_index) {
  DCHECK_NOT_NULL(count);
18 19
  if (Bytecodes::BytecodeHasHandler(bytecode, operand_scale)) {
    out << " \\\n  V(" << Bytecodes::ToString(bytecode, operand_scale, "")
20 21 22 23 24 25
        << "Handler, interpreter::OperandScale::k" << operand_scale
        << ", interpreter::Bytecode::k" << Bytecodes::ToString(bytecode) << ")";
    offset_table[table_index] = *count;
    (*count)++;
  } else {
    offset_table[table_index] = -1;
26 27 28 29 30 31 32 33 34 35
  }
}

void WriteHeader(const char* header_filename) {
  std::ofstream out(header_filename);

  out << "// Automatically generated from interpreter/bytecodes.h\n"
      << "// The following list macro is used to populate the builtins list\n"
      << "// with the bytecode handlers\n\n"
      << "#ifndef V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n"
36 37 38
      << "#define V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n\n"
      << "namespace v8 {\n"
      << "namespace internal {\n\n"
39
      << "#define BUILTIN_LIST_BYTECODE_HANDLERS(V)";
40 41 42 43 44 45 46 47 48 49

  constexpr int kTableSize =
      BytecodeOperands::kOperandScaleCount * Bytecodes::kBytecodeCount;
  int offset_table[kTableSize];
  int count = 0;
  int index = 0;

#define ADD_BYTECODES(Name, ...)                                             \
  WriteBytecode(out, Bytecode::k##Name, operand_scale, &count, offset_table, \
                index++);
50 51
  OperandScale operand_scale = OperandScale::kSingle;
  BYTECODE_LIST(ADD_BYTECODES)
52
  int single_count = count;
53 54
  operand_scale = OperandScale::kDouble;
  BYTECODE_LIST(ADD_BYTECODES)
55
  int wide_count = count - single_count;
56 57 58
  operand_scale = OperandScale::kQuadruple;
  BYTECODE_LIST(ADD_BYTECODES)
#undef ADD_BYTECODES
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  int extra_wide_count = count - wide_count - single_count;
  CHECK_GT(single_count, wide_count);
  CHECK_EQ(single_count, Bytecodes::kBytecodeCount);
  CHECK_EQ(wide_count, extra_wide_count);
  out << "\n\nconst int kNumberOfBytecodeHandlers = " << single_count << ";\n"
      << "const int kNumberOfWideBytecodeHandlers = " << wide_count << ";\n\n"
      << "// Mapping from (Bytecode + OperandScaleAsIndex * |Bytecodes|) to\n"
      << "// a dense form with all the illegal Bytecode/OperandScale\n"
      << "// combinations removed. Used to index into the builtins table.\n"
      << "constexpr int kBytecodeToBuiltinsMapping[" << kTableSize << "] = {\n"
      << "    ";

  for (int i = 0; i < kTableSize; ++i) {
    if (i == single_count || i == 2 * single_count) {
      out << "\n    ";
    }
    out << offset_table[i] << ", ";
  }

78
  out << "};\n\n"
79 80 81
      << "}  // namespace internal\n"
      << "}  // namespace v8\n"
      << "#endif  // V8_BUILTINS_GENERATED_BYTECODES_BUILTINS_LIST\n";
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

int main(int argc, const char* argv[]) {
  if (argc != 2) {
    std::cerr << "Usage: " << argv[0] << " <output filename>\n";
    std::exit(1);
  }

  v8::internal::interpreter::WriteHeader(argv[1]);

  return 0;
}