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 | #include "src/compiler/source-position.h" |
| 6 | #include "src/compiler/graph.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 7 | #include "src/compiler/node-aux-data.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | namespace compiler { |
| 12 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 13 | class SourcePositionTable::Decorator final : public GraphDecorator { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | public: |
| 15 | explicit Decorator(SourcePositionTable* source_positions) |
| 16 | : source_positions_(source_positions) {} |
| 17 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 18 | void Decorate(Node* node) final { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | source_positions_->table_.Set(node, source_positions_->current_position_); |
| 20 | } |
| 21 | |
| 22 | private: |
| 23 | SourcePositionTable* source_positions_; |
| 24 | }; |
| 25 | |
| 26 | |
| 27 | SourcePositionTable::SourcePositionTable(Graph* graph) |
| 28 | : graph_(graph), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 29 | decorator_(nullptr), |
| 30 | current_position_(SourcePosition::Unknown()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | table_(graph->zone()) {} |
| 32 | |
| 33 | |
| 34 | void SourcePositionTable::AddDecorator() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 35 | DCHECK_NULL(decorator_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | decorator_ = new (graph_->zone()) Decorator(this); |
| 37 | graph_->AddDecorator(decorator_); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void SourcePositionTable::RemoveDecorator() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 42 | DCHECK_NOT_NULL(decorator_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | graph_->RemoveDecorator(decorator_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 44 | decorator_ = nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 48 | SourcePosition SourcePositionTable::GetSourcePosition(Node* node) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | return table_.Get(node); |
| 50 | } |
| 51 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 52 | |
| 53 | void SourcePositionTable::Print(std::ostream& os) const { |
| 54 | os << "{"; |
| 55 | bool needs_comma = false; |
| 56 | for (auto i : table_) { |
| 57 | SourcePosition pos = i.second; |
| 58 | if (pos.IsKnown()) { |
| 59 | if (needs_comma) { |
| 60 | os << ","; |
| 61 | } |
| 62 | os << "\"" << i.first << "\"" |
| 63 | << ":" << pos.raw(); |
| 64 | needs_comma = true; |
| 65 | } |
| 66 | } |
| 67 | os << "}"; |
| 68 | } |
| 69 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | } // namespace compiler |
| 71 | } // namespace internal |
| 72 | } // namespace v8 |