blob: 3ac58d621787939383f8d442f241138e06df05a5 [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#ifndef V8_INTERPRETER_SOURCE_POSITION_TABLE_H_
6#define V8_INTERPRETER_SOURCE_POSITION_TABLE_H_
7
8#include "src/assert-scope.h"
Ben Murdochda12d292016-06-02 14:46:10 +01009#include "src/checks.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010010#include "src/handles.h"
Ben Murdochda12d292016-06-02 14:46:10 +010011#include "src/log.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010012#include "src/zone-containers.h"
13
14namespace v8 {
15namespace internal {
16
17class BytecodeArray;
Ben Murdochda12d292016-06-02 14:46:10 +010018class ByteArray;
Ben Murdoch097c5b22016-05-18 11:27:45 +010019class Isolate;
Ben Murdochda12d292016-06-02 14:46:10 +010020class Zone;
Ben Murdoch097c5b22016-05-18 11:27:45 +010021
22namespace interpreter {
23
Ben Murdochda12d292016-06-02 14:46:10 +010024struct PositionTableEntry {
25 PositionTableEntry()
26 : bytecode_offset(0), source_position(0), is_statement(false) {}
27 PositionTableEntry(int bytecode, int source, bool statement)
28 : bytecode_offset(bytecode),
29 source_position(source),
30 is_statement(statement) {}
31
32 int bytecode_offset;
33 int source_position;
34 bool is_statement;
35};
36
37class SourcePositionTableBuilder : public PositionsRecorder {
Ben Murdoch097c5b22016-05-18 11:27:45 +010038 public:
Ben Murdochda12d292016-06-02 14:46:10 +010039 SourcePositionTableBuilder(Isolate* isolate, Zone* zone)
40 : isolate_(isolate),
41 bytes_(zone),
42#ifdef ENABLE_SLOW_DCHECKS
43 raw_entries_(zone),
44#endif
45 candidate_(kUninitializedCandidateOffset, 0, false) {
46 }
Ben Murdoch097c5b22016-05-18 11:27:45 +010047
48 void AddStatementPosition(size_t bytecode_offset, int source_position);
49 void AddExpressionPosition(size_t bytecode_offset, int source_position);
Ben Murdochda12d292016-06-02 14:46:10 +010050 Handle<ByteArray> ToSourcePositionTable();
Ben Murdoch097c5b22016-05-18 11:27:45 +010051
52 private:
Ben Murdochda12d292016-06-02 14:46:10 +010053 static const int kUninitializedCandidateOffset = -1;
Ben Murdoch097c5b22016-05-18 11:27:45 +010054
Ben Murdochda12d292016-06-02 14:46:10 +010055 void AddEntry(const PositionTableEntry& entry);
56 void CommitEntry();
Ben Murdoch097c5b22016-05-18 11:27:45 +010057
58 Isolate* isolate_;
Ben Murdochda12d292016-06-02 14:46:10 +010059 ZoneVector<byte> bytes_;
60#ifdef ENABLE_SLOW_DCHECKS
61 ZoneVector<PositionTableEntry> raw_entries_;
62#endif
63 PositionTableEntry candidate_; // Next entry to be written, if initialized.
64 PositionTableEntry previous_; // Previously written entry, to compute delta.
Ben Murdoch097c5b22016-05-18 11:27:45 +010065};
66
67class SourcePositionTableIterator {
68 public:
Ben Murdochda12d292016-06-02 14:46:10 +010069 explicit SourcePositionTableIterator(ByteArray* byte_array);
Ben Murdoch097c5b22016-05-18 11:27:45 +010070
71 void Advance();
72
73 int bytecode_offset() const {
74 DCHECK(!done());
Ben Murdochda12d292016-06-02 14:46:10 +010075 return current_.bytecode_offset;
Ben Murdoch097c5b22016-05-18 11:27:45 +010076 }
77 int source_position() const {
78 DCHECK(!done());
Ben Murdochda12d292016-06-02 14:46:10 +010079 return current_.source_position;
Ben Murdoch097c5b22016-05-18 11:27:45 +010080 }
81 bool is_statement() const {
82 DCHECK(!done());
Ben Murdochda12d292016-06-02 14:46:10 +010083 return current_.is_statement;
Ben Murdoch097c5b22016-05-18 11:27:45 +010084 }
Ben Murdochda12d292016-06-02 14:46:10 +010085 bool done() const { return index_ == kDone; }
Ben Murdoch097c5b22016-05-18 11:27:45 +010086
87 private:
Ben Murdochda12d292016-06-02 14:46:10 +010088 static const int kDone = -1;
89
90 ByteArray* table_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010091 int index_;
Ben Murdochda12d292016-06-02 14:46:10 +010092 PositionTableEntry current_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010093 DisallowHeapAllocation no_gc;
94};
95
96} // namespace interpreter
97} // namespace internal
98} // namespace v8
99
100#endif // V8_INTERPRETER_SOURCE_POSITION_TABLE_H_