Chromium Code Reviews| OLD | NEW |
|---|---|
| 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)); | |
| 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)); | |
| 86 EncodeInt(bytes, entry.source_position); | |
| 87 } | |
| 88 | |
| 89 // Helper: Decode an integer. | |
| 90 void DecodeInt(ByteArray* bytes, int* index, int* v) { | |
| 91 byte current; | |
| 92 int n = 0; | |
| 93 int value = 0; | |
| 94 bool more; | |
| 95 do { | |
| 96 current = bytes->get((*index)++); | |
| 97 value |= ValueBits::decode(current) << (n * ValueBits::kSize); | |
| 98 n++; | |
| 99 more = MoreBit::decode(current); | |
| 100 } while (more); | |
| 101 | |
| 102 if (ValueBits::decode(current) == 0) { | |
| 103 value = -value; | |
| 104 } | |
| 105 *v = value; | |
| 106 } | |
| 107 | |
| 108 void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) { | |
| 109 int tmp; | |
| 110 DecodeInt(bytes, index, &tmp); | |
| 111 entry->is_statement = TypeBit::decode(tmp); | |
| 112 | |
| 113 // Directly shift tmp, because '>>' needs to be arithmetic shift in order to | |
| 114 // handle negative numbers properly. | |
| 115 entry->bytecode_offset = (tmp >> BytecodeOffsetBits::kShift); | |
| 116 | |
| 117 DecodeInt(bytes, index, &entry->source_position); | |
| 118 } | |
| 119 | |
| 120 } // namespace | |
| 17 | 121 |
| 18 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, | 122 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, |
| 19 int source_position) { | 123 int source_position) { |
| 20 int offset = static_cast<int>(bytecode_offset); | 124 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 } | 125 } |
| 28 | 126 |
| 29 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, | 127 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, |
| 30 int source_position) { | 128 int source_position) { |
| 31 int offset = static_cast<int>(bytecode_offset); | 129 AddEntry({static_cast<int>(bytecode_offset), source_position, false}); |
| 32 // If a position has already been assigned to this bytecode offset, | 130 } |
| 33 // do not reassign a new statement position. | 131 |
| 34 if (CodeOffsetHasPosition(offset)) return; | 132 void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) { |
| 35 uint32_t encoded = IsStatementField::encode(false) | | 133 if (!CodeOffsetHasPosition(entry.bytecode_offset)) { |
| 36 SourcePositionField::encode(source_position); | 134 EncodeCurrent(); |
| 37 entries_.push_back({offset, encoded}); | 135 current_ = entry; |
| 136 has_current_ = true; | |
| 137 | |
| 138 #ifdef ENABLE_SLOW_DCHECKS | |
| 139 raw_entries_.push_back(entry); | |
| 140 #endif | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void SourcePositionTableBuilder::EncodeCurrent() { | |
| 145 if (has_current_) { | |
| 146 PositionTableEntry tmp(current_); | |
| 147 SubtractFromEntry(tmp, previous_); | |
| 148 EncodeEntry(bytes_, tmp); | |
| 149 previous_ = current_; | |
| 150 has_current_ = false; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 bool SourcePositionTableBuilder::CodeOffsetHasPosition( | |
| 155 int bytecode_offset) const { | |
| 156 // Return whether bytecode offset already has a position assigned. | |
| 157 int most_recent_bytecode_offset = | |
| 158 (has_current_ ? current_ : previous_).bytecode_offset; | |
| 159 return bytes_.size() > 0 && (most_recent_bytecode_offset == bytecode_offset); | |
| 38 } | 160 } |
| 39 | 161 |
| 40 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { | 162 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { |
|
rmcilroy
2016/02/22 12:38:40
I much prefer what you've done here (in PS 5 and 6
vogelheim
2016/02/22 18:33:33
Done.
| |
| 41 int offset = static_cast<int>(bytecode_offset); | 163 int offset = static_cast<int>(bytecode_offset); |
| 42 // If we already added a source position table entry, but the bytecode array | 164 // 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 | 165 // builder ended up not outputting a bytecode for the corresponding bytecode |
| 44 // offset, we have to remove that entry. | 166 // offset, we have to remove that entry. |
| 45 if (CodeOffsetHasPosition(offset)) entries_.pop_back(); | 167 if (CodeOffsetHasPosition(offset)) { |
| 168 DCHECK(has_current_); | |
| 169 has_current_ = false; | |
| 170 | |
| 171 #ifdef ENABLE_SLOW_DCHECKS | |
| 172 raw_entries_.pop_back(); | |
| 173 #endif | |
| 174 } | |
| 46 } | 175 } |
| 47 | 176 |
| 48 Handle<FixedArray> SourcePositionTableBuilder::ToFixedArray() { | 177 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() { |
| 49 int length = static_cast<int>(entries_.size()); | 178 EncodeCurrent(); |
| 50 Handle<FixedArray> table = | 179 |
| 51 isolate_->factory()->NewFixedArray(length * 2, TENURED); | 180 Handle<ByteArray> table = isolate_->factory()->NewByteArray( |
| 52 for (int i = 0; i < length; i++) { | 181 static_cast<int>(bytes_.size()), TENURED); |
| 53 table->set(i * 2, Smi::FromInt(entries_[i].bytecode_offset)); | 182 if (bytes_.empty()) return table; |
| 54 table->set(i * 2 + 1, Smi::FromInt(entries_[i].source_position_and_type)); | 183 |
| 184 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size()); | |
| 185 | |
| 186 #ifdef ENABLE_SLOW_DCHECKS | |
| 187 // Brute force testing: Record all positions and decode | |
| 188 // the entire table to verify they are identical. | |
| 189 auto raw = raw_entries_.begin(); | |
| 190 for (SourcePositionTableIterator encoded(*table); !encoded.done(); | |
| 191 encoded.Advance(), raw++) { | |
| 192 DCHECK(raw != raw_entries_.end()); | |
| 193 DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset); | |
| 194 DCHECK_EQ(encoded.source_position(), raw->source_position); | |
| 195 DCHECK_EQ(encoded.is_statement(), raw->is_statement); | |
| 55 } | 196 } |
| 197 DCHECK(raw == raw_entries_.end()); | |
| 198 #endif | |
| 199 | |
| 56 return table; | 200 return table; |
| 57 } | 201 } |
| 58 | 202 |
| 59 SourcePositionTableIterator::SourcePositionTableIterator( | 203 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array) |
| 60 BytecodeArray* bytecode_array) | 204 : 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(); | 205 Advance(); |
| 66 } | 206 } |
| 67 | 207 |
| 68 void SourcePositionTableIterator::Advance() { | 208 void SourcePositionTableIterator::Advance() { |
| 69 if (index_ < length_) { | 209 DCHECK(!done()); |
| 70 int new_bytecode_offset = Smi::cast(table_->get(index_))->value(); | 210 DCHECK(index_ >= 0 && index_ <= table_->length()); |
| 71 // Bytecode offsets are in ascending order. | 211 if (index_ == table_->length()) { |
| 72 DCHECK(bytecode_offset_ < new_bytecode_offset || index_ == 0); | 212 index_ = kDone; |
| 73 bytecode_offset_ = new_bytecode_offset; | 213 } else { |
| 74 uint32_t source_position_and_type = | 214 PositionTableEntry tmp; |
| 75 static_cast<uint32_t>(Smi::cast(table_->get(index_ + 1))->value()); | 215 DecodeEntry(table_, &index_, &tmp); |
| 76 is_statement_ = IsStatementField::decode(source_position_and_type); | 216 AddAndSetEntry(current_, tmp); |
| 77 source_position_ = SourcePositionField::decode(source_position_and_type); | |
| 78 } | 217 } |
| 79 index_ += 2; | |
| 80 } | 218 } |
| 81 | 219 |
| 82 } // namespace interpreter | 220 } // namespace interpreter |
| 83 } // namespace internal | 221 } // namespace internal |
| 84 } // namespace v8 | 222 } // namespace v8 |
| OLD | NEW |