blob: d774d8bb7de14afc4e9f218ddd576944afbf2cff [file] [log] [blame]
Ben Murdoch4a90d5f2016-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
8// Clients of this interface shouldn't depend on lots of interpreter internals.
9// Do not include anything from src/interpreter other than
10// src/interpreter/bytecodes.h here!
11#include "src/base/macros.h"
12#include "src/builtins.h"
13#include "src/interpreter/bytecodes.h"
14#include "src/parsing/token.h"
15#include "src/runtime/runtime.h"
16
17namespace v8 {
18namespace internal {
19
20class Isolate;
21class Callable;
22class CompilationInfo;
23
Ben Murdochc5610432016-08-08 18:44:38 +010024namespace compiler {
25class Node;
26} // namespace compiler
27
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028namespace interpreter {
29
Ben Murdoch097c5b22016-05-18 11:27:45 +010030class InterpreterAssembler;
31
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032class Interpreter {
33 public:
34 explicit Interpreter(Isolate* isolate);
35 virtual ~Interpreter() {}
36
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 // Initializes the interpreter dispatch table.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 void Initialize();
39
Ben Murdoch097c5b22016-05-18 11:27:45 +010040 // Returns the interrupt budget which should be used for the profiler counter.
41 static int InterruptBudget();
42
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 // Generate bytecode for |info|.
44 static bool MakeBytecode(CompilationInfo* info);
45
Ben Murdoch097c5b22016-05-18 11:27:45 +010046 // Return bytecode handler for |bytecode|.
Ben Murdochda12d292016-06-02 14:46:10 +010047 Code* GetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale);
Ben Murdoch097c5b22016-05-18 11:27:45 +010048
49 // GC support.
50 void IterateDispatchTable(ObjectVisitor* v);
51
Ben Murdochda12d292016-06-02 14:46:10 +010052 // Disassembler support (only useful with ENABLE_DISASSEMBLER defined).
53 void TraceCodegen(Handle<Code> code);
54 const char* LookupNameOfBytecodeHandler(Code* code);
Ben Murdoch097c5b22016-05-18 11:27:45 +010055
Ben Murdochc5610432016-08-08 18:44:38 +010056 Local<v8::Object> GetDispatchCountersObject();
57
Ben Murdoch097c5b22016-05-18 11:27:45 +010058 Address dispatch_table_address() {
59 return reinterpret_cast<Address>(&dispatch_table_[0]);
60 }
61
Ben Murdochc5610432016-08-08 18:44:38 +010062 Address bytecode_dispatch_counters_table() {
63 return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
64 }
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 private:
67// Bytecode handler generator functions.
68#define DECLARE_BYTECODE_HANDLER_GENERATOR(Name, ...) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010069 void Do##Name(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 BYTECODE_LIST(DECLARE_BYTECODE_HANDLER_GENERATOR)
71#undef DECLARE_BYTECODE_HANDLER_GENERATOR
72
Ben Murdochc5610432016-08-08 18:44:38 +010073 // Generates code to perform the binary operation via |callable|.
Ben Murdochda12d292016-06-02 14:46:10 +010074 void DoBinaryOp(Callable callable, InterpreterAssembler* assembler);
75
Ben Murdochc5610432016-08-08 18:44:38 +010076 // Generates code to perform the binary operation via |function_id|.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 void DoBinaryOp(Runtime::FunctionId function_id,
Ben Murdoch097c5b22016-05-18 11:27:45 +010078 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079
Ben Murdochc5610432016-08-08 18:44:38 +010080 // Generates code to perform the binary operation via |Generator|.
81 template <class Generator>
82 void DoBinaryOp(InterpreterAssembler* assembler);
83
84 // Generates code to perform the unary operation via |Generator|.
85 template <class Generator>
86 void DoUnaryOp(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
88 // Generates code to perform the comparison operation associated with
89 // |compare_op|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010090 void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091
92 // Generates code to load a constant from the constant pool.
Ben Murdoch097c5b22016-05-18 11:27:45 +010093 void DoLoadConstant(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094
95 // Generates code to perform a global load via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010096 void DoLoadGlobal(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097
98 // Generates code to perform a global store via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010099 void DoStoreGlobal(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100
101 // Generates code to perform a named property load via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100102 void DoLoadIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103
104 // Generates code to perform a keyed property load via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100105 void DoKeyedLoadIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000106
107 // Generates code to perform a namedproperty store via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100108 void DoStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109
110 // Generates code to perform a keyed property store via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100111 void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112
113 // Generates code to perform a JS call.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100114 void DoJSCall(InterpreterAssembler* assembler, TailCallMode tail_call_mode);
115
116 // Generates code to perform a runtime call.
117 void DoCallRuntimeCommon(InterpreterAssembler* assembler);
118
119 // Generates code to perform a runtime call returning a pair.
120 void DoCallRuntimeForPairCommon(InterpreterAssembler* assembler);
121
122 // Generates code to perform a JS runtime call.
123 void DoCallJSRuntimeCommon(InterpreterAssembler* assembler);
124
Ben Murdochda12d292016-06-02 14:46:10 +0100125 // Generates code to perform a constructor call.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100126 void DoCallConstruct(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127
Ben Murdochda12d292016-06-02 14:46:10 +0100128 // Generates code to perform a type conversion.
129 void DoTypeConversionOp(Callable callable, InterpreterAssembler* assembler);
130
Ben Murdochc5610432016-08-08 18:44:38 +0100131 // Generates code to perform logical-not on boolean |value|.
132 void DoLogicalNotOp(compiler::Node* value, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133
134 // Generates code to perform delete via function_id.
135 void DoDelete(Runtime::FunctionId function_id,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100136 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137
138 // Generates code to perform a lookup slot load via |function_id|.
139 void DoLoadLookupSlot(Runtime::FunctionId function_id,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100140 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141
142 // Generates code to perform a lookup slot store depending on |language_mode|.
143 void DoStoreLookupSlot(LanguageMode language_mode,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100144 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145
Ben Murdochc5610432016-08-08 18:44:38 +0100146 uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
147
Ben Murdochda12d292016-06-02 14:46:10 +0100148 // Get dispatch table index of bytecode.
149 static size_t GetDispatchTableIndex(Bytecode bytecode,
150 OperandScale operand_scale);
151
Ben Murdoch097c5b22016-05-18 11:27:45 +0100152 bool IsDispatchTableInitialized();
153
Ben Murdochda12d292016-06-02 14:46:10 +0100154 static const int kNumberOfWideVariants = 3;
155 static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
Ben Murdochc5610432016-08-08 18:44:38 +0100156 static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157
158 Isolate* isolate_;
Ben Murdochc5610432016-08-08 18:44:38 +0100159 Address dispatch_table_[kDispatchTableSize];
160 v8::base::SmartArrayPointer<uintptr_t> bytecode_dispatch_counters_table_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161
162 DISALLOW_COPY_AND_ASSIGN(Interpreter);
163};
164
165} // namespace interpreter
166} // namespace internal
167} // namespace v8
168
169#endif // V8_INTERPRETER_INTERPRETER_H_