OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/interpreter/interpreter.h" | 5 #include "src/interpreter/interpreter.h" |
6 | 6 |
7 #include <fstream> | 7 #include <fstream> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
11 #include "src/compilation-info.h" | 11 #include "src/compilation-info.h" |
12 #include "src/compiler.h" | 12 #include "src/compiler.h" |
13 #include "src/counters.h" | 13 #include "src/counters.h" |
14 #include "src/interpreter/bytecode-generator.h" | 14 #include "src/interpreter/bytecode-generator.h" |
15 #include "src/interpreter/bytecodes.h" | 15 #include "src/interpreter/bytecodes.h" |
| 16 #include "src/interpreter/interpreter-generator.h" |
16 #include "src/log.h" | 17 #include "src/log.h" |
17 #include "src/objects.h" | 18 #include "src/objects.h" |
18 #include "src/setup-isolate.h" | |
19 | 19 |
20 namespace v8 { | 20 namespace v8 { |
21 namespace internal { | 21 namespace internal { |
22 namespace interpreter { | 22 namespace interpreter { |
23 | 23 |
24 class InterpreterCompilationJob final : public CompilationJob { | 24 class InterpreterCompilationJob final : public CompilationJob { |
25 public: | 25 public: |
26 explicit InterpreterCompilationJob(CompilationInfo* info); | 26 explicit InterpreterCompilationJob(CompilationInfo* info); |
27 | 27 |
28 protected: | 28 protected: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 BytecodeGenerator generator_; | 66 BytecodeGenerator generator_; |
67 RuntimeCallStats* runtime_call_stats_; | 67 RuntimeCallStats* runtime_call_stats_; |
68 RuntimeCallCounter background_execute_counter_; | 68 RuntimeCallCounter background_execute_counter_; |
69 bool print_bytecode_; | 69 bool print_bytecode_; |
70 | 70 |
71 DISALLOW_COPY_AND_ASSIGN(InterpreterCompilationJob); | 71 DISALLOW_COPY_AND_ASSIGN(InterpreterCompilationJob); |
72 }; | 72 }; |
73 | 73 |
74 Interpreter::Interpreter(Isolate* isolate) : isolate_(isolate) { | 74 Interpreter::Interpreter(Isolate* isolate) : isolate_(isolate) { |
75 memset(dispatch_table_, 0, sizeof(dispatch_table_)); | 75 memset(dispatch_table_, 0, sizeof(dispatch_table_)); |
| 76 } |
| 77 |
| 78 void Interpreter::Initialize() { |
| 79 if (!ShouldInitializeDispatchTable()) return; |
| 80 HandleScope scope(isolate_); |
76 | 81 |
77 if (FLAG_trace_ignition_dispatches) { | 82 if (FLAG_trace_ignition_dispatches) { |
78 static const int kBytecodeCount = static_cast<int>(Bytecode::kLast) + 1; | 83 static const int kBytecodeCount = static_cast<int>(Bytecode::kLast) + 1; |
79 bytecode_dispatch_counters_table_.reset( | 84 bytecode_dispatch_counters_table_.reset( |
80 new uintptr_t[kBytecodeCount * kBytecodeCount]); | 85 new uintptr_t[kBytecodeCount * kBytecodeCount]); |
81 memset(bytecode_dispatch_counters_table_.get(), 0, | 86 memset(bytecode_dispatch_counters_table_.get(), 0, |
82 sizeof(uintptr_t) * kBytecodeCount * kBytecodeCount); | 87 sizeof(uintptr_t) * kBytecodeCount * kBytecodeCount); |
83 } | 88 } |
| 89 |
| 90 // Generate bytecode handlers for all bytecodes and scales. |
| 91 const OperandScale kOperandScales[] = { |
| 92 #define VALUE(Name, _) OperandScale::k##Name, |
| 93 OPERAND_SCALE_LIST(VALUE) |
| 94 #undef VALUE |
| 95 }; |
| 96 |
| 97 for (OperandScale operand_scale : kOperandScales) { |
| 98 #define GENERATE_CODE(Name, ...) \ |
| 99 InstallBytecodeHandler(isolate_, Bytecode::k##Name, operand_scale); |
| 100 BYTECODE_LIST(GENERATE_CODE) |
| 101 #undef GENERATE_CODE |
| 102 } |
| 103 |
| 104 // Fill unused entries will the illegal bytecode handler. |
| 105 size_t illegal_index = |
| 106 GetDispatchTableIndex(Bytecode::kIllegal, OperandScale::kSingle); |
| 107 for (size_t index = 0; index < arraysize(dispatch_table_); ++index) { |
| 108 if (dispatch_table_[index] == nullptr) { |
| 109 dispatch_table_[index] = dispatch_table_[illegal_index]; |
| 110 } |
| 111 } |
| 112 |
| 113 // Initialization should have been successful. |
| 114 DCHECK(IsDispatchTableInitialized()); |
| 115 } |
| 116 |
| 117 bool Interpreter::ReuseExistingHandler(Bytecode bytecode, |
| 118 OperandScale operand_scale) { |
| 119 size_t index = GetDispatchTableIndex(bytecode, operand_scale); |
| 120 switch (bytecode) { |
| 121 case Bytecode::kCallProperty: |
| 122 case Bytecode::kCallProperty0: |
| 123 case Bytecode::kCallProperty1: |
| 124 case Bytecode::kCallProperty2: { |
| 125 const int offset = static_cast<int>(Bytecode::kCallProperty) - |
| 126 static_cast<int>(Bytecode::kCall); |
| 127 STATIC_ASSERT(offset == |
| 128 static_cast<int>(Bytecode::kCallProperty0) - |
| 129 static_cast<int>(Bytecode::kCall0)); |
| 130 STATIC_ASSERT(offset == |
| 131 static_cast<int>(Bytecode::kCallProperty1) - |
| 132 static_cast<int>(Bytecode::kCall1)); |
| 133 STATIC_ASSERT(offset == |
| 134 static_cast<int>(Bytecode::kCallProperty2) - |
| 135 static_cast<int>(Bytecode::kCall2)); |
| 136 CHECK_LT(offset, index); |
| 137 dispatch_table_[index] = dispatch_table_[index - offset]; |
| 138 return true; |
| 139 break; |
| 140 } |
| 141 default: |
| 142 return false; |
| 143 } |
| 144 } |
| 145 |
| 146 void Interpreter::InstallBytecodeHandler(Isolate* isolate, Bytecode bytecode, |
| 147 OperandScale operand_scale) { |
| 148 if (!Bytecodes::BytecodeHasHandler(bytecode, operand_scale)) return; |
| 149 if (ReuseExistingHandler(bytecode, operand_scale)) return; |
| 150 |
| 151 size_t index = GetDispatchTableIndex(bytecode, operand_scale); |
| 152 Handle<Code> code = GenerateBytecodeHandler(isolate, bytecode, operand_scale); |
| 153 dispatch_table_[index] = code->entry(); |
84 } | 154 } |
85 | 155 |
86 Code* Interpreter::GetBytecodeHandler(Bytecode bytecode, | 156 Code* Interpreter::GetBytecodeHandler(Bytecode bytecode, |
87 OperandScale operand_scale) { | 157 OperandScale operand_scale) { |
88 DCHECK(IsDispatchTableInitialized()); | 158 DCHECK(IsDispatchTableInitialized()); |
89 DCHECK(Bytecodes::BytecodeHasHandler(bytecode, operand_scale)); | 159 DCHECK(Bytecodes::BytecodeHasHandler(bytecode, operand_scale)); |
90 size_t index = GetDispatchTableIndex(bytecode, operand_scale); | 160 size_t index = GetDispatchTableIndex(bytecode, operand_scale); |
91 Address code_entry = dispatch_table_[index]; | 161 Address code_entry = dispatch_table_[index]; |
92 return Code::GetCodeFromTargetAddress(code_entry); | 162 return Code::GetCodeFromTargetAddress(code_entry); |
93 } | 163 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 278 } |
209 | 279 |
210 CompilationJob* Interpreter::NewCompilationJob(CompilationInfo* info) { | 280 CompilationJob* Interpreter::NewCompilationJob(CompilationInfo* info) { |
211 return new InterpreterCompilationJob(info); | 281 return new InterpreterCompilationJob(info); |
212 } | 282 } |
213 | 283 |
214 bool Interpreter::IsDispatchTableInitialized() { | 284 bool Interpreter::IsDispatchTableInitialized() { |
215 return dispatch_table_[0] != nullptr; | 285 return dispatch_table_[0] != nullptr; |
216 } | 286 } |
217 | 287 |
| 288 bool Interpreter::ShouldInitializeDispatchTable() { |
| 289 if (FLAG_trace_ignition || FLAG_trace_ignition_codegen || |
| 290 FLAG_trace_ignition_dispatches) { |
| 291 // Regenerate table to add bytecode tracing operations, print the assembly |
| 292 // code generated by TurboFan or instrument handlers with dispatch counters. |
| 293 return true; |
| 294 } |
| 295 return !IsDispatchTableInitialized(); |
| 296 } |
| 297 |
218 const char* Interpreter::LookupNameOfBytecodeHandler(Code* code) { | 298 const char* Interpreter::LookupNameOfBytecodeHandler(Code* code) { |
219 #ifdef ENABLE_DISASSEMBLER | 299 #ifdef ENABLE_DISASSEMBLER |
220 #define RETURN_NAME(Name, ...) \ | 300 #define RETURN_NAME(Name, ...) \ |
221 if (dispatch_table_[Bytecodes::ToByte(Bytecode::k##Name)] == \ | 301 if (dispatch_table_[Bytecodes::ToByte(Bytecode::k##Name)] == \ |
222 code->entry()) { \ | 302 code->entry()) { \ |
223 return #Name; \ | 303 return #Name; \ |
224 } | 304 } |
225 BYTECODE_LIST(RETURN_NAME) | 305 BYTECODE_LIST(RETURN_NAME) |
226 #undef RETURN_NAME | 306 #undef RETURN_NAME |
227 #endif // ENABLE_DISASSEMBLER | 307 #endif // ENABLE_DISASSEMBLER |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 counters_map->DefineOwnProperty(context, from_name_object, counters_row) | 363 counters_map->DefineOwnProperty(context, from_name_object, counters_row) |
284 .IsJust()); | 364 .IsJust()); |
285 } | 365 } |
286 | 366 |
287 return counters_map; | 367 return counters_map; |
288 } | 368 } |
289 | 369 |
290 } // namespace interpreter | 370 } // namespace interpreter |
291 } // namespace internal | 371 } // namespace internal |
292 } // namespace v8 | 372 } // namespace v8 |
OLD | NEW |