blob: 3bbd93fffb69f4f09a5fdcb377cabaf30700c65b [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// 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
Ben Murdochf91f0612016-11-29 16:50:11 +00008#include <memory>
9
Ben Murdoch014dc512016-03-22 12:00:34 +000010// 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"
Ben Murdochf91f0612016-11-29 16:50:11 +000014#include "src/builtins/builtins.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000015#include "src/interpreter/bytecodes.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000016#include "src/runtime/runtime.h"
17
18namespace v8 {
19namespace internal {
20
Rubin Xu7bc1b612021-02-16 09:38:50 +000021class BytecodeArray;
Ben Murdoch014dc512016-03-22 12:00:34 +000022class Callable;
Rubin Xu7bc1b612021-02-16 09:38:50 +000023class UnoptimizedCompilationJob;
24class FunctionLiteral;
25class Isolate;
26class ParseInfo;
27class RootVisitor;
28class SetupIsolateDelegate;
29template <typename>
30class ZoneVector;
Ben Murdochbcf72ee2016-08-08 18:44:38 +010031
Ben Murdochf2e39942016-05-18 10:25:55 +000032namespace interpreter {
Ben Murdoch83897452016-05-17 11:12:09 +010033
Ben Murdoch109988c2016-05-18 11:27:45 +010034class InterpreterAssembler;
35
Ben Murdoch014dc512016-03-22 12:00:34 +000036class Interpreter {
37 public:
38 explicit Interpreter(Isolate* isolate);
Rubin Xu7bc1b612021-02-16 09:38:50 +000039 virtual ~Interpreter() = default;
40 Interpreter(const Interpreter&) = delete;
41 Interpreter& operator=(const Interpreter&) = delete;
Ben Murdoch014dc512016-03-22 12:00:34 +000042
Rubin Xu7bc1b612021-02-16 09:38:50 +000043 // 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);
Ben Murdoch83897452016-05-17 11:12:09 +010050
Rubin Xu7bc1b612021-02-16 09:38:50 +000051 // 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);
Ben Murdoch109988c2016-05-18 11:27:45 +010058
Rubin Xu7bc1b612021-02-16 09:38:50 +000059 // 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);
Ben Murdoch014dc512016-03-22 12:00:34 +000063
Rubin Xu7bc1b612021-02-16 09:38:50 +000064 // Set the bytecode handler for |bytecode| and |operand_scale|.
65 void SetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale,
66 Code handler);
Ben Murdoch109988c2016-05-18 11:27:45 +010067
Rubin Xu7bc1b612021-02-16 09:38:50 +000068 // Disassembler support.
69 V8_EXPORT_PRIVATE const char* LookupNameOfBytecodeHandler(const Code code);
Ben Murdoch109988c2016-05-18 11:27:45 +010070
Ben Murdochf3b273f2017-01-17 12:11:28 +000071 V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject();
Ben Murdochbcf72ee2016-08-08 18:44:38 +010072
Rubin Xu7bc1b612021-02-16 09:38:50 +000073 void ForEachBytecode(const std::function<void(Bytecode, OperandScale)>& f);
74
75 void Initialize();
76
77 bool IsDispatchTableInitialized() const;
78
Ben Murdoch109988c2016-05-18 11:27:45 +010079 Address dispatch_table_address() {
80 return reinterpret_cast<Address>(&dispatch_table_[0]);
81 }
82
Ben Murdochbcf72ee2016-08-08 18:44:38 +010083 Address bytecode_dispatch_counters_table() {
84 return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
85 }
86
Rubin Xu7bc1b612021-02-16 09:38:50 +000087 Address address_of_interpreter_entry_trampoline_instruction_start() const {
88 return reinterpret_cast<Address>(
89 &interpreter_entry_trampoline_instruction_start_);
90 }
Ben Murdochf91f0612016-11-29 16:50:11 +000091
Ben Murdoch014dc512016-03-22 12:00:34 +000092 private:
Rubin Xu7bc1b612021-02-16 09:38:50 +000093 friend class SetupInterpreter;
94 friend class v8::internal::SetupIsolateDelegate;
Ben Murdoch014dc512016-03-22 12:00:34 +000095
Ben Murdochbcf72ee2016-08-08 18:44:38 +010096 uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
97
Ben Murdoch3b9bc312016-06-02 14:46:10 +010098 // Get dispatch table index of bytecode.
99 static size_t GetDispatchTableIndex(Bytecode bytecode,
100 OperandScale operand_scale);
101
Rubin Xu7bc1b612021-02-16 09:38:50 +0000102 static const int kNumberOfWideVariants = BytecodeOperands::kOperandScaleCount;
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100103 static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100104 static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
Ben Murdoch014dc512016-03-22 12:00:34 +0000105
106 Isolate* isolate_;
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100107 Address dispatch_table_[kDispatchTableSize];
Ben Murdochf91f0612016-11-29 16:50:11 +0000108 std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
Rubin Xu7bc1b612021-02-16 09:38:50 +0000109 Address interpreter_entry_trampoline_instruction_start_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000110};
111
112} // namespace interpreter
113} // namespace internal
114} // namespace v8
115
116#endif // V8_INTERPRETER_INTERPRETER_H_