Skip to content

Commit

Permalink
[lib/util] DataWriter.acquire returns Range<byte> internally
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Aug 15, 2024
1 parent 7c8e03d commit 6c1fd09
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 53 deletions.
5 changes: 1 addition & 4 deletions aeneas/src/mach/MachDataWriter.v3
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class MachDataWriter extends DataWriter {
}
def alignAddr(size: int) {
var rem = (pos + startAddr) & (size - 1);
if (rem > 0) {
acquire(size - rem);
at(pos + size - rem);
}
if (rem > 0) skipN(rem);
}
def posAddr() -> int {
return startAddr + pos;
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/main/Version.v3
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

// Updated by VCS scripts. DO NOT EDIT.
component Version {
def version: string = "III-7.1755";
def version: string = "III-7.1756";
var buildData: string;
}
80 changes: 35 additions & 45 deletions lib/util/DataWriter.v3
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,34 @@ class DataWriter {

// Write a byte.
def putb(data: int) -> this {
acquire(1)[pos++] = byte.view(data);
acquire(1)[0] = byte.view(data);
}
// Write two bytes.
def putbb(b0: int, b1: int) -> this {
var a = acquire(2), p = pos;
a[p + 0] = byte.view(b0);
a[p + 1] = byte.view(b1);
pos = p + 2;
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), p = pos;
a[p + 0] = byte.view(b0);
a[p + 1] = byte.view(b1);
a[p + 2] = byte.view(b2);
pos = p + 3;
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), p = pos;
a[p + 0] = byte.view(data);
a[p + 1] = byte.view(data >> 8);
pos = p + 2;
var a = acquire(2);
a[0] = byte.view(data);
a[1] = byte.view(data >> 8);
}
// Write a 32-bit little-endian integer.
def put_b32(data: int) -> this {
var a = acquire(4), p = pos;
a[p + 0] = byte.view(data);
a[p + 1] = byte.view(data >> 8);
a[p + 2] = byte.view(data >> 16);
a[p + 3] = byte.view(data >> 24);
pos = p + 4;
var a = acquire(4);
a[0] = byte.view(data);
a[1] = byte.view(data >> 8);
a[2] = byte.view(data >> 16);
a[3] = byte.view(data >> 24);
}
// Write a 64-bit little-endian integer.
def put_b64(data: long) -> this {
Expand All @@ -54,19 +50,17 @@ class DataWriter {
}
// Write a 16-bit big-endian integer.
def put_b16be(data: int) -> this {
var a = acquire(2), p = pos;
a[p + 0] = byte.view(data >> 8);
a[p + 1] = byte.view(data);
pos = p + 2;
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), p = pos;
a[p + 0] = byte.view(data >> 24);
a[p + 1] = byte.view(data >> 16);
a[p + 2] = byte.view(data >> 8);
a[p + 3] = byte.view(data);
pos = p + 4;
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 {
Expand Down Expand Up @@ -128,22 +122,17 @@ class DataWriter {
}
// Write the bytes from the given range to this writer.
def putr(data: Range<byte>) -> this {
var len = data.length;
acquire(len);
var a = this.data[pos ..+ len];
for (i < len) a[i] = data[i];
pos += len;
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), end = pos + length;
for (p = pos; p < end; p++) a[p] = 0;
pos = end;
var a = acquire(length);
for (i < a.length) a[i] = 0;
}
// Skip {length} bytes.
def skipN(length: int) -> this {
acquire(length);
pos = pos + length;
}
// Skip 5 bytes for a 32-bit LEB that will be written later.
def skip_leb32() -> int {
Expand All @@ -153,7 +142,6 @@ class DataWriter {
}
// Overwrite a 32-bit LEB at the current position.
def overwrite_uleb32(val: int) -> this {
acquire(5);
for (i < 4) {
putb(val | 0x80);
val >>= 7;
Expand All @@ -180,11 +168,13 @@ class DataWriter {
if (pos > max) max = pos;
else pos = max;
}
// Acquire {size} bytes of internal storage.
def acquire(size: int) -> Array<byte> {
var nsize = pos + size;
if (data == null || nsize > data.length) refill(this, nsize);
return data;
// 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 {
Expand Down
6 changes: 3 additions & 3 deletions test/lib/DataWriterTest.v3
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ def test_send2(t: LibTest) {
}

def assertAcquire(t: LibTest, w: DataWriter, n: int) {
var prev_pos = w.pos;
var a = w.acquire(1);
t.asserteq(a, w.data);
t.assert(a.length >= 1);
t.assert(w.pos + 1 <= a.length);
t.asserteq(1, a.length);
t.asserteq(1 + prev_pos, w.pos);
}

def test_acquire(t: LibTest) {
Expand Down

0 comments on commit 6c1fd09

Please sign in to comment.