|
1 | 1 | #pragma once
|
2 | 2 |
|
3 |
| -#include <memory> |
4 |
| -#include <mutable/backend/Backend.hpp> |
5 |
| -#include <mutable/IR/Operator.hpp> |
6 |
| -#include <mutable/util/macro.hpp> |
7 |
| -#include <mutable/util/memory.hpp> |
8 |
| -#include <unordered_map> |
| 3 | +#include <mutable/backend/WebAssembly.hpp> |
9 | 4 |
|
10 | 5 |
|
11 | 6 | /* Forward declaration of Binaryen classes. */
|
@@ -46,110 +41,4 @@ struct WasmModule
|
46 | 41 | void dump() const;
|
47 | 42 | };
|
48 | 43 |
|
49 |
| -/** A `WasmPlatform` provides an environment to compile and execute WebAssembly modules. */ |
50 |
| -struct WasmPlatform |
51 |
| -{ |
52 |
| - /** the size of a WebAssembly memory page, 64 KiB. */ |
53 |
| - static constexpr std::size_t WASM_PAGE_SIZE = 1UL << 16; |
54 |
| - /** The maximum memory of a WebAssembly module: 2^32 - 2^16 bytes ≈ 4 GiB */ |
55 |
| - static constexpr std::size_t WASM_MAX_MEMORY = (1UL << 32) - (1UL << 16); |
56 |
| - /** The alignment that is suitable for all built-in types. */ |
57 |
| - static constexpr std::size_t WASM_ALIGNMENT = 8; |
58 |
| - |
59 |
| - /** A `WasmContext` holds associated information of a WebAssembly module instance. */ |
60 |
| - struct WasmContext |
61 |
| - { |
62 |
| - enum config_t : uint64_t |
63 |
| - { |
64 |
| - TRAP_GUARD_PAGES = 0b1, ///< map guard pages with PROT_NONE to trap any accesses |
65 |
| - }; |
66 |
| - |
67 |
| - private: |
68 |
| - config_t config_; |
69 |
| - |
70 |
| - public: |
71 |
| - unsigned id; ///< a unique ID |
72 |
| - const Operator &plan; ///< current plan |
73 |
| - memory::AddressSpace vm; ///< WebAssembly module instance's virtual address space aka.\ *linear memory* |
74 |
| - uint32_t heap = 0; ///< beginning of the heap, encoded as offset from the beginning of the virtual address space |
75 |
| - |
76 |
| - WasmContext(uint32_t id, config_t configuration, const Operator &plan, std::size_t size); |
77 |
| - |
78 |
| - bool config(config_t cfg) const { return bool(cfg & config_); } |
79 |
| - |
80 |
| - /** Maps a table at the current start of `heap` and advances `heap` past the mapped region. Returns the address |
81 |
| - * (in linear memory) of the mapped table. Installs guard pages after each mapping. Acknowledges |
82 |
| - * `TRAP_GUARD_PAGES`. */ |
83 |
| - uint32_t map_table(const Table &table); |
84 |
| - |
85 |
| - /** Installs a guard page at the current `heap` and increments `heap` to the next page. Acknowledges |
86 |
| - * `TRAP_GUARD_PAGES`. */ |
87 |
| - void install_guard_page(); |
88 |
| - }; |
89 |
| - |
90 |
| - private: |
91 |
| - ///> maps unique IDs to `WasmContext` instances |
92 |
| - static inline std::unordered_map<unsigned, std::unique_ptr<WasmContext>> contexts_; |
93 |
| - |
94 |
| - public: |
95 |
| - /** Creates a new `WasmContext` with `size` bytes of virtual address space. */ |
96 |
| - static WasmContext & Create_Wasm_Context_For_ID(unsigned id, |
97 |
| - WasmContext::config_t configuration = WasmContext::config_t(0x0), |
98 |
| - const Operator &plan = NoOpOperator(std::cout), |
99 |
| - std::size_t size = WASM_MAX_MEMORY) |
100 |
| - { |
101 |
| - auto wasm_context = std::make_unique<WasmContext>(id, configuration, plan, size); |
102 |
| - auto res = contexts_.emplace(id, std::move(wasm_context)); |
103 |
| - M_insist(res.second, "WasmContext with that ID already exists"); |
104 |
| - return *res.first->second; |
105 |
| - } |
106 |
| - |
107 |
| - /** Disposes the `WasmContext` with ID `id`. */ |
108 |
| - static void Dispose_Wasm_Context(unsigned id) { |
109 |
| - auto res = contexts_.erase(id); |
110 |
| - (void) res; |
111 |
| - M_insist(res == 1, "There is no context with the given ID to erase"); |
112 |
| - } |
113 |
| - |
114 |
| - /** Disposes the `WasmContext` `ctx`. */ |
115 |
| - static void Dispose_Wasm_Context(const WasmContext &ctx) { Dispose_Wasm_Context(ctx.id); } |
116 |
| - |
117 |
| - /** Returns a reference to the `WasmContext` with ID `id`. */ |
118 |
| - static WasmContext & Get_Wasm_Context_By_ID(unsigned id) { |
119 |
| - auto it = contexts_.find(id); |
120 |
| - M_insist(it != contexts_.end(), "There is no context with the given ID"); |
121 |
| - return *it->second; |
122 |
| - } |
123 |
| - |
124 |
| - /** Tests if the `WasmContext` with ID `id` exists. */ |
125 |
| - static bool Has_Wasm_Context(unsigned id) { return contexts_.find(id) != contexts_.end(); } |
126 |
| - |
127 |
| - WasmPlatform() = default; |
128 |
| - virtual ~WasmPlatform() { } |
129 |
| - WasmPlatform(const WasmPlatform&) = delete; |
130 |
| - WasmPlatform(WasmPlatform&&) = default; |
131 |
| - |
132 |
| - /** Compiles the given `plan` for this `WasmPlatform`. */ |
133 |
| - virtual void compile(const Operator &plan) const = 0; |
134 |
| - |
135 |
| - /** Compiles the given `plan` to a `WasmModule` and executes it on this `WasmPlatform`. */ |
136 |
| - virtual void execute(const Operator &plan) = 0; |
137 |
| -}; |
138 |
| - |
139 |
| -/** A `Backend` to execute a plan on a specific `WasmPlatform`. */ |
140 |
| -struct WasmBackend : Backend |
141 |
| -{ |
142 |
| - private: |
143 |
| - std::unique_ptr<WasmPlatform> platform_; ///< the `WasmPlatform` of this backend |
144 |
| - |
145 |
| - public: |
146 |
| - WasmBackend(std::unique_ptr<WasmPlatform> platform) : platform_(std::move(platform)) { } |
147 |
| - |
148 |
| - /** Returns this backend's `WasmPlatform`. */ |
149 |
| - const WasmPlatform & platform() const { return *platform_; } |
150 |
| - |
151 |
| - /** Executes the given `plan` with this backend. */ |
152 |
| - void execute(const Operator &plan) const override; |
153 |
| -}; |
154 |
| - |
155 | 44 | }
|
0 commit comments