OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ | 5 #ifndef V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ |
6 #define V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ | 6 #define V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ |
7 | 7 |
8 #include "src/assert-scope.h" | 8 #include "src/assert-scope.h" |
9 #include "src/checks.h" | |
9 #include "src/handles.h" | 10 #include "src/handles.h" |
10 #include "src/zone.h" | |
11 #include "src/zone-containers.h" | 11 #include "src/zone-containers.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 class BytecodeArray; | 16 class BytecodeArray; |
17 class FixedArray; | 17 class ByteArray; |
18 class Isolate; | 18 class Isolate; |
19 class Zone; | |
19 | 20 |
20 namespace interpreter { | 21 namespace interpreter { |
21 | 22 |
23 struct PositionTableEntry { | |
24 int bytecode_offset; | |
25 int source_position; | |
26 bool is_statement; | |
27 }; | |
28 | |
29 // Helper class for SourcePositionTableBuilder + SourcePositionTableIterator. | |
30 // Each instance is meant for either encoding or decoding, but not both. | |
31 class SourcePositionTableCodec { | |
Yang
2016/02/17 19:54:51
I expect this to be only used by the builder and t
vogelheim
2016/02/18 13:06:09
I removed it entirely, per your other suggestion.
| |
32 public: | |
33 SourcePositionTableCodec(); | |
34 ~SourcePositionTableCodec(); | |
35 | |
36 void Encode(ZoneVector<byte>& bytes, PositionTableEntry entry); | |
37 void EncodeRevertPosition(ZoneVector<byte>& bytes, int bytecode_offset); | |
38 void Decode(const byte* bytes, int* index, PositionTableEntry* entry); | |
39 int GetPreviousBytecodeOffset() const { return previous_bytecode_offset_; } | |
40 | |
41 private: | |
42 int previous_bytecode_offset_; | |
43 int previous_source_position_; | |
44 }; | |
45 | |
22 class SourcePositionTableBuilder { | 46 class SourcePositionTableBuilder { |
23 public: | 47 public: |
24 explicit SourcePositionTableBuilder(Isolate* isolate, Zone* zone) | 48 explicit SourcePositionTableBuilder(Isolate* isolate, Zone* zone) |
25 : isolate_(isolate), entries_(zone) {} | 49 : isolate_(isolate), bytes_(zone) {} |
26 | 50 |
27 void AddStatementPosition(size_t bytecode_offset, int source_position); | 51 void AddStatementPosition(size_t bytecode_offset, int source_position); |
28 void AddExpressionPosition(size_t bytecode_offset, int source_position); | 52 void AddExpressionPosition(size_t bytecode_offset, int source_position); |
29 void RevertPosition(size_t bytecode_offset); | 53 void RevertPosition(size_t bytecode_offset); |
30 Handle<FixedArray> ToFixedArray(); | 54 Handle<ByteArray> ToSourcePositionTable(); |
31 | 55 |
32 private: | 56 private: |
33 struct Entry { | 57 void AddEntry(const PositionTableEntry& entry); |
34 int bytecode_offset; | |
35 uint32_t source_position_and_type; | |
36 }; | |
37 | |
38 bool CodeOffsetHasPosition(int bytecode_offset) { | 58 bool CodeOffsetHasPosition(int bytecode_offset) { |
39 // Return whether bytecode offset already has a position assigned. | 59 // Return whether bytecode offset already has a position assigned. |
40 return entries_.size() > 0 && | 60 return bytes_.size() > 0 && |
41 entries_.back().bytecode_offset == bytecode_offset; | 61 codec_.GetPreviousBytecodeOffset() == bytecode_offset; |
42 } | 62 } |
43 | 63 |
44 Isolate* isolate_; | 64 Isolate* isolate_; |
45 ZoneVector<Entry> entries_; | 65 ZoneVector<byte> bytes_; |
66 SourcePositionTableCodec codec_; | |
67 | |
68 #ifdef ENABLE_SLOW_DCHECKS | |
69 std::vector<PositionTableEntry> raw_entries_; | |
70 #endif | |
46 }; | 71 }; |
47 | 72 |
48 class SourcePositionTableIterator { | 73 class SourcePositionTableIterator { |
49 public: | 74 public: |
50 explicit SourcePositionTableIterator(BytecodeArray* bytecode_array); | 75 explicit SourcePositionTableIterator(BytecodeArray* bytecode_array); |
51 | 76 |
52 void Advance(); | 77 void Advance(); |
53 | 78 |
54 int bytecode_offset() const { | 79 int bytecode_offset() const { |
55 DCHECK(!done()); | 80 DCHECK(!done()); |
56 return bytecode_offset_; | 81 return current_.bytecode_offset; |
57 } | 82 } |
58 int source_position() const { | 83 int source_position() const { |
59 DCHECK(!done()); | 84 DCHECK(!done()); |
60 return source_position_; | 85 return current_.source_position; |
61 } | 86 } |
62 bool is_statement() const { | 87 bool is_statement() const { |
63 DCHECK(!done()); | 88 DCHECK(!done()); |
64 return is_statement_; | 89 return current_.is_statement; |
65 } | 90 } |
66 bool done() const { return index_ > length_; } | 91 bool done() const { return index_ == kDone; } |
67 | 92 |
68 private: | 93 private: |
69 FixedArray* table_; | 94 static const int kDone = -1; |
95 | |
96 // Allow access to private constructor to implement the testing functionality. | |
97 friend Handle<ByteArray> SourcePositionTableBuilder::ToSourcePositionTable(); | |
98 explicit SourcePositionTableIterator(ByteArray* byte_array); | |
Yang
2016/02/17 19:54:51
I'd actually be perfectly fine if we changed the p
vogelheim
2016/02/18 13:06:09
Done. I like your version better.
| |
99 | |
100 ByteArray* table_; | |
70 int index_; | 101 int index_; |
71 int length_; | 102 SourcePositionTableCodec codec_; |
72 bool is_statement_; | 103 PositionTableEntry current_; |
73 int bytecode_offset_; | |
74 int source_position_; | |
75 DisallowHeapAllocation no_gc; | 104 DisallowHeapAllocation no_gc; |
76 }; | 105 }; |
77 | 106 |
78 } // namespace interpreter | 107 } // namespace interpreter |
79 } // namespace internal | 108 } // namespace internal |
80 } // namespace v8 | 109 } // namespace v8 |
81 | 110 |
82 #endif // V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ | 111 #endif // V8_INTERPRETER_SOURCE_POSITION_TABLE_H_ |
OLD | NEW |