blob: b646bf83138c3c1705f73a96814e8e655a4b395e [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;
Ben Murdochf3b273f2017-01-17 12:11:28 +000025class CompilationJob;
Ben Murdoch014dc512016-03-22 12:00:34 +000026
Ben Murdochbcf72ee2016-08-08 18:44:38 +010027namespace compiler {
28class Node;
29} // namespace compiler
30
Ben Murdochf2e39942016-05-18 10:25:55 +000031namespace interpreter {
Ben Murdoch83897452016-05-17 11:12:09 +010032
Ben Murdoch109988c2016-05-18 11:27:45 +010033class InterpreterAssembler;
34
Ben Murdoch014dc512016-03-22 12:00:34 +000035class Interpreter {
36 public:
37 explicit Interpreter(Isolate* isolate);
38 virtual ~Interpreter() {}
39
Ben Murdoch109988c2016-05-18 11:27:45 +010040 // Initializes the interpreter dispatch table.
Ben Murdochf2e39942016-05-18 10:25:55 +000041 void Initialize();
Ben Murdoch83897452016-05-17 11:12:09 +010042
Ben Murdoch109988c2016-05-18 11:27:45 +010043 // Returns the interrupt budget which should be used for the profiler counter.
44 static int InterruptBudget();
45
Ben Murdochf3b273f2017-01-17 12:11:28 +000046 // Creates a compilation job which will generate bytecode for |info|.
47 static CompilationJob* NewCompilationJob(CompilationInfo* info);
Ben Murdoch014dc512016-03-22 12:00:34 +000048
Ben Murdoch109988c2016-05-18 11:27:45 +010049 // Return bytecode handler for |bytecode|.
Ben Murdoch3b9bc312016-06-02 14:46:10 +010050 Code* GetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale);
Ben Murdoch109988c2016-05-18 11:27:45 +010051
52 // GC support.
53 void IterateDispatchTable(ObjectVisitor* v);
54
Ben Murdoch3b9bc312016-06-02 14:46:10 +010055 // Disassembler support (only useful with ENABLE_DISASSEMBLER defined).
56 void TraceCodegen(Handle<Code> code);
57 const char* LookupNameOfBytecodeHandler(Code* code);
Ben Murdoch109988c2016-05-18 11:27:45 +010058
Ben Murdochf3b273f2017-01-17 12:11:28 +000059 V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject();
Ben Murdochbcf72ee2016-08-08 18:44:38 +010060
Ben Murdoch109988c2016-05-18 11:27:45 +010061 Address dispatch_table_address() {
62 return reinterpret_cast<Address>(&dispatch_table_[0]);
63 }
64
Ben Murdochbcf72ee2016-08-08 18:44:38 +010065 Address bytecode_dispatch_counters_table() {
66 return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
67 }
68
Ben Murdochf91f0612016-11-29 16:50:11 +000069 // TODO(ignition): Tune code size multiplier.
70 static const int kCodeSizeMultiplier = 32;
71
Ben Murdoch014dc512016-03-22 12:00:34 +000072 private:
73// Bytecode handler generator functions.
74#define DECLARE_BYTECODE_HANDLER_GENERATOR(Name, ...) \
Ben Murdoch109988c2016-05-18 11:27:45 +010075 void Do##Name(InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +000076 BYTECODE_LIST(DECLARE_BYTECODE_HANDLER_GENERATOR)
77#undef DECLARE_BYTECODE_HANDLER_GENERATOR
78
Ben Murdochbcf72ee2016-08-08 18:44:38 +010079 // Generates code to perform the binary operation via |Generator|.
80 template <class Generator>
81 void DoBinaryOp(InterpreterAssembler* assembler);
82
Ben Murdochf91f0612016-11-29 16:50:11 +000083 // Generates code to perform the binary operation via |Generator|.
84 template <class Generator>
85 void DoBinaryOpWithFeedback(InterpreterAssembler* assembler);
86
Ben Murdochf3b273f2017-01-17 12:11:28 +000087 // Generates code to perform the comparison via |Generator| while gathering
88 // type feedback.
89 template <class Generator>
90 void DoCompareOpWithFeedback(InterpreterAssembler* assembler);
91
Ben Murdochf91f0612016-11-29 16:50:11 +000092 // 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 Murdoch13e2dad2016-09-16 13:49:30 +0100101
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100102 // Generates code to perform the unary operation via |Generator|.
103 template <class Generator>
104 void DoUnaryOp(InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000105
Ben Murdochf91f0612016-11-29 16:50:11 +0000106 // 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 Murdoch014dc512016-03-22 12:00:34 +0000111 // Generates code to perform the comparison operation associated with
112 // |compare_op|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100113 void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000114
Ben Murdoch014dc512016-03-22 12:00:34 +0000115 // Generates code to perform a global store via |ic|.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100116 void DoStaGlobal(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000117
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100118 // Generates code to perform a named property store via |ic|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100119 void DoStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000120
121 // Generates code to perform a keyed property store via |ic|.
Ben Murdoch109988c2016-05-18 11:27:45 +0100122 void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000123
Ben Murdochf91f0612016-11-29 16:50:11 +0000124 // Generates code to perform a JS call that collects type feedback.
Ben Murdoch109988c2016-05-18 11:27:45 +0100125 void DoJSCall(InterpreterAssembler* assembler, TailCallMode tail_call_mode);
126
Ben Murdoch014dc512016-03-22 12:00:34 +0000127 // Generates code to perform delete via function_id.
128 void DoDelete(Runtime::FunctionId function_id,
Ben Murdoch109988c2016-05-18 11:27:45 +0100129 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000130
131 // Generates code to perform a lookup slot load via |function_id|.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100132 void DoLdaLookupSlot(Runtime::FunctionId function_id,
133 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000134
Ben Murdochf3b273f2017-01-17 12:11:28 +0000135 // 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 Murdoch13e2dad2016-09-16 13:49:30 +0100147 void DoStaLookupSlot(LanguageMode language_mode,
148 InterpreterAssembler* assembler);
149
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100150 // Generates code to load a context slot.
151 compiler::Node* BuildLoadContextSlot(InterpreterAssembler* assembler);
152
153 // Generates code to load a global.
Ben Murdochf3b273f2017-01-17 12:11:28 +0000154 compiler::Node* BuildLoadGlobal(Callable ic, compiler::Node* context,
155 compiler::Node* feedback_slot,
156 InterpreterAssembler* assembler);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100157
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 Murdochf91f0612016-11-29 16:50:11 +0000166 // 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 Murdoch13e2dad2016-09-16 13:49:30 +0100174
Ben Murdochf91f0612016-11-29 16:50:11 +0000175 // Generates code to perform the unary operation via |callable|.
176 compiler::Node* BuildUnaryOp(Callable callable,
177 InterpreterAssembler* assembler);
Ben Murdoch014dc512016-03-22 12:00:34 +0000178
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100179 uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
180
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100181 // Get dispatch table index of bytecode.
182 static size_t GetDispatchTableIndex(Bytecode bytecode,
183 OperandScale operand_scale);
184
Ben Murdoch109988c2016-05-18 11:27:45 +0100185 bool IsDispatchTableInitialized();
186
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100187 static const int kNumberOfWideVariants = 3;
188 static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100189 static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
Ben Murdoch014dc512016-03-22 12:00:34 +0000190
191 Isolate* isolate_;
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100192 Address dispatch_table_[kDispatchTableSize];
Ben Murdochf91f0612016-11-29 16:50:11 +0000193 std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
Ben Murdoch014dc512016-03-22 12:00:34 +0000194
195 DISALLOW_COPY_AND_ASSIGN(Interpreter);
196};
197
198} // namespace interpreter
199} // namespace internal
200} // namespace v8
201
202#endif // V8_INTERPRETER_INTERPRETER_H_