Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/interpreter/source-position-table.h" |
| 6 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 7 | #include "src/objects-inl.h" |
| 8 | #include "src/objects.h" |
| 9 | |
| 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | namespace interpreter { |
| 13 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 14 | // We'll use a simple encoding scheme to record the source positions. |
| 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 |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 117 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 118 | void SourcePositionTableBuilder::AddPosition(size_t bytecode_offset, |
| 119 | int source_position, |
| 120 | bool is_statement) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 121 | int offset = static_cast<int>(bytecode_offset); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 122 | AddEntry({offset, source_position, is_statement}); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 125 | void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 126 | PositionTableEntry tmp(entry); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 127 | SubtractFromEntry(tmp, previous_); |
| 128 | EncodeEntry(bytes_, tmp); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 129 | previous_ = entry; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 130 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 131 | if (entry.is_statement) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 132 | LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddStatementPositionEvent( |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 133 | jit_handler_data_, entry.bytecode_offset, |
| 134 | entry.source_position)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 135 | } |
| 136 | LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddPositionEvent( |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 137 | jit_handler_data_, entry.bytecode_offset, |
| 138 | entry.source_position)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 139 | |
| 140 | #ifdef ENABLE_SLOW_DCHECKS |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 141 | raw_entries_.push_back(entry); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 142 | #endif |
| 143 | } |
| 144 | |
| 145 | Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 146 | if (bytes_.empty()) return isolate_->factory()->empty_byte_array(); |
| 147 | |
| 148 | Handle<ByteArray> table = isolate_->factory()->NewByteArray( |
| 149 | static_cast<int>(bytes_.size()), TENURED); |
| 150 | |
| 151 | MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size()); |
| 152 | |
| 153 | #ifdef ENABLE_SLOW_DCHECKS |
| 154 | // Brute force testing: Record all positions and decode |
| 155 | // the entire table to verify they are identical. |
| 156 | auto raw = raw_entries_.begin(); |
| 157 | for (SourcePositionTableIterator encoded(*table); !encoded.done(); |
| 158 | encoded.Advance(), raw++) { |
| 159 | DCHECK(raw != raw_entries_.end()); |
| 160 | DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset); |
| 161 | DCHECK_EQ(encoded.source_position(), raw->source_position); |
| 162 | DCHECK_EQ(encoded.is_statement(), raw->is_statement); |
| 163 | } |
| 164 | DCHECK(raw == raw_entries_.end()); |
| 165 | #endif |
| 166 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 167 | return table; |
| 168 | } |
| 169 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 170 | SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array) |
| 171 | : table_(byte_array), index_(0), current_() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 172 | Advance(); |
| 173 | } |
| 174 | |
| 175 | void SourcePositionTableIterator::Advance() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 176 | DCHECK(!done()); |
| 177 | DCHECK(index_ >= 0 && index_ <= table_->length()); |
| 178 | if (index_ == table_->length()) { |
| 179 | index_ = kDone; |
| 180 | } else { |
| 181 | PositionTableEntry tmp; |
| 182 | DecodeEntry(table_, &index_, &tmp); |
| 183 | AddAndSetEntry(current_, tmp); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 184 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | } // namespace interpreter |
| 188 | } // namespace internal |
| 189 | } // namespace v8 |