diff --git a/tools/alloy-test/alloy-test.cc b/tools/alloy-test/alloy-test.cc index da558735a..295f8a1bf 100644 --- a/tools/alloy-test/alloy-test.cc +++ b/tools/alloy-test/alloy-test.cc @@ -17,29 +17,35 @@ using namespace alloy::hir; using namespace alloy::runtime; using alloy::frontend::ppc::PPCContext; -TEST_CASE("Meta test", "[test]") { +Value* LoadGPR(hir::HIRBuilder& b, int reg) { + return b.LoadContext(offsetof(PPCContext, r) + reg * 8, INT64_TYPE); +} + +void StoreGPR(hir::HIRBuilder& b, int reg, Value* value) { + b.StoreContext(offsetof(PPCContext, r) + reg * 8, value); +} + +TEST_CASE("ADD", "[instr]") { 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); + auto v = b.Add(LoadGPR(b, 4), LoadGPR(b, 5)); + StoreGPR(b, 3, v); b.Return(); - return true; }); test.Run([](PPCContext* ctx) { - ctx->r[5] = 10; - ctx->r[25] = 25; + ctx->r[4] = 10; + ctx->r[5] = 25; }, [](PPCContext* ctx) { - auto result = ctx->r[11]; + auto result = ctx->r[3]; REQUIRE(result == 0x23); }); test.Run([](PPCContext* ctx) { - ctx->r[5] = 10; - ctx->r[25] = 25; + ctx->r[4] = 10; + ctx->r[5] = 25; }, [](PPCContext* ctx) { - auto result = ctx->r[11]; + auto result = ctx->r[3]; REQUIRE(result == 0x24); }); } diff --git a/tools/alloy-test/test_util.h b/tools/alloy-test/test_util.h index 34f4964b4..c2406a925 100644 --- a/tools/alloy-test/test_util.h +++ b/tools/alloy-test/test_util.h @@ -36,7 +36,8 @@ int main(std::vector& args) { narrow_argv[i] = const_cast(narrow_arg.data()); narrow_args.push_back(std::move(narrow_arg)); } - return Catch::Session().run(int(args.size()), narrow_argv); + int ret = Catch::Session().run(int(args.size()), narrow_argv); + return ret; } class ThreadState : public alloy::runtime::ThreadState { @@ -89,57 +90,73 @@ class ThreadState : public alloy::runtime::ThreadState { class TestFunction { public: - TestFunction(std::function generator) { + TestFunction(std::function generator) { memory_size = 16 * 1024 * 1024; memory.reset(new SimpleMemory(memory_size)); - runtime.reset(new Runtime(memory.get())); - auto frontend = - std::make_unique(runtime.get()); - std::unique_ptr backend; - // backend = - // std::make_unique(runtime.get()); - // backend = - // std::make_unique(runtime.get()); - runtime->Initialize(std::move(frontend), std::move(backend)); + { + auto runtime = std::make_unique(memory.get()); + auto frontend = + std::make_unique(runtime.get()); + auto backend = + std::make_unique(runtime.get()); + runtime->Initialize(std::move(frontend), std::move(backend)); + runtimes.emplace_back(std::move(runtime)); + } + { + auto runtime = std::make_unique(memory.get()); + auto frontend = + std::make_unique(runtime.get()); + auto backend = + std::make_unique(runtime.get()); + runtime->Initialize(std::move(frontend), std::move(backend)); + runtimes.emplace_back(std::move(runtime)); + } - auto module = std::make_unique( - 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); + for (auto& runtime : runtimes) { + auto module = std::make_unique( + runtime.get(), "Test", + [](uint64_t address) { return address == 0x1000; }, + [generator](hir::HIRBuilder& b) { + generator(b); + return true; + }); + runtime->AddModule(std::move(module)); + } } ~TestFunction() { - runtime.reset(); + runtimes.clear(); memory.reset(); } void Run(std::function pre_call, std::function post_call) { - memory->Zero(0, memory_size); + for (auto& runtime : runtimes) { + 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( - runtime.get(), 100, stack_address, stack_size, thread_state_address); - auto ctx = thread_state->context(); - ctx->lr = 0xBEBEBEBE; + alloy::runtime::Function* fn; + runtime->ResolveFunction(0x1000, &fn); - pre_call(ctx); + 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( + runtime.get(), 100, stack_address, stack_size, thread_state_address); + auto ctx = thread_state->context(); + ctx->lr = 0xBEBEBEBE; - fn->Call(thread_state.get(), ctx->lr); + pre_call(ctx); - post_call(ctx); + fn->Call(thread_state.get(), ctx->lr); + + post_call(ctx); + } } size_t memory_size; std::unique_ptr memory; - std::unique_ptr runtime; - alloy::runtime::Function* fn; + std::vector> runtimes; }; } // namespace test