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 {
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() {
Reset();
@ -22,7 +22,7 @@ Arena::~Arena() {
delete chunk;
chunk = next;
}
head_chunk_ = NULL;
head_chunk_ = nullptr;
}
void Arena::Reset() {
@ -86,7 +86,7 @@ void* Arena::CloneContents() {
}
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);
}

View File

@ -25,7 +25,7 @@ class Arena {
void* Alloc(size_t size);
template <typename T>
T* Alloc() {
return (T*)Alloc(sizeof(T));
return reinterpret_cast<T*>(Alloc(sizeof(T)));
}
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 yi_high) {
// 128bit multiply, simplified for two input 64bit integers.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -41,7 +41,7 @@ class Breakpoint {
uint64_t address() const { return address_; }
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:
Type type_;
@ -53,7 +53,7 @@ class Breakpoint {
class DebugEvent {
public:
DebugEvent(Debugger* debugger) : debugger_(debugger) {}
virtual ~DebugEvent() {}
virtual ~DebugEvent() = default;
Debugger* debugger() const { return debugger_; }
protected:
@ -67,7 +67,7 @@ class BreakpointHitEvent : public DebugEvent {
: DebugEvent(debugger),
thread_state_(thread_state),
breakpoint_(breakpoint) {}
virtual ~BreakpointHitEvent() {}
~BreakpointHitEvent() override = default;
ThreadState* thread_state() const { return thread_state_; }
Breakpoint* breakpoint() const { return breakpoint_; }

View File

@ -26,11 +26,11 @@ EntryTable::~EntryTable() {
Entry* EntryTable::Get(uint64_t address) {
std::lock_guard<std::mutex> guard(lock_);
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) {
// TODO(benvanik): wait if needed?
if (entry->status != Entry::STATUS_READY) {
entry = NULL;
entry = nullptr;
}
}
return entry;
@ -39,7 +39,7 @@ Entry* EntryTable::Get(uint64_t address) {
Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) {
lock_.lock();
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;
if (entry) {
// 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");
std::lock_guard<std::mutex> guard(lock_);
std::vector<Function*> fns;
for (auto it = map_.begin(); it != map_.end(); ++it) {
Entry* entry = it->second;
for (auto& it : map_) {
Entry* entry = it.second;
if (address >= entry->address && address <= entry->end_address) {
if (entry->status == Entry::STATUS_READY) {
fns.push_back(entry->function);

View File

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

View File

@ -34,7 +34,7 @@ bool Module::ContainsAddress(uint64_t address) { return true; }
SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) {
lock_.lock();
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->status() == SymbolInfo::STATUS_DECLARING) {
// 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);
} else {
// 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,
uint64_t address,
SymbolInfo** out_symbol_info) {
*out_symbol_info = NULL;
*out_symbol_info = nullptr;
lock_.lock();
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;
if (symbol_info) {
// If we exist but are the wrong type, die.
@ -235,8 +235,8 @@ int Module::ReadMap(const char* file_name) {
continue;
}
// Don't overwrite names we've set elsewhere.
if (!fn_info->name()) {
// If it's an mangled C++ name (?name@...) just use the name.
if (fn_info->name().empty()) {
// If it's a mangled C++ name (?name@...) just use the name.
// TODO(benvanik): better demangling, or leave it to clients.
/*if (name[0] == '?') {
size_t at = name.find('@');

View File

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

View File

@ -13,17 +13,12 @@ namespace alloy {
namespace runtime {
RawModule::RawModule(Runtime* runtime)
: Module(runtime),
name_(0),
base_address_(0),
low_address_(0),
high_address_(0) {}
: Module(runtime), base_address_(0), low_address_(0), high_address_(0) {}
RawModule::~RawModule() {
if (base_address_) {
memory_->HeapFree(base_address_, high_address_ - low_address_);
}
xe_free(name_);
}
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);
// Setup debug info.
const char* name = xestrrchra(path, XE_PATH_SEPARATOR) + 1;
name_ = xestrdupa(name);
name_ = std::string(xestrrchra(path, XE_PATH_SEPARATOR) + 1);
// TODO(benvanik): debug info
low_address_ = base_address;

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_RUNTIME_RAW_MODULE_H_
#define ALLOY_RUNTIME_RAW_MODULE_H_
#include <string>
#include <alloy/runtime/module.h>
namespace alloy {
@ -18,16 +20,16 @@ namespace runtime {
class RawModule : public Module {
public:
RawModule(Runtime* runtime);
virtual ~RawModule();
~RawModule() override;
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:
char* name_;
std::string name_;
uint64_t base_address_;
uint64_t low_address_;
uint64_t high_address_;

View File

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

View File

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

View File

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

View File

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

View File

@ -14,13 +14,13 @@
namespace alloy {
namespace runtime {
thread_local ThreadState* thread_state_ = NULL;
thread_local ThreadState* thread_state_ = nullptr;
ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id)
: runtime_(runtime),
memory_(runtime->memory()),
thread_id_(thread_id),
name_(0),
name_(""),
backend_data_(0),
raw_context_(0) {
if (thread_id_ == UINT_MAX) {
@ -37,21 +37,8 @@ ThreadState::~ThreadState() {
runtime_->backend()->FreeThreadData(backend_data_);
}
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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,13 +20,14 @@ namespace tracing {
class Channel;
class Tracer;
bool Initialize(Channel* channel = 0);
bool Initialize(Channel* channel = nullptr);
void Shutdown();
void Flush();
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>
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 max
#undef Yield
#define strdup _strdup
#endif // WINCE || WIN32
#if XE_PLATFORM_XBOX360

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ public:
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:
ExportResolver* export_resolver_;

View File

@ -29,18 +29,16 @@ void UndefinedImport(PPCContext* ppc_state, void* arg0, void* arg1) {
XexModule::XexModule(
XenonRuntime* runtime) :
runtime_(runtime),
name_(0), path_(0), xex_(0),
xex_(0),
base_address_(0), low_address_(0), high_address_(0),
Module(runtime) {
}
XexModule::~XexModule() {
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;
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.
name_ = xestrdupa(name);
path_ = xestrdupa(path);
name_ = std::string(name);
path_ = std::string(path);
// TODO(benvanik): debug info
// Load a specified module map and diff.

View File

@ -10,6 +10,8 @@
#ifndef XENIA_CPU_XEX_MODULE_H_
#define XENIA_CPU_XEX_MODULE_H_
#include <string>
#include <alloy/runtime/module.h>
#include <xenia/core.h>
#include <xenia/kernel/util/xex2.h>
@ -28,11 +30,11 @@ public:
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:
int SetupImports(xe_xex2_ref xex);
@ -41,13 +43,13 @@ private:
private:
XenonRuntime* runtime_;
char* name_;
char* path_;
xe_xex2_ref xex_;
std::string name_;
std::string path_;
xe_xex2_ref xex_;
uint64_t base_address_;
uint64_t low_address_;
uint64_t high_address_;
uint64_t base_address_;
uint64_t low_address_;
uint64_t high_address_;
};

View File

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