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 #ifndef V8_SNAPSHOT_BUILTIN_SNAPSHOT_UTILS_H_ 6 #define V8_SNAPSHOT_BUILTIN_SNAPSHOT_UTILS_H_ 7 8 #include <functional> 9 10 #include "src/interpreter/interpreter.h" 11 12 namespace v8 { 13 namespace internal { 14 15 // Constants and utility methods used by builtin and bytecode handler 16 // (de)serialization. 17 class BuiltinSnapshotUtils : public AllStatic { 18 using Bytecode = interpreter::Bytecode; 19 using BytecodeOperands = interpreter::BytecodeOperands; 20 using Bytecodes = interpreter::Bytecodes; 21 using Interpreter = interpreter::Interpreter; 22 using OperandScale = interpreter::OperandScale; 23 24 public: 25 static const int kFirstBuiltinIndex = 0; 26 static const int kNumberOfBuiltins = Builtins::builtin_count; 27 28 static const int kFirstHandlerIndex = kFirstBuiltinIndex + kNumberOfBuiltins; 29 static const int kNumberOfHandlers = 30 Bytecodes::kBytecodeCount * BytecodeOperands::kOperandScaleCount; 31 32 // The number of code objects in the builtin snapshot. 33 // TODO(jgruber): This could be reduced by a bit since not every 34 // {bytecode, operand_scale} combination has an associated handler 35 // (see Bytecodes::BytecodeHasHandler). 36 static const int kNumberOfCodeObjects = kNumberOfBuiltins + kNumberOfHandlers; 37 38 // Indexes into the offsets vector contained in snapshot. 39 // See e.g. BuiltinSerializer::code_offsets_. 40 static bool IsBuiltinIndex(int maybe_index); 41 static bool IsHandlerIndex(int maybe_index); 42 static int BytecodeToIndex(Bytecode bytecode, OperandScale operand_scale); 43 44 // Converts an index back into the {bytecode,operand_scale} tuple. This is the 45 // inverse operation of BytecodeToIndex(). 46 static std::pair<Bytecode, OperandScale> BytecodeFromIndex(int index); 47 48 // Iteration over all {bytecode,operand_scale} pairs. Implemented here since 49 // (de)serialization depends on the iteration order. 50 static void ForEachBytecode(std::function<void(Bytecode, OperandScale)> f); 51 }; 52 53 } // namespace internal 54 } // namespace v8 55 56 #endif // V8_SNAPSHOT_BUILTIN_SNAPSHOT_UTILS_H_ 57