OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef VM_TOKEN_POSITION_H_ |
| 6 #define VM_TOKEN_POSITION_H_ |
| 7 |
| 8 #include "platform/utils.h" |
| 9 #include "vm/allocation.h" |
| 10 |
| 11 namespace dart { |
| 12 |
| 13 // The token space is organized as follows: |
| 14 // |
| 15 // Sentinel values start at -1 and move towards negative infinity: |
| 16 // kNoSourcePos -> -1 |
| 17 // ClassifyingTokenPositions 1 -> -1 - 1 |
| 18 // ClassifyingTokenPositions N -> -1 - N |
| 19 // |
| 20 // Synthetically created AstNodes are given real source positions but encoded |
| 21 // as negative numbers from [kSmiMin32, -1 - N]. For example: |
| 22 // |
| 23 // A source position of 0 in a synthetic AstNode would be encoded as -2 - N. |
| 24 // A source position of 1 in a synthetic AstNode would be encoded as -3 - N. |
| 25 // |
| 26 // All other AstNodes are given real source positions encoded as positive |
| 27 // integers. |
| 28 // |
| 29 // This organization allows for ~1 billion token positions. |
| 30 |
| 31 #define SENTINEL_TOKEN_DESCRIPTORS(V) \ |
| 32 V(NoSource, -1) \ |
| 33 V(Box, -2) \ |
| 34 V(ParallelMove, -3) \ |
| 35 V(TempMove, -4) \ |
| 36 V(Constant, -5) \ |
| 37 V(PushArgument, -6) \ |
| 38 V(ControlFlow, -7) \ |
| 39 V(Context, -8) \ |
| 40 V(MethodExtractor, -9) \ |
| 41 V(Last, -10) // Always keep this at the end. |
| 42 |
| 43 // A token position representing a debug safe source (real) position, |
| 44 // non-debug safe source (synthetic) positions, or a classifying value used |
| 45 // by the profiler. |
| 46 class TokenPosition { |
| 47 public: |
| 48 TokenPosition() |
| 49 : value_(kNoSource.value()) { |
| 50 } |
| 51 |
| 52 explicit TokenPosition(intptr_t value) |
| 53 : value_(value) { |
| 54 } |
| 55 |
| 56 bool operator==(const TokenPosition& b) const { |
| 57 return value() == b.value(); |
| 58 } |
| 59 |
| 60 bool operator!=(const TokenPosition& b) const { |
| 61 return !(*this == b); |
| 62 } |
| 63 |
| 64 bool operator<(const TokenPosition& b) const { |
| 65 // TODO(johnmccutchan): Assert that this is a source position. |
| 66 return value() < b.value(); |
| 67 } |
| 68 |
| 69 bool operator>(const TokenPosition& b) const { |
| 70 // TODO(johnmccutchan): Assert that this is a source position. |
| 71 return b < *this; |
| 72 } |
| 73 |
| 74 bool operator<=(const TokenPosition& b) { |
| 75 // TODO(johnmccutchan): Assert that this is a source position. |
| 76 return !(*this > b); |
| 77 } |
| 78 |
| 79 bool operator>=(const TokenPosition& b) { |
| 80 // TODO(johnmccutchan): Assert that this is a source position. |
| 81 return !(*this < b); |
| 82 } |
| 83 |
| 84 static const intptr_t kMaxSentinelDescriptors = 64; |
| 85 |
| 86 #define DECLARE_VALUES(name, value) \ |
| 87 static const TokenPosition k##name; |
| 88 SENTINEL_TOKEN_DESCRIPTORS(DECLARE_VALUES); |
| 89 #undef DECLARE_VALUES |
| 90 static const TokenPosition kMinSource; |
| 91 static const TokenPosition kMaxSource; |
| 92 |
| 93 // Decode from a snapshot. |
| 94 static TokenPosition SnapshotDecode(int32_t value); |
| 95 |
| 96 // Encode for writing into a snapshot. |
| 97 int32_t SnapshotEncode(); |
| 98 |
| 99 // Increment the token position. |
| 100 TokenPosition Next() { |
| 101 ASSERT(IsReal()); |
| 102 value_++; |
| 103 return *this; |
| 104 } |
| 105 |
| 106 // The raw value. |
| 107 // TODO(johnmccutchan): Make this private. |
| 108 intptr_t value() const { |
| 109 return value_; |
| 110 } |
| 111 |
| 112 // Return the source position. |
| 113 intptr_t Pos() const { |
| 114 if (IsSynthetic()) { |
| 115 return FromSynthetic().Pos(); |
| 116 } |
| 117 return value_; |
| 118 } |
| 119 |
| 120 // Token position constants. |
| 121 static const intptr_t kNoSourcePos = -1; |
| 122 static const intptr_t kMinSourcePos = 0; |
| 123 static const intptr_t kMaxSourcePos = kSmiMax32 - kMaxSentinelDescriptors - 2; |
| 124 |
| 125 // Is |this| a classifying sentinel source position? |
| 126 // Classifying positions are used by the profiler to group instructions whose |
| 127 // cost isn't naturally attributable to a source location. |
| 128 bool IsClassifying() const { |
| 129 return (value_ >= kBox.value()) && (value_ <= kLast.value()); |
| 130 } |
| 131 |
| 132 // Is |this| the no source position sentinel? |
| 133 bool IsNoSource() const { |
| 134 return *this == kNoSource; |
| 135 } |
| 136 |
| 137 // Is |this| a synthetic source position? |
| 138 // Synthetic source positions are used by the profiler to attribute ticks to a |
| 139 // pieces of source, but ignored by the debugger as potential breakpoints. |
| 140 bool IsSynthetic() const; |
| 141 |
| 142 // Is |this| a real source position? |
| 143 bool IsReal() const { |
| 144 return value_ >= kMinSourcePos; |
| 145 } |
| 146 |
| 147 // Is |this| a source position? |
| 148 bool IsSourcePosition() const { |
| 149 return IsReal() || IsNoSource() || IsSynthetic(); |
| 150 } |
| 151 |
| 152 // Is |this| a debug pause source position? |
| 153 bool IsDebugPause() const { |
| 154 // Sanity check some values here. |
| 155 ASSERT(kNoSource.value() == kNoSourcePos); |
| 156 ASSERT(kLast.value() < kNoSource.value()); |
| 157 ASSERT(kLast.value() > -kMaxSentinelDescriptors); |
| 158 return IsReal(); |
| 159 } |
| 160 |
| 161 // Convert |this| into a synthetic source position. Sentinel values remain |
| 162 // unchanged. |
| 163 TokenPosition ToSynthetic() const { |
| 164 const intptr_t value = value_; |
| 165 if (IsClassifying() || IsNoSource()) { |
| 166 return *this; |
| 167 } |
| 168 if (IsSynthetic()) { |
| 169 return *this; |
| 170 } |
| 171 const TokenPosition synthetic_value = |
| 172 TokenPosition((kLast.value() - 1) - value); |
| 173 ASSERT(synthetic_value.IsSynthetic()); |
| 174 ASSERT(synthetic_value.value() < kLast.value()); |
| 175 return synthetic_value; |
| 176 } |
| 177 |
| 178 // Convert |this| from a synthetic source position. Sentinel values remain |
| 179 // unchanged. |
| 180 TokenPosition FromSynthetic() const { |
| 181 const intptr_t synthetic_value = value_; |
| 182 if (IsClassifying() || IsNoSource()) { |
| 183 return *this; |
| 184 } |
| 185 if (!IsSynthetic()) { |
| 186 return *this; |
| 187 } |
| 188 const TokenPosition value = |
| 189 TokenPosition(-synthetic_value + (kLast.value() - 1)); |
| 190 ASSERT(!value.IsSynthetic()); |
| 191 return value; |
| 192 } |
| 193 |
| 194 const char* ToCString() const; |
| 195 |
| 196 private: |
| 197 int32_t value_; |
| 198 |
| 199 DISALLOW_ALLOCATION(); |
| 200 }; |
| 201 |
| 202 } // namespace dart |
| 203 |
| 204 #endif // VM_TOKEN_POSITION_H_ |
OLD | NEW |