Skip to content

Commit

Permalink
Alloy test shim.
Browse files Browse the repository at this point in the history
  • Loading branch information
benvanik committed Aug 23, 2014
1 parent 389de8b commit 2a9f164
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "third_party/wxWidgets"]
path = third_party/wxWidgets
url = https://github.com/wxWidgets/wxWidgets.git
[submodule "third_party/catch"]
path = third_party/catch
url = https://github.com/philsquared/Catch.git
2 changes: 1 addition & 1 deletion src/alloy/frontend/ppc/ppc_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#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>
#include <alloy/frontend/ppc/ppc_hir_builder.h>
#include <alloy/frontend/ppc/ppc_instr.h>
#include <alloy/frontend/ppc/ppc_scanner.h>
#include <alloy/reset_scope.h>
#include <alloy/runtime/runtime.h>
#include <xenia/profiling.h>

Expand Down
8 changes: 4 additions & 4 deletions src/alloy/runtime/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Module {
virtual bool ContainsAddress(uint64_t address);

SymbolInfo* LookupSymbol(uint64_t address, bool wait = true);
SymbolInfo::Status DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info);
SymbolInfo::Status DeclareVariable(uint64_t address,
VariableInfo** out_symbol_info);
virtual SymbolInfo::Status DeclareFunction(uint64_t address,
FunctionInfo** out_symbol_info);
virtual SymbolInfo::Status DeclareVariable(uint64_t address,
VariableInfo** out_symbol_info);

SymbolInfo::Status DefineFunction(FunctionInfo* symbol_info);
SymbolInfo::Status DefineVariable(VariableInfo* symbol_info);
Expand Down
2 changes: 2 additions & 0 deletions src/alloy/runtime/sources.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
'runtime.h',
'symbol_info.cc',
'symbol_info.h',
'test_module.cc',
'test_module.h',
'thread_state.cc',
'thread_state.h',
],
Expand Down
103 changes: 103 additions & 0 deletions src/alloy/runtime/test_module.cc
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions src/alloy/runtime/test_module.h
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions third_party/catch
Submodule catch added at 85d33e
47 changes: 47 additions & 0 deletions tools/alloy-test/alloy-test.cc
Original file line number Diff line number Diff line change
@@ -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);
29 changes: 29 additions & 0 deletions tools/alloy-test/alloy-test.gypi
Original file line number Diff line number Diff line change
@@ -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',
],
},
],
}
Loading

0 comments on commit 2a9f164

Please sign in to comment.