Commit 67937285 authored by yangguo's avatar yangguo Committed by Commit bot

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}
parent 5eaf4ac4
...@@ -23,19 +23,13 @@ namespace interpreter { ...@@ -23,19 +23,13 @@ namespace interpreter {
// - we record the difference from the previous position, // - we record the difference from the previous position,
// - we just stuff one bit for the type into the bytecode offset, // - we just stuff one bit for the type into the bytecode offset,
// - we write least-significant bits first, // - we write least-significant bits first,
// - negative numbers occur only rarely, so we use a denormalized // - we use zig-zag encoding to encode both positive and negative numbers.
// 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.
namespace { 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. // Each byte is encoded as MoreBit | ValueBits.
class MoreBit : public BitField8<bool, 7, 1> {}; 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. // Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
void AddAndSetEntry(PositionTableEntry& value, void AddAndSetEntry(PositionTableEntry& value,
...@@ -54,62 +48,57 @@ void SubtractFromEntry(PositionTableEntry& value, ...@@ -54,62 +48,57 @@ void SubtractFromEntry(PositionTableEntry& value,
// Helper: Encode an integer. // Helper: Encode an integer.
void EncodeInt(ZoneVector<byte>& bytes, int value) { void EncodeInt(ZoneVector<byte>& bytes, int value) {
bool sign = false; // Zig-zag encoding.
if (value < 0) { static const int kShift = kIntSize * kBitsPerByte - 1;
sign = true; value = ((value << 1) ^ (value >> kShift));
value = -value; DCHECK_GE(value, 0);
} unsigned int encoded = static_cast<unsigned int>(value);
bool more; bool more;
do { do {
more = value > ValueBits::kMax; more = encoded > ValueBits::kMax;
bytes.push_back(MoreBit::encode(more || sign) | bytes.push_back(MoreBit::encode(more) |
ValueBits::encode(value & ValueBits::kMax)); ValueBits::encode(encoded & ValueBits::kMask));
value >>= ValueBits::kSize; encoded >>= ValueBits::kSize;
} while (more); } while (more);
if (sign) {
bytes.push_back(MoreBit::encode(false) |
ValueBits::encode(kNegativeSignMarker));
}
} }
// Encode a PositionTableEntry. // Encode a PositionTableEntry.
void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) { void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
// 1 bit for sign + is_statement each, which leaves 30b for the value. // We only accept ascending bytecode offsets.
DCHECK(abs(entry.bytecode_offset) < (1 << 30)); DCHECK(entry.bytecode_offset >= 0);
EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1)); // 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); EncodeInt(bytes, entry.source_position);
} }
// Helper: Decode an integer. // Helper: Decode an integer.
void DecodeInt(ByteArray* bytes, int* index, int* v) { void DecodeInt(ByteArray* bytes, int* index, int* v) {
byte current; byte current;
int n = 0; int shift = 0;
int value = 0; int decoded = 0;
bool more; bool more;
do { do {
current = bytes->get((*index)++); current = bytes->get((*index)++);
value |= ValueBits::decode(current) << (n * ValueBits::kSize); decoded |= ValueBits::decode(current) << shift;
n++;
more = MoreBit::decode(current); more = MoreBit::decode(current);
shift += ValueBits::kSize;
} while (more); } while (more);
DCHECK_GE(decoded, 0);
if (ValueBits::decode(current) == kNegativeSignMarker) { decoded = (decoded >> 1) ^ (-(decoded & 1));
value = -value; *v = decoded;
}
*v = value;
} }
void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) { void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
int tmp; int tmp;
DecodeInt(bytes, index, &tmp); DecodeInt(bytes, index, &tmp);
entry->is_statement = (tmp & 1); if (tmp >= 0) {
entry->is_statement = true;
// Note that '>>' needs to be arithmetic shift in order to handle negative entry->bytecode_offset = tmp;
// numbers properly. } else {
entry->bytecode_offset = (tmp >> 1); entry->is_statement = false;
entry->bytecode_offset = -(tmp + 1);
}
DecodeInt(bytes, index, &entry->source_position); DecodeInt(bytes, index, &entry->source_position);
} }
......
...@@ -56,23 +56,26 @@ TEST_F(SourcePositionTableTest, EncodeExpression) { ...@@ -56,23 +56,26 @@ TEST_F(SourcePositionTableTest, EncodeExpression) {
TEST_F(SourcePositionTableTest, EncodeAscending) { TEST_F(SourcePositionTableTest, EncodeAscending) {
SourcePositionTableBuilder builder(isolate(), zone()); SourcePositionTableBuilder builder(isolate(), zone());
int accumulator = 0; int code_offset = 0;
int source_position = 0;
for (int i = 0; i < arraysize(offsets); i++) { for (int i = 0; i < arraysize(offsets); i++) {
accumulator += offsets[i]; code_offset += offsets[i];
source_position += offsets[i];
if (i % 2) { if (i % 2) {
builder.AddPosition(accumulator, accumulator, true); builder.AddPosition(code_offset, source_position, true);
} else { } 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++) { for (int i = 0; i < arraysize(offsets); i++) {
accumulator -= offsets[i]; code_offset += offsets[i];
source_position -= offsets[i];
if (i % 2) { if (i % 2) {
builder.AddPosition(accumulator, accumulator, true); builder.AddPosition(code_offset, source_position, true);
} else { } else {
builder.AddPosition(accumulator, accumulator, false); builder.AddPosition(code_offset, source_position, false);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment