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