Use zig-zag encoding in the source position table.
R=vogelheim@chromium.org Review-Url: https://codereview.chromium.org/2081703002 Cr-Commit-Position: refs/heads/master@{#37170}
This commit is contained in:
parent
5eaf4ac48f
commit
6793728528
@ -23,19 +23,13 @@ namespace interpreter {
|
||||
// - we record the difference from the previous position,
|
||||
// - we just stuff one bit for the type into the bytecode offset,
|
||||
// - we write least-significant bits first,
|
||||
// - negative numbers occur only rarely, so we use a denormalized
|
||||
// most-significant byte (a byte with all zeros, which normally wouldn't
|
||||
// make any sense) to encode a negative sign, so that we 'pay' nothing for
|
||||
// positive numbers, but have to pay a full byte for negative integers.
|
||||
// - we use zig-zag encoding to encode both positive and negative numbers.
|
||||
|
||||
namespace {
|
||||
|
||||
// A zero-value in the most-significant byte is used to mark negative numbers.
|
||||
const int kNegativeSignMarker = 0;
|
||||
|
||||
// Each byte is encoded as MoreBit | ValueBits.
|
||||
class MoreBit : public BitField8<bool, 7, 1> {};
|
||||
class ValueBits : public BitField8<int, 0, 7> {};
|
||||
class ValueBits : public BitField8<unsigned, 0, 7> {};
|
||||
|
||||
// Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
|
||||
void AddAndSetEntry(PositionTableEntry& value,
|
||||
@ -54,62 +48,57 @@ void SubtractFromEntry(PositionTableEntry& value,
|
||||
|
||||
// Helper: Encode an integer.
|
||||
void EncodeInt(ZoneVector<byte>& bytes, int value) {
|
||||
bool sign = false;
|
||||
if (value < 0) {
|
||||
sign = true;
|
||||
value = -value;
|
||||
}
|
||||
|
||||
// Zig-zag encoding.
|
||||
static const int kShift = kIntSize * kBitsPerByte - 1;
|
||||
value = ((value << 1) ^ (value >> kShift));
|
||||
DCHECK_GE(value, 0);
|
||||
unsigned int encoded = static_cast<unsigned int>(value);
|
||||
bool more;
|
||||
do {
|
||||
more = value > ValueBits::kMax;
|
||||
bytes.push_back(MoreBit::encode(more || sign) |
|
||||
ValueBits::encode(value & ValueBits::kMax));
|
||||
value >>= ValueBits::kSize;
|
||||
more = encoded > ValueBits::kMax;
|
||||
bytes.push_back(MoreBit::encode(more) |
|
||||
ValueBits::encode(encoded & ValueBits::kMask));
|
||||
encoded >>= ValueBits::kSize;
|
||||
} while (more);
|
||||
|
||||
if (sign) {
|
||||
bytes.push_back(MoreBit::encode(false) |
|
||||
ValueBits::encode(kNegativeSignMarker));
|
||||
}
|
||||
}
|
||||
|
||||
// Encode a PositionTableEntry.
|
||||
void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
|
||||
// 1 bit for sign + is_statement each, which leaves 30b for the value.
|
||||
DCHECK(abs(entry.bytecode_offset) < (1 << 30));
|
||||
EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1));
|
||||
// We only accept ascending bytecode offsets.
|
||||
DCHECK(entry.bytecode_offset >= 0);
|
||||
// Since bytecode_offset is not negative, we use sign to encode is_statement.
|
||||
EncodeInt(bytes, entry.is_statement ? entry.bytecode_offset
|
||||
: -entry.bytecode_offset - 1);
|
||||
EncodeInt(bytes, entry.source_position);
|
||||
}
|
||||
|
||||
// Helper: Decode an integer.
|
||||
void DecodeInt(ByteArray* bytes, int* index, int* v) {
|
||||
byte current;
|
||||
int n = 0;
|
||||
int value = 0;
|
||||
int shift = 0;
|
||||
int decoded = 0;
|
||||
bool more;
|
||||
do {
|
||||
current = bytes->get((*index)++);
|
||||
value |= ValueBits::decode(current) << (n * ValueBits::kSize);
|
||||
n++;
|
||||
decoded |= ValueBits::decode(current) << shift;
|
||||
more = MoreBit::decode(current);
|
||||
shift += ValueBits::kSize;
|
||||
} while (more);
|
||||
|
||||
if (ValueBits::decode(current) == kNegativeSignMarker) {
|
||||
value = -value;
|
||||
}
|
||||
*v = value;
|
||||
DCHECK_GE(decoded, 0);
|
||||
decoded = (decoded >> 1) ^ (-(decoded & 1));
|
||||
*v = decoded;
|
||||
}
|
||||
|
||||
void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
|
||||
int tmp;
|
||||
DecodeInt(bytes, index, &tmp);
|
||||
entry->is_statement = (tmp & 1);
|
||||
|
||||
// Note that '>>' needs to be arithmetic shift in order to handle negative
|
||||
// numbers properly.
|
||||
entry->bytecode_offset = (tmp >> 1);
|
||||
|
||||
if (tmp >= 0) {
|
||||
entry->is_statement = true;
|
||||
entry->bytecode_offset = tmp;
|
||||
} else {
|
||||
entry->is_statement = false;
|
||||
entry->bytecode_offset = -(tmp + 1);
|
||||
}
|
||||
DecodeInt(bytes, index, &entry->source_position);
|
||||
}
|
||||
|
||||
|
@ -56,23 +56,26 @@ TEST_F(SourcePositionTableTest, EncodeExpression) {
|
||||
TEST_F(SourcePositionTableTest, EncodeAscending) {
|
||||
SourcePositionTableBuilder builder(isolate(), zone());
|
||||
|
||||
int accumulator = 0;
|
||||
int code_offset = 0;
|
||||
int source_position = 0;
|
||||
for (int i = 0; i < arraysize(offsets); i++) {
|
||||
accumulator += offsets[i];
|
||||
code_offset += offsets[i];
|
||||
source_position += offsets[i];
|
||||
if (i % 2) {
|
||||
builder.AddPosition(accumulator, accumulator, true);
|
||||
builder.AddPosition(code_offset, source_position, true);
|
||||
} else {
|
||||
builder.AddPosition(accumulator, accumulator, false);
|
||||
builder.AddPosition(code_offset, source_position, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Also test negative offsets:
|
||||
// Also test negative offsets for source positions:
|
||||
for (int i = 0; i < arraysize(offsets); i++) {
|
||||
accumulator -= offsets[i];
|
||||
code_offset += offsets[i];
|
||||
source_position -= offsets[i];
|
||||
if (i % 2) {
|
||||
builder.AddPosition(accumulator, accumulator, true);
|
||||
builder.AddPosition(code_offset, source_position, true);
|
||||
} else {
|
||||
builder.AddPosition(accumulator, accumulator, false);
|
||||
builder.AddPosition(code_offset, source_position, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user