From 968414044e87be7399f73a01b718b4894bb65e01 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sat, 25 Jun 2022 18:56:44 -0700 Subject: Move EvalState and the eval coroutines to a separate header. --- aisa/aisa.h | 38 -------------------------------------- aisa/eval.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 3 ++- 3 files changed, 47 insertions(+), 39 deletions(-) create mode 100644 aisa/eval.h diff --git a/aisa/aisa.h b/aisa/aisa.h index 123b12a..c9a215c 100644 --- a/aisa/aisa.h +++ b/aisa/aisa.h @@ -15,28 +15,6 @@ namespace aisa { using regnum_t = std::uint_fast64_t; using regval_t = std::uint64_t; - template struct EvalState { - CRTP & crtp() noexcept { return static_cast(*this); } - - task async_load_reg(regnum_t rn) - { - while (true) { - if (auto rv = crtp().load_reg(rn); rv.has_value()) - co_return *rv; - co_await suspend(); - } - } - - task async_store_reg(regnum_t rn, regval_t rv) - { - while (true) { - if (crtp().store_reg(rn, rv)) - co_return; - co_await suspend(); - } - } - }; - struct Step { std::optional> predicate; std::vector source_regs; @@ -57,22 +35,6 @@ namespace aisa { } virtual std::vector compute_destinations(const std::vector &source_vals) const = 0; - - template task eval(State &state) const - { - if (predicate.has_value()) { - regval_t pval = co_await state.async_load_reg(predicate->first); - if (pval != predicate->second) - co_return; - } - std::vector source_vals; - source_vals.reserve(source_regs.size()); - for (unsigned int i = 0; i < source_regs.size(); ++i) - source_vals.emplace_back(co_await state.async_load_reg(source_regs[i])); - auto destination_vals = compute_destinations(source_vals); - for (unsigned int i = 0; i < destination_regs.size(); ++i) - co_await state.async_store_reg(destination_regs[i], destination_vals[i]); - } }; } diff --git a/aisa/eval.h b/aisa/eval.h new file mode 100644 index 0000000..f97740c --- /dev/null +++ b/aisa/eval.h @@ -0,0 +1,45 @@ +#pragma once + +#include "aisa/aisa.h" + +namespace aisa { + + template struct EvalState { + CRTP & crtp() noexcept { return static_cast(*this); } + + task async_load_reg(regnum_t rn) + { + while (true) { + if (auto rv = crtp().load_reg(rn); rv.has_value()) + co_return *rv; + co_await suspend(); + } + } + + task async_store_reg(regnum_t rn, regval_t rv) + { + while (true) { + if (crtp().store_reg(rn, rv)) + co_return; + co_await suspend(); + } + } + + task eval(const Step &step) + { + if (step.predicate.has_value()) { + regval_t pval = co_await async_load_reg(step.predicate->first); + if (pval != step.predicate->second) + co_return; + } + std::vector source_vals; + source_vals.reserve(step.source_regs.size()); + for (unsigned int i = 0; i < step.source_regs.size(); ++i) + source_vals.emplace_back(co_await async_load_reg(step.source_regs[i])); + auto destination_vals = step.compute_destinations(source_vals); + for (unsigned int i = 0; i < step.destination_regs.size(); ++i) + co_await async_store_reg(step.destination_regs[i], destination_vals[i]); + } + }; + +} diff --git a/main.cpp b/main.cpp index 25bd99b..6c0705c 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include "aisa/aisa.h" +#include "aisa/eval.h" #include "git-tag.h" int main(int argc, const char *argv[]) @@ -63,7 +64,7 @@ int main(int argc, const char *argv[]) std::cout << "\n\n\n"; - auto w = step.eval(state); + auto w = state.eval(step); std::cout << "run\n"; w(); std::cout << "run\n"; w(); std::cout << "run\n"; w(); -- cgit v1.2.3