Line data Source code
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 : #ifndef V8_SOURCE_POSITION_TABLE_H_
6 : #define V8_SOURCE_POSITION_TABLE_H_
7 :
8 : #include "src/assert-scope.h"
9 : #include "src/checks.h"
10 : #include "src/globals.h"
11 : #include "src/source-position.h"
12 : #include "src/zone/zone-containers.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : class ByteArray;
18 : template <typename T>
19 : class Handle;
20 : class Isolate;
21 : class Zone;
22 :
23 : struct PositionTableEntry {
24 : PositionTableEntry()
25 104887910 : : code_offset(0), source_position(0), is_statement(false) {}
26 : PositionTableEntry(int offset, int64_t source, bool statement)
27 23949566 : : code_offset(offset), source_position(source), is_statement(statement) {}
28 :
29 : int code_offset;
30 : int64_t source_position;
31 : bool is_statement;
32 : };
33 :
34 : class V8_EXPORT_PRIVATE SourcePositionTableBuilder {
35 : public:
36 : enum RecordingMode { OMIT_SOURCE_POSITIONS, RECORD_SOURCE_POSITIONS };
37 :
38 : SourcePositionTableBuilder(Zone* zone,
39 : RecordingMode mode = RECORD_SOURCE_POSITIONS);
40 :
41 : void AddPosition(size_t code_offset, SourcePosition source_position,
42 : bool is_statement);
43 :
44 : Handle<ByteArray> ToSourcePositionTable(Isolate* isolate);
45 :
46 : private:
47 : void AddEntry(const PositionTableEntry& entry);
48 :
49 : inline bool Omit() const { return mode_ == OMIT_SOURCE_POSITIONS; }
50 :
51 : RecordingMode mode_;
52 : ZoneVector<byte> bytes_;
53 : #ifdef ENABLE_SLOW_DCHECKS
54 : ZoneVector<PositionTableEntry> raw_entries_;
55 : #endif
56 : PositionTableEntry previous_; // Previously written entry, to compute delta.
57 : };
58 :
59 : class V8_EXPORT_PRIVATE SourcePositionTableIterator {
60 : public:
61 : // Used for saving/restoring the iterator.
62 : struct IndexAndPosition {
63 : int index_;
64 : PositionTableEntry position_;
65 : };
66 :
67 : // We expose two flavours of the iterator, depending on the argument passed
68 : // to the constructor:
69 :
70 : // Handlified iterator allows allocation, but it needs a handle (and thus
71 : // a handle scope). This is the preferred version.
72 : explicit SourcePositionTableIterator(Handle<ByteArray> byte_array);
73 :
74 : // Non-handlified iterator does not need a handle scope, but it disallows
75 : // allocation during its lifetime. This is useful if there is no handle
76 : // scope around.
77 : explicit SourcePositionTableIterator(ByteArray* byte_array);
78 :
79 : void Advance();
80 :
81 : int code_offset() const {
82 : DCHECK(!done());
83 : return current_.code_offset;
84 : }
85 : SourcePosition source_position() const {
86 : DCHECK(!done());
87 : return SourcePosition::FromRaw(current_.source_position);
88 : }
89 : bool is_statement() const {
90 : DCHECK(!done());
91 : return current_.is_statement;
92 : }
93 : bool done() const { return index_ == kDone; }
94 :
95 872 : IndexAndPosition GetState() const { return {index_, current_}; }
96 :
97 : void RestoreState(const IndexAndPosition& saved_state) {
98 872 : index_ = saved_state.index_;
99 872 : current_ = saved_state.position_;
100 : }
101 :
102 : private:
103 : static const int kDone = -1;
104 :
105 : ByteArray* raw_table_ = nullptr;
106 : Handle<ByteArray> table_;
107 : int index_ = 0;
108 : PositionTableEntry current_;
109 : DisallowHeapAllocation no_gc;
110 : };
111 :
112 : } // namespace internal
113 : } // namespace v8
114 :
115 : #endif // V8_SOURCE_POSITION_TABLE_H_
|