More C++11ification.

This commit is contained in:
Ben Vanik
2014-07-13 22:28:00 -07:00
parent 0a250d5e91
commit e9284dfaed
40 changed files with 211 additions and 152 deletions

View File

@@ -10,6 +10,8 @@
#ifndef ALLOY_BACKEND_ASSEMBLER_H_
#define ALLOY_BACKEND_ASSEMBLER_H_
#include <memory>
#include <alloy/core.h>
namespace alloy {
@@ -40,7 +42,7 @@ class Assembler {
virtual int Assemble(runtime::FunctionInfo* symbol_info,
hir::HIRBuilder* builder, uint32_t debug_info_flags,
runtime::DebugInfo* debug_info,
std::unique_ptr<runtime::DebugInfo> debug_info,
runtime::Function** out_function) = 0;
protected:

View File

@@ -10,6 +10,8 @@
#ifndef ALLOY_BACKEND_BACKEND_H_
#define ALLOY_BACKEND_BACKEND_H_
#include <memory>
#include <alloy/core.h>
#include <alloy/backend/machine_info.h>
@@ -37,7 +39,7 @@ class Backend {
virtual void* AllocThreadData();
virtual void FreeThreadData(void* thread_data);
virtual Assembler* CreateAssembler() = 0;
virtual std::unique_ptr<Assembler> CreateAssembler() = 0;
protected:
runtime::Runtime* runtime_;

View File

@@ -9,6 +9,7 @@
#include <alloy/backend/ivm/ivm_assembler.h>
#include <alloy/reset_scope.h>
#include <alloy/backend/backend.h>
#include <alloy/backend/ivm/ivm_intcode.h>
#include <alloy/backend/ivm/ivm_function.h>
@@ -21,6 +22,7 @@ namespace backend {
namespace ivm {
using alloy::hir::HIRBuilder;
using alloy::runtime::DebugInfo;
using alloy::runtime::Function;
using alloy::runtime::FunctionInfo;
@@ -47,10 +49,15 @@ void IVMAssembler::Reset() {
int IVMAssembler::Assemble(FunctionInfo* symbol_info, HIRBuilder* builder,
uint32_t debug_info_flags,
runtime::DebugInfo* debug_info,
std::unique_ptr<DebugInfo> debug_info,
Function** out_function) {
SCOPE_profile_cpu_f("alloy");
// Reset when we leave.
make_reset_scope(this);
IVMFunction* fn = new IVMFunction(symbol_info);
fn->set_debug_info(debug_info);
fn->set_debug_info(std::move(debug_info));
TranslationContext ctx;
ctx.register_count = 0;

View File

@@ -21,16 +21,16 @@ namespace ivm {
class IVMAssembler : public Assembler {
public:
IVMAssembler(Backend* backend);
virtual ~IVMAssembler();
~IVMAssembler() override;
virtual int Initialize();
int Initialize() override;
virtual void Reset();
void Reset() override;
virtual int Assemble(runtime::FunctionInfo* symbol_info,
hir::HIRBuilder* builder, uint32_t debug_info_flags,
runtime::DebugInfo* debug_info,
runtime::Function** out_function);
int Assemble(runtime::FunctionInfo* symbol_info, hir::HIRBuilder* builder,
uint32_t debug_info_flags,
std::unique_ptr<runtime::DebugInfo> debug_info,
runtime::Function** out_function) override;
private:
Arena intcode_arena_;

View File

@@ -47,7 +47,9 @@ void IVMBackend::FreeThreadData(void* thread_data) {
delete stack;
}
Assembler* IVMBackend::CreateAssembler() { return new IVMAssembler(this); }
std::unique_ptr<Assembler> IVMBackend::CreateAssembler() {
return std::make_unique<IVMAssembler>(this);
}
} // namespace ivm
} // namespace backend

View File

@@ -23,14 +23,14 @@ namespace ivm {
class IVMBackend : public Backend {
public:
IVMBackend(runtime::Runtime* runtime);
virtual ~IVMBackend();
~IVMBackend() override;
virtual int Initialize();
int Initialize() override;
virtual void* AllocThreadData();
virtual void FreeThreadData(void* thread_data);
void* AllocThreadData() override;
void FreeThreadData(void* thread_data) override;
virtual Assembler* CreateAssembler();
std::unique_ptr<Assembler> CreateAssembler() override;
};
} // namespace ivm

View File

@@ -216,7 +216,7 @@ int Translate_COMMENT(TranslationContext& ctx, Instr* i) {
ic->flags = i->flags;
ic->debug_flags = 0;
// HACK HACK HACK
char* src = xestrdupa((char*)i->src1.offset);
char* src = strdup(reinterpret_cast<char*>(i->src1.offset));
uint64_t src_p = (uint64_t)src;
ic->src1_reg = (uint32_t)src_p;
ic->src2_reg = (uint32_t)(src_p >> 32);

View File

@@ -9,6 +9,7 @@
#include <alloy/backend/x64/x64_assembler.h>
#include <alloy/reset_scope.h>
#include <alloy/backend/x64/x64_backend.h>
#include <alloy/backend/x64/x64_emitter.h>
#include <alloy/backend/x64/x64_function.h>
@@ -33,12 +34,9 @@ using alloy::runtime::Function;
using alloy::runtime::FunctionInfo;
X64Assembler::X64Assembler(X64Backend* backend)
: Assembler(backend), x64_backend_(backend), emitter_(0), allocator_(0) {}
: Assembler(backend), x64_backend_(backend) {}
X64Assembler::~X64Assembler() {
delete emitter_;
delete allocator_;
}
X64Assembler::~X64Assembler() = default;
int X64Assembler::Initialize() {
int result = Assembler::Initialize();
@@ -46,8 +44,8 @@ int X64Assembler::Initialize() {
return result;
}
allocator_ = new XbyakAllocator();
emitter_ = new X64Emitter(x64_backend_, allocator_);
allocator_.reset(new XbyakAllocator());
emitter_.reset(new X64Emitter(x64_backend_, allocator_.get()));
return result;
}
@@ -58,39 +56,39 @@ void X64Assembler::Reset() {
}
int X64Assembler::Assemble(FunctionInfo* symbol_info, HIRBuilder* builder,
uint32_t debug_info_flags, DebugInfo* debug_info,
uint32_t debug_info_flags,
std::unique_ptr<DebugInfo> debug_info,
Function** out_function) {
SCOPE_profile_cpu_f("alloy");
int result = 0;
// Reset when we leave.
make_reset_scope(this);
// Lower HIR -> x64.
void* machine_code = 0;
size_t code_size = 0;
result = emitter_->Emit(builder, debug_info_flags, debug_info, machine_code,
code_size);
XEEXPECTZERO(result);
int result = emitter_->Emit(builder, debug_info_flags, debug_info.get(),
machine_code, code_size);
if (result) {
return result;
}
// Stash generated machine code.
if (debug_info_flags & DebugInfoFlags::DEBUG_INFO_MACHINE_CODE_DISASM) {
DumpMachineCode(debug_info, machine_code, code_size, &string_buffer_);
DumpMachineCode(debug_info.get(), machine_code, code_size, &string_buffer_);
debug_info->set_machine_code_disasm(string_buffer_.ToString());
string_buffer_.Reset();
}
{
X64Function* fn = new X64Function(symbol_info);
fn->set_debug_info(debug_info);
fn->set_debug_info(std::move(debug_info));
fn->Setup(machine_code, code_size);
*out_function = fn;
result = 0;
}
XECLEANUP:
Reset();
return result;
return 0;
}
void X64Assembler::DumpMachineCode(DebugInfo* debug_info, void* machine_code,

View File

@@ -10,6 +10,8 @@
#ifndef ALLOY_BACKEND_X64_X64_ASSEMBLER_H_
#define ALLOY_BACKEND_X64_X64_ASSEMBLER_H_
#include <memory>
#include <alloy/core.h>
#include <alloy/backend/assembler.h>
@@ -25,16 +27,16 @@ class XbyakAllocator;
class X64Assembler : public Assembler {
public:
X64Assembler(X64Backend* backend);
virtual ~X64Assembler();
~X64Assembler() override;
virtual int Initialize();
int Initialize() override;
virtual void Reset();
void Reset() override;
virtual int Assemble(runtime::FunctionInfo* symbol_info,
hir::HIRBuilder* builder, uint32_t debug_info_flags,
runtime::DebugInfo* debug_info,
runtime::Function** out_function);
int Assemble(runtime::FunctionInfo* symbol_info, hir::HIRBuilder* builder,
uint32_t debug_info_flags,
std::unique_ptr<runtime::DebugInfo> debug_info,
runtime::Function** out_function) override;
private:
void DumpMachineCode(runtime::DebugInfo* debug_info, void* machine_code,
@@ -42,8 +44,8 @@ class X64Assembler : public Assembler {
private:
X64Backend* x64_backend_;
X64Emitter* emitter_;
XbyakAllocator* allocator_;
std::unique_ptr<X64Emitter> emitter_;
std::unique_ptr<XbyakAllocator> allocator_;
StringBuffer string_buffer_;
};

View File

@@ -47,17 +47,18 @@ int X64Backend::Initialize() {
return result;
}
auto allocator = new XbyakAllocator();
auto thunk_emitter = new X64ThunkEmitter(this, allocator);
// Generate thunks used to transition between jitted code and host code.
auto allocator = std::make_unique<XbyakAllocator>();
auto thunk_emitter = std::make_unique<X64ThunkEmitter>(this, allocator.get());
host_to_guest_thunk_ = thunk_emitter->EmitHostToGuestThunk();
guest_to_host_thunk_ = thunk_emitter->EmitGuestToHostThunk();
delete thunk_emitter;
delete allocator;
return result;
}
Assembler* X64Backend::CreateAssembler() { return new X64Assembler(this); }
std::unique_ptr<Assembler> X64Backend::CreateAssembler() {
return std::make_unique<X64Assembler>(this);
}
} // namespace x64
} // namespace backend

View File

@@ -28,15 +28,15 @@ typedef void* (*GuestToHostThunk)(void* target, void* arg0, void* arg1);
class X64Backend : public Backend {
public:
X64Backend(runtime::Runtime* runtime);
virtual ~X64Backend();
~X64Backend() override;
X64CodeCache* code_cache() const { return code_cache_; }
HostToGuestThunk host_to_guest_thunk() const { return host_to_guest_thunk_; }
GuestToHostThunk guest_to_host_thunk() const { return guest_to_host_thunk_; }
virtual int Initialize();
int Initialize() override;
virtual Assembler* CreateAssembler();
std::unique_ptr<Assembler> CreateAssembler() override;
private:
X64CodeCache* code_cache_;

View File

@@ -61,7 +61,7 @@ EMITTER(COMMENT, MATCH(I<OPCODE_COMMENT, VoidOp, OffsetOp>)) {
auto str = reinterpret_cast<const char*>(i.src1.value);
// TODO(benvanik): pass through.
// TODO(benvanik): don't just leak this memory.
auto str_copy = xestrdupa(str);
auto str_copy = strdup(str);
e.mov(e.rdx, reinterpret_cast<uint64_t>(str_copy));
e.CallNative(TraceString);
}

View File

@@ -10,6 +10,7 @@
#include <alloy/frontend/ppc/ppc_translator.h>
#include <alloy/alloy-private.h>
#include <alloy/reset_scope.h>
#include <alloy/compiler/compiler_passes.h>
#include <alloy/frontend/ppc/ppc_disasm.h>
#include <alloy/frontend/ppc/ppc_frontend.h>
@@ -34,10 +35,10 @@ namespace passes = alloy::compiler::passes;
PPCTranslator::PPCTranslator(PPCFrontend* frontend) : frontend_(frontend) {
Backend* backend = frontend->runtime()->backend();
scanner_ = new PPCScanner(frontend);
builder_ = new PPCHIRBuilder(frontend);
compiler_ = new Compiler(frontend->runtime());
assembler_ = backend->CreateAssembler();
scanner_.reset(new PPCScanner(frontend));
builder_.reset(new PPCHIRBuilder(frontend));
compiler_.reset(new Compiler(frontend->runtime()));
assembler_ = std::move(backend->CreateAssembler());
assembler_->Initialize();
bool validate = FLAGS_validate_hir;
@@ -78,18 +79,19 @@ PPCTranslator::PPCTranslator(PPCFrontend* frontend) : frontend_(frontend) {
compiler_->AddPass(std::make_unique<passes::FinalizationPass>());
}
PPCTranslator::~PPCTranslator() {
delete assembler_;
delete compiler_;
delete builder_;
delete scanner_;
}
PPCTranslator::~PPCTranslator() = default;
int PPCTranslator::Translate(FunctionInfo* symbol_info,
uint32_t debug_info_flags,
Function** out_function) {
SCOPE_profile_cpu_f("alloy");
// Reset() all caching when we leave.
make_reset_scope(builder_);
make_reset_scope(compiler_);
make_reset_scope(assembler_);
make_reset_scope(&string_buffer_);
// Scan the function to find its extents. We only need to do this if we
// haven't already been provided with them from some other source.
if (!symbol_info->has_end_address()) {
@@ -106,9 +108,9 @@ int PPCTranslator::Translate(FunctionInfo* symbol_info,
if (FLAGS_always_disasm) {
debug_info_flags |= DEBUG_INFO_ALL_DISASM;
}
DebugInfo* debug_info = NULL;
std::unique_ptr<DebugInfo> debug_info;
if (debug_info_flags) {
debug_info = new DebugInfo();
debug_info.reset(new DebugInfo());
}
// Stash source.
@@ -119,8 +121,10 @@ int PPCTranslator::Translate(FunctionInfo* symbol_info,
}
// Emit function.
int result = builder_->Emit(symbol_info, debug_info != NULL);
XEEXPECTZERO(result);
int result = builder_->Emit(symbol_info, debug_info != nullptr);
if (result) {
return result;
}
// Stash raw HIR.
if (debug_info_flags & DEBUG_INFO_RAW_HIR_DISASM) {
@@ -130,8 +134,10 @@ int PPCTranslator::Translate(FunctionInfo* symbol_info,
}
// Compile/optimize/etc.
result = compiler_->Compile(builder_);
XEEXPECTZERO(result);
result = compiler_->Compile(builder_.get());
if (result) {
return result;
}
// Stash optimized HIR.
if (debug_info_flags & DEBUG_INFO_HIR_DISASM) {
@@ -141,21 +147,13 @@ int PPCTranslator::Translate(FunctionInfo* symbol_info,
}
// Assemble to backend machine code.
result = assembler_->Assemble(symbol_info, builder_, debug_info_flags,
debug_info, out_function);
XEEXPECTZERO(result);
result = 0;
XECLEANUP:
result = assembler_->Assemble(symbol_info, builder_.get(), debug_info_flags,
std::move(debug_info), out_function);
if (result) {
delete debug_info;
return result;
}
builder_->Reset();
compiler_->Reset();
assembler_->Reset();
string_buffer_.Reset();
return result;
return 0;
};
void PPCTranslator::DumpSource(runtime::FunctionInfo* symbol_info,

View File

@@ -10,6 +10,8 @@
#ifndef ALLOY_FRONTEND_PPC_PPC_TRANSLATOR_H_
#define ALLOY_FRONTEND_PPC_PPC_TRANSLATOR_H_
#include <memory>
#include <alloy/core.h>
#include <alloy/backend/assembler.h>
#include <alloy/compiler/compiler.h>
@@ -37,10 +39,10 @@ class PPCTranslator {
private:
PPCFrontend* frontend_;
PPCScanner* scanner_;
PPCHIRBuilder* builder_;
compiler::Compiler* compiler_;
backend::Assembler* assembler_;
std::unique_ptr<PPCScanner> scanner_;
std::unique_ptr<PPCHIRBuilder> builder_;
std::unique_ptr<compiler::Compiler> compiler_;
std::unique_ptr<backend::Assembler> assembler_;
StringBuffer string_buffer_;
};

View File

@@ -544,7 +544,7 @@ void HIRBuilder::Comment(const char* format, ...) {
va_start(args, format);
xevsnprintfa(buffer, 1024, format, args);
va_end(args);
size_t len = xestrlena(buffer);
size_t len = strlen(buffer);
if (!len) {
return;
}

45
src/alloy/reset_scope.h Normal file
View File

@@ -0,0 +1,45 @@
/**
******************************************************************************
* 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 ALLOY_RESET_SCOPE_H_
#define ALLOY_RESET_SCOPE_H_
#include <mutex>
#include <alloy/core.h>
namespace alloy {
template <typename T>
class ResetScope {
public:
ResetScope(T* value) : value_(value) {}
~ResetScope() {
if (value_) {
value_->Reset();
}
}
private:
T* value_;
};
template <typename T>
inline ResetScope<T> make_reset_scope(T* value) {
return ResetScope<T>(value);
}
template <typename T>
inline ResetScope<T> make_reset_scope(const std::unique_ptr<T>& value) {
return ResetScope<T>(value.get());
}
} // namespace alloy
#endif // ALLOY_RESET_SCOPE_H_

View File

@@ -18,8 +18,7 @@ namespace runtime {
Function::Function(FunctionInfo* symbol_info)
: address_(symbol_info->address()),
symbol_info_(symbol_info),
debug_info_(0) {}
symbol_info_(symbol_info) {}
Function::~Function() = default;

View File

@@ -10,6 +10,7 @@
#ifndef ALLOY_RUNTIME_FUNCTION_H_
#define ALLOY_RUNTIME_FUNCTION_H_
#include <memory>
#include <mutex>
#include <vector>
@@ -31,8 +32,10 @@ class Function {
uint64_t address() const { return address_; }
FunctionInfo* symbol_info() const { return symbol_info_; }
DebugInfo* debug_info() const { return debug_info_; }
void set_debug_info(DebugInfo* debug_info) { debug_info_ = debug_info; }
DebugInfo* debug_info() const { return debug_info_.get(); }
void set_debug_info(std::unique_ptr<DebugInfo> debug_info) {
debug_info_ = std::move(debug_info);
}
int AddBreakpoint(Breakpoint* breakpoint);
int RemoveBreakpoint(Breakpoint* breakpoint);
@@ -48,7 +51,7 @@ class Function {
protected:
uint64_t address_;
FunctionInfo* symbol_info_;
DebugInfo* debug_info_;
std::unique_ptr<DebugInfo> debug_info_;
// TODO(benvanik): move elsewhere? DebugData?
std::mutex lock_;

View File

@@ -21,8 +21,8 @@ RawModule::~RawModule() {
}
}
int RawModule::LoadFile(uint64_t base_address, const char* path) {
FILE* file = fopen(path, "rb");
int RawModule::LoadFile(uint64_t base_address, const std::string& path) {
FILE* file = fopen(path.c_str(), "rb");
fseek(file, 0, SEEK_END);
size_t file_length = ftell(file);
fseek(file, 0, SEEK_SET);
@@ -42,7 +42,12 @@ int RawModule::LoadFile(uint64_t base_address, const char* path) {
fclose(file);
// Setup debug info.
name_ = std::string(xestrrchra(path, XE_PATH_SEPARATOR) + 1);
auto last_slash = path.find_last_of(poly::path_separator);
if (last_slash != std::string::npos) {
name_ = path.substr(last_slash + 1);
} else {
name_ = path;
}
// TODO(benvanik): debug info
low_address_ = base_address;

View File

@@ -22,7 +22,7 @@ class RawModule : public Module {
RawModule(Runtime* runtime);
~RawModule() override;
int LoadFile(uint64_t base_address, const char* path);
int LoadFile(uint64_t base_address, const std::string& path);
const std::string& name() const override { return name_; }

View File

@@ -10,6 +10,7 @@
'delegate.h',
'memory.cc',
'memory.h',
'reset_scope.h',
'string_buffer.cc',
'string_buffer.h',
'type_pool.h',

View File

@@ -52,8 +52,7 @@ class TypePool {
private:
std::mutex lock_;
typedef std::vector<T*> TList;
TList list_;
std::vector<T*> list_;
};
} // namespace alloy