interpreter.h 6.65 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
class CompilationInfo;
25
class CompilationJob;
26

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

31 32
namespace interpreter {

33 34
class InterpreterAssembler;

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

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

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

46 47
  // Creates a compilation job which will generate bytecode for |info|.
  static CompilationJob* NewCompilationJob(CompilationInfo* info);
48

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

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

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

59
  V8_EXPORT_PRIVATE Local<v8::Object> GetDispatchCountersObject();
60

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

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

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

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

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

83 84
  // Generates code to perform the comparison via |Generator| while gathering
  // type feedback.
85 86
  void DoCompareOpWithFeedback(Token::Value compare_op,
                               InterpreterAssembler* assembler);
87

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

93 94 95 96 97
  // 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);

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

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

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

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

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

116
  // Generates code to perform a JS call that collects type feedback.
117
  void DoJSCall(InterpreterAssembler* assembler, TailCallMode tail_call_mode);
118

119 120
  // Generates code to perform delete via function_id.
  void DoDelete(Runtime::FunctionId function_id,
121
                InterpreterAssembler* assembler);
122

123
  // Generates code to perform a lookup slot load via |function_id|.
124 125
  void DoLdaLookupSlot(Runtime::FunctionId function_id,
                       InterpreterAssembler* assembler);
126

127 128 129 130 131
  // Generates code to perform a lookup slot load via |function_id| that can
  // fast path to a context slot load.
  void DoLdaLookupContextSlot(Runtime::FunctionId function_id,
                              InterpreterAssembler* assembler);

132 133 134 135 136 137 138
  // Generates code to perform a lookup slot load via |function_id| that can
  // fast path to a global load.
  void DoLdaLookupGlobalSlot(Runtime::FunctionId function_id,
                             InterpreterAssembler* assembler);

  // Generates code to perform a lookup slot store depending on
  // |language_mode|.
139 140 141 142
  void DoStaLookupSlot(LanguageMode language_mode,
                       InterpreterAssembler* assembler);

  // Generates code to load a global.
143
  compiler::Node* BuildLoadGlobal(Callable ic, compiler::Node* context,
144
                                  compiler::Node* name_index,
145 146
                                  compiler::Node* feedback_slot,
                                  InterpreterAssembler* assembler);
147

148 149 150 151 152 153 154 155 156
  // 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);

157 158 159 160
  // Generates code to perform the unary operation via |callable|.
  compiler::Node* BuildUnaryOp(Callable callable,
                               InterpreterAssembler* assembler);

161 162
  uintptr_t GetDispatchCounter(Bytecode from, Bytecode to) const;

163 164 165 166
  // Get dispatch table index of bytecode.
  static size_t GetDispatchTableIndex(Bytecode bytecode,
                                      OperandScale operand_scale);

167
  bool IsDispatchTableInitialized();
168
  bool ShouldInitializeDispatchTable();
169

170 171
  static const int kNumberOfWideVariants = 3;
  static const int kDispatchTableSize = kNumberOfWideVariants * (kMaxUInt8 + 1);
172
  static const int kNumberOfBytecodes = static_cast<int>(Bytecode::kLast) + 1;
173

174
  Isolate* isolate_;
175
  Address dispatch_table_[kDispatchTableSize];
176
  std::unique_ptr<uintptr_t[]> bytecode_dispatch_counters_table_;
177 178 179 180 181 182 183 184 185

  DISALLOW_COPY_AND_ASSIGN(Interpreter);
};

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

#endif  // V8_INTERPRETER_INTERPRETER_H_