blob: 99a865b84ef6344dae6620ffbd62459d60080dfe [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// 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 Murdoch097c5b22016-05-18 11:27:45 +01007#include "src/objects-inl.h"
8#include "src/objects.h"
9
10namespace v8 {
11namespace internal {
12namespace interpreter {
13
Ben Murdochda12d292016-06-02 14:46:10 +010014// 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
31namespace {
32
33// A zero-value in the most-significant byte is used to mark negative numbers.
34const int kNegativeSignMarker = 0;
35
36// Each byte is encoded as MoreBit | ValueBits.
37class MoreBit : public BitField8<bool, 7, 1> {};
38class ValueBits : public BitField8<int, 0, 7> {};
39
40// Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
41void 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'.
49void 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.
56void 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.
78void 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.
86void 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
104void 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 Murdoch097c5b22016-05-18 11:27:45 +0100117
118void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset,
119 int source_position) {
120 int offset = static_cast<int>(bytecode_offset);
Ben Murdochda12d292016-06-02 14:46:10 +0100121 AddEntry({offset, source_position, true});
Ben Murdoch097c5b22016-05-18 11:27:45 +0100122}
123
124void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset,
125 int source_position) {
126 int offset = static_cast<int>(bytecode_offset);
Ben Murdochda12d292016-06-02 14:46:10 +0100127 AddEntry({offset, source_position, false});
Ben Murdoch097c5b22016-05-18 11:27:45 +0100128}
129
Ben Murdochda12d292016-06-02 14:46:10 +0100130void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
131 // Don't encode a new entry if this bytecode already has a source position
132 // assigned.
133 if (candidate_.bytecode_offset == entry.bytecode_offset) {
134 if (entry.is_statement) candidate_ = entry;
135 return;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100136 }
Ben Murdochda12d292016-06-02 14:46:10 +0100137
138 CommitEntry();
139 candidate_ = entry;
140}
141
142void SourcePositionTableBuilder::CommitEntry() {
143 if (candidate_.bytecode_offset == kUninitializedCandidateOffset) return;
144 PositionTableEntry tmp(candidate_);
145 SubtractFromEntry(tmp, previous_);
146 EncodeEntry(bytes_, tmp);
147 previous_ = candidate_;
148
149 if (candidate_.is_statement) {
150 LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddStatementPositionEvent(
151 jit_handler_data_, candidate_.bytecode_offset,
152 candidate_.source_position));
153 }
154 LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddPositionEvent(
155 jit_handler_data_, candidate_.bytecode_offset,
156 candidate_.source_position));
157
158#ifdef ENABLE_SLOW_DCHECKS
159 raw_entries_.push_back(candidate_);
160#endif
161}
162
163Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
164 CommitEntry();
165 if (bytes_.empty()) return isolate_->factory()->empty_byte_array();
166
167 Handle<ByteArray> table = isolate_->factory()->NewByteArray(
168 static_cast<int>(bytes_.size()), TENURED);
169
170 MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());
171
172#ifdef ENABLE_SLOW_DCHECKS
173 // Brute force testing: Record all positions and decode
174 // the entire table to verify they are identical.
175 auto raw = raw_entries_.begin();
176 for (SourcePositionTableIterator encoded(*table); !encoded.done();
177 encoded.Advance(), raw++) {
178 DCHECK(raw != raw_entries_.end());
179 DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset);
180 DCHECK_EQ(encoded.source_position(), raw->source_position);
181 DCHECK_EQ(encoded.is_statement(), raw->is_statement);
182 }
183 DCHECK(raw == raw_entries_.end());
184#endif
185
Ben Murdoch097c5b22016-05-18 11:27:45 +0100186 return table;
187}
188
Ben Murdochda12d292016-06-02 14:46:10 +0100189SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
190 : table_(byte_array), index_(0), current_() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100191 Advance();
192}
193
194void SourcePositionTableIterator::Advance() {
Ben Murdochda12d292016-06-02 14:46:10 +0100195 DCHECK(!done());
196 DCHECK(index_ >= 0 && index_ <= table_->length());
197 if (index_ == table_->length()) {
198 index_ = kDone;
199 } else {
200 PositionTableEntry tmp;
201 DecodeEntry(table_, &index_, &tmp);
202 AddAndSetEntry(current_, tmp);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100203 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100204}
205
206} // namespace interpreter
207} // namespace internal
208} // namespace v8