Sprucing up some of alloy.

This commit is contained in:
Ben Vanik 2014-07-13 21:15:37 -07:00
parent 48425da8ff
commit 9437d0b564
40 changed files with 246 additions and 228 deletions

View File

@ -12,7 +12,7 @@
namespace alloy { namespace alloy {
Arena::Arena(size_t chunk_size) Arena::Arena(size_t chunk_size)
: chunk_size_(chunk_size), head_chunk_(NULL), active_chunk_(NULL) {} : chunk_size_(chunk_size), head_chunk_(nullptr), active_chunk_(nullptr) {}
Arena::~Arena() { Arena::~Arena() {
Reset(); Reset();
@ -22,7 +22,7 @@ Arena::~Arena() {
delete chunk; delete chunk;
chunk = next; chunk = next;
} }
head_chunk_ = NULL; head_chunk_ = nullptr;
} }
void Arena::Reset() { void Arena::Reset() {
@ -86,7 +86,7 @@ void* Arena::CloneContents() {
} }
Arena::Chunk::Chunk(size_t chunk_size) Arena::Chunk::Chunk(size_t chunk_size)
: next(NULL), capacity(chunk_size), buffer(0), offset(0) { : next(nullptr), capacity(chunk_size), buffer(0), offset(0) {
buffer = (uint8_t*)xe_malloc(capacity); buffer = (uint8_t*)xe_malloc(capacity);
} }

View File

@ -25,7 +25,7 @@ class Arena {
void* Alloc(size_t size); void* Alloc(size_t size);
template <typename T> template <typename T>
T* Alloc() { T* Alloc() {
return (T*)Alloc(sizeof(T)); return reinterpret_cast<T*>(Alloc(sizeof(T)));
} }
void* CloneContents(); void* CloneContents();

View File

@ -2657,7 +2657,7 @@ int Translate_MUL(TranslationContext& ctx, Instr* i) {
} }
} }
#if !XE_COMPILER_MSVC #if XE_COMPILER_MSVC
uint64_t Mul128(uint64_t xi_low, uint64_t xi_high, uint64_t yi_low, uint64_t Mul128(uint64_t xi_low, uint64_t xi_high, uint64_t yi_low,
uint64_t yi_high) { uint64_t yi_high) {
// 128bit multiply, simplified for two input 64bit integers. // 128bit multiply, simplified for two input 64bit integers.

View File

@ -23,14 +23,14 @@ namespace alloy {
template <typename T> template <typename T>
class Delegate { class Delegate {
public: public:
typedef std::function<void(T&)> listener_t; typedef std::function<void(T&)> Listener;
void AddListener(listener_t const& listener) { void AddListener(Listener const& listener) {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
listeners_.push_back(listener); listeners_.push_back(listener);
} }
void RemoveListener(listener_t const& listener) { void RemoveListener(Listener const& listener) {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { for (auto it = listeners_.begin(); it != listeners_.end(); ++it) {
if (it == listener) { if (it == listener) {
@ -54,7 +54,7 @@ class Delegate {
private: private:
std::mutex lock_; std::mutex lock_;
std::vector<listener_t> listeners_; std::vector<Listener> listeners_;
}; };
} // namespace alloy } // namespace alloy

View File

@ -146,7 +146,7 @@ void HIRBuilder::DumpOp(StringBuffer* str, OpcodeSignatureType sig_type,
case OPCODE_SIG_TYPE_S: case OPCODE_SIG_TYPE_S:
if (true) { if (true) {
auto target = op->symbol_info; auto target = op->symbol_info;
str->Append(target->name() ? target->name() : "<fn>"); str->Append(!target->name().empty() ? target->name() : "<fn>");
} }
break; break;
case OPCODE_SIG_TYPE_V: case OPCODE_SIG_TYPE_V:

View File

@ -9,30 +9,21 @@
#include <alloy/memory.h> #include <alloy/memory.h>
#if !XE_LIKE_WIN32 #include <poly/memory.h>
#include <unistd.h>
#endif
namespace alloy { namespace alloy {
Memory::Memory() : membase_(0), reserve_address_(0) { Memory::Memory() : membase_(nullptr), reserve_address_(0) {
// TODO(benvanik): move to poly. system_page_size_ = poly::page_size();
#if XE_LIKE_WIN32
SYSTEM_INFO si;
GetSystemInfo(&si);
system_page_size_ = si.dwPageSize;
#else
system_page_size_ = getpagesize();
#endif
} }
Memory::~Memory() {} Memory::~Memory() = default;
int Memory::Initialize() { return 0; } int Memory::Initialize() { return 0; }
void Memory::Zero(uint64_t address, size_t size) { void Memory::Zero(uint64_t address, size_t size) {
uint8_t* p = membase_ + address; uint8_t* p = membase_ + address;
XEIGNORE(xe_zero_memory(p, size, 0, size)); memset(p, 0, size);
} }
void Memory::Fill(uint64_t address, size_t size, uint8_t value) { void Memory::Fill(uint64_t address, size_t size, uint8_t value) {
@ -42,15 +33,15 @@ void Memory::Fill(uint64_t address, size_t size, uint8_t value) {
void Memory::Copy(uint64_t dest, uint64_t src, size_t size) { void Memory::Copy(uint64_t dest, uint64_t src, size_t size) {
uint8_t* pdest = membase_ + dest; uint8_t* pdest = membase_ + dest;
uint8_t* psrc = membase_ + src; const uint8_t* psrc = membase_ + src;
XEIGNORE(xe_copy_memory(pdest, size, psrc, size)); memcpy(pdest, psrc, size);
} }
uint64_t Memory::SearchAligned(uint64_t start, uint64_t end, uint64_t Memory::SearchAligned(uint64_t start, uint64_t end,
const uint32_t* values, size_t value_count) { const uint32_t* values, size_t value_count) {
assert_true(start <= end); assert_true(start <= end);
const uint32_t* p = (const uint32_t*)(membase_ + start); const uint32_t* p = reinterpret_cast<const uint32_t*>(membase_ + start);
const uint32_t* pe = (const uint32_t*)(membase_ + end); const uint32_t* pe = reinterpret_cast<const uint32_t*>(membase_ + end);
while (p != pe) { while (p != pe) {
if (*p == values[0]) { if (*p == values[0]) {
const uint32_t* pc = p + 1; const uint32_t* pc = p + 1;
@ -62,7 +53,7 @@ uint64_t Memory::SearchAligned(uint64_t start, uint64_t end,
matched++; matched++;
} }
if (matched == value_count) { if (matched == value_count) {
return (uint64_t)((uint8_t*)p - membase_); return uint64_t(reinterpret_cast<const uint8_t*>(p) - membase_);
} }
} }
p++; p++;

View File

@ -13,12 +13,12 @@ namespace alloy {
namespace runtime { namespace runtime {
DebugInfo::DebugInfo() DebugInfo::DebugInfo()
: source_disasm_(0), : source_disasm_(nullptr),
raw_hir_disasm_(0), raw_hir_disasm_(nullptr),
hir_disasm_(0), hir_disasm_(nullptr),
machine_code_disasm_(0), machine_code_disasm_(nullptr),
source_map_count_(0), source_map_count_(0),
source_map_(NULL) {} source_map_(nullptr) {}
DebugInfo::~DebugInfo() { DebugInfo::~DebugInfo() {
xe_free(source_map_); xe_free(source_map_);
@ -44,7 +44,7 @@ SourceMapEntry* DebugInfo::LookupSourceOffset(uint64_t offset) {
return entry; return entry;
} }
} }
return NULL; return nullptr;
} }
SourceMapEntry* DebugInfo::LookupHIROffset(uint64_t offset) { SourceMapEntry* DebugInfo::LookupHIROffset(uint64_t offset) {
@ -55,7 +55,7 @@ SourceMapEntry* DebugInfo::LookupHIROffset(uint64_t offset) {
return entry; return entry;
} }
} }
return NULL; return nullptr;
} }
SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) { SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) {
@ -66,7 +66,7 @@ SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) {
return entry; return entry;
} }
} }
return NULL; return nullptr;
} }
} // namespace runtime } // namespace runtime

View File

@ -19,19 +19,18 @@ namespace runtime {
Breakpoint::Breakpoint(Type type, uint64_t address) Breakpoint::Breakpoint(Type type, uint64_t address)
: type_(type), address_(address) {} : type_(type), address_(address) {}
Breakpoint::~Breakpoint() {} Breakpoint::~Breakpoint() = default;
Debugger::Debugger(Runtime* runtime) : runtime_(runtime) {} Debugger::Debugger(Runtime* runtime) : runtime_(runtime) {}
Debugger::~Debugger() {} Debugger::~Debugger() = default;
int Debugger::SuspendAllThreads(uint32_t timeout_ms) { int Debugger::SuspendAllThreads(uint32_t timeout_ms) {
std::lock_guard<std::mutex> guard(threads_lock_); std::lock_guard<std::mutex> guard(threads_lock_);
int result = 0; int result = 0;
for (auto it = threads_.begin(); it != threads_.end(); ++it) { for (auto thread_state : threads_) {
ThreadState* thread_state = it->second; if (thread_state.second->Suspend(timeout_ms)) {
if (thread_state->Suspend(timeout_ms)) {
result = 1; result = 1;
} }
} }
@ -57,9 +56,8 @@ int Debugger::ResumeAllThreads(bool force) {
std::lock_guard<std::mutex> guard(threads_lock_); std::lock_guard<std::mutex> guard(threads_lock_);
int result = 0; int result = 0;
for (auto it = threads_.begin(); it != threads_.end(); ++it) { for (auto thread_state : threads_) {
ThreadState* thread_state = it->second; if (thread_state.second->Resume(force)) {
if (thread_state->Resume(force)) {
result = 1; result = 1;
} }
} }
@ -69,9 +67,8 @@ int Debugger::ResumeAllThreads(bool force) {
void Debugger::ForEachThread(std::function<void(ThreadState*)> callback) { void Debugger::ForEachThread(std::function<void(ThreadState*)> callback) {
std::lock_guard<std::mutex> guard(threads_lock_); std::lock_guard<std::mutex> guard(threads_lock_);
for (auto it = threads_.begin(); it != threads_.end(); ++it) { for (auto thread_state : threads_) {
ThreadState* thread_state = it->second; callback(thread_state.second);
callback(thread_state);
} }
} }
@ -87,8 +84,7 @@ int Debugger::AddBreakpoint(Breakpoint* breakpoint) {
auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address()); auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address());
// Add. // Add.
for (auto it = fns.begin(); it != fns.end(); ++it) { for (auto fn : fns) {
Function* fn = *it;
if (fn->AddBreakpoint(breakpoint)) { if (fn->AddBreakpoint(breakpoint)) {
return 1; return 1;
} }
@ -122,8 +118,7 @@ int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) {
auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address()); auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address());
// Remove. // Remove.
for (auto it = fns.begin(); it != fns.end(); ++it) { for (auto fn : fns) {
Function* fn = *it;
fn->RemoveBreakpoint(breakpoint); fn->RemoveBreakpoint(breakpoint);
} }
@ -181,8 +176,8 @@ void Debugger::OnFunctionDefined(FunctionInfo* symbol_info,
if (breakpoints.size()) { if (breakpoints.size()) {
// Breakpoints to add! // Breakpoints to add!
for (auto it = breakpoints.begin(); it != breakpoints.end(); ++it) { for (auto breakpoint : breakpoints) {
function->AddBreakpoint(*it); function->AddBreakpoint(breakpoint);
} }
} }
} }

View File

@ -41,7 +41,7 @@ class Breakpoint {
uint64_t address() const { return address_; } uint64_t address() const { return address_; }
const char* id() const { return id_.c_str(); } const char* id() const { return id_.c_str(); }
void set_id(const char* id) { id_ = id; } void set_id(const char* id) { id_ = std::string(id); }
private: private:
Type type_; Type type_;
@ -53,7 +53,7 @@ class Breakpoint {
class DebugEvent { class DebugEvent {
public: public:
DebugEvent(Debugger* debugger) : debugger_(debugger) {} DebugEvent(Debugger* debugger) : debugger_(debugger) {}
virtual ~DebugEvent() {} virtual ~DebugEvent() = default;
Debugger* debugger() const { return debugger_; } Debugger* debugger() const { return debugger_; }
protected: protected:
@ -67,7 +67,7 @@ class BreakpointHitEvent : public DebugEvent {
: DebugEvent(debugger), : DebugEvent(debugger),
thread_state_(thread_state), thread_state_(thread_state),
breakpoint_(breakpoint) {} breakpoint_(breakpoint) {}
virtual ~BreakpointHitEvent() {} ~BreakpointHitEvent() override = default;
ThreadState* thread_state() const { return thread_state_; } ThreadState* thread_state() const { return thread_state_; }
Breakpoint* breakpoint() const { return breakpoint_; } Breakpoint* breakpoint() const { return breakpoint_; }

View File

@ -26,11 +26,11 @@ EntryTable::~EntryTable() {
Entry* EntryTable::Get(uint64_t address) { Entry* EntryTable::Get(uint64_t address) {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
EntryMap::const_iterator it = map_.find(address); EntryMap::const_iterator it = map_.find(address);
Entry* entry = it != map_.end() ? it->second : NULL; Entry* entry = it != map_.end() ? it->second : nullptr;
if (entry) { if (entry) {
// TODO(benvanik): wait if needed? // TODO(benvanik): wait if needed?
if (entry->status != Entry::STATUS_READY) { if (entry->status != Entry::STATUS_READY) {
entry = NULL; entry = nullptr;
} }
} }
return entry; return entry;
@ -39,7 +39,7 @@ Entry* EntryTable::Get(uint64_t address) {
Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) { Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) {
lock_.lock(); lock_.lock();
EntryMap::const_iterator it = map_.find(address); EntryMap::const_iterator it = map_.find(address);
Entry* entry = it != map_.end() ? it->second : NULL; Entry* entry = it != map_.end() ? it->second : nullptr;
Entry::Status status; Entry::Status status;
if (entry) { if (entry) {
// If we aren't ready yet spin and wait. // If we aren't ready yet spin and wait.
@ -72,8 +72,8 @@ std::vector<Function*> EntryTable::FindWithAddress(uint64_t address) {
SCOPE_profile_cpu_f("alloy"); SCOPE_profile_cpu_f("alloy");
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
std::vector<Function*> fns; std::vector<Function*> fns;
for (auto it = map_.begin(); it != map_.end(); ++it) { for (auto& it : map_) {
Entry* entry = it->second; Entry* entry = it.second;
if (address >= entry->address && address <= entry->end_address) { if (address >= entry->address && address <= entry->end_address) {
if (entry->status == Entry::STATUS_READY) { if (entry->status == Entry::STATUS_READY) {
fns.push_back(entry->function); fns.push_back(entry->function);

View File

@ -26,9 +26,10 @@ Function::~Function() = default;
int Function::AddBreakpoint(Breakpoint* breakpoint) { int Function::AddBreakpoint(Breakpoint* breakpoint) {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
bool found = false; bool found = false;
for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { for (auto other : breakpoints_) {
if (*it == breakpoint) { if (other == breakpoint) {
found = true; found = true;
break;
} }
} }
if (!found) { if (!found) {
@ -54,9 +55,8 @@ int Function::RemoveBreakpoint(Breakpoint* breakpoint) {
Breakpoint* Function::FindBreakpoint(uint64_t address) { Breakpoint* Function::FindBreakpoint(uint64_t address) {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
Breakpoint* result = NULL; Breakpoint* result = nullptr;
for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { for (auto breakpoint : breakpoints_) {
Breakpoint* breakpoint = *it;
if (breakpoint->address() == address) { if (breakpoint->address() == address) {
result = breakpoint; result = breakpoint;
break; break;

View File

@ -34,7 +34,7 @@ bool Module::ContainsAddress(uint64_t address) { return true; }
SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
lock_.lock(); lock_.lock();
SymbolMap::const_iterator it = map_.find(address); SymbolMap::const_iterator it = map_.find(address);
SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr;
if (symbol_info) { if (symbol_info) {
if (symbol_info->status() == SymbolInfo::STATUS_DECLARING) { if (symbol_info->status() == SymbolInfo::STATUS_DECLARING) {
// Some other thread is declaring the symbol - wait. // Some other thread is declaring the symbol - wait.
@ -47,7 +47,7 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
} while (symbol_info->status() == SymbolInfo::STATUS_DECLARING); } while (symbol_info->status() == SymbolInfo::STATUS_DECLARING);
} else { } else {
// Immediate request, just return. // Immediate request, just return.
symbol_info = NULL; symbol_info = nullptr;
} }
} }
} }
@ -58,10 +58,10 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type, SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type,
uint64_t address, uint64_t address,
SymbolInfo** out_symbol_info) { SymbolInfo** out_symbol_info) {
*out_symbol_info = NULL; *out_symbol_info = nullptr;
lock_.lock(); lock_.lock();
SymbolMap::const_iterator it = map_.find(address); SymbolMap::const_iterator it = map_.find(address);
SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr;
SymbolInfo::Status status; SymbolInfo::Status status;
if (symbol_info) { if (symbol_info) {
// If we exist but are the wrong type, die. // If we exist but are the wrong type, die.
@ -235,8 +235,8 @@ int Module::ReadMap(const char* file_name) {
continue; continue;
} }
// Don't overwrite names we've set elsewhere. // Don't overwrite names we've set elsewhere.
if (!fn_info->name()) { if (fn_info->name().empty()) {
// If it's an mangled C++ name (?name@...) just use the name. // If it's a mangled C++ name (?name@...) just use the name.
// TODO(benvanik): better demangling, or leave it to clients. // TODO(benvanik): better demangling, or leave it to clients.
/*if (name[0] == '?') { /*if (name[0] == '?') {
size_t at = name.find('@'); size_t at = name.find('@');

View File

@ -32,7 +32,7 @@ class Module {
Memory* memory() const { return memory_; } Memory* memory() const { return memory_; }
virtual const char* name() const = 0; virtual const std::string& name() const = 0;
virtual bool ContainsAddress(uint64_t address); virtual bool ContainsAddress(uint64_t address);

View File

@ -13,17 +13,12 @@ namespace alloy {
namespace runtime { namespace runtime {
RawModule::RawModule(Runtime* runtime) RawModule::RawModule(Runtime* runtime)
: Module(runtime), : Module(runtime), base_address_(0), low_address_(0), high_address_(0) {}
name_(0),
base_address_(0),
low_address_(0),
high_address_(0) {}
RawModule::~RawModule() { RawModule::~RawModule() {
if (base_address_) { if (base_address_) {
memory_->HeapFree(base_address_, high_address_ - low_address_); memory_->HeapFree(base_address_, high_address_ - low_address_);
} }
xe_free(name_);
} }
int RawModule::LoadFile(uint64_t base_address, const char* path) { int RawModule::LoadFile(uint64_t base_address, const char* path) {
@ -47,8 +42,7 @@ int RawModule::LoadFile(uint64_t base_address, const char* path) {
fclose(file); fclose(file);
// Setup debug info. // Setup debug info.
const char* name = xestrrchra(path, XE_PATH_SEPARATOR) + 1; name_ = std::string(xestrrchra(path, XE_PATH_SEPARATOR) + 1);
name_ = xestrdupa(name);
// TODO(benvanik): debug info // TODO(benvanik): debug info
low_address_ = base_address; low_address_ = base_address;

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_RUNTIME_RAW_MODULE_H_ #ifndef ALLOY_RUNTIME_RAW_MODULE_H_
#define ALLOY_RUNTIME_RAW_MODULE_H_ #define ALLOY_RUNTIME_RAW_MODULE_H_
#include <string>
#include <alloy/runtime/module.h> #include <alloy/runtime/module.h>
namespace alloy { namespace alloy {
@ -18,16 +20,16 @@ namespace runtime {
class RawModule : public Module { class RawModule : public Module {
public: public:
RawModule(Runtime* runtime); RawModule(Runtime* runtime);
virtual ~RawModule(); ~RawModule() override;
int LoadFile(uint64_t base_address, const char* path); int LoadFile(uint64_t base_address, const char* path);
virtual const char* name() const { return name_; } const std::string& name() const override { return name_; }
virtual bool ContainsAddress(uint64_t address); bool ContainsAddress(uint64_t address) override;
private: private:
char* name_; std::string name_;
uint64_t base_address_; uint64_t base_address_;
uint64_t low_address_; uint64_t low_address_;
uint64_t high_address_; uint64_t high_address_;

View File

@ -26,8 +26,7 @@ namespace runtime {
using alloy::backend::Backend; using alloy::backend::Backend;
using alloy::frontend::Frontend; using alloy::frontend::Frontend;
Runtime::Runtime(Memory* memory) Runtime::Runtime(Memory* memory) : memory_(memory) {
: memory_(memory), debugger_(0), frontend_(0), backend_(0) {
tracing::Initialize(); tracing::Initialize();
} }
@ -41,19 +40,20 @@ Runtime::~Runtime() {
} }
} }
delete frontend_; debugger_.reset();
delete backend_; frontend_.reset();
delete debugger_; backend_.reset();
tracing::Flush(); tracing::Flush();
} }
int Runtime::Initialize(Frontend* frontend, Backend* backend) { int Runtime::Initialize(std::unique_ptr<Frontend> frontend,
std::unique_ptr<Backend> backend) {
// Must be initialized by subclass before calling into this. // Must be initialized by subclass before calling into this.
assert_not_null(memory_); assert_not_null(memory_);
// Create debugger first. Other types hook up to it. // Create debugger first. Other types hook up to it.
debugger_ = new Debugger(this); debugger_.reset(new Debugger(this));
if (frontend_ || backend_) { if (frontend_ || backend_) {
return 1; return 1;
@ -62,23 +62,23 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) {
if (!backend) { if (!backend) {
#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND
if (FLAGS_runtime_backend == "x64") { if (FLAGS_runtime_backend == "x64") {
backend = new alloy::backend::x64::X64Backend(this); backend.reset(new alloy::backend::x64::X64Backend(this));
} }
#endif // ALLOY_HAS_X64_BACKEND #endif // ALLOY_HAS_X64_BACKEND
#if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND
if (FLAGS_runtime_backend == "ivm") { if (FLAGS_runtime_backend == "ivm") {
backend = new alloy::backend::ivm::IVMBackend(this); backend.reset(new alloy::backend::ivm::IVMBackend(this));
} }
#endif // ALLOY_HAS_IVM_BACKEND #endif // ALLOY_HAS_IVM_BACKEND
if (FLAGS_runtime_backend == "any") { if (FLAGS_runtime_backend == "any") {
#if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND
if (!backend) { if (!backend) {
backend = new alloy::backend::x64::X64Backend(this); backend.reset(new alloy::backend::x64::X64Backend(this));
} }
#endif // ALLOY_HAS_X64_BACKEND #endif // ALLOY_HAS_X64_BACKEND
#if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND
if (!backend) { if (!backend) {
backend = new alloy::backend::ivm::IVMBackend(this); backend.reset(new alloy::backend::ivm::IVMBackend(this));
} }
#endif // ALLOY_HAS_IVM_BACKEND #endif // ALLOY_HAS_IVM_BACKEND
} }
@ -87,19 +87,20 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) {
if (!backend) { if (!backend) {
return 1; return 1;
} }
backend_ = backend;
frontend_ = frontend;
int result = backend_->Initialize(); int result = backend->Initialize();
if (result) { if (result) {
return result; return result;
} }
result = frontend_->Initialize(); result = frontend->Initialize();
if (result) { if (result) {
return result; return result;
} }
backend_ = std::move(backend);
frontend_ = std::move(frontend);
return 0; return 0;
} }
@ -114,7 +115,7 @@ Module* Runtime::GetModule(const char* name) {
Module* result = NULL; Module* result = NULL;
for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) { for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) {
Module* module = *it; Module* module = *it;
if (xestrcmpa(module->name(), name) == 0) { if (module->name() == name) {
result = module; result = module;
break; break;
} }
@ -135,7 +136,7 @@ std::vector<Function*> Runtime::FindFunctionsWithAddress(uint64_t address) {
int Runtime::ResolveFunction(uint64_t address, Function** out_function) { int Runtime::ResolveFunction(uint64_t address, Function** out_function) {
SCOPE_profile_cpu_f("alloy"); SCOPE_profile_cpu_f("alloy");
*out_function = NULL; *out_function = nullptr;
Entry* entry; Entry* entry;
Entry::Status status = entry_table_.GetOrCreate(address, &entry); Entry::Status status = entry_table_.GetOrCreate(address, &entry);
if (status == Entry::STATUS_NEW) { if (status == Entry::STATUS_NEW) {
@ -170,12 +171,12 @@ int Runtime::LookupFunctionInfo(uint64_t address,
FunctionInfo** out_symbol_info) { FunctionInfo** out_symbol_info) {
SCOPE_profile_cpu_f("alloy"); SCOPE_profile_cpu_f("alloy");
*out_symbol_info = NULL; *out_symbol_info = nullptr;
// TODO(benvanik): fast reject invalid addresses/log errors. // TODO(benvanik): fast reject invalid addresses/log errors.
// Find the module that contains the address. // Find the module that contains the address.
Module* code_module = NULL; Module* code_module = nullptr;
{ {
std::lock_guard<std::mutex> guard(modules_lock_); std::lock_guard<std::mutex> guard(modules_lock_);
// TODO(benvanik): sort by code address (if contiguous) so can bsearch. // TODO(benvanik): sort by code address (if contiguous) so can bsearch.
@ -203,7 +204,7 @@ int Runtime::LookupFunctionInfo(Module* module, uint64_t address,
// Atomic create/lookup symbol in module. // Atomic create/lookup symbol in module.
// If we get back the NEW flag we must declare it now. // If we get back the NEW flag we must declare it now.
FunctionInfo* symbol_info = NULL; FunctionInfo* symbol_info = nullptr;
SymbolInfo::Status symbol_status = SymbolInfo::Status symbol_status =
module->DeclareFunction(address, &symbol_info); module->DeclareFunction(address, &symbol_info);
if (symbol_status == SymbolInfo::STATUS_NEW) { if (symbol_status == SymbolInfo::STATUS_NEW) {
@ -224,7 +225,7 @@ int Runtime::DemandFunction(FunctionInfo* symbol_info,
Function** out_function) { Function** out_function) {
SCOPE_profile_cpu_f("alloy"); SCOPE_profile_cpu_f("alloy");
*out_function = NULL; *out_function = nullptr;
// Lock function for generation. If it's already being generated // Lock function for generation. If it's already being generated
// by another thread this will block and return DECLARED. // by another thread this will block and return DECLARED.
@ -232,7 +233,7 @@ int Runtime::DemandFunction(FunctionInfo* symbol_info,
SymbolInfo::Status symbol_status = module->DefineFunction(symbol_info); SymbolInfo::Status symbol_status = module->DefineFunction(symbol_info);
if (symbol_status == SymbolInfo::STATUS_NEW) { if (symbol_status == SymbolInfo::STATUS_NEW) {
// Symbol is undefined, so define now. // Symbol is undefined, so define now.
Function* function = NULL; Function* function = nullptr;
int result = int result =
frontend_->DefineFunction(symbol_info, DEBUG_INFO_DEFAULT, &function); frontend_->DefineFunction(symbol_info, DEBUG_INFO_DEFAULT, &function);
if (result) { if (result) {

View File

@ -10,6 +10,7 @@
#ifndef ALLOY_RUNTIME_RUNTIME_H_ #ifndef ALLOY_RUNTIME_RUNTIME_H_
#define ALLOY_RUNTIME_RUNTIME_H_ #define ALLOY_RUNTIME_RUNTIME_H_
#include <memory>
#include <mutex> #include <mutex>
#include <vector> #include <vector>
@ -35,11 +36,12 @@ class Runtime {
virtual ~Runtime(); virtual ~Runtime();
Memory* memory() const { return memory_; } Memory* memory() const { return memory_; }
Debugger* debugger() const { return debugger_; } Debugger* debugger() const { return debugger_.get(); }
frontend::Frontend* frontend() const { return frontend_; } frontend::Frontend* frontend() const { return frontend_.get(); }
backend::Backend* backend() const { return backend_; } backend::Backend* backend() const { return backend_.get(); }
int Initialize(frontend::Frontend* frontend, backend::Backend* backend = 0); int Initialize(std::unique_ptr<frontend::Frontend> frontend,
std::unique_ptr<backend::Backend> backend = 0);
int AddModule(Module* module); int AddModule(Module* module);
Module* GetModule(const char* name); Module* GetModule(const char* name);
@ -60,10 +62,10 @@ class Runtime {
protected: protected:
Memory* memory_; Memory* memory_;
Debugger* debugger_; std::unique_ptr<Debugger> debugger_;
frontend::Frontend* frontend_; std::unique_ptr<frontend::Frontend> frontend_;
backend::Backend* backend_; std::unique_ptr<backend::Backend> backend_;
EntryTable entry_table_; EntryTable entry_table_;
std::mutex modules_lock_; std::mutex modules_lock_;

View File

@ -17,30 +17,19 @@ SymbolInfo::SymbolInfo(Type type, Module* module, uint64_t address)
module_(module), module_(module),
status_(STATUS_DEFINING), status_(STATUS_DEFINING),
address_(address), address_(address),
name_(0) {} name_("") {}
SymbolInfo::~SymbolInfo() { SymbolInfo::~SymbolInfo() = default;
if (name_) {
xe_free(name_);
}
}
void SymbolInfo::set_name(const char* name) {
if (name_) {
xe_free(name_);
}
name_ = xestrdupa(name);
}
FunctionInfo::FunctionInfo(Module* module, uint64_t address) FunctionInfo::FunctionInfo(Module* module, uint64_t address)
: SymbolInfo(SymbolInfo::TYPE_FUNCTION, module, address), : SymbolInfo(SymbolInfo::TYPE_FUNCTION, module, address),
end_address_(0), end_address_(0),
behavior_(BEHAVIOR_DEFAULT), behavior_(BEHAVIOR_DEFAULT),
function_(0) { function_(0) {
xe_zero_struct(&extern_info_, sizeof(extern_info_)); memset(&extern_info_, 0, sizeof(extern_info_));
} }
FunctionInfo::~FunctionInfo() {} FunctionInfo::~FunctionInfo() = default;
void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) { void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) {
behavior_ = BEHAVIOR_EXTERN; behavior_ = BEHAVIOR_EXTERN;
@ -52,7 +41,7 @@ void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) {
VariableInfo::VariableInfo(Module* module, uint64_t address) VariableInfo::VariableInfo(Module* module, uint64_t address)
: SymbolInfo(SymbolInfo::TYPE_VARIABLE, module, address) {} : SymbolInfo(SymbolInfo::TYPE_VARIABLE, module, address) {}
VariableInfo::~VariableInfo() {} VariableInfo::~VariableInfo() = default;
} // namespace runtime } // namespace runtime
} // namespace alloy } // namespace alloy

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_RUNTIME_SYMBOL_INFO_H_ #ifndef ALLOY_RUNTIME_SYMBOL_INFO_H_
#define ALLOY_RUNTIME_SYMBOL_INFO_H_ #define ALLOY_RUNTIME_SYMBOL_INFO_H_
#include <string>
#include <alloy/core.h> #include <alloy/core.h>
namespace alloy { namespace alloy {
@ -43,8 +45,8 @@ class SymbolInfo {
void set_status(Status value) { status_ = value; } void set_status(Status value) { status_ = value; }
uint64_t address() const { return address_; } uint64_t address() const { return address_; }
const char* name() const { return name_; } const std::string& name() const { return name_; }
void set_name(const char* name); void set_name(const std::string& value) { name_ = value; }
protected: protected:
Type type_; Type type_;
@ -52,7 +54,7 @@ class SymbolInfo {
Status status_; Status status_;
uint64_t address_; uint64_t address_;
char* name_; std::string name_;
}; };
class FunctionInfo : public SymbolInfo { class FunctionInfo : public SymbolInfo {

View File

@ -14,13 +14,13 @@
namespace alloy { namespace alloy {
namespace runtime { namespace runtime {
thread_local ThreadState* thread_state_ = NULL; thread_local ThreadState* thread_state_ = nullptr;
ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id) ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id)
: runtime_(runtime), : runtime_(runtime),
memory_(runtime->memory()), memory_(runtime->memory()),
thread_id_(thread_id), thread_id_(thread_id),
name_(0), name_(""),
backend_data_(0), backend_data_(0),
raw_context_(0) { raw_context_(0) {
if (thread_id_ == UINT_MAX) { if (thread_id_ == UINT_MAX) {
@ -37,21 +37,8 @@ ThreadState::~ThreadState() {
runtime_->backend()->FreeThreadData(backend_data_); runtime_->backend()->FreeThreadData(backend_data_);
} }
if (thread_state_ == this) { if (thread_state_ == this) {
thread_state_ = NULL; thread_state_ = nullptr;
} }
if (name_) {
xe_free(name_);
}
}
void ThreadState::set_name(const char* value) {
if (value == name_) {
return;
}
if (name_) {
xe_free(name_);
}
name_ = xestrdupa(value);
} }
void ThreadState::Bind(ThreadState* thread_state) { void ThreadState::Bind(ThreadState* thread_state) {

View File

@ -27,8 +27,8 @@ class ThreadState {
Runtime* runtime() const { return runtime_; } Runtime* runtime() const { return runtime_; }
Memory* memory() const { return memory_; } Memory* memory() const { return memory_; }
uint32_t thread_id() const { return thread_id_; } uint32_t thread_id() const { return thread_id_; }
const char* name() const { return name_; } const std::string& name() const { return name_; }
void set_name(const char* value); void set_name(const std::string& value) { name_ = value; }
void* backend_data() const { return backend_data_; } void* backend_data() const { return backend_data_; }
void* raw_context() const { return raw_context_; } void* raw_context() const { return raw_context_; }
@ -43,7 +43,7 @@ class ThreadState {
Runtime* runtime_; Runtime* runtime_;
Memory* memory_; Memory* memory_;
uint32_t thread_id_; uint32_t thread_id_;
char* name_; std::string name_;
void* backend_data_; void* backend_data_;
void* raw_context_; void* raw_context_;
}; };

View File

@ -11,20 +11,21 @@
namespace alloy { namespace alloy {
StringBuffer::StringBuffer(size_t initial_capacity) StringBuffer::StringBuffer(size_t initial_capacity) : offset_(0) {
: buffer_(0), capacity_(0), offset_(0) { buffer_.resize(MAX(initial_capacity, 1024));
capacity_ = MAX(initial_capacity, 1024);
buffer_ = (char*)xe_calloc(capacity_);
buffer_[0] = 0;
} }
StringBuffer::~StringBuffer() { xe_free(buffer_); } StringBuffer::~StringBuffer() = default;
void StringBuffer::Reset() { void StringBuffer::Reset() {
offset_ = 0; offset_ = 0;
buffer_[0] = 0; buffer_[0] = 0;
} }
void StringBuffer::Append(const std::string& value) {
Append(value.c_str());
}
void StringBuffer::Append(const char* format, ...) { void StringBuffer::Append(const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@ -34,12 +35,10 @@ void StringBuffer::Append(const char* format, ...) {
void StringBuffer::AppendVarargs(const char* format, va_list args) { void StringBuffer::AppendVarargs(const char* format, va_list args) {
while (true) { while (true) {
int len = int len = vsnprintf(buffer_.data() + offset_, buffer_.size() - offset_ - 1,
xevsnprintfa(buffer_ + offset_, capacity_ - offset_ - 1, format, args); format, args);
if (len == -1) { if (len == -1) {
size_t old_capacity = capacity_; buffer_.resize(buffer_.size() * 2);
capacity_ = capacity_ * 2;
buffer_ = (char*)xe_realloc(buffer_, old_capacity, capacity_);
continue; continue;
} else { } else {
offset_ += len; offset_ += len;
@ -50,18 +49,16 @@ void StringBuffer::AppendVarargs(const char* format, va_list args) {
} }
void StringBuffer::AppendBytes(const uint8_t* buffer, size_t length) { void StringBuffer::AppendBytes(const uint8_t* buffer, size_t length) {
if (offset_ + length > capacity_) { if (offset_ + length > buffer_.size()) {
size_t old_capacity = capacity_; buffer_.resize(MAX(buffer_.size() * 2, buffer_.size() + length));
capacity_ = MAX(capacity_ * 2, capacity_ + length);
buffer_ = (char*)xe_realloc(buffer_, old_capacity, capacity_);
} }
xe_copy_memory(buffer_ + offset_, capacity_ - offset_, buffer, length); memcpy(buffer_.data() + offset_, buffer, length);
offset_ += length; offset_ += length;
buffer_[offset_] = 0; buffer_[offset_] = 0;
} }
const char* StringBuffer::GetString() const { return buffer_; } const char* StringBuffer::GetString() const { return buffer_.data(); }
char* StringBuffer::ToString() { return xestrdupa(buffer_); } char* StringBuffer::ToString() { return strdup(buffer_.data()); }
} // namespace alloy } // namespace alloy

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_STRING_BUFFER_H_ #ifndef ALLOY_STRING_BUFFER_H_
#define ALLOY_STRING_BUFFER_H_ #define ALLOY_STRING_BUFFER_H_
#include <vector>
#include <alloy/core.h> #include <alloy/core.h>
namespace alloy { namespace alloy {
@ -23,6 +25,7 @@ class StringBuffer {
void Reset(); void Reset();
void Append(const std::string& value);
void Append(const char* format, ...); void Append(const char* format, ...);
void AppendVarargs(const char* format, va_list args); void AppendVarargs(const char* format, va_list args);
void AppendBytes(const uint8_t* buffer, size_t length); void AppendBytes(const uint8_t* buffer, size_t length);
@ -32,8 +35,7 @@ class StringBuffer {
char* EncodeBase64(); char* EncodeBase64();
private: private:
char* buffer_; std::vector<char> buffer_;
size_t capacity_;
size_t offset_; size_t offset_;
}; };

View File

@ -12,9 +12,9 @@
namespace alloy { namespace alloy {
namespace tracing { namespace tracing {
Channel::Channel() {} Channel::Channel() = default;
Channel::~Channel() {} Channel::~Channel() = default;
} // namespace tracing } // namespace tracing
} // namespace alloy } // namespace alloy

View File

@ -13,17 +13,16 @@ namespace alloy {
namespace tracing { namespace tracing {
namespace channels { namespace channels {
FileChannel::FileChannel(const char* path) { FileChannel::FileChannel(const std::string& path) : path_(path) {
path_ = xestrdupa(path); file_ = fopen(path_.c_str(), "wb");
file_ = fopen(path, "wb");
} }
FileChannel::~FileChannel() { FileChannel::~FileChannel() {
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
fclose(file_); if (file_) {
file_ = 0; fclose(file_);
free(path_); file_ = nullptr;
path_ = 0; }
} }
void FileChannel::Write(size_t buffer_count, size_t buffer_lengths[], void FileChannel::Write(size_t buffer_count, size_t buffer_lengths[],

View File

@ -22,16 +22,16 @@ namespace channels {
class FileChannel : public Channel { class FileChannel : public Channel {
public: public:
FileChannel(const char* path); FileChannel(const std::string& path);
virtual ~FileChannel(); ~FileChannel() override;
virtual void Write(size_t buffer_count, size_t buffer_lengths[], void Write(size_t buffer_count, size_t buffer_lengths[],
const uint8_t* buffers[]); const uint8_t* buffers[]) override;
virtual void Flush(); void Flush() override;
private: private:
char* path_; std::string path_;
FILE* file_; FILE* file_;
std::mutex lock_; std::mutex lock_;
}; };

View File

@ -22,7 +22,7 @@ Tracer::Tracer(Channel* channel) : channel_(channel) {
thread_id_ = ++next_thread_id_; thread_id_ = ++next_thread_id_;
} }
Tracer::~Tracer() {} Tracer::~Tracer() = default;
void Tracer::WriteEvent(uint32_t event_type, size_t size, const uint8_t* data) { void Tracer::WriteEvent(uint32_t event_type, size_t size, const uint8_t* data) {
uint32_t header[] = { uint32_t header[] = {

View File

@ -26,7 +26,7 @@ class Tracer {
void set_thread_id(int value) { thread_id_ = value; } void set_thread_id(int value) { thread_id_ = value; }
void WriteEvent(uint32_t event_type, size_t size = 0, void WriteEvent(uint32_t event_type, size_t size = 0,
const uint8_t* data = 0); const uint8_t* data = nullptr);
private: private:
Channel* channel_; Channel* channel_;

View File

@ -24,13 +24,14 @@ DEFINE_string(trace_file, "", "Traces to the given file path.");
namespace alloy { namespace alloy {
namespace tracing { namespace tracing {
Channel* shared_channel = NULL; Channel* shared_channel = nullptr;
thread_local Tracer* thread_tracer = NULL; thread_local Tracer* thread_tracer = nullptr;
void CleanupTracing() { void CleanupTracing() {
if (shared_channel) { if (shared_channel) {
alloy::tracing::WriteEvent(EventType::TraceEOF({})); alloy::tracing::WriteEvent(EventType::TraceEOF({}));
shared_channel->Flush(); shared_channel->Flush();
shared_channel = nullptr;
} }
} }
@ -67,7 +68,7 @@ void Flush() {
Tracer* GetThreadTracer() { Tracer* GetThreadTracer() {
if (!shared_channel) { if (!shared_channel) {
return NULL; return nullptr;
} }
if (!thread_tracer) { if (!thread_tracer) {
thread_tracer = new Tracer(shared_channel); thread_tracer = new Tracer(shared_channel);

View File

@ -20,13 +20,14 @@ namespace tracing {
class Channel; class Channel;
class Tracer; class Tracer;
bool Initialize(Channel* channel = 0); bool Initialize(Channel* channel = nullptr);
void Shutdown(); void Shutdown();
void Flush(); void Flush();
Tracer* GetThreadTracer(); Tracer* GetThreadTracer();
void WriteEvent(uint32_t event_type, size_t size = 0, const void* data = 0); void WriteEvent(uint32_t event_type, size_t size = 0,
const void* data = nullptr);
template <typename T> template <typename T>
void WriteEvent(const T& ev) { void WriteEvent(const T& ev) {

32
src/poly/memory.cc Normal file
View File

@ -0,0 +1,32 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <poly/memory.h>
#if !XE_LIKE_WIN32
#include <unistd.h>
#endif // !XE_LIKE_WIN32
namespace poly {
size_t page_size() {
static size_t value = 0;
if (!value) {
#if XE_LIKE_WIN32
SYSTEM_INFO si;
GetSystemInfo(&si);
value = si.dwPageSize;
#else
value = getpagesize();
#endif // XE_LIKE_WIN32
}
return value;
}
} // namespace poly

22
src/poly/memory.h Normal file
View File

@ -0,0 +1,22 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_MEMORY_H_
#define POLY_MEMORY_H_
#include <poly/config.h>
#include <poly/platform.h>
namespace poly {
size_t page_size();
} // namespace poly
#endif // POLY_MEMORY_H_

View File

@ -142,6 +142,7 @@ XE_CPU: 32BIT | 64BIT | BIGENDIAN | LITTLEENDIAN
#undef min #undef min
#undef max #undef max
#undef Yield #undef Yield
#define strdup _strdup
#endif // WINCE || WIN32 #endif // WINCE || WIN32
#if XE_PLATFORM_XBOX360 #if XE_PLATFORM_XBOX360

View File

@ -7,6 +7,8 @@
'cxx_compat.h', 'cxx_compat.h',
'math.cc', 'math.cc',
'math.h', 'math.h',
'memory.cc',
'memory.h',
'platform.h', 'platform.h',
'poly-private.h', 'poly-private.h',
'poly.cc', 'poly.cc',

View File

@ -75,12 +75,10 @@ int Processor::Setup() {
return 1; return 1;
} }
Backend* backend = 0; std::unique_ptr<Backend> backend;
// Backend* backend = new alloy::backend::ivm::IVMBackend( // backend.reset(new alloy::backend::ivm::IVMBackend(runtime));
// runtime); // backend.reset(new alloy::backend::x64::X64Backend(runtime));
// Backend* backend = new alloy::backend::x64::X64Backend( int result = runtime_->Initialize(std::move(backend));
// runtime);
int result = runtime_->Initialize(backend);
if (result) { if (result) {
return result; return result;
} }

View File

@ -23,8 +23,8 @@ using namespace xe::cpu;
XenonRuntime::XenonRuntime( XenonRuntime::XenonRuntime(
alloy::Memory* memory, ExportResolver* export_resolver) : alloy::Memory* memory, ExportResolver* export_resolver) :
export_resolver_(export_resolver), Runtime(memory),
Runtime(memory) { export_resolver_(export_resolver) {
} }
XenonRuntime::~XenonRuntime() { XenonRuntime::~XenonRuntime() {
@ -32,11 +32,11 @@ XenonRuntime::~XenonRuntime() {
})); }));
} }
int XenonRuntime::Initialize(backend::Backend* backend) { int XenonRuntime::Initialize(std::unique_ptr<backend::Backend> backend) {
PPCFrontend* frontend = new PPCFrontend(this); std::unique_ptr<PPCFrontend> frontend(new PPCFrontend(this));
// TODO(benvanik): set options/etc. // TODO(benvanik): set options/etc.
int result = Runtime::Initialize(frontend, backend); int result = Runtime::Initialize(std::move(frontend), std::move(backend));
if (result) { if (result) {
return result; return result;
} }

View File

@ -31,7 +31,7 @@ public:
ExportResolver* export_resolver() const { return export_resolver_; } ExportResolver* export_resolver() const { return export_resolver_; }
virtual int Initialize(alloy::backend::Backend* backend = 0); virtual int Initialize(std::unique_ptr<alloy::backend::Backend> backend = 0);
private: private:
ExportResolver* export_resolver_; ExportResolver* export_resolver_;

View File

@ -29,18 +29,16 @@ void UndefinedImport(PPCContext* ppc_state, void* arg0, void* arg1) {
XexModule::XexModule( XexModule::XexModule(
XenonRuntime* runtime) : XenonRuntime* runtime) :
runtime_(runtime), runtime_(runtime),
name_(0), path_(0), xex_(0), xex_(0),
base_address_(0), low_address_(0), high_address_(0), base_address_(0), low_address_(0), high_address_(0),
Module(runtime) { Module(runtime) {
} }
XexModule::~XexModule() { XexModule::~XexModule() {
xe_xex2_release(xex_); xe_xex2_release(xex_);
xe_free(name_);
xe_free(path_);
} }
int XexModule::Load(const char* name, const char* path, xe_xex2_ref xex) { int XexModule::Load(const std::string& name, const std::string& path, xe_xex2_ref xex) {
int result; int result;
xex_ = xe_xex2_retain(xex); xex_ = xe_xex2_retain(xex);
@ -79,8 +77,8 @@ int XexModule::Load(const char* name, const char* path, xe_xex2_ref xex) {
} }
// Setup debug info. // Setup debug info.
name_ = xestrdupa(name); name_ = std::string(name);
path_ = xestrdupa(path); path_ = std::string(path);
// TODO(benvanik): debug info // TODO(benvanik): debug info
// Load a specified module map and diff. // Load a specified module map and diff.

View File

@ -10,6 +10,8 @@
#ifndef XENIA_CPU_XEX_MODULE_H_ #ifndef XENIA_CPU_XEX_MODULE_H_
#define XENIA_CPU_XEX_MODULE_H_ #define XENIA_CPU_XEX_MODULE_H_
#include <string>
#include <alloy/runtime/module.h> #include <alloy/runtime/module.h>
#include <xenia/core.h> #include <xenia/core.h>
#include <xenia/kernel/util/xex2.h> #include <xenia/kernel/util/xex2.h>
@ -28,11 +30,11 @@ public:
xe_xex2_ref xex() const { return xex_; } xe_xex2_ref xex() const { return xex_; }
int Load(const char* name, const char* path, xe_xex2_ref xex); int Load(const std::string& name, const std::string& path, xe_xex2_ref xex);
virtual const char* name() const { return name_; } const std::string& name() const override { return name_; }
virtual bool ContainsAddress(uint64_t address); bool ContainsAddress(uint64_t address) override;
private: private:
int SetupImports(xe_xex2_ref xex); int SetupImports(xe_xex2_ref xex);
@ -41,13 +43,13 @@ private:
private: private:
XenonRuntime* runtime_; XenonRuntime* runtime_;
char* name_; std::string name_;
char* path_; std::string path_;
xe_xex2_ref xex_; xe_xex2_ref xex_;
uint64_t base_address_; uint64_t base_address_;
uint64_t low_address_; uint64_t low_address_;
uint64_t high_address_; uint64_t high_address_;
}; };

View File

@ -8,6 +8,8 @@
*/ */
#include <alloy/alloy.h> #include <alloy/alloy.h>
#include <alloy/backend/ivm/ivm_backend.h>
#include <alloy/backend/x64/x64_backend.h>
#include <alloy/runtime/raw_module.h> #include <alloy/runtime/raw_module.h>
#include <xenia/export_resolver.h> #include <xenia/export_resolver.h>
#include <xenia/cpu/xenon_memory.h> #include <xenia/cpu/xenon_memory.h>
@ -31,12 +33,10 @@ int alloy_sandbox(int argc, xechar_t** argv) {
ExportResolver* export_resolver = new ExportResolver(); ExportResolver* export_resolver = new ExportResolver();
XenonRuntime* runtime = new XenonRuntime(memory, export_resolver); XenonRuntime* runtime = new XenonRuntime(memory, export_resolver);
Backend* backend = 0; std::unique_ptr<Backend> backend;
// Backend* backend = new alloy::backend::interpreter::InterpreterBackend( // backend.reset(new alloy::backend::ivm::IVMBackend(runtime));
// runtime); // backend.reset(new alloy::backend::x64::X64Backend(runtime));
// Backend* backend = new alloy::backend::x64::X64Backend( runtime->Initialize(std::move(backend));
// runtime);
runtime->Initialize(backend);
RawModule* module = new RawModule(runtime); RawModule* module = new RawModule(runtime);
module->LoadFile(0x82000000, "test\\codegen\\instr_add.bin"); module->LoadFile(0x82000000, "test\\codegen\\instr_add.bin");