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

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

Issue 1731883003: Encode interpreter::SourcePositionTable as variable-length ints. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Really fix leak. SourcePositionTableBuilder is member of a zone-allocated object. 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
28 // make any sense) to encode a negative sign, so that we 'pay' nothing for
29 // positive numbers, but have to pay a full byte for negative integers.
30
31 namespace {
32
33 // A zero-value in the most-significant byte is used to mark negative numbers.
34 const int kNegativeSignMarker = 0;
35
36 // Each byte is encoded as MoreBit | ValueBits.
37 class MoreBit : public BitField8<bool, 7, 1> {};
38 class ValueBits : public BitField8<int, 0, 7> {};
39
40 // Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
41 void AddAndSetEntry(PositionTableEntry& value,
42 const PositionTableEntry& other) {
43 value.bytecode_offset += other.bytecode_offset;
44 value.source_position += other.source_position;
45 value.is_statement = other.is_statement;
46 }
47
48 // Helper: Substract the offsets from 'other' from 'value'.
49 void SubtractFromEntry(PositionTableEntry& value,
50 const PositionTableEntry& other) {
51 value.bytecode_offset -= other.bytecode_offset;
52 value.source_position -= other.source_position;
53 }
54
55 // Helper: Encode an integer.
56 void EncodeInt(ZoneVector<byte>& bytes, int value) {
57 bool sign = false;
58 if (value < 0) {
59 sign = true;
60 value = -value;
61 }
62
63 bool more;
64 do {
65 more = value > ValueBits::kMax;
66 bytes.push_back(MoreBit::encode(more || sign) |
67 ValueBits::encode(value & ValueBits::kMax));
68 value >>= ValueBits::kSize;
69 } while (more);
70
71 if (sign) {
72 bytes.push_back(MoreBit::encode(false) |
73 ValueBits::encode(kNegativeSignMarker));
74 }
75 }
76
77 // Encode a PositionTableEntry.
78 void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
79 // 1 bit for sign + is_statement each, which leaves 30b for the value.
80 DCHECK(abs(entry.bytecode_offset) < (1 << 30));
81 EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1));
82 EncodeInt(bytes, entry.source_position);
83 }
84
85 // Helper: Decode an integer.
86 void DecodeInt(ByteArray* bytes, int* index, int* v) {
87 byte current;
88 int n = 0;
89 int value = 0;
90 bool more;
91 do {
92 current = bytes->get((*index)++);
93 value |= ValueBits::decode(current) << (n * ValueBits::kSize);
94 n++;
95 more = MoreBit::decode(current);
96 } while (more);
97
98 if (ValueBits::decode(current) == kNegativeSignMarker) {
99 value = -value;
100 }
101 *v = value;
102 }
103
104 void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
105 int tmp;
106 DecodeInt(bytes, index, &tmp);
107 entry->is_statement = (tmp & 1);
108
109 // Note that '>>' needs to be arithmetic shift in order to handle negative
110 // numbers properly.
111 entry->bytecode_offset = (tmp >> 1);
112
113 DecodeInt(bytes, index, &entry->source_position);
114 }
115
116 } // namespace
17 117
18 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, 118 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset,
19 int source_position) { 119 int source_position) {
20 int offset = static_cast<int>(bytecode_offset); 120 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 } 121 }
28 122
29 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, 123 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset,
30 int source_position) { 124 int source_position) {
31 int offset = static_cast<int>(bytecode_offset); 125 AddEntry({static_cast<int>(bytecode_offset), source_position, false});
32 // If a position has already been assigned to this bytecode offset,
33 // do not reassign a new statement position.
34 if (CodeOffsetHasPosition(offset)) return;
35 uint32_t encoded = IsStatementField::encode(false) |
36 SourcePositionField::encode(source_position);
37 entries_.push_back({offset, encoded});
38 } 126 }
39 127
40 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { 128 void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
41 int offset = static_cast<int>(bytecode_offset); 129 // Don't encode a new entry if this bytecode already has a source position
42 // If we already added a source position table entry, but the bytecode array 130 // assigned.
43 // builder ended up not outputting a bytecode for the corresponding bytecode 131 if (bytes_.size() > 0 && previous_.bytecode_offset == entry.bytecode_offset) {
44 // offset, we have to remove that entry. 132 return;
45 if (CodeOffsetHasPosition(offset)) entries_.pop_back(); 133 }
134
135 PositionTableEntry tmp(entry);
136 SubtractFromEntry(tmp, previous_);
137 EncodeEntry(bytes_, tmp);
138 previous_ = entry;
139
140 #ifdef ENABLE_SLOW_DCHECKS
141 raw_entries_.push_back(entry);
142 #endif
46 } 143 }
47 144
48 Handle<FixedArray> SourcePositionTableBuilder::ToFixedArray() { 145 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
49 int length = static_cast<int>(entries_.size()); 146 Handle<ByteArray> table = isolate_->factory()->NewByteArray(
50 Handle<FixedArray> table = 147 static_cast<int>(bytes_.size()), TENURED);
51 isolate_->factory()->NewFixedArray(length * 2, TENURED); 148 if (bytes_.empty()) return table;
52 for (int i = 0; i < length; i++) { 149
53 table->set(i * 2, Smi::FromInt(entries_[i].bytecode_offset)); 150 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());
54 table->set(i * 2 + 1, Smi::FromInt(entries_[i].source_position_and_type)); 151
152 #ifdef ENABLE_SLOW_DCHECKS
153 // Brute force testing: Record all positions and decode
154 // the entire table to verify they are identical.
155 auto raw = raw_entries_.begin();
156 for (SourcePositionTableIterator encoded(*table); !encoded.done();
157 encoded.Advance(), raw++) {
158 DCHECK(raw != raw_entries_.end());
159 DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset);
160 DCHECK_EQ(encoded.source_position(), raw->source_position);
161 DCHECK_EQ(encoded.is_statement(), raw->is_statement);
55 } 162 }
163 DCHECK(raw == raw_entries_.end());
164 #endif
165
56 return table; 166 return table;
57 } 167 }
58 168
59 SourcePositionTableIterator::SourcePositionTableIterator( 169 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
60 BytecodeArray* bytecode_array) 170 : 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(); 171 Advance();
66 } 172 }
67 173
68 void SourcePositionTableIterator::Advance() { 174 void SourcePositionTableIterator::Advance() {
69 if (index_ < length_) { 175 DCHECK(!done());
70 int new_bytecode_offset = Smi::cast(table_->get(index_))->value(); 176 DCHECK(index_ >= 0 && index_ <= table_->length());
71 // Bytecode offsets are in ascending order. 177 if (index_ == table_->length()) {
72 DCHECK(bytecode_offset_ < new_bytecode_offset || index_ == 0); 178 index_ = kDone;
73 bytecode_offset_ = new_bytecode_offset; 179 } else {
74 uint32_t source_position_and_type = 180 PositionTableEntry tmp;
75 static_cast<uint32_t>(Smi::cast(table_->get(index_ + 1))->value()); 181 DecodeEntry(table_, &index_, &tmp);
76 is_statement_ = IsStatementField::decode(source_position_and_type); 182 AddAndSetEntry(current_, tmp);
77 source_position_ = SourcePositionField::decode(source_position_and_type);
78 } 183 }
79 index_ += 2;
80 } 184 }
81 185
82 } // namespace interpreter 186 } // namespace interpreter
83 } // namespace internal 187 } // namespace internal
84 } // namespace v8 188 } // 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