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 | |
| 41 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 42 | class SourcePositionTable final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 44 | class Scope final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | public: |
| 46 | Scope(SourcePositionTable* source_positions, SourcePosition position) |
| 47 | : source_positions_(source_positions), |
| 48 | prev_position_(source_positions->current_position_) { |
| 49 | Init(position); |
| 50 | } |
| 51 | Scope(SourcePositionTable* source_positions, Node* node) |
| 52 | : source_positions_(source_positions), |
| 53 | prev_position_(source_positions->current_position_) { |
| 54 | Init(source_positions_->GetSourcePosition(node)); |
| 55 | } |
| 56 | ~Scope() { source_positions_->current_position_ = prev_position_; } |
| 57 | |
| 58 | private: |
| 59 | void Init(SourcePosition position) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 60 | if (position.IsKnown()) source_positions_->current_position_ = position; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 63 | SourcePositionTable* const source_positions_; |
| 64 | SourcePosition const prev_position_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | DISALLOW_COPY_AND_ASSIGN(Scope); |
| 66 | }; |
| 67 | |
| 68 | explicit SourcePositionTable(Graph* graph); |
| 69 | ~SourcePositionTable() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 70 | if (decorator_) RemoveDecorator(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void AddDecorator(); |
| 74 | void RemoveDecorator(); |
| 75 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 76 | SourcePosition GetSourcePosition(Node* node) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 78 | void Print(std::ostream& os) const; |
| 79 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | private: |
| 81 | class Decorator; |
| 82 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 83 | Graph* const graph_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | Decorator* decorator_; |
| 85 | SourcePosition current_position_; |
| 86 | NodeAuxData<SourcePosition> table_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(SourcePositionTable); |
| 89 | }; |
| 90 | |
| 91 | } // namespace compiler |
| 92 | } // namespace internal |
| 93 | } // namespace v8 |
| 94 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 95 | #endif // V8_COMPILER_SOURCE_POSITION_H_ |