summaryrefslogtreecommitdiff
path: root/aisa/eval.h
blob: 30dc617e8cde1fefb8f28fe4d7d810c42828b5b3 (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
#pragma once

#include <coroutine>
#include <optional>
#include <utility>
#include <vector>

#include "aisa/aisa.h"
#include "aisa/coroutine.h" // IWYU pragma: export

namespace aisa {

    template<typename CRTP> struct EvalState {
        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> 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<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]);
        }
    };

}