-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebassemblyModule.cs
251 lines (211 loc) · 8.4 KB
/
WebassemblyModule.cs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Emitted classes derive from this class
using System.Runtime.CompilerServices;
using System;
namespace Wasm2CIL {
public class WebassemblyModule
{
// The length of this memory is given in terms of the page size,
// which is 2^^16 or 65536
public const int PageSize = 65536;
protected byte [] memory;
public WebassemblyModule (int memory_default_size)
{
this.memory = new byte [PageSize * memory_default_size];
}
//private byte [] table;
// All of this will eventually have to be replaced with
// codegen that uses offsets from the memory array, to avoid
// copying. That, or pointers and unsafe.
//
// Unsafe would also allow us to do stackalloc
// We need to be able to call these memory functions when we
// have a stack that already has the offset on it. The easiest
// way to fix this and to fix the issue of wanting to *ensure*
// we only read a certain number of bytes is to do this with
// indirection with this protected function
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected byte [] Copy (int offset, int count)
{
byte [] result = new byte [count];
Array.Copy(memory, offset, result, 0, count);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long Load64BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 8), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long Load32BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadSigned16BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadUnsigned16BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadSigned8BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadUnsigned8BitAsSigned64 (int offset) {
return BitConverter.ToInt64 (this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong Load64BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64 (this.Copy (offset, 8), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadUnsigned32BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadSigned32BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadUnsigned16BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadSigned16BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadSigned8BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadUnsigned8BitAsUnsigned64 (int offset) {
return BitConverter.ToUInt64(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long Load32BitAsSigned32 (int offset) {
return BitConverter.ToInt32(this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadSigned16BitAsSigned32 (int offset) {
return BitConverter.ToInt32(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadUnsigned16BitAsSigned32 (int offset) {
return BitConverter.ToInt32(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadUnsigned8BitAsSigned32 (int offset) {
return BitConverter.ToInt32(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected long LoadSigned8BitAsSigned32 (int offset) {
return BitConverter.ToInt32(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong Load32BitAsUnsigned32 (int offset) {
return BitConverter.ToUInt32(this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadSigned16BitAsUnsigned32 (int offset) {
return BitConverter.ToUInt32(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadUnsigned16BitAsUnsigned32 (int offset) {
return BitConverter.ToUInt32(this.Copy (offset, 2), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadSigned8BitAsUnsigned32 (int offset) {
return BitConverter.ToUInt32(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ulong LoadUnsigned8BitAsUnsigned32 (int offset) {
return BitConverter.ToUInt32(this.Copy (offset, 1), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store64BitFrom64 (long input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
this.memory [offset + 2] = bytes [2];
this.memory [offset + 3] = bytes [3];
this.memory [offset + 4] = bytes [4];
this.memory [offset + 5] = bytes [5];
this.memory [offset + 6] = bytes [6];
this.memory [offset + 7] = bytes [7];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store32BitFrom32 (long input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
this.memory [offset + 2] = bytes [2];
this.memory [offset + 3] = bytes [3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store16BitFrom64 (long input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store8BitFrom64 (long input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store16BitFrom32 (int input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void Store8BitFrom32 (int input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected float LoadSingle (int offset) {
return BitConverter.ToSingle (this.Copy (offset, 4), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void StoreSingle (float input, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
this.memory [offset + 2] = bytes [3];
this.memory [offset + 3] = bytes [3];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected double LoadDouble (int offset) {
return BitConverter.ToDouble (this.Copy (offset, 8), 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void StoreDouble (double input, int align, int offset) {
var bytes = BitConverter.GetBytes (input);
this.memory [offset + 0] = bytes [0];
this.memory [offset + 1] = bytes [1];
this.memory [offset + 2] = bytes [2];
this.memory [offset + 3] = bytes [3];
this.memory [offset + 4] = bytes [4];
this.memory [offset + 5] = bytes [5];
this.memory [offset + 6] = bytes [6];
this.memory [offset + 7] = bytes [7];
}
protected uint CurrentMemory ()
{
return Convert.ToUInt32 (this.memory.Length / PageSize);
}
protected uint GrowMemory (uint addition)
{
var temp = this.memory;
var curr_length = temp.Length / PageSize;
var new_length = (curr_length + 1) * PageSize;
// Fixme: check maximum length
var new_memory = new byte [new_length];
Array.Copy(temp, this.memory, curr_length);
this.memory = new_memory;
return Convert.ToUInt32 (curr_length);
}
}
}