| 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" |
| 16 | #include "src/parsing/token.h" |
| 17 | #include "src/runtime/runtime.h" |
| 18 | |
| 19 | namespace v8 { |
| 20 | namespace internal { |
| 21 | |
| 22 | class Isolate; |
| 23 | class Callable; |
| 24 | class CompilationInfo; |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 25 | class CompilationJob; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 27 | namespace compiler { |
| 28 | class Node; |
| 29 | } // namespace compiler |
| 30 | |
| Ben Murdoch | f2e3994 | 2016-05-18 10:25:55 +0000 | [diff] [blame] | 31 | namespace interpreter { |
| Ben Murdoch | 8389745 | 2016-05-17 11:12:09 +0100 | [diff] [blame] | 32 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 33 | class InterpreterAssembler; |
| 34 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | class Interpreter { |
| 36 | public: |
| 37 | explicit Interpreter(Isolate* isolate); |
| 38 | virtual ~Interpreter() {} |
| 39 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 40 | // Initializes the interpreter dispatch table. |
| Ben Murdoch | f2e3994 | 2016-05-18 10:25:55 +0000 | [diff] [blame] | 41 | void Initialize(); |
| Ben Murdoch | 8389745 | 2016-05-17 11:12:09 +0100 | [diff] [blame] | 42 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 43 | // Returns the interrupt budget which should be used for the profiler counter. |
| 44 | static int InterruptBudget(); |
| 45 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 46 | // Creates a compilation job which will generate bytecode for |info|. |
| 47 | static CompilationJob* NewCompilationJob(CompilationInfo* info); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 49 | // Return bytecode handler for |bytecode|. |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 50 | Code* GetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 51 | |
| 52 | // GC support. |
| 53 | void IterateDispatchTable(ObjectVisitor* v); |
| 54 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 55 | // Disassembler support (only useful with ENABLE_DISASSEMBLER defined). |
| 56 | void TraceCodegen(Handle<Code> code); |
| 57 | const char* LookupNameOfBytecodeHandler(Code* code); |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 58 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 59 | V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject(); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 60 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 61 | Address dispatch_table_address() { |
| 62 | return reinterpret_cast<Address>(&dispatch_table_[0]); |
| 63 | } |
| 64 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 65 | Address bytecode_dispatch_counters_table() { |
| 66 | return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get()); |
| 67 | } |
| 68 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 69 | // TODO(ignition): Tune code size multiplier. |
| 70 | static const int kCodeSizeMultiplier = 32; |
| 71 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | private: |
| 73 | // Bytecode handler generator functions. |
| 74 | #define DECLARE_BYTECODE_HANDLER_GENERATOR(Name, ...) \ |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 75 | void Do##Name(InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | BYTECODE_LIST(DECLARE_BYTECODE_HANDLER_GENERATOR) |
| 77 | #undef DECLARE_BYTECODE_HANDLER_GENERATOR |
| 78 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 79 | // Generates code to perform the binary operation via |Generator|. |
| 80 | template <class Generator> |
| 81 | void DoBinaryOp(InterpreterAssembler* assembler); |
| 82 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 83 | // Generates code to perform the binary operation via |Generator|. |
| 84 | template <class Generator> |
| 85 | void DoBinaryOpWithFeedback(InterpreterAssembler* assembler); |
| 86 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 87 | // Generates code to perform the comparison via |Generator| while gathering |
| 88 | // type feedback. |
| 89 | template <class Generator> |
| 90 | void DoCompareOpWithFeedback(InterpreterAssembler* assembler); |
| 91 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 92 | // Generates code to perform the bitwise binary operation corresponding to |
| 93 | // |bitwise_op| while gathering type feedback. |
| 94 | void DoBitwiseBinaryOp(Token::Value bitwise_op, |
| 95 | InterpreterAssembler* assembler); |
| 96 | |
| 97 | // Generates code to perform the binary operation via |Generator| using |
| 98 | // an immediate value rather the accumulator as the rhs operand. |
| 99 | template <class Generator> |
| 100 | void DoBinaryOpWithImmediate(InterpreterAssembler* assembler); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 101 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 102 | // Generates code to perform the unary operation via |Generator|. |
| 103 | template <class Generator> |
| 104 | void DoUnaryOp(InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 106 | // Generates code to perform the unary operation via |Generator| while |
| 107 | // gatering type feedback. |
| 108 | template <class Generator> |
| 109 | void DoUnaryOpWithFeedback(InterpreterAssembler* assembler); |
| 110 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | // Generates code to perform the comparison operation associated with |
| 112 | // |compare_op|. |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 113 | void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 114 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | // Generates code to perform a global store via |ic|. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 116 | void DoStaGlobal(Callable ic, InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 118 | // Generates code to perform a named property store via |ic|. |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 119 | void DoStoreIC(Callable ic, InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | |
| 121 | // Generates code to perform a keyed property store via |ic|. |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 122 | void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 124 | // Generates code to perform a JS call that collects type feedback. |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 125 | void DoJSCall(InterpreterAssembler* assembler, TailCallMode tail_call_mode); |
| 126 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 127 | // Generates code to perform delete via function_id. |
| 128 | void DoDelete(Runtime::FunctionId function_id, |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 129 | InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | |
| 131 | // Generates code to perform a lookup slot load via |function_id|. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 132 | void DoLdaLookupSlot(Runtime::FunctionId function_id, |
| 133 | InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 134 | |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 135 | // Generates code to perform a lookup slot load via |function_id| that can |
| 136 | // fast path to a context slot load. |
| 137 | void DoLdaLookupContextSlot(Runtime::FunctionId function_id, |
| 138 | InterpreterAssembler* assembler); |
| 139 | |
| 140 | // Generates code to perform a lookup slot load via |function_id| that can |
| 141 | // fast path to a global load. |
| 142 | void DoLdaLookupGlobalSlot(Runtime::FunctionId function_id, |
| 143 | InterpreterAssembler* assembler); |
| 144 | |
| 145 | // Generates code to perform a lookup slot store depending on |
| 146 | // |language_mode|. |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 147 | void DoStaLookupSlot(LanguageMode language_mode, |
| 148 | InterpreterAssembler* assembler); |
| 149 | |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 150 | // Generates code to load a context slot. |
| 151 | compiler::Node* BuildLoadContextSlot(InterpreterAssembler* assembler); |
| 152 | |
| 153 | // Generates code to load a global. |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame^] | 154 | compiler::Node* BuildLoadGlobal(Callable ic, compiler::Node* context, |
| 155 | compiler::Node* feedback_slot, |
| 156 | InterpreterAssembler* assembler); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 157 | |
| 158 | // Generates code to load a named property. |
| 159 | compiler::Node* BuildLoadNamedProperty(Callable ic, |
| 160 | InterpreterAssembler* assembler); |
| 161 | |
| 162 | // Generates code to load a keyed property. |
| 163 | compiler::Node* BuildLoadKeyedProperty(Callable ic, |
| 164 | InterpreterAssembler* assembler); |
| 165 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 166 | // Generates code to prepare the result for ForInPrepare. Cache data |
| 167 | // are placed into the consecutive series of registers starting at |
| 168 | // |output_register|. |
| 169 | void BuildForInPrepareResult(compiler::Node* output_register, |
| 170 | compiler::Node* cache_type, |
| 171 | compiler::Node* cache_array, |
| 172 | compiler::Node* cache_length, |
| 173 | InterpreterAssembler* assembler); |
| Ben Murdoch | 13e2dad | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 174 | |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 175 | // Generates code to perform the unary operation via |callable|. |
| 176 | compiler::Node* BuildUnaryOp(Callable callable, |
| 177 | InterpreterAssembler* assembler); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 178 | |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 179 | uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const; |
| 180 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 181 | // Get dispatch table index of bytecode. |
| 182 | static size_t GetDispatchTableIndex(Bytecode bytecode, |
| 183 | OperandScale operand_scale); |
| 184 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 185 | bool IsDispatchTableInitialized(); |
| 186 | |
| Ben Murdoch | 3b9bc31 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 187 | static const int kNumberOfWideVariants = 3; |
| 188 | static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1); |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 189 | static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 190 | |
| 191 | Isolate* isolate_; |
| Ben Murdoch | bcf72ee | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 192 | Address dispatch_table_[kDispatchTableSize]; |
| Ben Murdoch | f91f061 | 2016-11-29 16:50:11 +0000 | [diff] [blame] | 193 | std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 194 | |
| 195 | DISALLOW_COPY_AND_ASSIGN(Interpreter); |
| 196 | }; |
| 197 | |
| 198 | } // namespace interpreter |
| 199 | } // namespace internal |
| 200 | } // namespace v8 |
| 201 | |
| 202 | #endif // V8_INTERPRETER_INTERPRETER_H_ |