1 // Copyright 2017 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 "src/snapshot/builtin-snapshot-utils.h"
6
7 namespace v8 {
8 namespace internal {
9
10 // static
IsBuiltinIndex(int maybe_index)11 bool BuiltinSnapshotUtils::IsBuiltinIndex(int maybe_index) {
12 return (kFirstBuiltinIndex <= maybe_index &&
13 maybe_index < kFirstBuiltinIndex + kNumberOfBuiltins);
14 }
15
16 // static
IsHandlerIndex(int maybe_index)17 bool BuiltinSnapshotUtils::IsHandlerIndex(int maybe_index) {
18 return (kFirstHandlerIndex <= maybe_index &&
19 maybe_index < kFirstHandlerIndex + kNumberOfHandlers);
20 }
21
22 // static
BytecodeToIndex(Bytecode bytecode,OperandScale operand_scale)23 int BuiltinSnapshotUtils::BytecodeToIndex(Bytecode bytecode,
24 OperandScale operand_scale) {
25 int index =
26 BuiltinSnapshotUtils::kNumberOfBuiltins + static_cast<int>(bytecode);
27 switch (operand_scale) { // clang-format off
28 case OperandScale::kSingle: return index;
29 case OperandScale::kDouble: return index + Bytecodes::kBytecodeCount;
30 case OperandScale::kQuadruple: return index + 2 * Bytecodes::kBytecodeCount;
31 } // clang-format on
32 UNREACHABLE();
33 }
34
35 // static
36 std::pair<interpreter::Bytecode, interpreter::OperandScale>
BytecodeFromIndex(int index)37 BuiltinSnapshotUtils::BytecodeFromIndex(int index) {
38 DCHECK(IsHandlerIndex(index));
39
40 const int x = index - BuiltinSnapshotUtils::kNumberOfBuiltins;
41 Bytecode bytecode = Bytecodes::FromByte(x % Bytecodes::kBytecodeCount);
42 switch (x / Bytecodes::kBytecodeCount) { // clang-format off
43 case 0: return {bytecode, OperandScale::kSingle};
44 case 1: return {bytecode, OperandScale::kDouble};
45 case 2: return {bytecode, OperandScale::kQuadruple};
46 default: UNREACHABLE();
47 } // clang-format on
48 }
49
50 // static
ForEachBytecode(std::function<void (Bytecode,OperandScale)> f)51 void BuiltinSnapshotUtils::ForEachBytecode(
52 std::function<void(Bytecode, OperandScale)> f) {
53 static const OperandScale kOperandScales[] = {
54 #define VALUE(Name, _) OperandScale::k##Name,
55 OPERAND_SCALE_LIST(VALUE)
56 #undef VALUE
57 };
58
59 for (OperandScale operand_scale : kOperandScales) {
60 for (int i = 0; i < Bytecodes::kBytecodeCount; i++) {
61 f(Bytecodes::FromByte(i), operand_scale);
62 }
63 }
64 }
65
66 } // namespace internal
67 } // namespace v8
68