summaryrefslogtreecommitdiff
path: root/chromium/v8/src/snapshot/builtin-snapshot-utils.cc
blob: e32a857c0b4563150a2ccbb0bb1ffbcd27cc2f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2017 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 "src/snapshot/builtin-snapshot-utils.h"

namespace v8 {
namespace internal {

// static
bool BuiltinSnapshotUtils::IsBuiltinIndex(int maybe_index) {
  return (kFirstBuiltinIndex <= maybe_index &&
          maybe_index < kFirstBuiltinIndex + kNumberOfBuiltins);
}

// static
bool BuiltinSnapshotUtils::IsHandlerIndex(int maybe_index) {
  return (kFirstHandlerIndex <= maybe_index &&
          maybe_index < kFirstHandlerIndex + kNumberOfHandlers);
}

// static
int BuiltinSnapshotUtils::BytecodeToIndex(Bytecode bytecode,
                                          OperandScale operand_scale) {
  int index =
      BuiltinSnapshotUtils::kNumberOfBuiltins + static_cast<int>(bytecode);
  switch (operand_scale) {  // clang-format off
    case OperandScale::kSingle: return index;
    case OperandScale::kDouble: return index + Bytecodes::kBytecodeCount;
    case OperandScale::kQuadruple: return index + 2 * Bytecodes::kBytecodeCount;
  }  // clang-format on
  UNREACHABLE();
}

// static
std::pair<interpreter::Bytecode, interpreter::OperandScale>
BuiltinSnapshotUtils::BytecodeFromIndex(int index) {
  DCHECK(IsHandlerIndex(index));

  const int x = index - BuiltinSnapshotUtils::kNumberOfBuiltins;
  Bytecode bytecode = Bytecodes::FromByte(x % Bytecodes::kBytecodeCount);
  switch (x / Bytecodes::kBytecodeCount) {  // clang-format off
    case 0: return {bytecode, OperandScale::kSingle};
    case 1: return {bytecode, OperandScale::kDouble};
    case 2: return {bytecode, OperandScale::kQuadruple};
    default: UNREACHABLE();
  }  // clang-format on
}

// static
void BuiltinSnapshotUtils::ForEachBytecode(
    std::function<void(Bytecode, OperandScale)> f) {
  static const OperandScale kOperandScales[] = {
#define VALUE(Name, _) OperandScale::k##Name,
      OPERAND_SCALE_LIST(VALUE)
#undef VALUE
  };

  for (OperandScale operand_scale : kOperandScales) {
    for (int i = 0; i < Bytecodes::kBytecodeCount; i++) {
      f(Bytecodes::FromByte(i), operand_scale);
    }
  }
}

}  // namespace internal
}  // namespace v8