interpreter.h 6.97 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTERPRETER_INTERPRETER_H_
#define V8_INTERPRETER_INTERPRETER_H_

8 9
#include <memory>

10 11 12 13
// Clients of this interface shouldn't depend on lots of interpreter internals.
// Do not include anything from src/interpreter other than
// src/interpreter/bytecodes.h here!
#include "src/base/macros.h"
14
#include "src/builtins/builtins.h"
15
#include "src/interpreter/bytecodes.h"
16
#include "src/parsing/token.h"
17
#include "src/runtime/runtime.h"
18 19 20 21 22

namespace v8 {
namespace internal {

class Isolate;
23
class Callable;
24 25
class CompilationInfo;

26 27 28 29
namespace compiler {
class Node;
}  // namespace compiler

30 31
namespace interpreter {

32 33
class InterpreterAssembler;

34 35 36 37 38
class Interpreter {
 public:
  explicit Interpreter(Isolate* isolate);
  virtual ~Interpreter() {}

39
  // Initializes the interpreter dispatch table.
40
  void Initialize();
41

42 43 44
  // Returns the interrupt budget which should be used for the profiler counter.
  static int InterruptBudget();

45 46 47
  // Generate bytecode for |info|.
  static bool MakeBytecode(CompilationInfo* info);

48
  // Return bytecode handler for |bytecode|.
49
  Code* GetBytecodeHandler(Bytecode bytecode, OperandScale operand_scale);
50

51 52 53
  // GC support.
  void IterateDispatchTable(ObjectVisitor* v);

54 55 56
  // Disassembler support (only useful with ENABLE_DISASSEMBLER defined).
  void TraceCodegen(Handle<Code> code);
  const char* LookupNameOfBytecodeHandler(Code* code);
57

58
  Local<v8::Object> GetDispatchCountersObject();
59

60 61 62 63
  Address dispatch_table_address() {
    return reinterpret_cast<Address>(&dispatch_table_[0]);
  }

64 65
  Address bytecode_dispatch_counters_table() {
    return reinterpret_cast<Address>(bytecode_dispatch_counters_table_.get());
66 67
  }

68 69 70
  // TODO(ignition): Tune code size multiplier.
  static const int kCodeSizeMultiplier = 32;

71 72
 private:
// Bytecode handler generator functions.
73
#define DECLARE_BYTECODE_HANDLER_GENERATOR(Name, ...) \
74
  void Do##Name(InterpreterAssembler* assembler);
75 76 77
  BYTECODE_LIST(DECLARE_BYTECODE_HANDLER_GENERATOR)
#undef DECLARE_BYTECODE_HANDLER_GENERATOR

78
  // Generates code to perform the binary operation via |Generator|.
79 80 81
  template <class Generator>
  void DoBinaryOp(InterpreterAssembler* assembler);

82 83 84 85
  // Generates code to perform the binary operation via |Generator|.
  template <class Generator>
  void DoBinaryOpWithFeedback(InterpreterAssembler* assembler);

86 87 88 89 90
  // Generates code to perform the bitwise binary operation corresponding to
  // |bitwise_op| while gathering type feedback.
  void DoBitwiseBinaryOp(Token::Value bitwise_op,
                         InterpreterAssembler* assembler);

91 92 93 94 95
  // Generates code to perform the binary operation via |Generator| using
  // an immediate value rather the accumulator as the rhs operand.
  template <class Generator>
  void DoBinaryOpWithImmediate(InterpreterAssembler* assembler);

96 97 98
  // Generates code to perform the unary operation via |Generator|.
  template <class Generator>
  void DoUnaryOp(InterpreterAssembler* assembler);
99

100 101 102 103 104
  // Generates code to perform the unary operation via |Generator| while
  // gatering type feedback.
  template <class Generator>
  void DoUnaryOpWithFeedback(InterpreterAssembler* assembler);

105 106
  // Generates code to perform the comparison operation associated with
  // |compare_op|.
107
  void DoCompareOp(Token::Value compare_op, InterpreterAssembler* assembler);
108

109
  // Generates code to perform a global store via |ic|.
110
  void DoStaGlobal(Callable ic, InterpreterAssembler* assembler);
111

112
  // Generates code to perform a named property store via |ic|.
113
  void DoStoreIC(Callable ic, InterpreterAssembler* assembler);
114 115

  // Generates code to perform a keyed property store via |ic|.
116
  void DoKeyedStoreIC(Callable ic, InterpreterAssembler* assembler);
117

118
  // Generates code to perform a JS call that collects type feedback.
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

130
  // Generates code to perform a constructor call.
131
  void DoCallConstruct(InterpreterAssembler* assembler);
132

133 134
  // Generates code to perform delete via function_id.
  void DoDelete(Runtime::FunctionId function_id,
135
                InterpreterAssembler* assembler);
136

137
  // Generates code to perform a lookup slot load via |function_id|.
138 139
  void DoLdaLookupSlot(Runtime::FunctionId function_id,
                       InterpreterAssembler* assembler);
140 141

  // Generates code to perform a lookup slot store depending on |language_mode|.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  void DoStaLookupSlot(LanguageMode language_mode,
                       InterpreterAssembler* assembler);

  // Generates a node with the undefined constant.
  compiler::Node* BuildLoadUndefined(InterpreterAssembler* assembler);

  // Generates code to load a context slot.
  compiler::Node* BuildLoadContextSlot(InterpreterAssembler* assembler);

  // Generates code to load a global.
  compiler::Node* BuildLoadGlobal(Callable ic, InterpreterAssembler* assembler);

  // Generates code to load a named property.
  compiler::Node* BuildLoadNamedProperty(Callable ic,
                                         InterpreterAssembler* assembler);

  // Generates code to load a keyed property.
  compiler::Node* BuildLoadKeyedProperty(Callable ic,
                                         InterpreterAssembler* assembler);
161

162 163 164 165 166 167 168 169 170
  // Generates code to prepare the result for ForInPrepare. Cache data
  // are placed into the consecutive series of registers starting at
  // |output_register|.
  void BuildForInPrepareResult(compiler::Node* output_register,
                               compiler::Node* cache_type,
                               compiler::Node* cache_array,
                               compiler::Node* cache_length,
                               InterpreterAssembler* assembler);

171 172 173 174
  // Generates code to perform the unary operation via |callable|.
  compiler::Node* BuildUnaryOp(Callable callable,
                               InterpreterAssembler* assembler);

175 176
  uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;

177 178 179 180
  // Get dispatch table index of bytecode.
  static size_t GetDispatchTableIndex(Bytecode bytecode,
                                      OperandScale operand_scale);

181 182
  bool IsDispatchTableInitialized();

183 184
  static const int kNumberOfWideVariants = 3;
  static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
185
  static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
186

187
  Isolate* isolate_;
188
  Address dispatch_table_[kDispatchTableSize];
189
  std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
190 191 192 193 194 195 196 197 198

  DISALLOW_COPY_AND_ASSIGN(Interpreter);
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_INTERPRETER_H_