#ifndef V8_EXECUTION_SIMULATOR_BASE_H_
#define V8_EXECUTION_SIMULATOR_BASE_H_
#include <type_traits>
#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_LOONG64 || \
V8_TARGET_ARCH_RISCV64
#include "include/v8-fast-api-calls.h"
#endif
#include "src/base/hashmap.h"
#include "src/common/globals.h"
#include "src/execution/isolate.h"
#if defined(USE_SIMULATOR)
namespace v8 {
namespace internal {
class Instruction;
class Redirection;
class SimulatorBase {
public:
static void InitializeOncePerProcess();
static void GlobalTearDown();
static base::Mutex* redirection_mutex() { return redirection_mutex_; }
static Redirection* redirection() { return redirection_; }
static void set_redirection(Redirection* r) { redirection_ = r; }
static base::Mutex* i_cache_mutex() { return i_cache_mutex_; }
static base::CustomMatcherHashMap* i_cache() { return i_cache_; }
static Address RedirectExternalReference(Address external_function,
ExternalReference::Type type);
static Address UnwrapRedirection(Address redirection_trampoline);
protected:
template <typename Return, typename SimT, typename CallImpl, typename... Args>
static Return VariadicCall(SimT* sim, CallImpl call, Address entry,
Args... args) {
std::array<intptr_t, sizeof...(args)> args_arr{{ConvertArg(args)...}};
intptr_t ret = (sim->*call)(entry, args_arr.size(), args_arr.data());
return ConvertReturn<Return>(ret);
}
template <typename T>
static typename std::enable_if<std::is_integral<T>::value, T>::type
ConvertReturn(intptr_t ret) {
static_assert(sizeof(T) <= sizeof(intptr_t), "type bigger than ptrsize");
return static_cast<T>(ret);
}
template <typename T>
static typename std::enable_if<std::is_pointer<T>::value, T>::type
ConvertReturn(intptr_t ret) {
return reinterpret_cast<T>(ret);
}
template <typename T>
static typename std::enable_if<std::is_base_of<Object, T>::value, T>::type
ConvertReturn(intptr_t ret) {
return Tagged<Object>(ret);
}
#if V8_TARGET_ARCH_ARM64 || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_LOONG64 || \
V8_TARGET_ARCH_RISCV64
template <typename T>
static typename std::enable_if<std::is_same<T, v8::AnyCType>::value, T>::type
ConvertReturn(intptr_t ret) {
v8::AnyCType result;
result.int64_value = static_cast<int64_t>(ret);
return result;
}
#endif
template <typename T>
static typename std::enable_if<std::is_void<T>::value, T>::type ConvertReturn(
intptr_t ret) {}
template <typename T>
static typename std::enable_if<std::is_integral<T>::value, intptr_t>::type
ConvertArg(T arg) {
static_assert(sizeof(T) <= sizeof(intptr_t), "type bigger than ptrsize");
#if V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_LOONG64 || \
V8_TARGET_ARCH_RISCV32 || V8_TARGET_ARCH_RISCV64
using signed_t = typename std::make_signed<T>::type;
return static_cast<intptr_t>(static_cast<signed_t>(arg));
#else
return static_cast<intptr_t>(arg);
#endif
}
template <typename T>
static typename std::enable_if<std::is_pointer<T>::value, intptr_t>::type
ConvertArg(T arg) {
return reinterpret_cast<intptr_t>(arg);
}
template <typename T>
static
typename std::enable_if<std::is_floating_point<T>::value, intptr_t>::type
ConvertArg(T arg) {
UNREACHABLE();
}
private:
static base::Mutex* redirection_mutex_;
static Redirection* redirection_;
static base::Mutex* i_cache_mutex_;
static base::CustomMatcherHashMap* i_cache_;
};
class Redirection {
public:
Redirection(Address external_function, ExternalReference::Type type);
Address address_of_instruction() {
#if ABI_USES_FUNCTION_DESCRIPTORS
return reinterpret_cast<Address>(function_descriptor_);
#else
return reinterpret_cast<Address>(&instruction_);
#endif
}
void* external_function() {
return reinterpret_cast<void*>(external_function_);
}
ExternalReference::Type type() { return type_; }
static Redirection* Get(Address external_function,
ExternalReference::Type type);
static Redirection* FromInstruction(Instruction* instruction) {
Address addr_of_instruction = reinterpret_cast<Address>(instruction);
Address addr_of_redirection =
addr_of_instruction - offsetof(Redirection, instruction_);
return reinterpret_cast<Redirection*>(addr_of_redirection);
}
static void* UnwrapRedirection(intptr_t reg) {
Redirection* redirection = FromInstruction(
reinterpret_cast<Instruction*>(reinterpret_cast<void*>(reg)));
return redirection->external_function();
}
static void DeleteChain(Redirection* redirection) {
while (redirection != nullptr) {
Redirection* next = redirection->next_;
delete redirection;
redirection = next;
}
}
private:
Address external_function_;
uint32_t instruction_;
ExternalReference::Type type_;
Redirection* next_;
#if ABI_USES_FUNCTION_DESCRIPTORS
intptr_t function_descriptor_[3];
#endif
};
class SimulatorData {
public:
void RegisterFunctionsAndSignatures(Address* c_functions,
const CFunctionInfo* const* c_signatures,
unsigned num_functions);
const EncodedCSignature& GetSignatureForTarget(Address target);
void AddSignatureForTargetForTesting(Address target,
const EncodedCSignature& signature) {
AddSignatureForTarget(target, signature);
}
private:
void AddSignatureForTarget(Address target,
const EncodedCSignature& signature) {
target_to_signature_table_[target] = signature;
}
v8::base::Mutex signature_map_mutex_;
typedef std::unordered_map<Address, EncodedCSignature> TargetToSignatureTable;
TargetToSignatureTable target_to_signature_table_;
};
}
}
#endif
#endif