blob: a301e9369ef6e8827e2f9351402e8759217ec581 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#pragma once
#include <coroutine>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "aisa/aisa.h"
#include "aisa/coroutine.h" // IWYU pragma: export
namespace aisa {
template<typename CRTP> struct EvalState {
struct EvalContext {
task<void> coroutine;
bool resume() { return coroutine(); }
};
CRTP & crtp() noexcept { return static_cast<CRTP &>(*this); }
task<regval_t> async_load_reg(regnum_t rn)
{
while (true) {
if (auto rv = crtp().load_reg(rn); rv.has_value())
co_return *rv;
co_await std::suspend_always{};
}
}
task<void> async_store_reg(regnum_t rn, regval_t rv)
{
while (true) {
if (crtp().store_reg(rn, rv))
co_return;
co_await std::suspend_always{};
}
}
task<void> async_evaluate(EvalContext &contex, 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<regval_t> 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]);
}
std::unique_ptr<EvalContext> operator()(const Step &step)
{
auto context = std::make_unique<EvalContext>();
context->coroutine = async_evaluate(*context, step);
return context;
}
};
}
|