Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: runtime/vm/token_descriptor.h

Issue 1644793002: Replace intptr_t with TokenDescriptor (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_DESCRIPTOR_H_
6 #define VM_TOKEN_DESCRIPTOR_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 // ClassifyingTokenDescriptors 1 -> -1 - 1
18 // ClassifyingTokenDescriptors 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 // NOTE: While token positions are passed around as an intptr_t they are encoded
32 // into the snapshot as an int32_t.
33
34 #define SENTINEL_TOKEN_DESCRIPTORS(V) \
35 V(NoSource, -1) \
36 V(Box, -2) \
37 V(ParallelMove, -3) \
38 V(TempMove, -4) \
39 V(Constant, -5) \
40 V(PushArgument, -6) \
41 V(ControlFlow, -7) \
42 V(Context, -8) \
43 V(MethodExtractor, -9) \
44 V(Last, -10) // Always keep this at the end.
45
46 // An immutable token position representing a debug safe source position,
47 // a synthetic source position, or a classifying token position.
48 class TokenDescriptor {
49 public:
50 TokenDescriptor()
51 : value_(kNoSource.value()) {
52 }
53
54 explicit TokenDescriptor(intptr_t value)
55 : value_(value) {
56 }
57
58 inline bool operator==(const TokenDescriptor& b) const {
59 return value() == b.value();
60 }
61
62 inline bool operator!=(const TokenDescriptor& b) const {
63 return !(*this == b);
64 }
65
66 inline bool operator<(const TokenDescriptor& b) const {
Ivan Posva 2016/02/01 17:16:52 These operators mean that synthesized locations ar
Cutch 2016/02/02 18:00:20 Acknowledged.
67 return value() < b.value();
68 }
69
70 inline bool operator>(const TokenDescriptor& b) const {
71 return b < *this;
72 }
73
74 inline bool operator<=(const TokenDescriptor& b) {
75 return !(*this > b);
76 }
77
78 inline bool operator>=(const TokenDescriptor& b) {
79 return !(*this < b);
80 }
81
82 static const intptr_t kMaxSentinelDescriptors = 64;
83
84 #define DECLARE_VALUES(name, value) \
85 static const TokenDescriptor k##name;
86 SENTINEL_TOKEN_DESCRIPTORS(DECLARE_VALUES);
87 #undef DECLARE_VALUES
88 static const TokenDescriptor kMinSource;
89 static const TokenDescriptor kMaxSource;
90
91 // Decode from a snapshot.
92 static TokenDescriptor SnapshotDecode(int32_t value);
93
94 // Encode for writing into a snapshot.
95 int32_t SnapshotEncode();
96
97 void Next() {
98 ASSERT(IsReal());
99 value_++;
100 }
101
102 // The raw value.
103 intptr_t value() const {
104 return value_;
105 }
106
107 // Token position constants.
108 static const intptr_t kNoSourcePos = -1;
109 static const intptr_t kMinSourcePos = 0;
110 static const intptr_t kMaxSourcePos = kSmiMax32 - kMaxSentinelDescriptors - 2;
111
112 // Is |this| a classifying sentinel source position?
113 bool IsClassifying() const {
114 return (value_ >= kBox.value()) && (value_ <= kLast.value());
115 }
116
117 // Is |this| the no source position sentinel?
118 bool IsNoSource() const {
119 return *this == kNoSource;
120 }
121
122 // Is |this| a synthetic source position?
123 bool IsSynthetic() const;
124
125 // Is |this| a real source position?
126 bool IsReal() const {
127 return value_ >= kMinSourcePos;
128 }
129
130 // Is |this| a source position?
131 bool IsSourcePosition() const {
132 return IsReal() || IsNoSource() || IsSynthetic();
133 }
134
135 // Is |this| a debug pause source position?
136 bool IsDebugPause() const {
137 // Sanity check some values here.
138 ASSERT(kNoSource.value() == kNoSourcePos);
139 ASSERT(kLast.value() < kNoSource.value());
140 ASSERT(kLast.value() > -kMaxSentinelDescriptors);
141 return IsReal();
142 }
143
144 // Encode |value| into a synthetic source position.
145 static TokenDescriptor ToSynthetic(TokenDescriptor tp) {
146 const intptr_t value = tp.value();
147 if (tp.IsClassifying() || tp.IsNoSource()) {
148 return tp;
149 }
150 if (tp.IsSynthetic()) {
151 return tp;
152 }
153 const TokenDescriptor synthetic_value =
154 TokenDescriptor((kLast.value() - 1) - value);
155 ASSERT(synthetic_value.IsSynthetic());
156 ASSERT(synthetic_value.value() < kLast.value());
157 return synthetic_value;
158 }
159
160 // Decode |synthetic_value| from a synthetic source position.
161 static TokenDescriptor FromSynthetic(TokenDescriptor tp) {
162 const intptr_t synthetic_value = tp.value();
163 if (tp.IsClassifying() || tp.IsNoSource()) {
164 return tp;
165 }
166 if (!tp.IsSynthetic()) {
167 return tp;
168 }
169 const TokenDescriptor value =
170 TokenDescriptor(-synthetic_value + (kLast.value() - 1));
171 ASSERT(!value.IsSynthetic());
172 return value;
173 }
174
175 const char* ToCString() const;
176
177 private:
178 int32_t value_;
179
180 DISALLOW_ALLOCATION();
181 };
182
183 } // namespace dart
184
185 #endif // VM_TOKEN_DESCRIPTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698