summaryrefslogtreecommitdiff
path: root/aisa/async.h
blob: 42e99e71f0470e6fb2009762ada516703bbb8b37 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#pragma once

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

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

namespace aisa {

    template<typename CRTP> struct AsyncEval {
        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_fetch_mem(byte_t *bytes, addr_t physical_addr, addr_t size)
        {
            while (true) {
                if (crtp().fetch_mem(bytes, physical_addr, size))
                    co_return;
                co_await std::suspend_always{};
            }
        }

        task<void> async_store_mem(addr_t physical_addr, const byte_t *bytes, addr_t size)
        {
            while (true) {
                if (crtp().store_mem(physical_addr, bytes, size))
                    co_return;
                co_await std::suspend_always{};
            }
        }

        task<bool> async_predicate(const Step &step)
        {
            if (step.predicate.has_value()) {
                regval_t pval = co_await crtp().async_load_reg(step.predicate->first);
                co_return pval == step.predicate->second;
            }
            co_return true;
        }

        task<void> async_load_source_registers(const Step &step, Wires &w)
        {
            w.source_vals.resize(step.source_regs.size());
            for (unsigned int i = 0; i < step.source_regs.size(); ++i)
                w.source_vals[i] = co_await crtp().async_load_reg(step.source_regs[i]);
        }

        task<void> async_load_source_memory(const Step &step, const MemInfo &mi, Wires &w)
        {
            w.memory_val.resize(mi.size);
            co_await crtp().async_fetch_mem(w.memory_val.data(), mi.physical_addr, mi.size);
        }

        task<void> async_load_sources(const Step &step, Wires &w)
        {
            co_await crtp().async_load_source_registers(step, w);

            if (step.mop == MOp::LOAD) {
                auto mi = step.meminfo(w);
                co_await crtp().async_load_source_memory(step, mi, w);
            }
        }

        task<void> async_write_destination_registers(const Step &step, const Wires &w)
        {
            for (unsigned int i = 0; i < step.destination_regs.size(); ++i)
                co_await crtp().async_store_reg(step.destination_regs[i], w.destination_vals[i]);
        }

        task<void> async_write_destination_memory(const Step &step, const MemInfo &mi, const Wires &w)
        {
            co_await crtp().async_store_mem(mi.physical_addr, w.memory_val.data(), mi.size);
        }

        task<void> async_write_destinations(const Step &step, const Wires &w)
        {
            if (w.aborted)
                co_return;

            co_await crtp().async_write_destination_registers(step, w);

            if (step.mop == MOp::STORE) {
                auto mi = step.meminfo(w);
                co_await crtp().async_write_destination_memory(step, mi, w);
            }
        }

        task<void> async_push_task(std::unique_ptr<const Task> &&task)
        {
            while (true) {
                if (crtp().push_task(std::move(task)))
                    co_return;
                co_await std::suspend_always{};
            }
        }

        task<void> async_new_task(std::unique_ptr<const Task> &&task, regval_t environment_val)
        {
            auto rn = task->environment;
            co_await crtp().async_push_task(std::move(task));
            co_await crtp().async_store_reg(rn, environment_val);
        }

        task<Wires> async_run_step(const Step &step)
        {
            Wires w;

            if (!co_await crtp().async_predicate(step))
                co_return std::move(w);

            co_await crtp().async_load_sources(step, w);

            step.evaluate(w);

            co_await crtp().async_write_destinations(step, w);

            if (w.new_task.has_value())
                co_await crtp().async_new_task(std::move(w.new_task->first), w.new_task->second);

            co_return std::move(w);
        }

        task<const Task *> async_top_task()
        {
            while (true) {
                if (auto rv = crtp().top_task(); rv.has_value())
                    co_return &**rv;
                co_await std::suspend_always{};
            }
        }

        task<void> async_pop_task()
        {
            while (true) {
                if (crtp().pop_task())
                    co_return;
                co_await std::suspend_always{};
            }
        }

        task<std::optional<std::unique_ptr<const Step>>> async_fetch_step()
        {
            auto task = co_await crtp().async_top_task();

            auto rn = task->environment;
            auto rv = co_await crtp().async_load_reg(rn);

            auto step = task->step(rv);
            if (step.has_value()) {
                co_await crtp().async_store_reg(rn, step->second);
                co_return std::move(step->first);
            } else {
                co_await crtp().async_pop_task();
                co_return {};
            }
        }

        task<std::pair<std::unique_ptr<const Step>, Wires>> async_fetch_and_run_step()
        {
            while (true) {
                if (auto step = co_await crtp().async_fetch_step(); step.has_value()) {
                    auto wires = co_await crtp().async_run_step(**step);
                    co_return {std::move(*step), std::move(wires)};
                }
            }
        }

        task<void> async_run_subtree()
        {
            while (true) {
                if (auto step = co_await crtp().async_fetch_step(); step.has_value())
                    co_await crtp().async_run_step_and_subtree(**step);
                else
                    break;
            }
        }

        task<Wires> async_run_step_and_subtree(const Step &step)
        {
            auto w = co_await crtp().async_run_step(step);

            if (w.new_task.has_value())
                co_await crtp().async_run_subtree();

            co_return std::move(w);
        }

        task<void> async_setup_initial_task(const ISA &isa)
        {
            auto task = isa.initial_task();

            co_await crtp().async_new_task(std::move(task.first), task.second);
        }

        task<void> async_run(const ISA &isa)
        {
            co_await crtp().async_setup_initial_task(isa);

            co_await crtp().async_run_subtree();
        }

    };

}