C++11ing some things.

This commit is contained in:
Ben Vanik 2014-07-13 21:53:31 -07:00
parent 29e4c35c38
commit 0a250d5e91
28 changed files with 113 additions and 143 deletions

View File

@ -15,14 +15,14 @@ namespace backend {
using alloy::runtime::Runtime;
Backend::Backend(Runtime* runtime) : runtime_(runtime) {
xe_zero_struct(&machine_info_, sizeof(machine_info_));
memset(&machine_info_, 0, sizeof(machine_info_));
}
Backend::~Backend() {}
Backend::~Backend() = default;
int Backend::Initialize() { return 0; }
void* Backend::AllocThreadData() { return NULL; }
void* Backend::AllocThreadData() { return nullptr; }
void Backend::FreeThreadData(void* thread_data) {}

View File

@ -17,24 +17,13 @@ namespace compiler {
using alloy::hir::HIRBuilder;
using alloy::runtime::Runtime;
Compiler::Compiler(Runtime* runtime) : runtime_(runtime) {
scratch_arena_ = new Arena();
}
Compiler::Compiler(Runtime* runtime) : runtime_(runtime) {}
Compiler::~Compiler() {
Reset();
Compiler::~Compiler() { Reset(); }
for (auto it = passes_.begin(); it != passes_.end(); ++it) {
CompilerPass* pass = *it;
delete pass;
}
delete scratch_arena_;
}
void Compiler::AddPass(CompilerPass* pass) {
void Compiler::AddPass(std::unique_ptr<CompilerPass> pass) {
pass->Initialize(this);
passes_.push_back(pass);
passes_.push_back(std::move(pass));
}
void Compiler::Reset() {}
@ -44,9 +33,8 @@ int Compiler::Compile(HIRBuilder* builder) {
// TODO(benvanik): sophisticated stuff. Run passes in parallel, run until they
// stop changing things, etc.
for (auto it = passes_.begin(); it != passes_.end(); ++it) {
CompilerPass* pass = *it;
scratch_arena_->Reset();
for (auto& pass : passes_) {
scratch_arena_.Reset();
if (pass->Run(builder)) {
return 1;
}

View File

@ -10,6 +10,9 @@
#ifndef ALLOY_COMPILER_COMPILER_H_
#define ALLOY_COMPILER_COMPILER_H_
#include <memory>
#include <vector>
#include <alloy/core.h>
#include <alloy/hir/hir_builder.h>
@ -30,9 +33,9 @@ class Compiler {
~Compiler();
runtime::Runtime* runtime() const { return runtime_; }
Arena* scratch_arena() const { return scratch_arena_; }
Arena* scratch_arena() { return &scratch_arena_; }
void AddPass(CompilerPass* pass);
void AddPass(std::unique_ptr<CompilerPass> pass);
void Reset();
@ -40,10 +43,9 @@ class Compiler {
private:
runtime::Runtime* runtime_;
Arena* scratch_arena_;
Arena scratch_arena_;
typedef std::vector<CompilerPass*> PassList;
PassList passes_;
std::vector<std::unique_ptr<CompilerPass>> passes_;
};
} // namespace compiler

View File

@ -16,7 +16,7 @@ namespace compiler {
CompilerPass::CompilerPass() : runtime_(0), compiler_(0) {}
CompilerPass::~CompilerPass() {}
CompilerPass::~CompilerPass() = default;
int CompilerPass::Initialize(Compiler* compiler) {
runtime_ = compiler->runtime();

View File

@ -19,9 +19,9 @@ namespace passes {
class ConstantPropagationPass : public CompilerPass {
public:
ConstantPropagationPass();
virtual ~ConstantPropagationPass();
~ConstantPropagationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
void PropagateCarry(hir::Value* v, bool did_carry);

View File

@ -19,11 +19,11 @@ namespace passes {
class ContextPromotionPass : public CompilerPass {
public:
ContextPromotionPass();
virtual ~ContextPromotionPass();
virtual ~ContextPromotionPass() override;
virtual int Initialize(Compiler* compiler);
int Initialize(Compiler* compiler) override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
void PromoteBlock(hir::Block* block);

View File

@ -19,9 +19,9 @@ namespace passes {
class ControlFlowAnalysisPass : public CompilerPass {
public:
ControlFlowAnalysisPass();
virtual ~ControlFlowAnalysisPass();
~ControlFlowAnalysisPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
};

View File

@ -21,9 +21,9 @@ namespace passes {
class DataFlowAnalysisPass : public CompilerPass {
public:
DataFlowAnalysisPass();
virtual ~DataFlowAnalysisPass();
~DataFlowAnalysisPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
uint32_t LinearizeBlocks(hir::HIRBuilder* builder);

View File

@ -21,9 +21,9 @@ namespace passes {
class DeadCodeEliminationPass : public CompilerPass {
public:
DeadCodeEliminationPass();
virtual ~DeadCodeEliminationPass();
~DeadCodeEliminationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
void MakeNopRecursive(hir::Instr* i);

View File

@ -19,9 +19,9 @@ namespace passes {
class FinalizationPass : public CompilerPass {
public:
FinalizationPass();
virtual ~FinalizationPass();
~FinalizationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
};

View File

@ -24,9 +24,9 @@ namespace passes {
class RegisterAllocationPass : public CompilerPass {
public:
RegisterAllocationPass(const backend::MachineInfo* machine_info);
virtual ~RegisterAllocationPass();
~RegisterAllocationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
// TODO(benvanik): rewrite all this set shit -- too much indirection, the

View File

@ -19,9 +19,9 @@ namespace passes {
class SimplificationPass : public CompilerPass {
public:
SimplificationPass();
virtual ~SimplificationPass();
~SimplificationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
void EliminateConversions(hir::HIRBuilder* builder);

View File

@ -19,9 +19,9 @@ namespace passes {
class ValidationPass : public CompilerPass {
public:
ValidationPass();
virtual ~ValidationPass();
~ValidationPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
int ValidateInstruction(hir::Block* block, hir::Instr* instr);

View File

@ -19,9 +19,9 @@ namespace passes {
class ValueReductionPass : public CompilerPass {
public:
ValueReductionPass();
virtual ~ValueReductionPass();
~ValueReductionPass() override;
virtual int Run(hir::HIRBuilder* builder);
int Run(hir::HIRBuilder* builder) override;
private:
void ComputeLastUse(hir::Value* value);

View File

@ -14,10 +14,9 @@
namespace alloy {
namespace frontend {
Frontend::Frontend(runtime::Runtime* runtime)
: runtime_(runtime), context_info_(0) {}
Frontend::Frontend(runtime::Runtime* runtime) : runtime_(runtime) {}
Frontend::~Frontend() { delete context_info_; }
Frontend::~Frontend() = default;
Memory* Frontend::memory() const { return runtime_->memory(); }

View File

@ -10,6 +10,8 @@
#ifndef ALLOY_FRONTEND_FRONTEND_H_
#define ALLOY_FRONTEND_FRONTEND_H_
#include <memory>
#include <alloy/core.h>
#include <alloy/memory.h>
#include <alloy/frontend/context_info.h>
@ -32,7 +34,7 @@ class Frontend {
runtime::Runtime* runtime() const { return runtime_; }
Memory* memory() const;
ContextInfo* context_info() const { return context_info_; }
ContextInfo* context_info() const { return context_info_.get(); }
virtual int Initialize();
@ -43,7 +45,7 @@ class Frontend {
protected:
runtime::Runtime* runtime_;
ContextInfo* context_info_;
std::unique_ptr<ContextInfo> context_info_;
};
} // namespace frontend

View File

@ -46,10 +46,10 @@ void CleanupOnShutdown() {}
PPCFrontend::PPCFrontend(Runtime* runtime) : Frontend(runtime) {
InitializeIfNeeded();
ContextInfo* info =
new ContextInfo(sizeof(PPCContext), offsetof(PPCContext, thread_state));
std::unique_ptr<ContextInfo> context_info(
new ContextInfo(sizeof(PPCContext), offsetof(PPCContext, thread_state)));
// Add fields/etc.
context_info_ = info;
context_info_ = std::move(context_info);
}
PPCFrontend::~PPCFrontend() {

View File

@ -43,23 +43,24 @@ PPCTranslator::PPCTranslator(PPCFrontend* frontend) : frontend_(frontend) {
bool validate = FLAGS_validate_hir;
// Build the CFG first.
compiler_->AddPass(new passes::ControlFlowAnalysisPass());
compiler_->AddPass(std::make_unique<passes::ControlFlowAnalysisPass>());
// Passes are executed in the order they are added. Multiple of the same
// pass type may be used.
if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(new passes::ContextPromotionPass());
if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(new passes::SimplificationPass());
if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(new passes::ConstantPropagationPass());
if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(new passes::SimplificationPass());
if (validate) compiler_->AddPass(new passes::ValidationPass());
// compiler_->AddPass(new passes::DeadStoreEliminationPass());
// if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(new passes::DeadCodeEliminationPass());
if (validate) compiler_->AddPass(new passes::ValidationPass());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
compiler_->AddPass(std::make_unique<passes::ContextPromotionPass>());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
compiler_->AddPass(std::make_unique<passes::SimplificationPass>());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
compiler_->AddPass(std::make_unique<passes::ConstantPropagationPass>());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
compiler_->AddPass(std::make_unique<passes::SimplificationPass>());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
// compiler_->AddPass(std::make_unique<passes::DeadStoreEliminationPass>());
// if (validate)
// compiler_->AddPass(std::make_unique<passes::ValidationPass>());
compiler_->AddPass(std::make_unique<passes::DeadCodeEliminationPass>());
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
//// Removes all unneeded variables. Try not to add new ones after this.
// compiler_->AddPass(new passes::ValueReductionPass());
@ -69,12 +70,12 @@ PPCTranslator::PPCTranslator(PPCFrontend* frontend) : frontend_(frontend) {
// Will modify the HIR to add loads/stores.
// This should be the last pass before finalization, as after this all
// registers are assigned and ready to be emitted.
compiler_->AddPass(
new passes::RegisterAllocationPass(backend->machine_info()));
if (validate) compiler_->AddPass(new passes::ValidationPass());
compiler_->AddPass(std::make_unique<passes::RegisterAllocationPass>(
backend->machine_info()));
if (validate) compiler_->AddPass(std::make_unique<passes::ValidationPass>());
// Must come last. The HIR is not really HIR after this.
compiler_->AddPass(new passes::FinalizationPass());
compiler_->AddPass(std::make_unique<passes::FinalizationPass>());
}
PPCTranslator::~PPCTranslator() {

View File

@ -107,12 +107,10 @@ class Debugger {
Runtime* runtime_;
std::mutex threads_lock_;
typedef std::unordered_map<uint32_t, ThreadState*> ThreadMap;
ThreadMap threads_;
std::unordered_map<uint32_t, ThreadState*> threads_;
std::mutex breakpoints_lock_;
typedef std::multimap<uint64_t, Breakpoint*> BreakpointMultimap;
BreakpointMultimap breakpoints_;
std::multimap<uint64_t, Breakpoint*> breakpoints_;
};
} // namespace runtime

View File

@ -16,7 +16,7 @@ EntryTable::EntryTable() = default;
EntryTable::~EntryTable() {
std::lock_guard<std::mutex> guard(lock_);
EntryMap::iterator it = map_.begin();
auto& it = map_.begin();
for (; it != map_.end(); ++it) {
Entry* entry = it->second;
delete entry;
@ -25,7 +25,7 @@ EntryTable::~EntryTable() {
Entry* EntryTable::Get(uint64_t address) {
std::lock_guard<std::mutex> guard(lock_);
EntryMap::const_iterator it = map_.find(address);
const auto& it = map_.find(address);
Entry* entry = it != map_.end() ? it->second : nullptr;
if (entry) {
// TODO(benvanik): wait if needed?
@ -38,7 +38,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);
const auto& it = map_.find(address);
Entry* entry = it != map_.end() ? it->second : nullptr;
Entry::Status status;
if (entry) {

View File

@ -47,8 +47,7 @@ class EntryTable {
private:
// TODO(benvanik): replace with a better data structure.
std::mutex lock_;
typedef std::unordered_map<uint64_t, Entry*> EntryMap;
EntryMap map_;
std::unordered_map<uint64_t, Entry*> map_;
};
} // namespace runtime

View File

@ -20,20 +20,13 @@ namespace runtime {
Module::Module(Runtime* runtime)
: runtime_(runtime), memory_(runtime->memory()) {}
Module::~Module() {
std::lock_guard<std::mutex> guard(lock_);
SymbolMap::iterator it = map_.begin();
for (; it != map_.end(); ++it) {
SymbolInfo* symbol_info = it->second;
delete symbol_info;
}
}
Module::~Module() = default;
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);
const auto it = map_.find(address);
SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr;
if (symbol_info) {
if (symbol_info->status() == SymbolInfo::STATUS_DECLARING) {
@ -60,7 +53,7 @@ SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type,
SymbolInfo** out_symbol_info) {
*out_symbol_info = nullptr;
lock_.lock();
SymbolMap::const_iterator it = map_.find(address);
auto it = map_.find(address);
SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr;
SymbolInfo::Status status;
if (symbol_info) {
@ -91,7 +84,7 @@ SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type,
break;
}
map_[address] = symbol_info;
list_.push_back(symbol_info);
list_.emplace_back(symbol_info);
status = SymbolInfo::STATUS_NEW;
}
lock_.unlock();
@ -157,10 +150,9 @@ SymbolInfo::Status Module::DefineVariable(VariableInfo* symbol_info) {
void Module::ForEachFunction(std::function<void(FunctionInfo*)> callback) {
SCOPE_profile_cpu_f("alloy");
std::lock_guard<std::mutex> guard(lock_);
for (auto it = list_.begin(); it != list_.end(); ++it) {
SymbolInfo* symbol_info = *it;
for (auto& symbol_info : list_) {
if (symbol_info->type() == SymbolInfo::TYPE_FUNCTION) {
FunctionInfo* info = (FunctionInfo*)symbol_info;
FunctionInfo* info = static_cast<FunctionInfo*>(symbol_info.get());
callback(info);
}
}
@ -173,9 +165,9 @@ void Module::ForEachFunction(size_t since, size_t& version,
size_t count = list_.size();
version = count;
for (size_t n = since; n < count; n++) {
SymbolInfo* symbol_info = list_[n];
auto& symbol_info = list_[n];
if (symbol_info->type() == SymbolInfo::TYPE_FUNCTION) {
FunctionInfo* info = (FunctionInfo*)symbol_info;
FunctionInfo* info = static_cast<FunctionInfo*>(symbol_info.get());
callback(info);
}
}

View File

@ -63,10 +63,8 @@ class Module {
private:
// TODO(benvanik): replace with a better data structure.
std::mutex lock_;
typedef std::unordered_map<uint64_t, SymbolInfo*> SymbolMap;
SymbolMap map_;
typedef std::vector<SymbolInfo*> SymbolList;
SymbolList list_;
std::unordered_map<uint64_t, SymbolInfo*> map_;
std::vector<std::unique_ptr<SymbolInfo>> list_;
};
} // namespace runtime

View File

@ -30,11 +30,7 @@ Runtime::Runtime(Memory* memory) : memory_(memory) {}
Runtime::~Runtime() {
{
std::lock_guard<std::mutex> guard(modules_lock_);
for (ModuleList::iterator it = modules_.begin(); it != modules_.end();
++it) {
Module* module = *it;
delete module;
}
modules_.clear();
}
debugger_.reset();
@ -99,28 +95,28 @@ int Runtime::Initialize(std::unique_ptr<Frontend> frontend,
return 0;
}
int Runtime::AddModule(Module* module) {
int Runtime::AddModule(std::unique_ptr<Module> module) {
std::lock_guard<std::mutex> guard(modules_lock_);
modules_.push_back(module);
modules_.push_back(std::move(module));
return 0;
}
Module* Runtime::GetModule(const char* name) {
std::lock_guard<std::mutex> guard(modules_lock_);
Module* result = NULL;
for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) {
Module* module = *it;
for (const auto& module : modules_) {
if (module->name() == name) {
result = module;
break;
return module.get();
}
}
return result;
return nullptr;
}
Runtime::ModuleList Runtime::GetModules() {
std::vector<Module*> Runtime::GetModules() {
std::lock_guard<std::mutex> guard(modules_lock_);
ModuleList clone = modules_;
std::vector<Module*> clone(modules_.size());
for (const auto& module : modules_) {
clone.push_back(module.get());
}
return clone;
}
@ -176,11 +172,9 @@ int Runtime::LookupFunctionInfo(uint64_t address,
std::lock_guard<std::mutex> guard(modules_lock_);
// TODO(benvanik): sort by code address (if contiguous) so can bsearch.
// TODO(benvanik): cache last module low/high, as likely to be in there.
for (ModuleList::const_iterator it = modules_.begin(); it != modules_.end();
++it) {
Module* module = *it;
for (const auto& module : modules_) {
if (module->ContainsAddress(address)) {
code_module = module;
code_module = module.get();
break;
}
}

View File

@ -28,9 +28,6 @@ namespace alloy {
namespace runtime {
class Runtime {
public:
typedef std::vector<Module*> ModuleList;
public:
explicit Runtime(Memory* memory);
virtual ~Runtime();
@ -43,9 +40,10 @@ class Runtime {
int Initialize(std::unique_ptr<frontend::Frontend> frontend,
std::unique_ptr<backend::Backend> backend = 0);
int AddModule(Module* module);
int AddModule(std::unique_ptr<Module> module);
Module* GetModule(const char* name);
ModuleList GetModules();
Module* GetModule(const std::string& name) { return GetModule(name.c_str()); }
std::vector<Module*> GetModules();
std::vector<Function*> FindFunctionsWithAddress(uint64_t address);
@ -69,7 +67,7 @@ class Runtime {
EntryTable entry_table_;
std::mutex modules_lock_;
ModuleList modules_;
std::vector<std::unique_ptr<Module>> modules_;
};
} // namespace runtime

View File

@ -69,7 +69,7 @@ class FunctionInfo : public SymbolInfo {
public:
FunctionInfo(Module* module, uint64_t address);
virtual ~FunctionInfo();
~FunctionInfo() override;
bool has_end_address() const { return end_address_ > 0; }
uint64_t end_address() const { return end_address_; }
@ -101,9 +101,7 @@ class FunctionInfo : public SymbolInfo {
class VariableInfo : public SymbolInfo {
public:
VariableInfo(Module* module, uint64_t address);
virtual ~VariableInfo();
private:
~VariableInfo() override;
};
} // namespace runtime

View File

@ -103,25 +103,26 @@ XECLEANUP:
X_STATUS XUserModule::LoadFromMemory(const void* addr, const size_t length) {
Processor* processor = kernel_state()->processor();
XenonRuntime* runtime = processor->runtime();
XexModule* xex_module = NULL;
// Load the XEX into memory and decrypt.
xe_xex2_options_t xex_options;
xe_zero_struct(&xex_options, sizeof(xex_options));
xex_ = xe_xex2_load(kernel_state()->memory(), addr, length, xex_options);
XEEXPECTNOTNULL(xex_);
if (!xex_) {
return X_STATUS_UNSUCCESSFUL;
}
// Prepare the module for execution.
// Runtime takes ownership.
xex_module = new XexModule(runtime);
XEEXPECTZERO(xex_module->Load(name_, path_, xex_));
XEEXPECTZERO(runtime->AddModule(xex_module));
auto xex_module = std::make_unique<XexModule>(runtime);
if (xex_module->Load(name_, path_, xex_)) {
return X_STATUS_UNSUCCESSFUL;
}
if (runtime->AddModule(std::move(xex_module))) {
return X_STATUS_UNSUCCESSFUL;
}
return X_STATUS_SUCCESS;
XECLEANUP:
delete xex_module;
return X_STATUS_UNSUCCESSFUL;
}
void* XUserModule::GetProcAddressByOrdinal(uint16_t ordinal) {

View File

@ -38,9 +38,9 @@ int alloy_sandbox(int argc, xechar_t** argv) {
// backend.reset(new alloy::backend::x64::X64Backend(runtime));
runtime->Initialize(std::move(backend));
RawModule* module = new RawModule(runtime);
auto module = std::make_unique<RawModule>(runtime);
module->LoadFile(0x82000000, "test\\codegen\\instr_add.bin");
runtime->AddModule(module);
runtime->AddModule(std::move(module));
XenonThreadState* thread_state =
new XenonThreadState(runtime, 100, 64 * 1024, 0);