blob: 81db1d2e3eff4f97d0f93e484b83d321b8115259 [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#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
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15// Encapsulates encoding and decoding of sources positions from which Nodes
16// originated.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017class SourcePosition final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018 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 Murdoch4a90d5f2016-03-22 12:00:34 +000023 bool IsKnown() const { return raw() != kUnknownPosition; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
25 int raw() const { return raw_; }
26
27 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 static const int kUnknownPosition = RelocInfo::kNoPosition;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029 int raw_;
30};
31
32
33inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) {
34 return lhs.raw() == rhs.raw();
35}
36
37inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) {
38 return !(lhs == rhs);
39}
40
41
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042class SourcePositionTable final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 class Scope final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 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 Murdoch4a90d5f2016-03-22 12:00:34 +000060 if (position.IsKnown()) source_positions_->current_position_ = position;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 }
62
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 SourcePositionTable* const source_positions_;
64 SourcePosition const prev_position_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 DISALLOW_COPY_AND_ASSIGN(Scope);
66 };
67
68 explicit SourcePositionTable(Graph* graph);
69 ~SourcePositionTable() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 if (decorator_) RemoveDecorator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 }
72
73 void AddDecorator();
74 void RemoveDecorator();
75
Emily Bernierd0a1eb72015-03-24 16:35:39 -040076 SourcePosition GetSourcePosition(Node* node) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 void Print(std::ostream& os) const;
79
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 private:
81 class Decorator;
82
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 Graph* const graph_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 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 Murdoch4a90d5f2016-03-22 12:00:34 +000095#endif // V8_COMPILER_SOURCE_POSITION_H_