Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: src/interpreter/source-position-table.cc

Issue 1704943002: Encode interpreter::SourcePositionTable as variable-length ints. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Ross' feedback: Separate byte encoding from other processing. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/interpreter/source-position-table.h ('k') | src/objects.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/interpreter/source-position-table.h" 5 #include "src/interpreter/source-position-table.h"
6 6
7 #include "src/assembler.h"
8 #include "src/objects-inl.h" 7 #include "src/objects-inl.h"
9 #include "src/objects.h" 8 #include "src/objects.h"
10 9
11 namespace v8 { 10 namespace v8 {
12 namespace internal { 11 namespace internal {
13 namespace interpreter { 12 namespace interpreter {
14 13
15 class IsStatementField : public BitField<bool, 0, 1> {}; 14 // We'll use a simple encoding scheme to record the source positions.
16 class SourcePositionField : public BitField<int, 1, 30> {}; 15 // Conceptually, each position consists of:
16 // - bytecode_offset: An integer index into the BytecodeArray
17 // - source_position: An integer index into the source string.
18 // - position type: Each position is either a statement or an expression.
19 //
20 // The basic idea for the encoding is to use a variable-length integer coding,
21 // where each byte contains 7 bits of payload data, and 1 'more' bit that
22 // determines whether additional bytes follow. Additionally:
23 // - we record the difference from the previous position,
24 // - we just stuff one bit for the type into the bytecode offset,
25 // - we write least-significant bits first,
26 // - negative numbers occur only rarely, so we use a denormalized
27 // most-significant byte (a byte with all zeros, which normally wouldn't make
28 // any sense)
29 // to encode a negative sign, so that we 'pay' nothing for positive numbers,
30 // but have to pay a full byte for negative integers.
31
32 namespace {
33 // Each byte is encoded as MoreBit | ValueBits.
34 class MoreBit : public BitField8<bool, 7, 1> {};
35 class ValueBits : public BitField8<int, 0, 7> {};
36
37 // Bytecode offsets also include a bit for the position type in its least
38 // significant digit.
39 // Beware that the bytecode offset can be negative, and the default BitField
40 // implementation might not do the right thing in this case.
41 class TypeBit : public BitField<bool, 0, 1> {};
42 class BytecodeOffsetBits : public BitField<int, 1, 31> {};
43
44 // Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
45 void AddAndSetEntry(PositionTableEntry& value,
46 const PositionTableEntry& other) {
47 value.bytecode_offset += other.bytecode_offset;
48 value.source_position += other.source_position;
49 value.is_statement = other.is_statement;
50 }
51
52 // Helper: Substract the offsets from 'other' from 'value'.
53 void SubtractFromEntry(PositionTableEntry& value,
54 const PositionTableEntry& other) {
55 value.bytecode_offset -= other.bytecode_offset;
56 value.source_position -= other.source_position;
57 }
58
59 // Helper: Encode an integer.
60 void EncodeInt(ZoneVector<byte>& bytes, int value) {
61 DCHECK(abs(value) < (1 << 30));
rmcilroy 2016/02/22 12:38:40 Is there a reason this DCHECK is needed?
vogelheim 2016/02/22 18:33:33 Done.
62 bool sign = false;
63 if (value < 0) {
64 sign = true;
65 value = -value;
66 }
67
68 bool more;
69 do {
70 more = value > ValueBits::kMax;
71 bytes.push_back(MoreBit::encode(more || sign) |
72 ValueBits::encode(value & ValueBits::kMax));
73 value >>= ValueBits::kSize;
74 } while (more);
75
76 if (sign) {
77 bytes.push_back(MoreBit::encode(false));
78 }
79 }
80
81 // Encode a PositionTableEntry.
82 void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
83 EncodeInt(bytes, TypeBit::encode(entry.is_statement) |
84 BytecodeOffsetBits::encode(entry.bytecode_offset &
85 BytecodeOffsetBits::kMax));
rmcilroy 2016/02/22 12:38:40 Could you add a DCHECK that entry.bytecode_offset
vogelheim 2016/02/22 18:33:33 Done.
86 EncodeInt(bytes, entry.source_position);
87 }
88
89 // Helper: Scan one value backwards.
90 // *index is expected to point behind the current 'complete' value encoding
91 // and will be set to point behind he previous one.
92 void EncodeScanBackwards(ZoneVector<byte>& bytes, int* index) {
93 DCHECK(bytes.size() > 0);
94 DCHECK(*index > 0);
95
96 // Index is expected to point behind a 'complete' value encoding.
97 (*index)--;
98 DCHECK(!MoreBit::decode(bytes[*index]));
99
100 // Scan back until we either hit the beginning of our bytes, or another byte
101 // without 'more' bit.
102 while (*index > 0 && MoreBit::decode(bytes[(*index) - 1])) {
103 (*index)--;
104 }
105 DCHECK(*index >= 0);
106 }
107
108 // Helper: Decode an integer.
109 void DecodeInt(const byte* bytes, int* index, int* v) {
110 byte current;
111 int n = 0;
112 int value = 0;
113 bool more;
114 do {
115 current = bytes[(*index)++];
116 value |= ValueBits::decode(current) << (n * ValueBits::kSize);
117 n++;
118 more = MoreBit::decode(current);
119 } while (more);
120
121 if (ValueBits::decode(current) == 0) {
122 value = -value;
123 }
124 *v = value;
125 }
126
127 void DecodeEntry(const byte* bytes, int* index, PositionTableEntry* entry) {
128 int tmp;
129 DecodeInt(bytes, index, &tmp);
130 entry->is_statement = TypeBit::decode(tmp);
131
132 // Directly shift tmp, because '>>' needs to be arithmetic shift in order to
133 // handle negative numbers properly.
134 entry->bytecode_offset = (tmp >> BytecodeOffsetBits::kShift);
rmcilroy 2016/02/22 12:38:40 Personally I would just avoid using TypeBit / Byte
vogelheim 2016/02/22 18:33:33 Done.
135
136 DecodeInt(bytes, index, &entry->source_position);
137 }
138
139 } // namespace
17 140
18 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, 141 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset,
19 int source_position) { 142 int source_position) {
20 int offset = static_cast<int>(bytecode_offset); 143 AddEntry({static_cast<int>(bytecode_offset), source_position, true});
21 // If a position has already been assigned to this bytecode offset,
22 // do not reassign a new statement position.
23 if (CodeOffsetHasPosition(offset)) return;
24 uint32_t encoded = IsStatementField::encode(true) |
25 SourcePositionField::encode(source_position);
26 entries_.push_back({offset, encoded});
27 } 144 }
28 145
29 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, 146 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset,
30 int source_position) { 147 int source_position) {
31 int offset = static_cast<int>(bytecode_offset); 148 AddEntry({static_cast<int>(bytecode_offset), source_position, false});
32 // If a position has already been assigned to this bytecode offset, 149 }
33 // do not reassign a new statement position. 150
34 if (CodeOffsetHasPosition(offset)) return; 151 void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
35 uint32_t encoded = IsStatementField::encode(false) | 152 if (!CodeOffsetHasPosition(entry.bytecode_offset)) {
36 SourcePositionField::encode(source_position); 153 PositionTableEntry tmp(entry);
37 entries_.push_back({offset, encoded}); 154 SubtractFromEntry(tmp, previous_);
155 EncodeEntry(bytes_, tmp);
156 previous_ = entry;
157
158 #ifdef ENABLE_SLOW_DCHECKS
159 raw_entries_.push_back(entry);
160 #endif
161 }
38 } 162 }
39 163
40 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { 164 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) {
41 int offset = static_cast<int>(bytecode_offset); 165 int offset = static_cast<int>(bytecode_offset);
42 // If we already added a source position table entry, but the bytecode array 166 // If we already added a source position table entry, but the bytecode array
43 // builder ended up not outputting a bytecode for the corresponding bytecode 167 // builder ended up not outputting a bytecode for the corresponding bytecode
44 // offset, we have to remove that entry. 168 // offset, we have to remove that entry.
45 if (CodeOffsetHasPosition(offset)) entries_.pop_back(); 169 if (CodeOffsetHasPosition(offset)) {
170 // Reverting means we need to discard 2 variable length ints. We also need
171 // to read them, in order to fixup the previous_*_ member variables.
172
173 // Scan backwards for 2 values.
174 int index = static_cast<int>(bytes_.size());
175 EncodeScanBackwards(bytes_, &index);
176 EncodeScanBackwards(bytes_, &index);
177
178 // Read from index to adjust previous_.
179 int tmp_index = index;
180 PositionTableEntry tmp_entry;
181 DecodeEntry(&*bytes_.begin(), &tmp_index, &tmp_entry);
182 SubtractFromEntry(previous_, tmp_entry);
183
184 bytes_.resize(index);
185
186 #ifdef ENABLE_SLOW_DCHECKS
187 raw_entries_.pop_back();
188 #endif
189 }
46 } 190 }
47 191
48 Handle<FixedArray> SourcePositionTableBuilder::ToFixedArray() { 192 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
49 int length = static_cast<int>(entries_.size()); 193 Handle<ByteArray> table = isolate_->factory()->NewByteArray(
50 Handle<FixedArray> table = 194 static_cast<int>(bytes_.size()), TENURED);
51 isolate_->factory()->NewFixedArray(length * 2, TENURED); 195 if (bytes_.empty()) return table;
52 for (int i = 0; i < length; i++) { 196
53 table->set(i * 2, Smi::FromInt(entries_[i].bytecode_offset)); 197 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());
54 table->set(i * 2 + 1, Smi::FromInt(entries_[i].source_position_and_type)); 198
199 #ifdef ENABLE_SLOW_DCHECKS
200 // Brute force testing: Record all positions and decode
201 // the entire table to verify they are identical.
202 auto raw = raw_entries_.begin();
203 for (SourcePositionTableIterator encoded(*table); !encoded.done();
204 encoded.Advance(), raw++) {
205 DCHECK(raw != raw_entries_.end());
206 DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset);
207 DCHECK_EQ(encoded.source_position(), raw->source_position);
208 DCHECK_EQ(encoded.is_statement(), raw->is_statement);
55 } 209 }
210 DCHECK(raw == raw_entries_.end());
211 #endif
212
56 return table; 213 return table;
57 } 214 }
58 215
59 SourcePositionTableIterator::SourcePositionTableIterator( 216 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
60 BytecodeArray* bytecode_array) 217 : table_(byte_array), index_(0), current_() {
61 : table_(bytecode_array->source_position_table()),
62 index_(0),
63 length_(table_->length()) {
64 DCHECK(table_->length() % 2 == 0);
65 Advance(); 218 Advance();
66 } 219 }
67 220
68 void SourcePositionTableIterator::Advance() { 221 void SourcePositionTableIterator::Advance() {
69 if (index_ < length_) { 222 DCHECK(!done());
70 int new_bytecode_offset = Smi::cast(table_->get(index_))->value(); 223 DCHECK(index_ >= 0 && index_ <= table_->length());
71 // Bytecode offsets are in ascending order. 224 if (index_ == table_->length()) {
72 DCHECK(bytecode_offset_ < new_bytecode_offset || index_ == 0); 225 index_ = kDone;
73 bytecode_offset_ = new_bytecode_offset; 226 } else {
74 uint32_t source_position_and_type = 227 PositionTableEntry tmp;
75 static_cast<uint32_t>(Smi::cast(table_->get(index_ + 1))->value()); 228 DecodeEntry(table_->GetDataStartAddress(), &index_, &tmp);
76 is_statement_ = IsStatementField::decode(source_position_and_type); 229 AddAndSetEntry(current_, tmp);
77 source_position_ = SourcePositionField::decode(source_position_and_type);
78 } 230 }
79 index_ += 2;
80 } 231 }
81 232
82 } // namespace interpreter 233 } // namespace interpreter
83 } // namespace internal 234 } // namespace internal
84 } // namespace v8 235 } // namespace v8
OLDNEW
« no previous file with comments | « src/interpreter/source-position-table.h ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698