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/objects-inl.h" | 7 #include "src/objects-inl.h" |
8 #include "src/objects.h" | 8 #include "src/objects.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 namespace interpreter { | 12 namespace interpreter { |
13 | 13 |
14 // We'll use a simple encoding scheme to record the source positions. | 14 // We'll use a simple encoding scheme to record the source positions. |
15 // Conceptually, each position consists of: | 15 // Conceptually, each position consists of: |
16 // - bytecode_offset: An integer index into the BytecodeArray | 16 // - bytecode_offset: An integer index into the BytecodeArray |
17 // - source_position: An integer index into the source string. | 17 // - source_position: An integer index into the source string. |
18 // - position type: Each position is either a statement or an expression. | 18 // - position type: Each position is either a statement or an expression. |
19 // | 19 // |
20 // The basic idea for the encoding is to use a variable-length integer coding, | 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 | 21 // where each byte contains 7 bits of payload data, and 1 'more' bit that |
22 // determines whether additional bytes follow. Additionally: | 22 // determines whether additional bytes follow. Additionally: |
23 // - we record the difference from the previous position, | 23 // - we record the difference from the previous position, |
24 // - we just stuff one bit for the type into the bytecode offset, | 24 // - we just stuff one bit for the type into the bytecode offset, |
25 // - we write least-significant bits first, | 25 // - we write least-significant bits first, |
26 // - negative numbers occur only rarely, so we use a denormalized | 26 // - we use zig-zag encoding to encode both positive and negative numbers. |
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 | 27 |
31 namespace { | 28 namespace { |
32 | 29 |
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. | 30 // Each byte is encoded as MoreBit | ValueBits. |
37 class MoreBit : public BitField8<bool, 7, 1> {}; | 31 class MoreBit : public BitField8<bool, 7, 1> {}; |
38 class ValueBits : public BitField8<int, 0, 7> {}; | 32 class ValueBits : public BitField8<unsigned, 0, 7> {}; |
39 | 33 |
40 // Helper: Add the offsets from 'other' to 'value'. Also set is_statement. | 34 // Helper: Add the offsets from 'other' to 'value'. Also set is_statement. |
41 void AddAndSetEntry(PositionTableEntry& value, | 35 void AddAndSetEntry(PositionTableEntry& value, |
42 const PositionTableEntry& other) { | 36 const PositionTableEntry& other) { |
43 value.bytecode_offset += other.bytecode_offset; | 37 value.bytecode_offset += other.bytecode_offset; |
44 value.source_position += other.source_position; | 38 value.source_position += other.source_position; |
45 value.is_statement = other.is_statement; | 39 value.is_statement = other.is_statement; |
46 } | 40 } |
47 | 41 |
48 // Helper: Substract the offsets from 'other' from 'value'. | 42 // Helper: Substract the offsets from 'other' from 'value'. |
49 void SubtractFromEntry(PositionTableEntry& value, | 43 void SubtractFromEntry(PositionTableEntry& value, |
50 const PositionTableEntry& other) { | 44 const PositionTableEntry& other) { |
51 value.bytecode_offset -= other.bytecode_offset; | 45 value.bytecode_offset -= other.bytecode_offset; |
52 value.source_position -= other.source_position; | 46 value.source_position -= other.source_position; |
53 } | 47 } |
54 | 48 |
55 // Helper: Encode an integer. | 49 // Helper: Encode an integer. |
56 void EncodeInt(ZoneVector<byte>& bytes, int value) { | 50 void EncodeInt(ZoneVector<byte>& bytes, int value) { |
57 bool sign = false; | 51 // Zig-zag encoding. |
58 if (value < 0) { | 52 static const int kShift = kIntSize * kBitsPerByte - 1; |
59 sign = true; | 53 value = ((value << 1) ^ (value >> kShift)); |
60 value = -value; | 54 DCHECK_GE(value, 0); |
61 } | 55 unsigned int encoded = static_cast<unsigned int>(value); |
62 | |
63 bool more; | 56 bool more; |
64 do { | 57 do { |
65 more = value > ValueBits::kMax; | 58 more = encoded > ValueBits::kMax; |
66 bytes.push_back(MoreBit::encode(more || sign) | | 59 bytes.push_back(MoreBit::encode(more) | |
67 ValueBits::encode(value & ValueBits::kMax)); | 60 ValueBits::encode(encoded & ValueBits::kMask)); |
68 value >>= ValueBits::kSize; | 61 encoded >>= ValueBits::kSize; |
69 } while (more); | 62 } while (more); |
70 | |
71 if (sign) { | |
72 bytes.push_back(MoreBit::encode(false) | | |
73 ValueBits::encode(kNegativeSignMarker)); | |
74 } | |
75 } | 63 } |
76 | 64 |
77 // Encode a PositionTableEntry. | 65 // Encode a PositionTableEntry. |
78 void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) { | 66 void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) { |
79 // 1 bit for sign + is_statement each, which leaves 30b for the value. | 67 // We only accept ascending bytecode offsets. |
80 DCHECK(abs(entry.bytecode_offset) < (1 << 30)); | 68 DCHECK(entry.bytecode_offset >= 0); |
81 EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1)); | 69 // Since bytecode_offset is not negative, we use sign to encode is_statement. |
| 70 EncodeInt(bytes, entry.is_statement ? entry.bytecode_offset |
| 71 : -entry.bytecode_offset - 1); |
82 EncodeInt(bytes, entry.source_position); | 72 EncodeInt(bytes, entry.source_position); |
83 } | 73 } |
84 | 74 |
85 // Helper: Decode an integer. | 75 // Helper: Decode an integer. |
86 void DecodeInt(ByteArray* bytes, int* index, int* v) { | 76 void DecodeInt(ByteArray* bytes, int* index, int* v) { |
87 byte current; | 77 byte current; |
88 int n = 0; | 78 int shift = 0; |
89 int value = 0; | 79 int decoded = 0; |
90 bool more; | 80 bool more; |
91 do { | 81 do { |
92 current = bytes->get((*index)++); | 82 current = bytes->get((*index)++); |
93 value |= ValueBits::decode(current) << (n * ValueBits::kSize); | 83 decoded |= ValueBits::decode(current) << shift; |
94 n++; | |
95 more = MoreBit::decode(current); | 84 more = MoreBit::decode(current); |
| 85 shift += ValueBits::kSize; |
96 } while (more); | 86 } while (more); |
97 | 87 DCHECK_GE(decoded, 0); |
98 if (ValueBits::decode(current) == kNegativeSignMarker) { | 88 decoded = (decoded >> 1) ^ (-(decoded & 1)); |
99 value = -value; | 89 *v = decoded; |
100 } | |
101 *v = value; | |
102 } | 90 } |
103 | 91 |
104 void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) { | 92 void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) { |
105 int tmp; | 93 int tmp; |
106 DecodeInt(bytes, index, &tmp); | 94 DecodeInt(bytes, index, &tmp); |
107 entry->is_statement = (tmp & 1); | 95 if (tmp >= 0) { |
108 | 96 entry->is_statement = true; |
109 // Note that '>>' needs to be arithmetic shift in order to handle negative | 97 entry->bytecode_offset = tmp; |
110 // numbers properly. | 98 } else { |
111 entry->bytecode_offset = (tmp >> 1); | 99 entry->is_statement = false; |
112 | 100 entry->bytecode_offset = -(tmp + 1); |
| 101 } |
113 DecodeInt(bytes, index, &entry->source_position); | 102 DecodeInt(bytes, index, &entry->source_position); |
114 } | 103 } |
115 | 104 |
116 } // namespace | 105 } // namespace |
117 | 106 |
118 void SourcePositionTableBuilder::AddPosition(size_t bytecode_offset, | 107 void SourcePositionTableBuilder::AddPosition(size_t bytecode_offset, |
119 int source_position, | 108 int source_position, |
120 bool is_statement) { | 109 bool is_statement) { |
121 int offset = static_cast<int>(bytecode_offset); | 110 int offset = static_cast<int>(bytecode_offset); |
122 AddEntry({offset, source_position, is_statement}); | 111 AddEntry({offset, source_position, is_statement}); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } else { | 169 } else { |
181 PositionTableEntry tmp; | 170 PositionTableEntry tmp; |
182 DecodeEntry(table_, &index_, &tmp); | 171 DecodeEntry(table_, &index_, &tmp); |
183 AddAndSetEntry(current_, tmp); | 172 AddAndSetEntry(current_, tmp); |
184 } | 173 } |
185 } | 174 } |
186 | 175 |
187 } // namespace interpreter | 176 } // namespace interpreter |
188 } // namespace internal | 177 } // namespace internal |
189 } // namespace v8 | 178 } // namespace v8 |
OLD | NEW |