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. 17 class SourcePosition final { 18 public: raw_(raw)19 explicit SourcePosition(int raw = kUnknownPosition) : raw_(raw) {} 20 Unknown()21 static SourcePosition Unknown() { return SourcePosition(kUnknownPosition); } IsUnknown()22 bool IsUnknown() const { return raw() == kUnknownPosition; } IsKnown()23 bool IsKnown() const { return raw() != kUnknownPosition; } 24 raw()25 int raw() const { return raw_; } 26 27 private: 28 static const int kUnknownPosition = RelocInfo::kNoPosition; 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 class SourcePositionTable final : public ZoneObject { 42 public: 43 class Scope final { 44 public: Scope(SourcePositionTable * source_positions,SourcePosition position)45 Scope(SourcePositionTable* source_positions, SourcePosition position) 46 : source_positions_(source_positions), 47 prev_position_(source_positions->current_position_) { 48 Init(position); 49 } Scope(SourcePositionTable * source_positions,Node * node)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 } ~Scope()55 ~Scope() { source_positions_->current_position_ = prev_position_; } 56 57 private: Init(SourcePosition position)58 void Init(SourcePosition position) { 59 if (position.IsKnown()) source_positions_->current_position_ = position; 60 } 61 62 SourcePositionTable* const source_positions_; 63 SourcePosition const prev_position_; 64 DISALLOW_COPY_AND_ASSIGN(Scope); 65 }; 66 67 explicit SourcePositionTable(Graph* graph); 68 69 void AddDecorator(); 70 void RemoveDecorator(); 71 72 SourcePosition GetSourcePosition(Node* node) const; 73 void SetSourcePosition(Node* node, SourcePosition position); 74 75 void Print(std::ostream& os) const; 76 77 private: 78 class Decorator; 79 80 Graph* const graph_; 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 92 #endif // V8_COMPILER_SOURCE_POSITION_H_ 93