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 AbstractCode;
18 class BytecodeArray;
19 class ByteArray;
20 template <typename T>
21 class Handle;
22 class Isolate;
23 class Zone;
24 
25 struct PositionTableEntry {
PositionTableEntryPositionTableEntry26   PositionTableEntry()
27       : code_offset(0), source_position(0), is_statement(false) {}
PositionTableEntryPositionTableEntry28   PositionTableEntry(int offset, int64_t source, bool statement)
29       : code_offset(offset), source_position(source), is_statement(statement) {}
30 
31   int code_offset;
32   int64_t source_position;
33   bool is_statement;
34 };
35 
36 class V8_EXPORT_PRIVATE SourcePositionTableBuilder {
37  public:
38   enum RecordingMode { OMIT_SOURCE_POSITIONS, RECORD_SOURCE_POSITIONS };
39 
40   SourcePositionTableBuilder(Zone* zone,
41                              RecordingMode mode = RECORD_SOURCE_POSITIONS);
42 
43   void AddPosition(size_t code_offset, SourcePosition source_position,
44                    bool is_statement);
45 
46   Handle<ByteArray> ToSourcePositionTable(Isolate* isolate,
47                                           Handle<AbstractCode> code);
48 
49  private:
50   void AddEntry(const PositionTableEntry& entry);
51 
Omit()52   inline bool Omit() const { return mode_ == OMIT_SOURCE_POSITIONS; }
53 
54   RecordingMode mode_;
55   ZoneVector<byte> bytes_;
56 #ifdef ENABLE_SLOW_DCHECKS
57   ZoneVector<PositionTableEntry> raw_entries_;
58 #endif
59   PositionTableEntry previous_;  // Previously written entry, to compute delta.
60 };
61 
62 class V8_EXPORT_PRIVATE SourcePositionTableIterator {
63  public:
64   explicit SourcePositionTableIterator(ByteArray* byte_array);
65 
66   void Advance();
67 
code_offset()68   int code_offset() const {
69     DCHECK(!done());
70     return current_.code_offset;
71   }
source_position()72   SourcePosition source_position() const {
73     DCHECK(!done());
74     return SourcePosition::FromRaw(current_.source_position);
75   }
is_statement()76   bool is_statement() const {
77     DCHECK(!done());
78     return current_.is_statement;
79   }
done()80   bool done() const { return index_ == kDone; }
81 
82  private:
83   static const int kDone = -1;
84 
85   ByteArray* table_;
86   int index_;
87   PositionTableEntry current_;
88   DisallowHeapAllocation no_gc;
89 };
90 
91 }  // namespace internal
92 }  // namespace v8
93 
94 #endif  // V8_SOURCE_POSITION_TABLE_H_
95