summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/source-position-table.cc
blob: 99a865b84ef6344dae6620ffbd62459d60080dfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/interpreter/source-position-table.h"

#include "src/objects-inl.h"
#include "src/objects.h"

namespace v8 {
namespace internal {
namespace interpreter {

// We'll use a simple encoding scheme to record the source positions.
// Conceptually, each position consists of:
// - bytecode_offset: An integer index into the BytecodeArray
// - source_position: An integer index into the source string.
// - position type: Each position is either a statement or an expression.
//
// The basic idea for the encoding is to use a variable-length integer coding,
// where each byte contains 7 bits of payload data, and 1 'more' bit that
// determines whether additional bytes follow. Additionally:
// - we record the difference from the previous position,
// - we just stuff one bit for the type into the bytecode offset,
// - we write least-significant bits first,
// - negative numbers occur only rarely, so we use a denormalized
//   most-significant byte (a byte with all zeros, which normally wouldn't
//   make any sense) to encode a negative sign, so that we 'pay' nothing for
//   positive numbers, but have to pay a full byte for negative integers.

namespace {

// A zero-value in the most-significant byte is used to mark negative numbers.
const int kNegativeSignMarker = 0;

// Each byte is encoded as MoreBit | ValueBits.
class MoreBit : public BitField8<bool, 7, 1> {};
class ValueBits : public BitField8<int, 0, 7> {};

// Helper: Add the offsets from 'other' to 'value'. Also set is_statement.
void AddAndSetEntry(PositionTableEntry& value,
                    const PositionTableEntry& other) {
  value.bytecode_offset += other.bytecode_offset;
  value.source_position += other.source_position;
  value.is_statement = other.is_statement;
}

// Helper: Substract the offsets from 'other' from 'value'.
void SubtractFromEntry(PositionTableEntry& value,
                       const PositionTableEntry& other) {
  value.bytecode_offset -= other.bytecode_offset;
  value.source_position -= other.source_position;
}

// Helper: Encode an integer.
void EncodeInt(ZoneVector<byte>& bytes, int value) {
  bool sign = false;
  if (value < 0) {
    sign = true;
    value = -value;
  }

  bool more;
  do {
    more = value > ValueBits::kMax;
    bytes.push_back(MoreBit::encode(more || sign) |
                    ValueBits::encode(value & ValueBits::kMax));
    value >>= ValueBits::kSize;
  } while (more);

  if (sign) {
    bytes.push_back(MoreBit::encode(false) |
                    ValueBits::encode(kNegativeSignMarker));
  }
}

// Encode a PositionTableEntry.
void EncodeEntry(ZoneVector<byte>& bytes, const PositionTableEntry& entry) {
  // 1 bit for sign + is_statement each, which leaves 30b for the value.
  DCHECK(abs(entry.bytecode_offset) < (1 << 30));
  EncodeInt(bytes, (entry.is_statement ? 1 : 0) | (entry.bytecode_offset << 1));
  EncodeInt(bytes, entry.source_position);
}

// Helper: Decode an integer.
void DecodeInt(ByteArray* bytes, int* index, int* v) {
  byte current;
  int n = 0;
  int value = 0;
  bool more;
  do {
    current = bytes->get((*index)++);
    value |= ValueBits::decode(current) << (n * ValueBits::kSize);
    n++;
    more = MoreBit::decode(current);
  } while (more);

  if (ValueBits::decode(current) == kNegativeSignMarker) {
    value = -value;
  }
  *v = value;
}

void DecodeEntry(ByteArray* bytes, int* index, PositionTableEntry* entry) {
  int tmp;
  DecodeInt(bytes, index, &tmp);
  entry->is_statement = (tmp & 1);

  // Note that '>>' needs to be arithmetic shift in order to handle negative
  // numbers properly.
  entry->bytecode_offset = (tmp >> 1);

  DecodeInt(bytes, index, &entry->source_position);
}

}  // namespace

void SourcePositionTableBuilder::AddStatementPosition(size_t bytecode_offset,
                                                      int source_position) {
  int offset = static_cast<int>(bytecode_offset);
  AddEntry({offset, source_position, true});
}

void SourcePositionTableBuilder::AddExpressionPosition(size_t bytecode_offset,
                                                       int source_position) {
  int offset = static_cast<int>(bytecode_offset);
  AddEntry({offset, source_position, false});
}

void SourcePositionTableBuilder::AddEntry(const PositionTableEntry& entry) {
  // Don't encode a new entry if this bytecode already has a source position
  // assigned.
  if (candidate_.bytecode_offset == entry.bytecode_offset) {
    if (entry.is_statement) candidate_ = entry;
    return;
  }

  CommitEntry();
  candidate_ = entry;
}

void SourcePositionTableBuilder::CommitEntry() {
  if (candidate_.bytecode_offset == kUninitializedCandidateOffset) return;
  PositionTableEntry tmp(candidate_);
  SubtractFromEntry(tmp, previous_);
  EncodeEntry(bytes_, tmp);
  previous_ = candidate_;

  if (candidate_.is_statement) {
    LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddStatementPositionEvent(
                                 jit_handler_data_, candidate_.bytecode_offset,
                                 candidate_.source_position));
  }
  LOG_CODE_EVENT(isolate_, CodeLinePosInfoAddPositionEvent(
                               jit_handler_data_, candidate_.bytecode_offset,
                               candidate_.source_position));

#ifdef ENABLE_SLOW_DCHECKS
  raw_entries_.push_back(candidate_);
#endif
}

Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable() {
  CommitEntry();
  if (bytes_.empty()) return isolate_->factory()->empty_byte_array();

  Handle<ByteArray> table = isolate_->factory()->NewByteArray(
      static_cast<int>(bytes_.size()), TENURED);

  MemCopy(table->GetDataStartAddress(), &*bytes_.begin(), bytes_.size());

#ifdef ENABLE_SLOW_DCHECKS
  // Brute force testing: Record all positions and decode
  // the entire table to verify they are identical.
  auto raw = raw_entries_.begin();
  for (SourcePositionTableIterator encoded(*table); !encoded.done();
       encoded.Advance(), raw++) {
    DCHECK(raw != raw_entries_.end());
    DCHECK_EQ(encoded.bytecode_offset(), raw->bytecode_offset);
    DCHECK_EQ(encoded.source_position(), raw->source_position);
    DCHECK_EQ(encoded.is_statement(), raw->is_statement);
  }
  DCHECK(raw == raw_entries_.end());
#endif

  return table;
}

SourcePositionTableIterator::SourcePositionTableIterator(ByteArray* byte_array)
    : table_(byte_array), index_(0), current_() {
  Advance();
}

void SourcePositionTableIterator::Advance() {
  DCHECK(!done());
  DCHECK(index_ >= 0 && index_ <= table_->length());
  if (index_ == table_->length()) {
    index_ = kDone;
  } else {
    PositionTableEntry tmp;
    DecodeEntry(table_, &index_, &tmp);
    AddAndSetEntry(current_, tmp);
  }
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8