Alloy test shim.

This commit is contained in:
Ben Vanik 2014-08-23 14:53:36 -07:00
parent 389de8b107
commit 2a9f164f8e
11 changed files with 391 additions and 5 deletions

3
.gitmodules vendored
View File

@ -19,3 +19,6 @@
[submodule "third_party/wxWidgets"] [submodule "third_party/wxWidgets"]
path = third_party/wxWidgets path = third_party/wxWidgets
url = https://github.com/wxWidgets/wxWidgets.git url = https://github.com/wxWidgets/wxWidgets.git
[submodule "third_party/catch"]
path = third_party/catch
url = https://github.com/philsquared/Catch.git

View File

@ -10,13 +10,13 @@
#include <alloy/frontend/ppc/ppc_translator.h> #include <alloy/frontend/ppc/ppc_translator.h>
#include <alloy/alloy-private.h> #include <alloy/alloy-private.h>
#include <alloy/reset_scope.h>
#include <alloy/compiler/compiler_passes.h> #include <alloy/compiler/compiler_passes.h>
#include <alloy/frontend/ppc/ppc_disasm.h> #include <alloy/frontend/ppc/ppc_disasm.h>
#include <alloy/frontend/ppc/ppc_frontend.h> #include <alloy/frontend/ppc/ppc_frontend.h>
#include <alloy/frontend/ppc/ppc_hir_builder.h> #include <alloy/frontend/ppc/ppc_hir_builder.h>
#include <alloy/frontend/ppc/ppc_instr.h> #include <alloy/frontend/ppc/ppc_instr.h>
#include <alloy/frontend/ppc/ppc_scanner.h> #include <alloy/frontend/ppc/ppc_scanner.h>
#include <alloy/reset_scope.h>
#include <alloy/runtime/runtime.h> #include <alloy/runtime/runtime.h>
#include <xenia/profiling.h> #include <xenia/profiling.h>

View File

@ -37,9 +37,9 @@ class Module {
virtual bool ContainsAddress(uint64_t address); virtual bool ContainsAddress(uint64_t address);
SymbolInfo* LookupSymbol(uint64_t address, bool wait = true); SymbolInfo* LookupSymbol(uint64_t address, bool wait = true);
SymbolInfo::Status DeclareFunction(uint64_t address, virtual SymbolInfo::Status DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info); FunctionInfo** out_symbol_info);
SymbolInfo::Status DeclareVariable(uint64_t address, virtual SymbolInfo::Status DeclareVariable(uint64_t address,
VariableInfo** out_symbol_info); VariableInfo** out_symbol_info);
SymbolInfo::Status DefineFunction(FunctionInfo* symbol_info); SymbolInfo::Status DefineFunction(FunctionInfo* symbol_info);

View File

@ -19,6 +19,8 @@
'runtime.h', 'runtime.h',
'symbol_info.cc', 'symbol_info.cc',
'symbol_info.h', 'symbol_info.h',
'test_module.cc',
'test_module.h',
'thread_state.cc', 'thread_state.cc',
'thread_state.h', 'thread_state.h',
], ],

View File

