diff --git a/BUILD.gn b/BUILD.gn index b27a323094..9e2c0291e5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1994,6 +1994,8 @@ v8_source_set("v8_base") { "src/safepoint-table.h", "src/setup-isolate.h", "src/signature.h", + "src/simulator-base.cc", + "src/simulator-base.h", "src/simulator.h", "src/snapshot/builtin-deserializer-allocator.cc", "src/snapshot/builtin-deserializer-allocator.h", diff --git a/src/arm/simulator-arm.cc b/src/arm/simulator-arm.cc index 9aa35312bf..d7ea88798d 100644 --- a/src/arm/simulator-arm.cc +++ b/src/arm/simulator-arm.cc @@ -640,14 +640,6 @@ void Simulator::CheckICache(base::CustomMatcherHashMap* i_cache, } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ::v8::internal::ExternalReference::set_redirector(isolate, - &RedirectExternalReference); -} - - Simulator::Simulator(Isolate* isolate) : isolate_(isolate) { i_cache_ = isolate_->simulator_i_cache(); if (i_cache_ == nullptr) { @@ -726,12 +718,11 @@ class Redirection { swi_instruction_(al | (0xF * B24) | kCallRtRedirected), type_(type), next_(nullptr) { - next_ = isolate->simulator_redirection(); - Simulator::current(isolate)-> - FlushICache(isolate->simulator_i_cache(), - reinterpret_cast(&swi_instruction_), - Instruction::kInstrSize); - isolate->set_simulator_redirection(this); + next_ = Simulator::redirection(); + Simulator::FlushICache(isolate->simulator_i_cache(), + reinterpret_cast(&swi_instruction_), + Instruction::kInstrSize); + Simulator::set_redirection(this); } void* address_of_swi_instruction() { @@ -743,7 +734,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -783,9 +774,16 @@ class Redirection { // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { if (i_cache != nullptr) { for (base::HashMap::Entry* entry = i_cache->Start(); entry != nullptr; entry = i_cache->Next(entry)) { @@ -795,12 +793,10 @@ void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, } } - -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address_of_swi_instruction(); } diff --git a/src/arm/simulator-arm.h b/src/arm/simulator-arm.h index 1cd9090b8a..7c91bbfc05 100644 --- a/src/arm/simulator-arm.h +++ b/src/arm/simulator-arm.h @@ -24,6 +24,7 @@ #include "src/arm/constants-arm.h" #include "src/assembler.h" #include "src/base/hashmap.h" +#include "src/simulator-base.h" namespace v8 { namespace internal { @@ -58,8 +59,7 @@ class CachePage { char validity_map_[kValidityMapSize]; // One byte per line. }; - -class Simulator { +class Simulator : public SimulatorBase { public: friend class ArmDebugger; enum Register { @@ -159,11 +159,6 @@ class Simulator { // Executes ARM instructions until the PC reaches end_sim_pc. void Execute(); - // Call on program start. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - // V8 generally calls into generated JS code with 5 parameters and into // generated RegExp code with 7 parameters. This is a convenience function, // which sets up the simulator state and grabs the result on return. @@ -325,11 +320,6 @@ class Simulator { static CachePage* GetCachePage(base::CustomMatcherHashMap* i_cache, void* page); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference( - Isolate* isolate, void* external_function, - v8::internal::ExternalReference::Type type); - // Handle arguments and return value for runtime FP functions. void GetFpArgs(double* x, double* y, int32_t* z); void SetFpResult(const double& result); diff --git a/src/arm64/simulator-arm64.cc b/src/arm64/simulator-arm64.cc index 6426fab69f..2c18c37058 100644 --- a/src/arm64/simulator-arm64.cc +++ b/src/arm64/simulator-arm64.cc @@ -98,13 +98,6 @@ SimSystemRegister SimSystemRegister::DefaultValueFor(SystemRegister id) { } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ExternalReference::set_redirector(isolate, &RedirectExternalReference); -} - - // Get the active Simulator for the current thread. Simulator* Simulator::current(Isolate* isolate) { Isolate::PerIsolateThreadData* isolate_data = @@ -472,9 +465,9 @@ class Redirection { : external_function_(external_function), type_(type), next_(nullptr) { redirect_call_.SetInstructionBits( HLT | Assembler::ImmException(kImmExceptionIsRedirectedCall)); - next_ = isolate->simulator_redirection(); + next_ = Simulator::redirection(); // TODO(all): Simulator flush I cache - isolate->set_simulator_redirection(this); + Simulator::set_redirection(this); } void* address_of_redirect_call() { @@ -488,7 +481,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -528,9 +521,17 @@ class Redirection { // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { + // TODO(all): Simulator flush I cache } @@ -761,12 +762,10 @@ void Simulator::DoRuntimeCall(Instruction* instr) { set_pc(return_address); } - -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address_of_redirect_call(); } diff --git a/src/arm64/simulator-arm64.h b/src/arm64/simulator-arm64.h index 762e9d23c9..e920066356 100644 --- a/src/arm64/simulator-arm64.h +++ b/src/arm64/simulator-arm64.h @@ -16,6 +16,7 @@ #include "src/assembler.h" #include "src/base/compiler-specific.h" #include "src/globals.h" +#include "src/simulator-base.h" #include "src/utils.h" namespace v8 { @@ -636,7 +637,9 @@ class LogicVRegister { bool round_[kQRegSize]; }; -class Simulator : public DecoderVisitor { +// Using multiple inheritance here is permitted because {DecoderVisitor} is a +// pure interface class with only pure virtual methods. +class Simulator : public DecoderVisitor, public SimulatorBase { public: static void FlushICache(base::CustomMatcherHashMap* i_cache, void* start, size_t size) { @@ -652,10 +655,6 @@ class Simulator : public DecoderVisitor { // System functions. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - static Simulator* current(v8::internal::Isolate* isolate); class CallArgument; @@ -762,10 +761,6 @@ class Simulator : public DecoderVisitor { void ResetState(); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type); void DoRuntimeCall(Instruction* instr); // Run the simulator. diff --git a/src/isolate.cc b/src/isolate.cc index a6665328d2..56965654c7 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -2770,9 +2770,8 @@ Isolate::~Isolate() { allocator_ = nullptr; #if USE_SIMULATOR - Simulator::TearDown(simulator_i_cache_, simulator_redirection_); + Simulator::TearDown(simulator_i_cache_); simulator_i_cache_ = nullptr; - simulator_redirection_ = nullptr; #endif } diff --git a/src/isolate.h b/src/isolate.h index 75e426534b..8d45641c5b 100644 --- a/src/isolate.h +++ b/src/isolate.h @@ -384,10 +384,9 @@ class ThreadLocalTop BASE_EMBEDDED { #if USE_SIMULATOR -#define ISOLATE_INIT_SIMULATOR_LIST(V) \ - V(bool, simulator_initialized, false) \ - V(base::CustomMatcherHashMap*, simulator_i_cache, nullptr) \ - V(Redirection*, simulator_redirection, nullptr) +#define ISOLATE_INIT_SIMULATOR_LIST(V) \ + V(bool, simulator_initialized, false) \ + V(base::CustomMatcherHashMap*, simulator_i_cache, nullptr) #else #define ISOLATE_INIT_SIMULATOR_LIST(V) @@ -1327,9 +1326,6 @@ class Isolate { #ifdef USE_SIMULATOR base::Mutex* simulator_i_cache_mutex() { return &simulator_i_cache_mutex_; } - base::Mutex* simulator_redirection_mutex() { - return &simulator_redirection_mutex_; - } #endif void set_allow_atomics_wait(bool set) { allow_atomics_wait_ = set; } @@ -1665,7 +1661,6 @@ class Isolate { #ifdef USE_SIMULATOR base::Mutex simulator_i_cache_mutex_; - base::Mutex simulator_redirection_mutex_; #endif bool allow_atomics_wait_; diff --git a/src/mips/simulator-mips.cc b/src/mips/simulator-mips.cc index 5f2d526ca1..efcc071e01 100644 --- a/src/mips/simulator-mips.cc +++ b/src/mips/simulator-mips.cc @@ -878,14 +878,6 @@ void Simulator::CheckICache(base::CustomMatcherHashMap* i_cache, } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ::v8::internal::ExternalReference::set_redirector(isolate, - &RedirectExternalReference); -} - - Simulator::Simulator(Isolate* isolate) : isolate_(isolate) { i_cache_ = isolate_->simulator_i_cache(); if (i_cache_ == nullptr) { @@ -949,12 +941,11 @@ class Redirection { swi_instruction_(rtCallRedirInstr), type_(type), next_(nullptr) { - next_ = isolate->simulator_redirection(); - Simulator::current(isolate)-> - FlushICache(isolate->simulator_i_cache(), - reinterpret_cast(&swi_instruction_), - Instruction::kInstrSize); - isolate->set_simulator_redirection(this); + next_ = Simulator::redirection(); + Simulator::FlushICache(isolate->simulator_i_cache(), + reinterpret_cast(&swi_instruction_), + Instruction::kInstrSize); + Simulator::set_redirection(this); } void* address_of_swi_instruction() { @@ -966,7 +957,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -1006,9 +997,16 @@ class Redirection { // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { if (i_cache != nullptr) { for (base::CustomMatcherHashMap::Entry* entry = i_cache->Start(); entry != nullptr; entry = i_cache->Next(entry)) { @@ -1018,12 +1016,10 @@ void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, } } - -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address_of_swi_instruction(); } diff --git a/src/mips/simulator-mips.h b/src/mips/simulator-mips.h index d60cdbc96e..e5b506b849 100644 --- a/src/mips/simulator-mips.h +++ b/src/mips/simulator-mips.h @@ -21,6 +21,7 @@ #include "src/assembler.h" #include "src/base/hashmap.h" +#include "src/simulator-base.h" namespace v8 { namespace internal { @@ -91,7 +92,7 @@ class SimInstruction : public InstructionGetters { } }; -class Simulator { +class Simulator : public SimulatorBase { public: friend class MipsDebugger; @@ -236,11 +237,6 @@ class Simulator { // Executes MIPS instructions until the PC reaches end_sim_pc. void Execute(); - // Call on program start. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - // V8 generally calls into generated JS code with 5 parameters and into // generated RegExp code with 7 parameters. This is a convenience function, // which sets up the simulator state and grabs the result on return. @@ -505,11 +501,6 @@ class Simulator { // Exceptions. void SignalException(Exception e); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type); - // Handle arguments and return value for runtime FP functions. void GetFpArgs(double* x, double* y, int32_t* z); void SetFpResult(const double& result); diff --git a/src/mips64/simulator-mips64.cc b/src/mips64/simulator-mips64.cc index 4b4603ea26..e0e1a906fb 100644 --- a/src/mips64/simulator-mips64.cc +++ b/src/mips64/simulator-mips64.cc @@ -809,14 +809,6 @@ void Simulator::CheckICache(base::CustomMatcherHashMap* i_cache, } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ::v8::internal::ExternalReference::set_redirector(isolate, - &RedirectExternalReference); -} - - Simulator::Simulator(Isolate* isolate) : isolate_(isolate) { i_cache_ = isolate_->simulator_i_cache(); if (i_cache_ == nullptr) { @@ -882,12 +874,11 @@ class Redirection { swi_instruction_(rtCallRedirInstr), type_(type), next_(nullptr) { - next_ = isolate->simulator_redirection(); - Simulator::current(isolate)-> - FlushICache(isolate->simulator_i_cache(), - reinterpret_cast(&swi_instruction_), - Instruction::kInstrSize); - isolate->set_simulator_redirection(this); + next_ = Simulator::redirection(); + Simulator::FlushICache(isolate->simulator_i_cache(), + reinterpret_cast(&swi_instruction_), + Instruction::kInstrSize); + Simulator::set_redirection(this); } void* address_of_swi_instruction() { @@ -899,7 +890,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -939,9 +930,16 @@ class Redirection { // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { if (i_cache != nullptr) { for (base::HashMap::Entry* entry = i_cache->Start(); entry != nullptr; entry = i_cache->Next(entry)) { @@ -951,12 +949,10 @@ void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, } } - -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address_of_swi_instruction(); } diff --git a/src/mips64/simulator-mips64.h b/src/mips64/simulator-mips64.h index 556771db91..d3c38d796f 100644 --- a/src/mips64/simulator-mips64.h +++ b/src/mips64/simulator-mips64.h @@ -21,6 +21,7 @@ #include "src/assembler.h" #include "src/base/hashmap.h" +#include "src/simulator-base.h" namespace v8 { namespace internal { @@ -91,7 +92,7 @@ class SimInstruction : public InstructionGetters { } }; -class Simulator { +class Simulator : public SimulatorBase { public: friend class MipsDebugger; @@ -238,11 +239,6 @@ class Simulator { // Executes MIPS instructions until the PC reaches end_sim_pc. void Execute(); - // Call on program start. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - // V8 generally calls into generated JS code with 5 parameters and into // generated RegExp code with 7 parameters. This is a convenience function, // which sets up the simulator state and grabs the result on return. @@ -527,11 +523,6 @@ class Simulator { // Exceptions. void SignalException(Exception e); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type); - // Handle arguments and return value for runtime FP functions. void GetFpArgs(double* x, double* y, int32_t* z); void SetFpResult(const double& result); diff --git a/src/ppc/simulator-ppc.cc b/src/ppc/simulator-ppc.cc index ebdcda1551..e4c15373a7 100644 --- a/src/ppc/simulator-ppc.cc +++ b/src/ppc/simulator-ppc.cc @@ -733,14 +733,6 @@ void Simulator::CheckICache(base::CustomMatcherHashMap* i_cache, } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ::v8::internal::ExternalReference::set_redirector(isolate, - &RedirectExternalReference); -} - - Simulator::Simulator(Isolate* isolate) : isolate_(isolate) { i_cache_ = isolate_->simulator_i_cache(); if (i_cache_ == nullptr) { @@ -807,11 +799,11 @@ class Redirection { swi_instruction_(rtCallRedirInstr | kCallRtRedirected), type_(type), next_(nullptr) { - next_ = isolate->simulator_redirection(); - Simulator::current(isolate)->FlushICache( - isolate->simulator_i_cache(), - reinterpret_cast(&swi_instruction_), Instruction::kInstrSize); - isolate->set_simulator_redirection(this); + next_ = Simulator::redirection(); + Simulator::FlushICache(isolate->simulator_i_cache(), + reinterpret_cast(&swi_instruction_), + Instruction::kInstrSize); + Simulator::set_redirection(this); if (ABI_USES_FUNCTION_DESCRIPTORS) { function_descriptor_[0] = reinterpret_cast(&swi_instruction_); function_descriptor_[1] = 0; @@ -832,7 +824,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -880,9 +872,16 @@ class Redirection { // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { if (i_cache != nullptr) { for (base::HashMap::Entry* entry = i_cache->Start(); entry != nullptr; entry = i_cache->Next(entry)) { @@ -892,12 +891,10 @@ void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, } } - -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address(); } diff --git a/src/ppc/simulator-ppc.h b/src/ppc/simulator-ppc.h index 74196d3f42..e8d4349e33 100644 --- a/src/ppc/simulator-ppc.h +++ b/src/ppc/simulator-ppc.h @@ -21,6 +21,7 @@ #include "src/assembler.h" #include "src/base/hashmap.h" #include "src/ppc/constants-ppc.h" +#include "src/simulator-base.h" namespace v8 { namespace internal { @@ -51,8 +52,7 @@ class CachePage { char validity_map_[kValidityMapSize]; // One byte per line. }; - -class Simulator { +class Simulator : public SimulatorBase { public: friend class PPCDebugger; enum Register { @@ -167,11 +167,6 @@ class Simulator { // Executes PPC instructions until the PC reaches end_sim_pc. void Execute(); - // Call on program start. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - // V8 generally calls into generated JS code with 5 parameters and into // generated RegExp code with 7 parameters. This is a convenience function, // which sets up the simulator state and grabs the result on return. @@ -298,11 +293,6 @@ class Simulator { static CachePage* GetCachePage(base::CustomMatcherHashMap* i_cache, void* page); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference( - Isolate* isolate, void* external_function, - v8::internal::ExternalReference::Type type); - // Handle arguments and return value for runtime FP functions. void GetFpArgs(double* x, double* y, intptr_t* z); void SetFpResult(const double& result); diff --git a/src/s390/simulator-s390.cc b/src/s390/simulator-s390.cc index 513e4b2d76..70b90ecc86 100644 --- a/src/s390/simulator-s390.cc +++ b/src/s390/simulator-s390.cc @@ -728,15 +728,6 @@ void Simulator::CheckICache(base::CustomMatcherHashMap* i_cache, } } -void Simulator::Initialize(Isolate* isolate) { - if (isolate->simulator_initialized()) return; - isolate->set_simulator_initialized(true); - ::v8::internal::ExternalReference::set_redirector(isolate, - &RedirectExternalReference); - static base::OnceType once = V8_ONCE_INIT; - base::CallOnce(&once, &Simulator::EvalTableInit); -} - Simulator::EvaluateFuncType Simulator::EvalTable[] = {nullptr}; void Simulator::EvalTableInit() { @@ -1494,6 +1485,8 @@ Simulator::Simulator(Isolate* isolate) : isolate_(isolate) { isolate_->set_simulator_i_cache(i_cache_); } Initialize(isolate); + static base::OnceType once = V8_ONCE_INIT; + base::CallOnce(&once, &Simulator::EvalTableInit); // Set up simulator support first. Some of this information is needed to // setup the architecture state. #if V8_TARGET_ARCH_S390X @@ -1558,11 +1551,11 @@ class Redirection { #endif type_(type), next_(nullptr) { - next_ = isolate->simulator_redirection(); - Simulator::current(isolate)->FlushICache( - isolate->simulator_i_cache(), - reinterpret_cast(&swi_instruction_), sizeof(FourByteInstr)); - isolate->set_simulator_redirection(this); + next_ = Simulator::redirection(); + Simulator::FlushICache(isolate->simulator_i_cache(), + reinterpret_cast(&swi_instruction_), + sizeof(FourByteInstr)); + Simulator::set_redirection(this); if (ABI_USES_FUNCTION_DESCRIPTORS) { function_descriptor_[0] = reinterpret_cast(&swi_instruction_); function_descriptor_[1] = 0; @@ -1583,7 +1576,7 @@ class Redirection { static Redirection* Get(Isolate* isolate, void* external_function, ExternalReference::Type type) { - Redirection* current = isolate->simulator_redirection(); + Redirection* current = Simulator::redirection(); for (; current != nullptr; current = current->next_) { if (current->external_function_ == external_function && current->type_ == type) { @@ -1630,9 +1623,16 @@ class Redirection { }; // static -void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, - Redirection* first) { - Redirection::DeleteChain(first); +void SimulatorBase::GlobalTearDown() { + delete redirection_mutex_; + redirection_mutex_ = nullptr; + + Redirection::DeleteChain(redirection_); + redirection_ = nullptr; +} + +// static +void SimulatorBase::TearDown(base::CustomMatcherHashMap* i_cache) { if (i_cache != nullptr) { for (base::HashMap::Entry* entry = i_cache->Start(); entry != nullptr; entry = i_cache->Next(entry)) { @@ -1642,11 +1642,10 @@ void Simulator::TearDown(base::CustomMatcherHashMap* i_cache, } } -void* Simulator::RedirectExternalReference(Isolate* isolate, - void* external_function, - ExternalReference::Type type) { - base::LockGuard lock_guard( - isolate->simulator_redirection_mutex()); +void* SimulatorBase::RedirectExternalReference(Isolate* isolate, + void* external_function, + ExternalReference::Type type) { + base::LockGuard lock_guard(Simulator::redirection_mutex()); Redirection* redirection = Redirection::Get(isolate, external_function, type); return redirection->address(); } diff --git a/src/s390/simulator-s390.h b/src/s390/simulator-s390.h index 933b37b3ec..3ccd608c28 100644 --- a/src/s390/simulator-s390.h +++ b/src/s390/simulator-s390.h @@ -20,6 +20,7 @@ #include "src/assembler.h" #include "src/base/hashmap.h" #include "src/s390/constants-s390.h" +#include "src/simulator-base.h" namespace v8 { namespace internal { @@ -50,7 +51,7 @@ class CachePage { char validity_map_[kValidityMapSize]; // One byte per line. }; -class Simulator { +class Simulator : public SimulatorBase { public: friend class S390Debugger; enum Register { @@ -162,11 +163,6 @@ class Simulator { // Executes S390 instructions until the PC reaches end_sim_pc. void Execute(); - // Call on program start. - static void Initialize(Isolate* isolate); - - static void TearDown(base::CustomMatcherHashMap* i_cache, Redirection* first); - // V8 generally calls into generated JS code with 5 parameters and into // generated RegExp code with 7 parameters. This is a convenience function, // which sets up the simulator state and grabs the result on return. @@ -396,11 +392,6 @@ class Simulator { static CachePage* GetCachePage(base::CustomMatcherHashMap* i_cache, void* page); - // Runtime call support. Uses the isolate in a thread-safe way. - static void* RedirectExternalReference( - Isolate* isolate, void* external_function, - v8::internal::ExternalReference::Type type); - // Handle arguments and return value for runtime FP functions. void GetFpArgs(double* x, double* y, intptr_t* z); void SetFpResult(const double& result); diff --git a/src/simulator-base.cc b/src/simulator-base.cc new file mode 100644 index 0000000000..5b311f18d9 --- /dev/null +++ b/src/simulator-base.cc @@ -0,0 +1,37 @@ +// Copyright 2017 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. + +#include "src/simulator-base.h" + +#include "src/assembler.h" +#include "src/isolate.h" + +#if defined(USE_SIMULATOR) + +namespace v8 { +namespace internal { + +// static +base::Mutex* SimulatorBase::redirection_mutex_ = nullptr; + +// static +Redirection* SimulatorBase::redirection_ = nullptr; + +// static +void SimulatorBase::InitializeOncePerProcess() { + DCHECK_NULL(redirection_mutex_); + redirection_mutex_ = new base::Mutex(); +} + +// static +void SimulatorBase::Initialize(Isolate* isolate) { + if (isolate->simulator_initialized()) return; + isolate->set_simulator_initialized(true); + ExternalReference::set_redirector(isolate, &RedirectExternalReference); +} + +} // namespace internal +} // namespace v8 + +#endif // defined(USE_SIMULATOR) diff --git a/src/simulator-base.h b/src/simulator-base.h new file mode 100644 index 0000000000..ee30dbb41b --- /dev/null +++ b/src/simulator-base.h @@ -0,0 +1,45 @@ +// Copyright 2017 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_SIMULATOR_BASE_H_ +#define V8_SIMULATOR_BASE_H_ + +#include "src/assembler.h" +#include "src/globals.h" + +namespace v8 { +namespace internal { + +class Redirection; + +class SimulatorBase { + public: + // Call on process start and exit. + static void InitializeOncePerProcess(); + // TODO(mstarzinger): Move implementation to "simulator-base.cc" file. + static void GlobalTearDown(); + + // Call on isolate initialization and teardown. + static void Initialize(Isolate* isolate); + // TODO(mstarzinger): Move implementation to "simulator-base.cc" file. + static void TearDown(base::CustomMatcherHashMap* i_cache); + + static base::Mutex* redirection_mutex() { return redirection_mutex_; } + static Redirection* redirection() { return redirection_; } + static void set_redirection(Redirection* r) { redirection_ = r; } + + private: + // Runtime call support. Uses the isolate in a thread-safe way. + static void* RedirectExternalReference( + Isolate* isolate, void* external_function, + v8::internal::ExternalReference::Type type); + + static base::Mutex* redirection_mutex_; + static Redirection* redirection_; +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_SIMULATOR_BASE_H_ diff --git a/src/simulator.h b/src/simulator.h index 5fa13dbb23..ed4cbf9ad9 100644 --- a/src/simulator.h +++ b/src/simulator.h @@ -6,6 +6,7 @@ #define V8_SIMULATOR_H_ #include "src/globals.h" +#include "src/simulator-base.h" #if V8_TARGET_ARCH_IA32 #include "src/ia32/simulator-ia32.h" diff --git a/src/v8.cc b/src/v8.cc index 16107fdefc..ab3f7853b4 100644 --- a/src/v8.cc +++ b/src/v8.cc @@ -19,6 +19,7 @@ #include "src/objects-inl.h" #include "src/profiler/heap-profiler.h" #include "src/runtime-profiler.h" +#include "src/simulator.h" #include "src/snapshot/natives.h" #include "src/snapshot/snapshot.h" #include "src/tracing/tracing-category-observer.h" @@ -42,6 +43,9 @@ bool V8::Initialize() { void V8::TearDown() { +#if defined(USE_SIMULATOR) + Simulator::GlobalTearDown(); +#endif Bootstrapper::TearDownExtensions(); ElementsAccessor::TearDown(); RegisteredExtension::UnregisterAll(); @@ -69,6 +73,9 @@ void V8::InitializeOncePerProcessImpl() { Isolate::InitializeOncePerProcess(); +#if defined(USE_SIMULATOR) + Simulator::InitializeOncePerProcess(); +#endif sampler::Sampler::SetUp(); CpuFeatures::Probe(false); ElementsAccessor::InitializeOncePerProcess(); diff --git a/src/v8.gyp b/src/v8.gyp index 753732d19b..5520fbe7e1 100644 --- a/src/v8.gyp +++ b/src/v8.gyp @@ -1346,6 +1346,8 @@ 'safepoint-table.h', 'setup-isolate.h', 'signature.h', + 'simulator-base.cc', + 'simulator-base.h', 'simulator.h', 'snapshot/builtin-deserializer-allocator.cc', 'snapshot/builtin-deserializer-allocator.h',