-
Notifications
You must be signed in to change notification settings - Fork 51
/
DataWriter.v3
271 lines (268 loc) · 8.2 KB
/
DataWriter.v3
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// A utility for writing data items into an array of bytes, offering routines
// to write bytes, integers, LEBs, etc. A data writer has an internal storage
// that by default collects all bytes written, like a {Vector}, but this behavior
// can be overridden by changing the {refill} function. E.g. the {refill}
// function can be overridden to stream already-written bytes to I/O.
class DataWriter {
def var data: Range<byte>; // array containing data
def var pos: int; // current position
private var max: int; // the maximum position ever accessed
var refill = DataWriter.growI2X; // refill behavior
// Write a byte.
def putb(data: int) -> this {
acquire(1)[0] = byte.view(data);
}
// Write two bytes.
def putbb(b0: int, b1: int) -> this {
var a = acquire(2);
a[0] = byte.view(b0);
a[1] = byte.view(b1);
}
// Write three bytes.
def putbbb(b0: int, b1: int, b2: int) -> this {
var a = acquire(3);
a[0] = byte.view(b0);
a[1] = byte.view(b1);
a[2] = byte.view(b2);
}
// Write a 16-bit little-endian integer.
def put_b16(data: int) -> this {
var a = acquire(2);
DataWriters.write_range_i16(a, i16.view(data));
}
// Write a 32-bit little-endian integer.
def put_b32(data: int) -> this {
var a = acquire(4);
DataWriters.write_range_i32(a, data);
}
// Write a 64-bit little-endian integer.
def put_b64(data: long) -> this {
var a = acquire(8);
DataWriters.write_range_i64(a, data);
}
// Write a 16-bit big-endian integer.
def put_b16be(data: int) -> this {
var a = acquire(2);
a[0] = byte.view(data >> 8);
a[1] = byte.view(data);
}
// Write a 32-bit big-endian integer.
def put_b32be(data: int) -> this {
var a = acquire(4);
a[0] = byte.view(data >> 24);
a[1] = byte.view(data >> 16);
a[2] = byte.view(data >> 8);
a[3] = byte.view(data);
}
// Write a 64-bit big-endian integer.
def put_b64be(data: long) -> this {
put_b32be(int.view(data >> 32));
put_b32be(int.view(data));
}
// Write a signed 32-bit LEB.
def put_sleb32(data: int) -> this {
if (data >= 0) {
while (data >= 0x40) {
putb(0x80 | (data & 0x7F));
data = data >> 7;
}
} else {
while ((data >> 6) != -1) {
putb(0x80 | (data & 0x7F));
data = data >> 7;
}
}
putb(data & 0x7F);
}
// Write a signed 64-bit LEB.
def put_sleb64(data: long) -> this {
if (data >= 0) {
while (data >= 0x40) {
putb(0x80 | int.view(data & 0x7F));
data = data >> 7;
}
} else {
while ((data >> 6) != -1) {
putb(0x80 | int.view(data & 0x7F));
data = data >> 7;
}
}
putb(int.view(data & 0x7F));
}
// Write an unsigned 32-bit LEB.
def put_uleb32(data: u32) -> this {
while (data >= 0x80) {
putb(int.view(0x80u | (data & 0x7F)));
data = data >> 7;
}
putb(int.view(data));
}
// Write all the bytes of the given array to this writer.
def puta(data: Array<byte>) -> this {
if (data == null) return;
putr(data);
}
// Write the bytes from the given range in the array to this writer.
def putk(data: Array<byte>, start: int, end: int) -> this {
if (data == null) return;
putr(data[start ... end]);
}
// Write the bytes from the given range in the array to this writer and return {void}.
def putkv(data: Array<byte>, start: int, end: int) {
if (data == null) return;
putr(data[start ... end]);
}
// Write the bytes from the given range to this writer.
def putr(data: Range<byte>) -> this {
var a = acquire(data.length);
for (i < a.length) a[i] = data[i];
}
// Write {length} zero bytes.
def zeroN(length: int) -> this {
var a = acquire(length);
for (i < a.length) a[i] = 0;
}
// Skip {length} bytes.
def skipN(length: int) -> this {
acquire(length);
}
// Skip 5 bytes for a 32-bit LEB that will be written later.
def skip_leb32() -> int {
var oldpos = pos;
skipN(5);
return oldpos;
}
// Overwrite a 32-bit unsigned LEB at the current position.
def overwrite_uleb32(val: int) -> this {
for (i < 4) {
putb(val | 0x80);
val >>>= 7;
}
putb(val);
}
// Overwrite a 32-bit signed LEB at the current position.
def overwrite_sleb32(val: int) -> this {
for (i < 4) {
putb(val | 0x80);
val >>= 7;
}
putb(val & 0x7F);
}
// Set the current position to {npos}.
def at(npos: int) -> this {
if (pos > max) max = pos; // remember the maximum pos
pos = npos;
}
// Align the current position to a multiple of {size} bytes.
def align(size: int) -> this {
var rem = pos & (size - 1);
if (rem > 0) skipN(size - rem);
}
// Return the maximum position ever accessed.
def end() -> int {
if (pos > max) max = pos;
return max;
}
// Set the position to be the end.
def atEnd() -> this {
if (pos > max) max = pos;
else pos = max;
}
// Acquire {size} bytes of internal storage and advance the position.
def acquire(size: int) -> Range<byte> {
var end = pos + size;
if (data == null || end > data.length) refill(this, end);
var result = data[pos ... end];
pos = end;
return result;
}
// Grow the internal storage to be at least 2X bigger.
private def growI2X(nlength: int) -> this {
if (data == null) {
data = Array<byte>.new(10 + nlength);
} else if (nlength > data.length) {
data = Ranges.grow(data, data.length * 2 + nlength);
}
}
// Grow the internal storage of this data writer to the new length.
def grow(nlength: int) -> this {
if (data == null) data = Array<byte>.new(nlength);
else if (nlength > data.length) data = Ranges.grow(data, nlength);
}
// Get an alias for all data between {0} and {end()}.
def alias() -> Range<byte> {
return data[0 ... end()];
}
// Copy this data into a new, appropriately-sized array.
def copy() -> Array<byte> {
return Ranges.dup(data[0 ... end()]);
}
// Extract all data from this writer, leaving it empty.
def extract() -> Array<byte> {
var result = Ranges.dup(data[0 ... end()]);
data = null;
pos = max = 0;
return result;
}
// Trim the data in this buffer to {npos} length.
def trim(npos: int) {
pos = max = npos;
}
// Send the data of this writer to the given function, avoiding an intermediate copy.
// Note that it is implementation dependent if {f} is called multiple times, e.g. if
// the internal storage is fragmented.
def send<R>(f: Range<byte> -> R) -> R {
var r = alias();
return if(r.length > 0, f(r));
}
// Clear all bytes to 0 and reset the position.
def clear() -> this {
pos = max = 0;
var x = data;
for (i < x.length) x[i] = 0;
}
// Reset the current data, position, and max.
def reset(ndata: Array<byte>, npos: int, nmax: int) -> this {
data = ndata;
pos = npos;
max = nmax;
}
// Render the data into the given {buf}.
def render(baseAddr: int, startPos: int, size: int, buf: StringBuilder) {
var line = 16;
for (i = 0; i < size; i = i + line) {
buf.puts(" ").putx(baseAddr + i);
buf.puts(": ");
var start = startPos + i;
var end = start + line;
if (end > max) end = max;
for (i = start; i < end; i++) {
buf.putx_8(data[i]);
buf.sp();
}
buf.ln();
}
}
}
// Optimized utility routines for writing data in byte ranges without the overhead of constructing
// a {Decoder} object. Uses layouts to avoid byte-by-byte writes, both combining multiple bounds
// checks and using native (full word) writes when possible.
component DataWriters {
def write_range_i8(r: Range<byte>, val: i8) { Ref<Layout_i8>.of(r).val = val; }
def write_range_i16(r: Range<byte>, val: i16) { Ref<Layout_i16>.of(r).val = val; }
def write_range_i32(r: Range<byte>, val: i32) { Ref<Layout_i32>.of(r).val = val; }
def write_range_i64(r: Range<byte>, val: i64) { Ref<Layout_i64>.of(r).val = val; }
def write_range_u8(r: Range<byte>, val: u8) { Ref<Layout_u8>.of(r).val = val; }
def write_range_u16(r: Range<byte>, val: u16) { Ref<Layout_u16>.of(r).val = val; }
def write_range_u32(r: Range<byte>, val: u32) { Ref<Layout_u32>.of(r).val = val; }
def write_range_u64(r: Range<byte>, val: u64) { Ref<Layout_u64>.of(r).val = val; }
def write_range_float(r: Range<byte>, val: float) { Ref<Layout_float>.of(r).val = val; }
def write_range_double(r: Range<byte>, val: double) { Ref<Layout_double>.of(r).val = val; }
def write_range_u128(r: Range<byte>, val: (u64, u64)) {
var l = Ref<Layout_u128>.of(r);
l.lo_val = val.0;
l.hi_val = val.1;
}
}