@ -0,0 +1,103 @@
/**
******************************************************************************
* 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 <alloy/runtime/test_module.h>
#include <alloy/compiler/compiler_passes.h>
#include <alloy/reset_scope.h>
#include <alloy/runtime/runtime.h>
#include <poly/platform.h>
#include <poly/string.h>
namespace alloy {
namespace runtime {
using alloy::backend::Backend;
using alloy::compiler::Compiler;
using alloy::hir::HIRBuilder;
using alloy::runtime::Function;
using alloy::runtime::FunctionInfo;
namespace passes = alloy::compiler::passes;
TestModule::TestModule(Runtime* runtime, const std::string& name,
std::function<bool(uint64_t)> contains_address,
std::function<bool(hir::HIRBuilder&)> generate)
: Module(runtime),
name_(name),
contains_address_(contains_address),
generate_(generate) {
builder_.reset(new HIRBuilder());
compiler_.reset(new Compiler(runtime));
assembler_ = std::move(runtime->backend()->CreateAssembler());
assembler_->Initialize();
// Merge blocks early. This will let us use more context in other passes.
// The CFG is required for simplification and dirtied by it.
compiler_->AddPass(std::make_unique<passes::ControlFlowAnalysisPass>());
compiler_->AddPass(std::make_unique<passes::ControlFlowSimplificationPass>());
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.
compiler_->AddPass(std::make_unique<passes::ContextPromotionPass>());
compiler_->AddPass(std::make_unique<passes::SimplificationPass>());
compiler_->AddPass(std::make_unique<passes::ConstantPropagationPass>());
compiler_->AddPass(std::make_unique<passes::SimplificationPass>());
// compiler_->AddPass(std::make_unique<passes::DeadStoreEliminationPass>());
compiler_->AddPass(std::make_unique<passes::DeadCodeEliminationPass>());
//// Removes all unneeded variables. Try not to add new ones after this.
// compiler_->AddPass(new passes::ValueReductionPass());
// Register allocation for the target backend.
// 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(std::make_unique<passes::RegisterAllocationPass>(
runtime->backend()->machine_info()));
// Must come last. The HIR is not really HIR after this.
compiler_->AddPass(std::make_unique<passes::FinalizationPass>());
}
TestModule::~TestModule() = default;
bool TestModule::ContainsAddress(uint64_t address) {
return contains_address_(address);
}
SymbolInfo::Status TestModule::DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info) {
SymbolInfo::Status status = Module::DeclareFunction(address, out_symbol_info);
if (status == SymbolInfo::STATUS_NEW) {
auto symbol_info = *out_symbol_info;
// Reset() all caching when we leave.
make_reset_scope(compiler_);
make_reset_scope(assembler_);
if (!generate_(*builder_.get())) {
symbol_info->set_status(SymbolInfo::STATUS_FAILED);
return SymbolInfo::STATUS_FAILED;
}
compiler_->Compile(builder_.get());
Function* fn = nullptr;
assembler_->Assemble(symbol_info, builder_.get(), 0, nullptr, 0, &fn);
symbol_info->set_function(fn);
status = SymbolInfo::STATUS_DEFINED;
symbol_info->set_status(status);
}
return status;
}
} // namespace runtime
} // namespace alloy

View File

@ -0,0 +1,52 @@
/**
******************************************************************************
* 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_RUNTIME_TEST_MODULE_H_
#define ALLOY_RUNTIME_TEST_MODULE_H_
#include <functional>
#include <memory>
#include <string>
#include <alloy/backend/assembler.h>
#include <alloy/compiler/compiler.h>
#include <alloy/hir/hir_builder.h>
#include <alloy/runtime/module.h>
namespace alloy {
namespace runtime {
class TestModule : public Module {
public:
TestModule(Runtime* runtime, const std::string& name,
std::function<bool(uint64_t)> contains_address,
std::function<bool(hir::HIRBuilder&)> generate);
~TestModule() override;
const std::string& name() const override { return name_; }
bool ContainsAddress(uint64_t address) override;
SymbolInfo::Status DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info) override;
private:
std::string name_;
std::function<bool(uint64_t)> contains_address_;
std::function<bool(hir::HIRBuilder&)> generate_;
std::unique_ptr<hir::HIRBuilder> builder_;
std::unique_ptr<compiler::Compiler> compiler_;
std::unique_ptr<backend::Assembler> assembler_;
};
} // namespace runtime
} // namespace alloy
#endif // ALLOY_RUNTIME_TEST_MODULE_H_

1
third_party/catch vendored Submodule

@ -0,0 +1 @@
Subproject commit 85d33e2cbd5354c36000424f823fc87707a24c3c

View File

@ -0,0 +1,47 @@
/**
******************************************************************************
* 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. *
******************************************************************************
*/
#define CATCH_CONFIG_RUNNER
#include <third_party/catch/single_include/catch.hpp>
#include <tools/alloy-test/test_util.h>
using namespace alloy;
using namespace alloy::hir;
using namespace alloy::runtime;
using alloy::frontend::ppc::PPCContext;
TEST_CASE("Meta test", "[test]") {
alloy::test::TestFunction test([](hir::HIRBuilder& b) {
auto r = b.Add(b.LoadContext(offsetof(PPCContext, r) + 5 * 8, INT64_TYPE),
b.LoadContext(offsetof(PPCContext, r) + 25 * 8, INT64_TYPE));
b.StoreContext(offsetof(PPCContext, r) + 11 * 8, r);
b.Return();
return true;
});
test.Run([](PPCContext* ctx) {
ctx->r[5] = 10;
ctx->r[25] = 25;
},
[](PPCContext* ctx) {
auto result = ctx->r[11];
REQUIRE(result == 0x23);
});
test.Run([](PPCContext* ctx) {
ctx->r[5] = 10;
ctx->r[25] = 25;
},
[](PPCContext* ctx) {
auto result = ctx->r[11];
REQUIRE(result == 0x24);
});
}
DEFINE_ENTRY_POINT(L"alloy-test", L"?", alloy::test::main);

View File

@ -0,0 +1,29 @@
# Copyright 2014 Ben Vanik. All Rights Reserved.
{
'targets': [
{
'target_name': 'alloy-test',
'type': 'executable',
'msvs_settings': {
'VCLinkerTool': {
'SubSystem': '1'
},
},
'dependencies': [
'alloy',
'xenia',
],
'include_dirs': [
'.',
],
'sources': [
'alloy-test.cc',
'test_util.h',
],
},
],
}

View File

