| 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" |
| 7 #include "src/objects-inl.h" | 8 #include "src/objects-inl.h" |
| 8 #include "src/objects.h" | 9 #include "src/objects.h" |
| 9 | 10 |
| 10 namespace v8 { | 11 namespace v8 { |
| 11 namespace internal { | 12 namespace internal { |
| 12 namespace interpreter { | 13 namespace interpreter { |
| 13 | 14 |
| 14 // We'll use a simple encoding scheme to record the source positions. | 15 class IsStatementField : public BitField<bool, 0, 1> {}; |
| 15 // Conceptually, each position consists of: | 16 class SourcePositionField : public BitField<int, 1, 30> {}; |
| 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 | |
| 117 | 17 |
| 118 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, | 18 void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset, |
| 119 int source_position) { | 19 int source_position) { |
| 120 AddEntry({static_cast<int>(bytecode_offset), source_position, true}); | 20 int offset = static_cast<int>(bytecode_offset); |
| 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}); |
| 121 } | 27 } |
| 122 | 28 |
| 123 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, | 29 void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset, |
| 124 int source_position) { | 30 int source_position) { |
| 125 AddEntry({static_cast<int>(bytecode_offset), source_position, false}); | 31 int offset = static_cast<int>(bytecode_offset); |
| 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}); |
| 126 } | 38 } |
| 127 | 39 |
| 128 void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) { | 40 void SourcePositionTableBuilder::RevertPosition(size_t bytecode_offset) { |
| 129 // Don't encode a new entry if this bytecode already has a source position | 41 int offset = static_cast<int>(bytecode_offset); |
| 130 // assigned. | 42 // If we already added a source position table entry, but the bytecode array |
| 131 if (bytes_.size() > 0 && previous_.bytecode_offset == entry.bytecode_offset) { | 43 // builder ended up not outputting a bytecode for the corresponding bytecode |
| 132 return; | 44 // offset, we have to remove that entry. |
| 133 } | 45 if (CodeOffsetHasPosition(offset)) entries_.pop_back(); |
| 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 | |
| 143 } | 46 } |
| 144 | 47 |
| 145 Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() { | 48 Handle<FixedArray> SourcePositionTableBuilder::ToFixedArray() { |
| 146 Handle<ByteArray> table = isolate_->factory()->NewByteArray( | 49 int length = static_cast<int>(entries_.size()); |
| 147 static_cast<int>(bytes_.size()), TENURED); | 50 Handle<FixedArray> table = |
| 148 if (bytes_.empty()) return table; | 51 isolate_->factory()->NewFixedArray(length * 2, TENURED); |
| 149 | 52 for (int i = 0; i < length; i++) { |
| 150 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size()); | 53 table->set(i * 2, Smi::FromInt(entries_[i].bytecode_offset)); |
| 151 | 54 table->set(i * 2 + 1, Smi::FromInt(entries_[i].source_position_and_type)); |
| 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); | |
| 162 } | 55 } |
| 163 DCHECK(raw == raw_entries_.end()); | |
| 164 #endif | |
| 165 | |
| 166 return table; | 56 return table; |
| 167 } | 57 } |
| 168 | 58 |
| 169 SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array) | 59 SourcePositionTableIterator::SourcePositionTableIterator( |
| 170 : table_(byte_array), index_(0), current_() { | 60 BytecodeArray* bytecode_array) |
| 61 : table_(bytecode_array->source_position_table()), |
| 62 index_(0), |
| 63 length_(table_->length()) { |
| 64 DCHECK(table_->length() % 2 == 0); |
| 171 Advance(); | 65 Advance(); |
| 172 } | 66 } |
| 173 | 67 |
| 174 void SourcePositionTableIterator::Advance() { | 68 void SourcePositionTableIterator::Advance() { |
| 175 DCHECK(!done()); | 69 if (index_ < length_) { |
| 176 DCHECK(index_ >= 0 && index_ <= table_->length()); | 70 int new_bytecode_offset = Smi::cast(table_->get(index_))->value(); |
| 177 if (index_ == table_->length()) { | 71 // Bytecode offsets are in ascending order. |
| 178 index_ = kDone; | 72 DCHECK(bytecode_offset_ < new_bytecode_offset || index_ == 0); |
| 179 } else { | 73 bytecode_offset_ = new_bytecode_offset; |
| 180 PositionTableEntry tmp; | 74 uint32_t source_position_and_type = |
| 181 DecodeEntry(table_, &index_, &tmp); | 75 static_cast<uint32_t>(Smi::cast(table_->get(index_ + 1))->value()); |
| 182 AddAndSetEntry(current_, tmp); | 76 is_statement_ = IsStatementField::decode(source_position_and_type); |
| 77 source_position_ = SourcePositionField::decode(source_position_and_type); |
| 183 } | 78 } |
| 79 index_ += 2; |
| 184 } | 80 } |
| 185 | 81 |
| 186 } // namespace interpreter | 82 } // namespace interpreter |
| 187 } // namespace internal | 83 } // namespace internal |
| 188 } // namespace v8 | 84 } // namespace v8 |
| OLD | NEW |