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