OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_SOURCE_POSITION_H_ | 5 #ifndef V8_SOURCE_POSITION_H_ |
6 #define V8_SOURCE_POSITION_H_ | 6 #define V8_SOURCE_POSITION_H_ |
7 | 7 |
8 #include <ostream> | 8 #include <ostream> |
9 | 9 |
10 #include "src/flags.h" | 10 #include "src/flags.h" |
11 #include "src/globals.h" | 11 #include "src/globals.h" |
12 #include "src/handles.h" | |
12 #include "src/utils.h" | 13 #include "src/utils.h" |
13 | 14 |
14 namespace v8 { | 15 namespace v8 { |
15 namespace internal { | 16 namespace internal { |
16 | 17 |
17 // This class encapsulates encoding and decoding of sources positions from | 18 class Code; |
18 // which hydrogen values originated. | 19 class CompilationInfo; |
19 // When FLAG_track_hydrogen_positions is set this object encodes the | 20 class Script; |
20 // identifier of the inlining and absolute offset from the start of the | 21 class SharedFunctionInfo; |
21 // inlined function. | 22 struct SourcePositionInfo; |
22 // When the flag is not set we simply track absolute offset from the | 23 |
23 // script start. | 24 // SourcePosition stores script_offset (or kNoSourcePosition indicating an |
24 class SourcePosition { | 25 // unknown script offset) and inlining_id (or kNotInlined indicating that the |
26 // source position refers to the outer, not inlined function). | |
27 // A defined script_offset is a nonnegative 31bit integer. | |
28 // A defined inlining_id is a nonnegative 16bit integer. | |
29 // A defined inlining_id refers to positions in | |
30 // CompilationInfo::inlined_functions or | |
31 // DeoptimizationInputData::InliningPositions, depending on the compilation | |
32 // stage. | |
33 class SourcePosition final { | |
vogelheim
2016/11/07 17:53:27
How about:
SourcePosition stores
- script_offset
| |
25 public: | 34 public: |
26 static SourcePosition Unknown() { | 35 explicit SourcePosition(int script_offset = kNoSourcePosition, |
27 return SourcePosition::FromRaw(kNoPosition); | 36 int inlining_id = kNotInlined) |
37 : value_(0) { | |
38 SetScriptOffset(script_offset); | |
39 SetInliningId(inlining_id); | |
28 } | 40 } |
29 | 41 |
30 bool IsUnknown() const { return value_ == kNoPosition; } | 42 static SourcePosition Unknown() { return SourcePosition(); } |
43 bool IsKnown() const { | |
44 return ScriptOffset() != kNoSourcePosition || InliningId() != kNotInlined; | |
45 } | |
46 bool isInlined() const { return InliningId() != kNotInlined; } | |
31 | 47 |
32 uint32_t position() const { return PositionField::decode(value_); } | 48 std::vector<SourcePositionInfo> InliningStack(Handle<Code> code) const; |
33 void set_position(uint32_t position) { | 49 std::vector<SourcePositionInfo> InliningStack(CompilationInfo* code) const; |
34 if (FLAG_hydrogen_track_positions) { | 50 |
35 value_ = static_cast<uint32_t>(PositionField::update(value_, position)); | 51 void Print(std::ostream& out, Code* function) const; |
36 } else { | 52 |
37 value_ = position; | 53 int ScriptOffset() const { return ScriptOffsetField::decode(value_) - 1; } |
38 } | 54 int InliningId() const { return InliningIdField::decode(value_) - 1; } |
55 | |
56 void SetScriptOffset(int script_offset) { | |
57 DCHECK(script_offset <= ScriptOffsetField::kMax - 2); | |
58 DCHECK(script_offset >= kNoSourcePosition); | |
59 value_ = ScriptOffsetField::update(value_, script_offset + 1); | |
60 } | |
61 void SetInliningId(int inlining_id) { | |
62 DCHECK(inlining_id <= InliningIdField::kMax - 2); | |
63 DCHECK(inlining_id >= kNotInlined); | |
64 value_ = InliningIdField::update(value_, inlining_id + 1); | |
39 } | 65 } |
40 | 66 |
41 uint32_t inlining_id() const { return InliningIdField::decode(value_); } | 67 static const int kNotInlined = -1; |
42 void set_inlining_id(uint32_t inlining_id) { | 68 STATIC_ASSERT(kNoSourcePosition == -1); |
43 if (FLAG_hydrogen_track_positions) { | |
44 value_ = | |
45 static_cast<uint32_t>(InliningIdField::update(value_, inlining_id)); | |
46 } | |
47 } | |
48 | 69 |
49 uint32_t raw() const { return value_; } | 70 uint64_t raw() const { return value_; } |
50 | 71 static SourcePosition FromRaw(uint64_t raw) { |
51 private: | |
52 static const uint32_t kNoPosition = static_cast<uint32_t>(kNoSourcePosition); | |
53 typedef BitField<uint32_t, 0, 9> InliningIdField; | |
54 | |
55 // Offset from the start of the inlined function. | |
56 typedef BitField<uint32_t, 9, 23> PositionField; | |
57 | |
58 friend class HPositionInfo; | |
59 friend class Deoptimizer; | |
60 | |
61 static SourcePosition FromRaw(uint32_t raw_position) { | |
62 SourcePosition position; | 72 SourcePosition position; |
63 position.value_ = raw_position; | 73 position.value_ = raw; |
64 return position; | 74 return position; |
65 } | 75 } |
66 | 76 |
67 // If FLAG_hydrogen_track_positions is set contains bitfields InliningIdField | 77 private: |
68 // and PositionField. | 78 void Print(std::ostream& out, SharedFunctionInfo* function) const; |
69 // Otherwise contains absolute offset from the script start. | 79 SourcePositionInfo Info(Handle<SharedFunctionInfo> script) const; |
70 uint32_t value_; | 80 |
81 // InliningId is in the high bits for better compression in | |
82 // SourcePositionTable. | |
83 typedef BitField64<int, 0, 31> ScriptOffsetField; | |
84 typedef BitField64<int, 31, 16> InliningIdField; | |
85 // Leaving the highest bit untouched to allow for signed conversion. | |
86 uint64_t value_; | |
71 }; | 87 }; |
72 | 88 |
73 inline std::ostream& operator<<(std::ostream& os, const SourcePosition& p) { | 89 inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) { |
74 if (p.IsUnknown()) { | 90 return lhs.raw() == rhs.raw(); |
75 return os << "<?>"; | |
76 } else if (FLAG_hydrogen_track_positions) { | |
77 return os << "<" << p.inlining_id() << ":" << p.position() << ">"; | |
78 } else { | |
79 return os << "<0:" << p.raw() << ">"; | |
80 } | |
81 } | 91 } |
82 | 92 |
93 inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) { | |
94 return !(lhs == rhs); | |
95 } | |
96 | |
97 struct InliningPosition { | |
98 // position of the inlined call | |
99 SourcePosition position; | |
100 | |
101 // references position in DeoptimizationInputData::literals() | |
102 int inlined_function_id; | |
103 }; | |
104 | |
105 struct SourcePositionInfo { | |
106 explicit SourcePositionInfo(SourcePosition pos) : position(pos) {} | |
107 | |
108 SourcePosition position; | |
109 MaybeHandle<SharedFunctionInfo> function; | |
110 int line = -1; | |
111 int column = -1; | |
112 }; | |
113 | |
114 std::ostream& operator<<(std::ostream& out, const SourcePosition& pos); | |
115 | |
116 std::ostream& operator<<(std::ostream& out, const SourcePositionInfo& pos); | |
117 std::ostream& operator<<(std::ostream& out, | |
118 const std::vector<SourcePositionInfo>& stack); | |
119 | |
83 } // namespace internal | 120 } // namespace internal |
84 } // namespace v8 | 121 } // namespace v8 |
85 | 122 |
86 #endif // V8_SOURCE_POSITION_H_ | 123 #endif // V8_SOURCE_POSITION_H_ |
OLD | NEW |