@ -0,0 +1,148 @@
/**
******************************************************************************
* 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_TEST_TEST_UTIL_H_
#define ALLOY_TEST_TEST_UTIL_H_
#include <alloy/alloy.h>
#include <alloy/backend/ivm/ivm_backend.h>
#include <alloy/backend/x64/x64_backend.h>
#include <alloy/frontend/ppc/ppc_context.h>
#include <alloy/frontend/ppc/ppc_frontend.h>
#include <alloy/hir/hir_builder.h>
#include <alloy/runtime/test_module.h>
#include <poly/main.h>
#include <poly/poly.h>
#include <third_party/catch/single_include/catch.hpp>
namespace alloy {
namespace test {
using alloy::frontend::ppc::PPCContext;
using alloy::runtime::Runtime;
int main(std::vector<std::wstring>& args) {
std::vector<std::string> narrow_args;
auto narrow_argv = new char* [args.size()];
for (size_t i = 0; i < args.size(); ++i) {
auto narrow_arg = poly::to_string(args[i]);
narrow_argv[i] = const_cast<char*>(narrow_arg.data());
narrow_args.push_back(std::move(narrow_arg));
}
return Catch::Session().run(int(args.size()), narrow_argv);
}
class ThreadState : public alloy::runtime::ThreadState {
public:
ThreadState(Runtime* runtime, uint32_t thread_id, uint64_t stack_address,
size_t stack_size, uint64_t thread_state_address)
: alloy::runtime::ThreadState(runtime, thread_id),
stack_address_(stack_address),
stack_size_(stack_size),
thread_state_address_(thread_state_address) {
memset(memory_->Translate(stack_address_), 0, stack_size_);
// Allocate with 64b alignment.
context_ = (PPCContext*)calloc(1, sizeof(PPCContext));
assert_true((reinterpret_cast<uint64_t>(context_) & 0xF) == 0);
// Stash pointers to common structures that callbacks may need.
context_->reserve_address = memory_->reserve_address();
context_->membase = memory_->membase();
context_->runtime = runtime;
context_->thread_state = this;
// Set initial registers.
context_->r[1] = stack_address_ + stack_size;
context_->r[13] = thread_state_address_;
// Pad out stack a bit, as some games seem to overwrite the caller by about
// 16 to 32b.
context_->r[1] -= 64;
raw_context_ = context_;
runtime_->debugger()->OnThreadCreated(this);
}
~ThreadState() override {
runtime_->debugger()->OnThreadDestroyed(this);
free(context_);
}
PPCContext* context() const { return context_; }
private:
uint64_t stack_address_;
size_t stack_size_;
uint64_t thread_state_address_;
// NOTE: must be 64b aligned for SSE ops.
PPCContext* context_;
};
class TestFunction {
public:
TestFunction(std::function<bool(hir::HIRBuilder& b)> generator) {
memory_size = 16 * 1024 * 1024;
memory.reset(new SimpleMemory(memory_size));
runtime.reset(new Runtime(memory.get()));
auto frontend =
std::make_unique<alloy::frontend::ppc::PPCFrontend>(runtime.get());
std::unique_ptr<alloy::backend::Backend> backend;
// backend =
// std::make_unique<alloy::backend::ivm::IVMBackend>(runtime.get());
// backend =
// std::make_unique<alloy::backend::x64::X64Backend>(runtime.get());
runtime->Initialize(std::move(frontend), std::move(backend));
auto module = std::make_unique<alloy::runtime::TestModule>(
runtime.get(), "Test",
[](uint64_t address) { return address == 0x1000; },
[generator](hir::HIRBuilder& b) { return generator(b); });
runtime->AddModule(std::move(module));
runtime->ResolveFunction(0x1000, &fn);
}
~TestFunction() {
runtime.reset();
memory.reset();
}
void Run(std::function<void(PPCContext*)> pre_call,
std::function<void(PPCContext*)> post_call) {
memory->Zero(0, memory_size);
uint64_t stack_size = 64 * 1024;
uint64_t stack_address = memory_size - stack_size;
uint64_t thread_state_address = stack_address - 0x1000;
auto thread_state = std::make_unique<ThreadState>(
runtime.get(), 100, stack_address, stack_size, thread_state_address);
auto ctx = thread_state->context();
ctx->lr = 0xBEBEBEBE;
pre_call(ctx);
fn->Call(thread_state.get(), ctx->lr);
post_call(ctx);
}
size_t memory_size;
std::unique_ptr<Memory> memory;
std::unique_ptr<Runtime> runtime;
alloy::runtime::Function* fn;
};
} // namespace test
} // namespace alloy
#endif // ALLOY_TEST_TEST_UTIL_H_

View File

@ -2,6 +2,7 @@
{ {
'includes': [ 'includes': [
'alloy-sandbox/alloy-sandbox.gypi', 'alloy-sandbox/alloy-sandbox.gypi',
'alloy-test/alloy-test.gypi',
'xenia-compare/xenia-compare.gypi', 'xenia-compare/xenia-compare.gypi',
'xenia-debug/xenia-debug.gypi', 'xenia-debug/xenia-debug.gypi',
'xenia-run/xenia-run.gypi', 'xenia-run/xenia-run.gypi',