5 #include "src/source-position-table.h" 7 #include "src/objects-inl.h" 8 #include "src/objects.h" 30 class MoreBit :
public BitField8<bool, 7, 1> {};
31 class ValueBits :
public BitField8<unsigned, 0, 7> {};
34 void AddAndSetEntry(PositionTableEntry& value,
35 const PositionTableEntry& other) {
36 value.code_offset += other.code_offset;
37 value.source_position += other.source_position;
38 value.is_statement = other.is_statement;
42 void SubtractFromEntry(PositionTableEntry& value,
43 const PositionTableEntry& other) {
44 value.code_offset -= other.code_offset;
45 value.source_position -= other.source_position;
50 void EncodeInt(std::vector<byte>& bytes, T value) {
52 static const int kShift =
sizeof(T) * kBitsPerByte - 1;
53 value = ((value << 1) ^ (value >> kShift));
55 auto encoded =
static_cast<typename std::make_unsigned<T>::type
>(value);
58 more = encoded > ValueBits::kMax;
60 MoreBit::encode(more) | ValueBits::encode(encoded & ValueBits::kMask);
61 bytes.push_back(current);
62 encoded >>= ValueBits::kSize;
67 void EncodeEntry(std::vector<byte>& bytes,
const PositionTableEntry& entry) {
69 DCHECK_GE(entry.code_offset, 0);
72 entry.is_statement ? entry.code_offset : -entry.code_offset - 1);
73 EncodeInt(bytes, entry.source_position);
78 T DecodeInt(Vector<const byte> bytes,
int* index) {
84 current = bytes[(*index)++];
85 decoded |=
static_cast<typename std::make_unsigned<T>::type
>(
86 ValueBits::decode(current))
88 more = MoreBit::decode(current);
89 shift += ValueBits::kSize;
91 DCHECK_GE(decoded, 0);
92 decoded = (decoded >> 1) ^ (-(decoded & 1));
96 void DecodeEntry(Vector<const byte> bytes,
int* index,
97 PositionTableEntry* entry) {
98 int tmp = DecodeInt<int>(bytes, index);
100 entry->is_statement =
true;
101 entry->code_offset = tmp;
103 entry->is_statement =
false;
104 entry->code_offset = -(tmp + 1);
106 entry->source_position = DecodeInt<int64_t>(bytes, index);
109 Vector<const byte> VectorFromByteArray(ByteArray byte_array) {
110 return Vector<const byte>(byte_array->GetDataStartAddress(),
111 byte_array->length());
114 #ifdef ENABLE_SLOW_DCHECKS 115 void CheckTableEquals(std::vector<PositionTableEntry>& raw_entries,
116 SourcePositionTableIterator& encoded) {
119 auto raw = raw_entries.begin();
120 for (; !encoded.done(); encoded.Advance(), raw++) {
121 DCHECK(raw != raw_entries.end());
122 DCHECK_EQ(encoded.code_offset(), raw->code_offset);
123 DCHECK_EQ(encoded.source_position().raw(), raw->source_position);
124 DCHECK_EQ(encoded.is_statement(), raw->is_statement);
126 DCHECK(raw == raw_entries.end());
132 SourcePositionTableBuilder::SourcePositionTableBuilder(
133 SourcePositionTableBuilder::RecordingMode mode)
134 : mode_(mode), previous_() {}
136 void SourcePositionTableBuilder::AddPosition(
size_t code_offset,
137 SourcePosition source_position,
140 DCHECK(source_position.IsKnown());
141 int offset =
static_cast<int>(code_offset);
142 AddEntry({offset, source_position.raw(), is_statement});
145 void SourcePositionTableBuilder::AddEntry(
const PositionTableEntry& entry) {
146 PositionTableEntry tmp(entry);
147 SubtractFromEntry(tmp, previous_);
148 EncodeEntry(bytes_, tmp);
150 #ifdef ENABLE_SLOW_DCHECKS 151 raw_entries_.push_back(entry);
155 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable(
157 if (bytes_.empty())
return isolate->factory()->empty_byte_array();
160 Handle<ByteArray> table = isolate->factory()->NewByteArray(
161 static_cast<int>(bytes_.size()), TENURED);
162 MemCopy(table->GetDataStartAddress(), bytes_.data(), bytes_.size());
164 #ifdef ENABLE_SLOW_DCHECKS 167 SourcePositionTableIterator it(*table);
168 CheckTableEquals(raw_entries_, it);
170 mode_ = OMIT_SOURCE_POSITIONS;
175 OwnedVector<byte> SourcePositionTableBuilder::ToSourcePositionTableVector() {
176 if (bytes_.empty())
return OwnedVector<byte>();
179 OwnedVector<byte> table = OwnedVector<byte>::Of(bytes_);
181 #ifdef ENABLE_SLOW_DCHECKS 184 SourcePositionTableIterator it(table.as_vector());
185 CheckTableEquals(raw_entries_, it);
187 mode_ = OMIT_SOURCE_POSITIONS;
192 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray byte_array)
193 : raw_table_(VectorFromByteArray(byte_array)) {
197 SourcePositionTableIterator::SourcePositionTableIterator(
198 Handle<ByteArray> byte_array)
199 : table_(byte_array) {
207 SourcePositionTableIterator::SourcePositionTableIterator(
208 Vector<const byte> bytes)
209 : raw_table_(bytes) {
217 void SourcePositionTableIterator::Advance() {
218 Vector<const byte> bytes =
219 table_.is_null() ? raw_table_ : VectorFromByteArray(*table_);
221 DCHECK(index_ >= 0 && index_ <= bytes.length());
222 if (index_ >= bytes.length()) {
225 PositionTableEntry tmp;
226 DecodeEntry(bytes, &index_, &tmp);
227 AddAndSetEntry(current_, tmp);