blob: 468486cf8f93d89eb45a0ef235930e9e4495f2ae [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 |Generator|.
74 template <class Generator>
75 void DoBinaryOp(InterpreterAssembler* assembler);
76
Ben Murdoch61f157c2016-09-16 13:49:30 +010077 // Generates code to perform the unary operation via |callable|.
78 void DoUnaryOp(Callable callable, InterpreterAssembler* assembler);
79
Ben Murdochc5610432016-08-08 18:44:38 +010080 // Generates code to perform the unary operation via |Generator|.
81 template <class Generator>
82 void DoUnaryOp(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083
84 // Generates code to perform the comparison operation associated with
85 // |compare_op|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010086 void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 // Generates code to perform a global store via |ic|.
Ben Murdoch61f157c2016-09-16 13:49:30 +010089 void DoStaGlobal(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090
Ben Murdoch61f157c2016-09-16 13:49:30 +010091 // Generates code to perform a named property store via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010092 void DoStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093
94 // Generates code to perform a keyed property store via |ic|.
Ben Murdoch097c5b22016-05-18 11:27:45 +010095 void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096
97 // Generates code to perform a JS call.
Ben Murdoch097c5b22016-05-18 11:27:45 +010098 void DoJSCall(InterpreterAssembler* assembler, TailCallMode tail_call_mode);
99
100 // Generates code to perform a runtime call.
101 void DoCallRuntimeCommon(InterpreterAssembler* assembler);
102
103 // Generates code to perform a runtime call returning a pair.
104 void DoCallRuntimeForPairCommon(InterpreterAssembler* assembler);
105
106 // Generates code to perform a JS runtime call.
107 void DoCallJSRuntimeCommon(InterpreterAssembler* assembler);
108
Ben Murdochda12d292016-06-02 14:46:10 +0100109 // Generates code to perform a constructor call.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100110 void DoCallConstruct(InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 // Generates code to perform delete via function_id.
113 void DoDelete(Runtime::FunctionId function_id,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100114 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115
116 // Generates code to perform a lookup slot load via |function_id|.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100117 void DoLdaLookupSlot(Runtime::FunctionId function_id,
118 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119
120 // Generates code to perform a lookup slot store depending on |language_mode|.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100121 void DoStaLookupSlot(LanguageMode language_mode,
122 InterpreterAssembler* assembler);
123
124 // Generates a node with the undefined constant.
125 compiler::Node* BuildLoadUndefined(InterpreterAssembler* assembler);
126
127 // Generates code to load a context slot.
128 compiler::Node* BuildLoadContextSlot(InterpreterAssembler* assembler);
129
130 // Generates code to load a global.
131 compiler::Node* BuildLoadGlobal(Callable ic, InterpreterAssembler* assembler);
132
133 // Generates code to load a named property.
134 compiler::Node* BuildLoadNamedProperty(Callable ic,
135 InterpreterAssembler* assembler);
136
137 // Generates code to load a keyed property.
138 compiler::Node* BuildLoadKeyedProperty(Callable ic,
139 InterpreterAssembler* assembler);
140
141 // Generates code to perform logical-not on boolean |value| and returns the
142 // result.
143 compiler::Node* BuildLogicalNot(compiler::Node* value,
144 InterpreterAssembler* assembler);
145
146 // Generates code to convert |value| to a boolean and returns the
147 // result.
148 compiler::Node* BuildToBoolean(compiler::Node* value,
149 InterpreterAssembler* assembler);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150
Ben Murdochc5610432016-08-08 18:44:38 +0100151 uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;
152
Ben Murdochda12d292016-06-02 14:46:10 +0100153 // Get dispatch table index of bytecode.
154 static size_t GetDispatchTableIndex(Bytecode bytecode,
155 OperandScale operand_scale);
156
Ben Murdoch097c5b22016-05-18 11:27:45 +0100157 bool IsDispatchTableInitialized();
158
Ben Murdochda12d292016-06-02 14:46:10 +0100159 static const int kNumberOfWideVariants = 3;
160 static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
Ben Murdochc5610432016-08-08 18:44:38 +0100161 static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162
163 Isolate* isolate_;
Ben Murdochc5610432016-08-08 18:44:38 +0100164 Address dispatch_table_[kDispatchTableSize];
165 v8::base::SmartArrayPointer<uintptr_t> bytecode_dispatch_counters_table_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166
167 DISALLOW_COPY_AND_ASSIGN(Interpreter);
168};
169
170} // namespace interpreter
171} // namespace internal
172} // namespace v8
173
174#endif // V8_INTERPRETER_INTERPRETER_H_