Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 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_COMPILER_SOURCE_POSITION_H_ |
| 6 | #define V8_COMPILER_SOURCE_POSITION_H_ |
| 7 | |
| 8 | #include "src/assembler.h" |
| 9 | #include "src/compiler/node-aux-data.h" |
| 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | namespace compiler { |
| 14 | |
| 15 | // Encapsulates encoding and decoding of sources positions from which Nodes |
| 16 | // originated. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | class SourcePosition final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | public: |
| 19 | explicit SourcePosition(int raw = kUnknownPosition) : raw_(raw) {} |
| 20 | |
| 21 | static SourcePosition Unknown() { return SourcePosition(kUnknownPosition); } |
| 22 | bool IsUnknown() const { return raw() == kUnknownPosition; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 23 | bool IsKnown() const { return raw() != kUnknownPosition; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | |
| 25 | int raw() const { return raw_; } |
| 26 | |
| 27 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | static const int kUnknownPosition = RelocInfo::kNoPosition; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 29 | int raw_; |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) { |
| 34 | return lhs.raw() == rhs.raw(); |
| 35 | } |
| 36 | |
| 37 | inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) { |
| 38 | return !(lhs == rhs); |
| 39 | } |
| 40 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 41 | class SourcePositionTable final : public ZoneObject { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 43 | class Scope final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | public: |
| 45 | Scope(SourcePositionTable* source_positions, SourcePosition position) |
| 46 | : source_positions_(source_positions), |
| 47 | prev_position_(source_positions->current_position_) { |
| 48 | Init(position); |
| 49 | } |
| 50 | Scope(SourcePositionTable* source_positions, Node* node) |
| 51 | : source_positions_(source_positions), |
| 52 | prev_position_(source_positions->current_position_) { |
| 53 | Init(source_positions_->GetSourcePosition(node)); |
| 54 | } |
| 55 | ~Scope() { source_positions_->current_position_ = prev_position_; } |
| 56 | |
| 57 | private: |
| 58 | void Init(SourcePosition position) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | if (position.IsKnown()) source_positions_->current_position_ = position; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 62 | SourcePositionTable* const source_positions_; |
| 63 | SourcePosition const prev_position_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | DISALLOW_COPY_AND_ASSIGN(Scope); |
| 65 | }; |
| 66 | |
| 67 | explicit SourcePositionTable(Graph* graph); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | |
| 69 | void AddDecorator(); |
| 70 | void RemoveDecorator(); |
| 71 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 72 | SourcePosition GetSourcePosition(Node* node) const; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 73 | void SetSourcePosition(Node* node, SourcePosition position); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 75 | void Print(std::ostream& os) const; |
| 76 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | private: |
| 78 | class Decorator; |
| 79 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | Graph* const graph_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | Decorator* decorator_; |
| 82 | SourcePosition current_position_; |
| 83 | NodeAuxData<SourcePosition> table_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(SourcePositionTable); |
| 86 | }; |
| 87 | |
| 88 | } // namespace compiler |
| 89 | } // namespace internal |
| 90 | } // namespace v8 |
| 91 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | #endif // V8_COMPILER_SOURCE_POSITION_H_ |