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/eval.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 aisa/eval.h (limited to 'aisa/eval.h') 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]); + } + }; + +} -- cgit v1.2.3