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

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: Make windows compiler happy. 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
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 // Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
38 void AddAndSetEntry(PositionTableEntry& value,
39 const PositionTableEntry& other) {
40 value.bytecode_offset += other.bytecode_offset;
41 value.source_position += other.source_position;
42 value.is_statement = other.is_statement;
43 }
44
45 // Helper: Substract the offsets from 'other' from 'value'.
46 void SubtractFromEntry(PositionTableEntry& value,
47 const PositionTableEntry& other) {
48 value.bytecode_offset -= other.bytecode_offset;
49 value.source_position -= other.source_position;
50 }
51
52 // Helper: Encode an integer.
53 void EncodeInt(ZoneVector<byte>& bytes, int value) {
54 bool sign = false;
55 if (value < 0) {
56 sign = true;
57 value = -value;
58 }
59
60 bool more;
61 do {
62 more = value > ValueBits::kMax;
63 bytes.push_back(MoreBit::encode(more || sign) |
64 ValueBits::encode(value & ValueBits::kMax));
65 value >>= ValueBits::kSize;
66 } while (more);
67
68 if (sign) {
69 bytes.push_back(MoreBit::encode(false));
Yang 2016/02/22 21:56:25 Maybe we could use a constant here for the value b
vogelheim 2016/02/24 12:29:30 Done.
70 }
71 }
72
73 // Encode a PositionTableEntry.
74 void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
75 // 1 bit for sign + is_statement each, which leaves 30b for the value.
76 DCHECK(abs(entry.bytecode_offset) < (1 << 30));
77 EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1));
78 EncodeInt(bytes, entry.source_position);
79 }
80
81 // Helper: Decode an integer.
82 void DecodeInt(ByteArray* bytes, int* index, int* v) {
83 byte current;
84 int n = 0;
85 int value = 0;
86 bool more;
87 do {
88 current = bytes->get((*index)++);
89 value |= ValueBits::decode(current) << (n * ValueBits::kSize);
90 n++;
91 more = MoreBit::decode(current);
92 } while (more);
93
94 if (ValueBits::decode(current) == 0) {
Yang 2016/02/22 21:56:25 We would compare this with kNegativeSentinel, inst
vogelheim 2016/02/24 12:29:30 Larry wants "more magic moments", so there you go.
95 value = -value;
96 }
97 *v = value;
98 }
99
100 void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
101 int tmp;
102 DecodeInt(bytes, index, &tmp);
103 entry->is_statement = (tmp & 1);
104
105 // Note that '>>' needs to be arithmetic shift in order to handle negative
106 // numbers properly.
107 entry->bytecode_offset = (tmp >> 1);
108
109 DecodeInt(bytes, index, &entry->source_position);
110 }
111
112 } // namespace
17 113
18 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, 114 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset,
19 int source_position) { 115 int source_position) {
20 int offset = static_cast<int>(bytecode_offset); 116 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 } 117 }
28 118
29 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, 119 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset,
30 int source_position) { 120 int source_position) {
31 int offset = static_cast<int>(bytecode_offset); 121 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 } 122 }
39 123
40 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { 124 void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
41 int offset = static_cast<int>(bytecode_offset); 125 // 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 126 // assigned.
43 // builder ended up not outputting a bytecode for the corresponding bytecode 127 if (bytes_.size() > 0 && previous_.bytecode_offset == entry.bytecode_offset) {
44 // offset, we have to remove that entry. 128 return;
45 if (CodeOffsetHasPosition(offset)) entries_.pop_back(); 129 }
130
131 PositionTableEntry tmp(entry);
132 SubtractFromEntry(tmp, previous_);
133 EncodeEntry(bytes_, tmp);
134 previous_ = entry;
135
136 #ifdef ENABLE_SLOW_DCHECKS
137 raw_entries_.push_back(entry);
138 #endif
46 } 139 }
47 140
48 Handle<FixedArray> SourcePositionTableBuilder::ToFixedArray() { 141 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
49 int length = static_cast<int>(entries_.size()); 142 Handle<ByteArray> table = isolate_->factory()->NewByteArray(
50 Handle<FixedArray> table = 143 static_cast<int>(bytes_.size()), TENURED);
51 isolate_->factory()->NewFixedArray(length * 2, TENURED); 144 if (bytes_.empty()) return table;
52 for (int i = 0; i < length; i++) { 145
53 table->set(i * 2, Smi::FromInt(entries_[i].bytecode_offset)); 146 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());
54 table->set(i * 2 + 1, Smi::FromInt(entries_[i].source_position_and_type)); 147
148 #ifdef ENABLE_SLOW_DCHECKS
149 // Brute force testing: Record all positions and decode
150 // the entire table to verify they are identical.
151 auto raw = raw_entries_.begin();
152 for (SourcePositionTableIterator encoded(*table); !encoded.done();
153 encoded.Advance(), raw++) {
154 DCHECK(raw != raw_entries_.end());
155 DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset);
156 DCHECK_EQ(encoded.source_position(), raw->source_position);
157 DCHECK_EQ(encoded.is_statement(), raw->is_statement);
55 } 158 }
159 DCHECK(raw == raw_entries_.end());
160 #endif
161
56 return table; 162 return table;
57 } 163 }
58 164
59 SourcePositionTableIterator::SourcePositionTableIterator( 165 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
60 BytecodeArray* bytecode_array) 166 : 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(); 167 Advance();
66 } 168 }
67 169
68 void SourcePositionTableIterator::Advance() { 170 void SourcePositionTableIterator::Advance() {
69 if (index_ < length_) { 171 DCHECK(!done());
70 int new_bytecode_offset = Smi::cast(table_->get(index_))->value(); 172 DCHECK(index_ >= 0 && index_ <= table_->length());
71 // Bytecode offsets are in ascending order. 173 if (index_ == table_->length()) {
72 DCHECK(bytecode_offset_ < new_bytecode_offset || index_ == 0); 174 index_ = kDone;
73 bytecode_offset_ = new_bytecode_offset; 175 } else {
74 uint32_t source_position_and_type = 176 PositionTableEntry tmp;
75 static_cast<uint32_t>(Smi::cast(table_->get(index_ + 1))->value()); 177 DecodeEntry(table_, &index_, &tmp);
76 is_statement_ = IsStatementField::decode(source_position_and_type); 178 AddAndSetEntry(current_, tmp);
77 source_position_ = SourcePositionField::decode(source_position_and_type);
78 } 179 }
79 index_ += 2;
80 } 180 }
81 181
82 } // namespace interpreter 182 } // namespace interpreter
83 } // namespace internal 183 } // namespace internal
84 } // namespace v8 184 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698