blob: 48361ecac771d92a2899fae83dcc014b02002424 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/compiler/node-aux-data.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008
9namespace v8 {
10namespace internal {
11namespace compiler {
12
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013class SourcePositionTable::Decorator final : public GraphDecorator {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014 public:
15 explicit Decorator(SourcePositionTable* source_positions)
16 : source_positions_(source_positions) {}
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018 void Decorate(Node* node) final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019 source_positions_->table_.Set(node, source_positions_->current_position_);
20 }
21
22 private:
23 SourcePositionTable* source_positions_;
24};
25
26
27SourcePositionTable::SourcePositionTable(Graph* graph)
28 : graph_(graph),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 decorator_(nullptr),
30 current_position_(SourcePosition::Unknown()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 table_(graph->zone()) {}
32
33
34void SourcePositionTable::AddDecorator() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 DCHECK_NULL(decorator_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 decorator_ = new (graph_->zone()) Decorator(this);
37 graph_->AddDecorator(decorator_);
38}
39
40
41void SourcePositionTable::RemoveDecorator() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 DCHECK_NOT_NULL(decorator_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 graph_->RemoveDecorator(decorator_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 decorator_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045}
46
47
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048SourcePosition SourcePositionTable::GetSourcePosition(Node* node) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 return table_.Get(node);
50}
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052
53void 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 Murdochb8a8cc12014-11-26 15:28:44 +000070} // namespace compiler
71} // namespace internal
72} // namespace v8