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);
}