2016-02-04 10:50:01 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-06-28 05:51:09 +00:00
|
|
|
#include "src/source-position-table.h"
|
2016-02-04 10:50:01 +00:00
|
|
|
|
|
|
|
#include "src/objects-inl.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-02-24 17:12:31 +00:00
|
|
|
// We'll use a simple encoding scheme to record the source positions.
|
|
|
|
// Conceptually, each position consists of:
|
2016-06-28 05:51:09 +00:00
|
|
|
// - code_offset: An integer index into the BytecodeArray or code.
|
2016-02-24 17:12:31 +00:00
|
|
|
// - source_position: An integer index into the source string.
|
|
|
|
// - position type: Each position is either a statement or an expression.
|
|
|
|
//
|
|
|
|
// The basic idea for the encoding is to use a variable-length integer coding,
|
|
|
|
// where each byte contains 7 bits of payload data, and 1 'more' bit that
|
|
|
|
// determines whether additional bytes follow. Additionally:
|
|
|
|
// - we record the difference from the previous position,
|
2016-06-28 05:51:09 +00:00
|
|
|
// - we just stuff one bit for the type into the code offset,
|
2016-02-24 17:12:31 +00:00
|
|
|
// - we write least-significant bits first,
|
2016-06-22 09:06:17 +00:00
|
|
|
// - we use zig-zag encoding to encode both positive and negative numbers.
|
2016-02-24 17:12:31 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Each byte is encoded as MoreBit | ValueBits.
|
|
|
|
class MoreBit : public BitField8<bool, 7, 1> {};
|
2016-06-22 09:06:17 +00:00
|
|
|
class ValueBits : public BitField8<unsigned, 0, 7> {};
|
2016-02-24 17:12:31 +00:00
|
|
|
|
|
|
|
// Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
|
|
|
|
void AddAndSetEntry(PositionTableEntry& value,
|
|
|
|
const PositionTableEntry& other) {
|
2016-06-28 05:51:09 +00:00
|
|
|
value.code_offset += other.code_offset;
|
2016-02-24 17:12:31 +00:00
|
|
|
value.source_position += other.source_position;
|
|
|
|
value.is_statement = other.is_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: Substract the offsets from 'other' from 'value'.
|
|
|
|
void SubtractFromEntry(PositionTableEntry& value,
|
|
|
|
const PositionTableEntry& other) {
|
2016-06-28 05:51:09 +00:00
|
|
|
value.code_offset -= other.code_offset;
|
2016-02-24 17:12:31 +00:00
|
|
|
value.source_position -= other.source_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: Encode an integer.
|
|
|
|
void EncodeInt(ZoneVector<byte>& bytes, int value) {
|
2016-06-22 09:06:17 +00:00
|
|
|
// 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);
|
2016-02-24 17:12:31 +00:00
|
|
|
bool more;
|
|
|
|
do {
|
2016-06-22 09:06:17 +00:00
|
|
|
more = encoded > ValueBits::kMax;
|
|
|
|
bytes.push_back(MoreBit::encode(more) |
|
|
|
|
ValueBits::encode(encoded & ValueBits::kMask));
|
|
|
|
encoded >>= ValueBits::kSize;
|
2016-02-24 17:12:31 +00:00
|
|
|
} while (more);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a PositionTableEntry.
|
|
|
|
void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
|
2016-06-28 05:51:09 +00:00
|
|
|
// We only accept ascending code offsets.
|
|
|
|
DCHECK(entry.code_offset >= 0);
|
|
|
|
// Since code_offset is not negative, we use sign to encode is_statement.
|
|
|
|
EncodeInt(bytes,
|
|
|
|
entry.is_statement ? entry.code_offset : -entry.code_offset - 1);
|
2016-02-24 17:12:31 +00:00
|
|
|
EncodeInt(bytes, entry.source_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: Decode an integer.
|
|
|
|
void DecodeInt(ByteArray* bytes, int* index, int* v) {
|
|
|
|
byte current;
|
2016-06-22 09:06:17 +00:00
|
|
|
int shift = 0;
|
|
|
|
int decoded = 0;
|
2016-02-24 17:12:31 +00:00
|
|
|
bool more;
|
|
|
|
do {
|
|
|
|
current = bytes->get((*index)++);
|
2016-06-22 09:06:17 +00:00
|
|
|
decoded |= ValueBits::decode(current) << shift;
|
2016-02-24 17:12:31 +00:00
|
|
|
more = MoreBit::decode(current);
|
2016-06-22 09:06:17 +00:00
|
|
|
shift += ValueBits::kSize;
|
2016-02-24 17:12:31 +00:00
|
|
|
} while (more);
|
2016-06-22 09:06:17 +00:00
|
|
|
DCHECK_GE(decoded, 0);
|
|
|
|
decoded = (decoded >> 1) ^ (-(decoded & 1));
|
|
|
|
*v = decoded;
|
2016-02-24 17:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
|
|
|
|
int tmp;
|
|
|
|
DecodeInt(bytes, index, &tmp);
|
2016-06-22 09:06:17 +00:00
|
|
|
if (tmp >= 0) {
|
|
|
|
entry->is_statement = true;
|
2016-06-28 05:51:09 +00:00
|
|
|
entry->code_offset = tmp;
|
2016-06-22 09:06:17 +00:00
|
|
|
} else {
|
|
|
|
entry->is_statement = false;
|
2016-06-28 05:51:09 +00:00
|
|
|
entry->code_offset = -(tmp + 1);
|
2016-06-22 09:06:17 +00:00
|
|
|
}
|
2016-02-24 17:12:31 +00:00
|
|
|
DecodeInt(bytes, index, &entry->source_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2016-02-04 10:50:01 +00:00
|
|
|
|
2016-06-28 05:51:09 +00:00
|
|
|
void SourcePositionTableBuilder::AddPosition(size_t code_offset,
|
2016-05-12 19:18:07 +00:00
|
|
|
int source_position,
|
|
|
|
bool is_statement) {
|
2016-06-28 05:51:09 +00:00
|
|
|
int offset = static_cast<int>(code_offset);
|
2016-05-12 19:18:07 +00:00
|
|
|
AddEntry({offset, source_position, is_statement});
|
2016-02-05 13:29:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 09:13:27 +00:00
|
|
|
void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
|
2016-05-12 19:18:07 +00:00
|
|
|
PositionTableEntry tmp(entry);
|
2016-02-24 17:12:31 +00:00
|
|
|
SubtractFromEntry(tmp, previous_);
|
|
|
|
EncodeEntry(bytes_, tmp);
|
2016-05-12 19:18:07 +00:00
|
|
|
previous_ = entry;
|
2016-02-24 17:12:31 +00:00
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
if (entry.is_statement) {
|
2016-04-05 09:13:27 +00:00
|
|
|
LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddStatementPositionEvent(
|
2016-06-28 05:51:09 +00:00
|
|
|
jit_handler_data_, entry.code_offset,
|
2016-05-12 19:18:07 +00:00
|
|
|
entry.source_position));
|
2016-04-05 09:13:27 +00:00
|
|
|
}
|
|
|
|
LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddPositionEvent(
|
2016-06-28 05:51:09 +00:00
|
|
|
jit_handler_data_, entry.code_offset,
|
2016-05-12 19:18:07 +00:00
|
|
|
entry.source_position));
|
2016-04-05 09:13:27 +00:00
|
|
|
|
2016-02-24 17:12:31 +00:00
|
|
|
#ifdef ENABLE_SLOW_DCHECKS
|
2016-05-12 19:18:07 +00:00
|
|
|
raw_entries_.push_back(entry);
|
2016-02-24 17:12:31 +00:00
|
|
|
#endif
|
2016-02-04 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 17:12:31 +00:00
|
|
|
Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
|
2016-03-02 10:30:30 +00:00
|
|
|
if (bytes_.empty()) return isolate_->factory()->empty_byte_array();
|
|
|
|
|
2016-02-24 17:12:31 +00:00
|
|
|
Handle<ByteArray> table = isolate_->factory()->NewByteArray(
|
|
|
|
static_cast<int>(bytes_.size()), TENURED);
|
|
|
|
|
|
|
|
MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());
|
|
|
|
|
|
|
|
#ifdef ENABLE_SLOW_DCHECKS
|
|
|
|
// Brute force testing: Record all positions and decode
|
|
|
|
// the entire table to verify they are identical.
|
|
|
|
auto raw = raw_entries_.begin();
|
|
|
|
for (SourcePositionTableIterator encoded(*table); !encoded.done();
|
|
|
|
encoded.Advance(), raw++) {
|
|
|
|
DCHECK(raw != raw_entries_.end());
|
2016-06-28 05:51:09 +00:00
|
|
|
DCHECK_EQ(encoded.code_offset(), raw->code_offset);
|
2016-02-24 17:12:31 +00:00
|
|
|
DCHECK_EQ(encoded.source_position(), raw->source_position);
|
|
|
|
DCHECK_EQ(encoded.is_statement(), raw->is_statement);
|
2016-02-04 10:50:01 +00:00
|
|
|
}
|
2016-02-24 17:12:31 +00:00
|
|
|
DCHECK(raw == raw_entries_.end());
|
|
|
|
#endif
|
|
|
|
|
2016-02-04 10:50:01 +00:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2016-02-24 17:12:31 +00:00
|
|
|
SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
|
|
|
|
: table_(byte_array), index_(0), current_() {
|
2016-02-04 10:50:01 +00:00
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourcePositionTableIterator::Advance() {
|
2016-02-24 17:12:31 +00:00
|
|
|
DCHECK(!done());
|
|
|
|
DCHECK(index_ >= 0 && index_ <= table_->length());
|
|
|
|
if (index_ == table_->length()) {
|
|
|
|
index_ = kDone;
|
|
|
|
} else {
|
|
|
|
PositionTableEntry tmp;
|
|
|
|
DecodeEntry(table_, &index_, &tmp);
|
|
|
|
AddAndSetEntry(current_, tmp);
|
2016-02-04 10:50:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|