diff options
| author | Julian Blake Kongslie | 2022-06-25 18:56:44 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2022-06-25 18:56:44 -0700 |
| commit | 968414044e87be7399f73a01b718b4894bb65e01 (patch) | |
| tree | b4ce807d1645d68589028213d9b08d0b496c1b85 | |
| parent | Testing some actual support for destinations and custom steps. (diff) | |
| download | issim-968414044e87be7399f73a01b718b4894bb65e01.tar.xz | |
Move EvalState and the eval coroutines to a separate header.
| -rw-r--r-- | aisa/aisa.h | 38 | ||||
| -rw-r--r-- | aisa/eval.h | 45 | ||||
| -rw-r--r-- | main.cpp | 3 |
3 files changed, 47 insertions, 39 deletions
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 { | |||
| 15 | using regnum_t = std::uint_fast64_t; | 15 | using regnum_t = std::uint_fast64_t; |
| 16 | using regval_t = std::uint64_t; | 16 | using regval_t = std::uint64_t; |
| 17 | 17 | ||
| 18 | template<typename CRTP> struct EvalState { | ||
| 19 | CRTP & crtp() noexcept { return static_cast<CRTP &>(*this); } | ||
| 20 | |||
| 21 | task<regval_t> async_load_reg(regnum_t rn) | ||
| 22 | { | ||
| 23 | while (true) { | ||
| 24 | if (auto rv = crtp().load_reg(rn); rv.has_value()) | ||
| 25 | co_return *rv; | ||
| 26 | co_await suspend(); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | task<void> async_store_reg(regnum_t rn, regval_t rv) | ||
| 31 | { | ||
| 32 | while (true) { | ||
| 33 | if (crtp().store_reg(rn, rv)) | ||
| 34 | co_return; | ||
| 35 | co_await suspend(); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct Step { | 18 | struct Step { |
| 41 | std::optional<std::pair<regnum_t, regval_t>> predicate; | 19 | std::optional<std::pair<regnum_t, regval_t>> predicate; |
| 42 | std::vector<regnum_t> source_regs; | 20 | std::vector<regnum_t> source_regs; |
| @@ -57,22 +35,6 @@ namespace aisa { | |||
| 57 | } | 35 | } |
| 58 | 36 | ||
| 59 | virtual std::vector<regval_t> compute_destinations(const std::vector<regval_t> &source_vals) const = 0; | 37 | virtual std::vector<regval_t> compute_destinations(const std::vector<regval_t> &source_vals) const = 0; |
| 60 | |||
| 61 | template<typename State> task<void> eval(State &state) const | ||
| 62 | { | ||
| 63 | if (predicate.has_value()) { | ||
| 64 | regval_t pval = co_await state.async_load_reg(predicate->first); | ||
| 65 | if (pval != predicate->second) | ||
| 66 | co_return; | ||
| 67 | } | ||
| 68 | std::vector<regval_t> source_vals; | ||
| 69 | source_vals.reserve(source_regs.size()); | ||
| 70 | for (unsigned int i = 0; i < source_regs.size(); ++i) | ||
| 71 | source_vals.emplace_back(co_await state.async_load_reg(source_regs[i])); | ||
| 72 | auto destination_vals = compute_destinations(source_vals); | ||
| 73 | for (unsigned int i = 0; i < destination_regs.size(); ++i) | ||
| 74 | co_await state.async_store_reg(destination_regs[i], destination_vals[i]); | ||
| 75 | } | ||
| 76 | }; | 38 | }; |
| 77 | 39 | ||
| 78 | } | 40 | } |
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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "aisa/aisa.h" | ||
| 4 | |||
| 5 | namespace aisa { | ||
| 6 | |||
| 7 | template<typename CRTP> struct EvalState { | ||
| 8 | CRTP & crtp() noexcept { return static_cast<CRTP &>(*this); } | ||
| 9 | |||
| 10 | task<regval_t> async_load_reg(regnum_t rn) | ||
| 11 | { | ||
| 12 | while (true) { | ||
| 13 | if (auto rv = crtp().load_reg(rn); rv.has_value()) | ||
| 14 | co_return *rv; | ||
| 15 | co_await suspend(); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | task<void> async_store_reg(regnum_t rn, regval_t rv) | ||
| 20 | { | ||
| 21 | while (true) { | ||
| 22 | if (crtp().store_reg(rn, rv)) | ||
| 23 | co_return; | ||
| 24 | co_await suspend(); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | task<void> eval(const Step &step) | ||
| 29 | { | ||
| 30 | if (step.predicate.has_value()) { | ||
| 31 | regval_t pval = co_await async_load_reg(step.predicate->first); | ||
| 32 | if (pval != step.predicate->second) | ||
| 33 | co_return; | ||
| 34 | } | ||
| 35 | std::vector<regval_t> source_vals; | ||
| 36 | source_vals.reserve(step.source_regs.size()); | ||
| 37 | for (unsigned int i = 0; i < step.source_regs.size(); ++i) | ||
| 38 | source_vals.emplace_back(co_await async_load_reg(step.source_regs[i])); | ||
| 39 | auto destination_vals = step.compute_destinations(source_vals); | ||
| 40 | for (unsigned int i = 0; i < step.destination_regs.size(); ++i) | ||
| 41 | co_await async_store_reg(step.destination_regs[i], destination_vals[i]); | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | } | ||
| @@ -3,6 +3,7 @@ | |||
| 3 | #include <optional> | 3 | #include <optional> |
| 4 | 4 | ||
| 5 | #include "aisa/aisa.h" | 5 | #include "aisa/aisa.h" |
| 6 | #include "aisa/eval.h" | ||
| 6 | #include "git-tag.h" | 7 | #include "git-tag.h" |
| 7 | 8 | ||
| 8 | int main(int argc, const char *argv[]) | 9 | int main(int argc, const char *argv[]) |
| @@ -63,7 +64,7 @@ int main(int argc, const char *argv[]) | |||
| 63 | 64 | ||
| 64 | std::cout << "\n\n\n"; | 65 | std::cout << "\n\n\n"; |
| 65 | 66 | ||
| 66 | auto w = step.eval(state); | 67 | auto w = state.eval(step); |
| 67 | std::cout << "run\n"; w(); | 68 | std::cout << "run\n"; w(); |
| 68 | std::cout << "run\n"; w(); | 69 | std::cout << "run\n"; w(); |
| 69 | std::cout << "run\n"; w(); | 70 | std::cout << "run\n"; w(); |
