• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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_INTERPRETER_INTERPRETER_H_
6 #define V8_INTERPRETER_INTERPRETER_H_
7 
8 #include <memory>
9 
10 // Clients of this interface shouldn't depend on lots of interpreter internals.
11 // Do not include anything from src/interpreter other than
12 // src/interpreter/bytecodes.h here!
13 #include "src/base/macros.h"
14 #include "src/builtins/builtins.h"
15 #include "src/interpreter/bytecodes.h"
16 #include "src/runtime/runtime.h"
17 
18 namespace v8 {
19 namespace internal {
20 
21 class BytecodeArray;
22 class Callable;
23 class UnoptimizedCompilationJob;
24 class FunctionLiteral;
25 class Isolate;
26 class ParseInfo;
27 class RootVisitor;
28 class SetupIsolateDelegate;
29 template <typename>
30 class ZoneVector;
31 
32 namespace interpreter {
33 
34 class InterpreterAssembler;
35 
36 class Interpreter {
37  public:
38   explicit Interpreter(Isolate* isolate);
39   virtual ~Interpreter() = default;
40   Interpreter(const Interpreter&) = delete;
41   Interpreter& operator=(const Interpreter&) = delete;
42 
43   // Creates a compilation job which will generate bytecode for |literal|.
44   // Additionally, if |eager_inner_literals| is not null, adds any eagerly
45   // compilable inner FunctionLiterals to this list.
46   static std::unique_ptr<UnoptimizedCompilationJob> NewCompilationJob(
47       ParseInfo* parse_info, FunctionLiteral* literal,
48       AccountingAllocator* allocator,
49       std::vector<FunctionLiteral*>* eager_inner_literals);
50 
51   // Creates a compilation job which will generate source positions for
52   // |literal| and when finalized, store the result into |existing_bytecode|.
53   static std::unique_ptr<UnoptimizedCompilationJob>
54   NewSourcePositionCollectionJob(ParseInfo* parse_info,
55                                  FunctionLiteral* literal,
56                                  Handle<BytecodeArray> existing_bytecode,
57                                  AccountingAllocator* allocator);
58 
59   // If the bytecode handler for |bytecode| and |operand_scale| has not yet
60   // been loaded, deserialize it. Then return the handler.
61   V8_EXPORT_PRIVATE Code GetBytecodeHandler(Bytecode bytecode,
62                                             OperandScale operand_scale);
63 
64   // Set the bytecode handler for |bytecode| and |operand_scale|.
65   void SetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale,
66                           Code handler);
67 
68   // Disassembler support.
69   V8_EXPORT_PRIVATE const char* LookupNameOfBytecodeHandler(const Code code);
70 
71   V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject();
72 
73   void ForEachBytecode(const std::function<void(Bytecode, OperandScale)>& f);
74 
75   void Initialize();
76 
77   bool IsDispatchTableInitialized() const;
78 
dispatch_table_address()79   Address dispatch_table_address() {
80     return reinterpret_cast<Address>(&dispatch_table_[0]);
81   }
82 
bytecode_dispatch_counters_table()83   Address bytecode_dispatch_counters_table() {
84     return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
85   }
86 
address_of_interpreter_entry_trampoline_instruction_start()87   Address address_of_interpreter_entry_trampoline_instruction_start() const {
88     return reinterpret_cast<Address>(
89         &interpreter_entry_trampoline_instruction_start_);
90   }
91 
92  private:
93   friend class SetupInterpreter;
94   friend class v8::internal::SetupIsolateDelegate;
95 
96   uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
97 
98   // Get dispatch table index of bytecode.
99   static size_t GetDispatchTableIndex(Bytecode bytecode,
100                                       OperandScale operand_scale);
101 
102   static const int kNumberOfWideVariants = BytecodeOperands::kOperandScaleCount;
103   static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
104   static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
105 
106   Isolate* isolate_;
107   Address dispatch_table_[kDispatchTableSize];
108   std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
109   Address interpreter_entry_trampoline_instruction_start_;
110 };
111 
112 }  // namespace interpreter
113 }  // namespace internal
114 }  // namespace v8
115 
116 #endif  // V8_INTERPRETER_INTERPRETER_H_
117