blob: bbd01029994d678b5f727d2dbba83ca90ab9c4cf [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"
16#include "src/parsing/token.h"
17#include "src/runtime/runtime.h"
18
19namespace v8 {
20namespace internal {
21
22class Isolate;
23class Callable;
24class CompilationInfo;
25
Ben Murdochbcf72ee2016-08-08 18:44:38 +010026namespace compiler {
27class Node;
28} // namespace compiler
29
Ben Murdochf2e39942016-05-18 10:25:55 +000030namespace interpreter {
Ben Murdoch83897452016-05-17 11:12:09 +010031
Ben Murdoch109988c2016-05-18 11:27:45 +010032class InterpreterAssembler;
33
Ben Murdoch014dc512016-03-22 12:00:34 +000034class Interpreter {
35 public:
36 explicit Interpreter(Isolate* isolate);
37 virtual ~Interpreter() {}
38
Ben Murdoch109988c2016-05-18 11:27:45 +010039 // Initializes the interpreter dispatch table.
Ben Murdochf2e39942016-05-18 10:25:55 +000040 void Initialize();
Ben Murdoch83897452016-05-17 11:12:09 +010041
Ben Murdoch109988c2016-05-18 11:27:45 +010042 // Returns the interrupt budget which should be used for the profiler counter.
43 static int InterruptBudget();
44
Ben Murdoch014dc512016-03-22 12:00:34 +000045 // Generate bytecode for |info|.
46 static bool MakeBytecode(CompilationInfo* info);
47
Ben Murdoch109988c2016-05-18 11:27:45 +010048 // Return bytecode handler for |bytecode|.
Ben Murdoch3b9bc312016-06-02 14:46:10 +010049 Code* GetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale);
Ben Murdoch109988c2016-05-18 11:27:45 +010050
51 // GC support.
52 void IterateDispatchTable(ObjectVisitor* v);
53
Ben Murdoch3b9bc312016-06-02 14:46:10 +010054 // Disassembler support (only useful with ENABLE_DISASSEMBLER defined).
55 void TraceCodegen(Handle<Code> code);
56 const char* LookupNameOfBytecodeHandler(Code* code);
Ben Murdoch109988c2016-05-18 11:27:45 +010057
Ben Murdochbcf72ee2016-08-08 18:44:38 +010058 Local<v8::Object> GetDispatchCountersObject();
59
Ben Murdoch109988c2016-05-18 11:27:45 +010060 Address dispatch_table_address() {
61 return reinterpret_cast<Address>(&dispatch_table_[0]);
62 }
63
Ben Murdochbcf72ee2016-08-08 18:44:38 +010064 Address bytecode_dispatch_counters_table() {
65 return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
66 }
67
Ben Murdochf91f0612016-11-29 16:50:11 +000068 // TODO(ignition): Tune code size multiplier.
69 static const int kCodeSizeMultiplier = 32;
70
Ben Murdoch014dc512016-03-22 12:00:34 +000071 private:
72// Bytecode handler generator functions.
73#define DECLARE_BYTECODE_HANDLER_GENERATOR(Name, ...) \
Ben Murdoch109988c2016-05-18 11:27:45 +010074 void Do##Name(InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +000075 BYTECODE_LIST(DECLARE_BYTECODE_HANDLER_GENERATOR)
76#undef DECLARE_BYTECODE_HANDLER_GENERATOR
77
Ben Murdochbcf72ee2016-08-08 18:44:38 +010078 // Generates code to perform the binary operation via |Generator|.
79 template <class Generator>
80 void DoBinaryOp(InterpreterAssembler* assembler);
81
Ben Murdochf91f0612016-11-29 16:50:11 +000082 // 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 Murdoch13e2dad2016-09-16 13:49:30 +010095
Ben Murdochbcf72ee2016-08-08 18:44:38 +010096 // Generates code to perform the unary operation via |Generator|.
97 template <class Generator>
98 void DoUnaryOp(InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +000099
Ben Murdochf91f0612016-11-29 16:50:11 +0000100 // 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 Murdoch014dc512016-03-22 12:00:34 +0000105 // Generates code to perform the comparison operation associated with
106 // |compare_op|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100107 void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000108
Ben Murdoch014dc512016-03-22 12:00:34 +0000109 // Generates code to perform a global store via |ic|.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100110 void DoStaGlobal(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000111
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100112 // Generates code to perform a named property store via |ic|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100113 void DoStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000114
115 // Generates code to perform a keyed property store via |ic|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100116 void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000117
Ben Murdochf91f0612016-11-29 16:50:11 +0000118 // Generates code to perform a JS call that collects type feedback.
Ben Murdoch109988c2016-05-18 11:27:45 +0100119 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 Murdoch3b9bc312016-06-02 14:46:10 +0100130 // Generates code to perform a constructor call.
Ben Murdoch109988c2016-05-18 11:27:45 +0100131 void DoCallConstruct(InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000132
Ben Murdoch014dc512016-03-22 12:00:34 +0000133 // Generates code to perform delete via function_id.
134 void DoDelete(Runtime::FunctionId function_id,
Ben Murdoch109988c2016-05-18 11:27:45 +0100135 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000136
137 // Generates code to perform a lookup slot load via |function_id|.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100138 void DoLdaLookupSlot(Runtime::FunctionId function_id,
139 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000140
141 // Generates code to perform a lookup slot store depending on |language_mode|.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100142 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 Murdochf91f0612016-11-29 16:50:11 +0000162 // 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 Murdoch13e2dad2016-09-16 13:49:30 +0100170
Ben Murdochf91f0612016-11-29 16:50:11 +0000171 // Generates code to perform the unary operation via |callable|.
172 compiler::Node* BuildUnaryOp(Callable callable,
173 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000174
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100175 uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
176
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100177 // Get dispatch table index of bytecode.
178 static size_t GetDispatchTableIndex(Bytecode bytecode,
179 OperandScale operand_scale);
180
Ben Murdoch109988c2016-05-18 11:27:45 +0100181 bool IsDispatchTableInitialized();
182
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100183 static const int kNumberOfWideVariants = 3;
184 static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100185 static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
Ben Murdoch014dc512016-03-22 12:00:34 +0000186
187 Isolate* isolate_;
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100188 Address dispatch_table_[kDispatchTableSize];
Ben Murdochf91f0612016-11-29 16:50:11 +0000189 std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000190
191 DISALLOW_COPY_AND_ASSIGN(Interpreter);
192};
193
194} // namespace interpreter
195} // namespace internal
196} // namespace v8
197
198#endif // V8_INTERPRETER_INTERPRETER_H_