| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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" |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 14 | #include "src/builtins/builtins.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | #include "src/interpreter/bytecodes.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | #include "src/runtime/runtime.h" |
| 17 | |
| 18 | namespace v8 { |
| 19 | namespace internal { |
| 20 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 21 | class BytecodeArray; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | class Callable; |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 23 | class UnoptimizedCompilationJob; |
| 24 | class FunctionLiteral; |
| 25 | class Isolate; |
| 26 | class ParseInfo; |
| 27 | class RootVisitor; |
| 28 | class SetupIsolateDelegate; |
| 29 | template <typename> |
| 30 | class ZoneVector; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 31 | |
| Ben Murdoch | f2e3994 | 2016-05-18 10:25:55 +0000 | [diff] [blame] | 32 | namespace interpreter { |
| Ben Murdoch | 8389745 | 2016-05-17 11:12:09 +0100 | [diff] [blame] | 33 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 34 | class InterpreterAssembler; |
| 35 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | class Interpreter { |
| 37 | public: |
| 38 | explicit Interpreter(Isolate* isolate); |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 39 | virtual ~Interpreter() = default; |
| 40 | Interpreter(const Interpreter&) = delete; |
| 41 | Interpreter& operator=(const Interpreter&) = delete; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 42 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 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); |
| Ben Murdoch | 8389745 | 2016-05-17 11:12:09 +0100 | [diff] [blame] | 50 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 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); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 58 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 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); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 63 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 64 | // Set the bytecode handler for |bytecode| and |operand_scale|. |
| 65 | void SetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale, |
| 66 | Code handler); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 67 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 68 | // Disassembler support. |
| 69 | V8_EXPORT_PRIVATE const char* LookupNameOfBytecodeHandler(const Code code); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 70 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 71 | V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject(); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 72 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 73 | void ForEachBytecode(const std::function<void(Bytecode, OperandScale)>& f); |
| 74 | |
| 75 | void Initialize(); |
| 76 | |
| 77 | bool IsDispatchTableInitialized() const; |
| 78 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 79 | Address dispatch_table_address() { |
| 80 | return reinterpret_cast<Address>(&dispatch_table_[0]); |
| 81 | } |
| 82 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 83 | Address bytecode_dispatch_counters_table() { |
| 84 | return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get()); |
| 85 | } |
| 86 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 87 | Address address_of_interpreter_entry_trampoline_instruction_start() const { |
| 88 | return reinterpret_cast<Address>( |
| 89 | &interpreter_entry_trampoline_instruction_start_); |
| 90 | } |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 91 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | private: |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 93 | friend class SetupInterpreter; |
| 94 | friend class v8::internal::SetupIsolateDelegate; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 96 | uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const; |
| 97 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 98 | // Get dispatch table index of bytecode. |
| 99 | static size_t GetDispatchTableIndex(Bytecode bytecode, |
| 100 | OperandScale operand_scale); |
| 101 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 102 | static const int kNumberOfWideVariants = BytecodeOperands::kOperandScaleCount; |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 103 | static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 104 | static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | |
| 106 | Isolate* isolate_; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 107 | Address dispatch_table_[kDispatchTableSize]; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 108 | std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_; |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 109 | Address interpreter_entry_trampoline_instruction_start_; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace interpreter |
| 113 | } // namespace internal |
| 114 | } // namespace v8 |
| 115 | |
| 116 | #endif // V8_INTERPRETER_INTERPRETER_H_ |