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

Side by Side Diff: src/types.cc

Issue 558193003: Redesign of the internal type system. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: This addresses the issue I pointed out on Friday. In order to keep the changes as simple as possib… Created 6 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // 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 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 #include "src/types.h" 5 #include "src/types.h"
6 6
7 #include "src/ostreams.h" 7 #include "src/ostreams.h"
8 #include "src/types-inl.h" 8 #include "src/types-inl.h"
9 9
10 namespace v8 { 10 namespace v8 {
11 namespace internal { 11 namespace internal {
12 12
13 13
14 // ----------------------------------------------------------------------------- 14 // -----------------------------------------------------------------------------
15 // Range-related custom order on doubles. 15 // Range-related helper functions.
16 // We want -0 to be less than +0.
17 16
18 static bool dle(double x, double y) { 17 template<class Config>
19 return x <= y && (x != 0 || IsMinusZero(x) || !IsMinusZero(y)); 18 typename TypeImpl<Config>::Limits TypeImpl<Config>::intersect(
19 Limits lhs, Limits rhs) {
20 DisallowHeapAllocation no_allocation;
rossberg 2014/09/15 15:58:29 Can't this function be simplified to return Lim
neis1 2014/09/16 10:04:00 No, that would compare the objects instead of the
21 Limits result(lhs);
22 if (lhs.min->Number() < rhs.min->Number()) result.min = rhs.min;
23 if (lhs.max->Number() > rhs.max->Number()) result.max = rhs.max;
24 return result;
20 } 25 }
21 26
22 27
23 static bool deq(double x, double y) { 28 template<class Config>
24 return dle(x, y) && dle(y, x); 29 typename TypeImpl<Config>::Limits TypeImpl<Config>::union_(
30 Limits lhs, Limits rhs) {
31 DisallowHeapAllocation no_allocation;
32 Limits result(lhs);
33 if (lhs.min->Number() > rhs.min->Number()) result.min = rhs.min;
34 if (lhs.max->Number() < rhs.max->Number()) result.max = rhs.max;
35 return result;
36 }
37
38
39 template<class Config>
40 bool TypeImpl<Config>::overlap(
41 typename TypeImpl<Config>::RangeType* lhs,
42 typename TypeImpl<Config>::RangeType* rhs) {
43 DisallowHeapAllocation no_allocation;
44 typename TypeImpl<Config>::Limits lim = intersect(Limits(lhs), Limits(rhs));
45 return lim.min->Number() <= lim.max->Number();
46 }
47
48
49 template<class Config>
50 bool TypeImpl<Config>::contains(
51 typename TypeImpl<Config>::RangeType* lhs,
52 typename TypeImpl<Config>::RangeType* rhs) {
53 DisallowHeapAllocation no_allocation;
54 return lhs->Min() <= rhs->Min() && rhs->Max() <= lhs->Max();
55 }
56
57
58 template<class Config>
59 bool TypeImpl<Config>::contains(
60 typename TypeImpl<Config>::RangeType* range, i::Object* val) {
61 DisallowHeapAllocation no_allocation;
62 return IsInteger(val)
63 && range->Min() <= val->Number() && val->Number() <= range->Max();
25 } 64 }
26 65
27 66
28 // ----------------------------------------------------------------------------- 67 // -----------------------------------------------------------------------------
29 // Glb and lub computation. 68 // Glb and lub computation.
30 69
70
31 // The largest bitset subsumed by this type. 71 // The largest bitset subsumed by this type.
32 template<class Config> 72 template<class Config>
33 int TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) { 73 int TypeImpl<Config>::BitsetType::Glb(TypeImpl* type) {
34 DisallowHeapAllocation no_allocation; 74 DisallowHeapAllocation no_allocation;
35 if (type->IsBitset()) { 75 if (type->IsBitset()) {
36 return type->AsBitset(); 76 return type->AsBitset();
37 } else if (type->IsUnion()) { 77 } else if (type->IsUnion()) {
38 UnionHandle unioned = handle(type->AsUnion()); 78 UnionHandle unioned = handle(type->AsUnion());
39 DCHECK(unioned->Wellformed()); 79 SLOW_DCHECK(unioned->Wellformed());
40 return unioned->Get(0)->BitsetGlb(); // Other BitsetGlb's are kNone anyway. 80 return unioned->Get(0)->BitsetGlb(); // Shortcut.
rossberg 2014/09/15 15:58:29 I don't think this comment got clearer ;)
neis1 2014/09/16 10:03:59 Done.
41 } else { 81 } else {
42 return kNone; 82 return kNone;
43 } 83 }
44 } 84 }
45 85
46 86
47 // The smallest bitset subsuming this type. 87 // The smallest bitset subsuming this type.
48 template<class Config> 88 template<class Config>
49 int TypeImpl<Config>::BitsetType::Lub(TypeImpl* type) { 89 int TypeImpl<Config>::BitsetType::Lub(TypeImpl* type) {
50 DisallowHeapAllocation no_allocation; 90 DisallowHeapAllocation no_allocation;
51 if (type->IsBitset()) { 91 if (type->IsBitset()) return type->AsBitset();
52 return type->AsBitset(); 92 if (type->IsUnion()) {
53 } else if (type->IsUnion()) {
54 UnionHandle unioned = handle(type->AsUnion());
55 int bitset = kNone; 93 int bitset = kNone;
56 for (int i = 0; i < unioned->Length(); ++i) { 94 for (int i = 0; i < type->AsUnion()->Length(); ++i) {
57 bitset |= unioned->Get(i)->BitsetLub(); 95 bitset |= type->AsUnion()->Get(i)->BitsetLub();
58 } 96 }
59 return bitset; 97 return bitset;
60 } else if (type->IsClass()) {
61 // Little hack to avoid the need for a region for handlification here...
62 return Config::is_class(type) ? Lub(*Config::as_class(type)) :
63 type->AsClass()->Bound(NULL)->AsBitset();
64 } else if (type->IsConstant()) {
65 return type->AsConstant()->Bound()->AsBitset();
66 } else if (type->IsRange()) {
67 return type->AsRange()->Bound()->AsBitset();
68 } else if (type->IsContext()) {
69 return type->AsContext()->Bound()->AsBitset();
70 } else if (type->IsArray()) {
71 return type->AsArray()->Bound()->AsBitset();
72 } else if (type->IsFunction()) {
73 return type->AsFunction()->Bound()->AsBitset();
74 } else {
75 UNREACHABLE();
76 return kNone;
77 } 98 }
99 if (type->IsClass()) return Lub(*type->AsClass()->Map());
100 if (type->IsConstant()) return Lub(*type->AsConstant()->Value());
101 if (type->IsRange()) return Lub(Limits(type->AsRange()));
102 if (type->IsContext()) return kInternal & kTaggedPtr;
103 if (type->IsArray()) return kArray;
104 if (type->IsFunction()) return kFunction;
105 UNREACHABLE();
106 return kNone;
78 } 107 }
79 108
80 109
81 // The smallest bitset subsuming this type, ignoring explicit bounds.
82 template<class Config>
83 int TypeImpl<Config>::BitsetType::InherentLub(TypeImpl* type) {
84 DisallowHeapAllocation no_allocation;
85 if (type->IsBitset()) {
86 return type->AsBitset();
87 } else if (type->IsUnion()) {
88 UnionHandle unioned = handle(type->AsUnion());
89 int bitset = kNone;
90 for (int i = 0; i < unioned->Length(); ++i) {
91 bitset |= unioned->Get(i)->InherentBitsetLub();
92 }
93 return bitset;
94 } else if (type->IsClass()) {
95 return Lub(*type->AsClass()->Map());
96 } else if (type->IsConstant()) {
97 return Lub(*type->AsConstant()->Value());
98 } else if (type->IsRange()) {
99 return Lub(type->AsRange()->Min(), type->AsRange()->Max());
100 } else if (type->IsContext()) {
101 return kInternal & kTaggedPtr;
102 } else if (type->IsArray()) {
103 return kArray;
104 } else if (type->IsFunction()) {
105 return kFunction;
106 } else {
107 UNREACHABLE();
108 return kNone;
109 }
110 }
111
112
113 template<class Config>
114 int TypeImpl<Config>::BitsetType::Lub(i::Object* value) {
115 DisallowHeapAllocation no_allocation;
116 if (value->IsNumber()) {
117 return Lub(value->Number()) & (value->IsSmi() ? kTaggedInt : kTaggedPtr);
118 }
119 return Lub(i::HeapObject::cast(value)->map());
120 }
121
122
123 template<class Config>
124 int TypeImpl<Config>::BitsetType::Lub(double value) {
125 DisallowHeapAllocation no_allocation;
126 if (i::IsMinusZero(value)) return kMinusZero;
127 if (std::isnan(value)) return kNaN;
128 if (IsUint32Double(value)) return Lub(FastD2UI(value));
129 if (IsInt32Double(value)) return Lub(FastD2I(value));
130 return kOtherNumber;
131 }
132
133
134 template<class Config>
135 int TypeImpl<Config>::BitsetType::Lub(double min, double max) {
136 DisallowHeapAllocation no_allocation;
137 DCHECK(dle(min, max));
138 if (deq(min, max)) return BitsetType::Lub(min); // Singleton range.
139 int bitset = BitsetType::kNumber ^ SEMANTIC(BitsetType::kNaN);
140 if (dle(0, min) || max < 0) bitset ^= SEMANTIC(BitsetType::kMinusZero);
141 return bitset;
142 // TODO(neis): Could refine this further by doing more checks on min/max.
143 }
144
145
146 template<class Config>
147 int TypeImpl<Config>::BitsetType::Lub(int32_t value) {
148 if (value >= 0x40000000) {
149 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
150 }
151 if (value >= 0) return kUnsignedSmall;
152 if (value >= -0x40000000) return kOtherSignedSmall;
153 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall;
154 }
155
156
157 template<class Config>
158 int TypeImpl<Config>::BitsetType::Lub(uint32_t value) {
159 DisallowHeapAllocation no_allocation;
160 if (value >= 0x80000000u) return kOtherUnsigned32;
161 if (value >= 0x40000000u) {
162 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
163 }
164 return kUnsignedSmall;
165 }
166
167
168 template<class Config> 110 template<class Config>
169 int TypeImpl<Config>::BitsetType::Lub(i::Map* map) { 111 int TypeImpl<Config>::BitsetType::Lub(i::Map* map) {
170 DisallowHeapAllocation no_allocation; 112 DisallowHeapAllocation no_allocation;
171 switch (map->instance_type()) { 113 switch (map->instance_type()) {
172 case STRING_TYPE: 114 case STRING_TYPE:
173 case ONE_BYTE_STRING_TYPE: 115 case ONE_BYTE_STRING_TYPE:
174 case CONS_STRING_TYPE: 116 case CONS_STRING_TYPE:
175 case CONS_ONE_BYTE_STRING_TYPE: 117 case CONS_ONE_BYTE_STRING_TYPE:
176 case SLICED_STRING_TYPE: 118 case SLICED_STRING_TYPE:
177 case SLICED_ONE_BYTE_STRING_TYPE: 119 case SLICED_ONE_BYTE_STRING_TYPE:
178 case EXTERNAL_STRING_TYPE: 120 case EXTERNAL_STRING_TYPE:
179 case EXTERNAL_ONE_BYTE_STRING_TYPE: 121 case EXTERNAL_ONE_BYTE_STRING_TYPE:
180 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: 122 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
181 case SHORT_EXTERNAL_STRING_TYPE: 123 case SHORT_EXTERNAL_STRING_TYPE:
182 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE: 124 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
183 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: 125 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
126 return kOtherString;
184 case INTERNALIZED_STRING_TYPE: 127 case INTERNALIZED_STRING_TYPE:
185 case ONE_BYTE_INTERNALIZED_STRING_TYPE: 128 case ONE_BYTE_INTERNALIZED_STRING_TYPE:
186 case EXTERNAL_INTERNALIZED_STRING_TYPE: 129 case EXTERNAL_INTERNALIZED_STRING_TYPE:
187 case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: 130 case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
188 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: 131 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
189 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE: 132 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
190 case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: 133 case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
191 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: 134 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
192 return kString; 135 return kInternalizedString;
193 case SYMBOL_TYPE: 136 case SYMBOL_TYPE:
194 return kSymbol; 137 return kSymbol;
195 case ODDBALL_TYPE: { 138 case ODDBALL_TYPE: {
196 Heap* heap = map->GetHeap(); 139 Heap* heap = map->GetHeap();
197 if (map == heap->undefined_map()) return kUndefined; 140 if (map == heap->undefined_map()) return kUndefined;
198 if (map == heap->null_map()) return kNull; 141 if (map == heap->null_map()) return kNull;
199 if (map == heap->boolean_map()) return kBoolean; 142 if (map == heap->boolean_map()) return kBoolean;
200 DCHECK(map == heap->the_hole_map() || 143 DCHECK(map == heap->the_hole_map() ||
201 map == heap->uninitialized_map() || 144 map == heap->uninitialized_map() ||
202 map == heap->no_interceptor_result_sentinel_map() || 145 map == heap->no_interceptor_result_sentinel_map() ||
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 case FOREIGN_TYPE: 197 case FOREIGN_TYPE:
255 case CODE_TYPE: 198 case CODE_TYPE:
256 return kInternal & kTaggedPtr; 199 return kInternal & kTaggedPtr;
257 default: 200 default:
258 UNREACHABLE(); 201 UNREACHABLE();
259 return kNone; 202 return kNone;
260 } 203 }
261 } 204 }
262 205
263 206
207 template<class Config>
208 int TypeImpl<Config>::BitsetType::Lub(i::Object* value) {
209 DisallowHeapAllocation no_allocation;
210 if (value->IsNumber()) {
211 return Lub(value->Number()) & (value->IsSmi() ? kTaggedInt : kTaggedPtr);
212 }
213 return Lub(i::HeapObject::cast(value)->map());
214 }
215
216
217 template<class Config>
218 int TypeImpl<Config>::BitsetType::Lub(double value) {
219 DisallowHeapAllocation no_allocation;
220 if (i::IsMinusZero(value)) return kMinusZero;
221 if (std::isnan(value)) return kNaN;
222 if (IsUint32Double(value)) return Lub(FastD2UI(value));
223 if (IsInt32Double(value)) return Lub(FastD2I(value));
224 return kOtherNumber;
225 }
226
227
228 template<class Config>
229 int TypeImpl<Config>::BitsetType::Lub(int32_t value) {
230 DisallowHeapAllocation no_allocation;
231 if (value >= 0x40000000) {
232 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
233 }
234 if (value >= 0) return kUnsignedSmall;
235 if (value >= -0x40000000) return kOtherSignedSmall;
236 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall;
237 }
238
239
240 template<class Config>
241 int TypeImpl<Config>::BitsetType::Lub(uint32_t value) {
242 DisallowHeapAllocation no_allocation;
243 if (value >= 0x80000000u) return kOtherUnsigned32;
244 if (value >= 0x40000000u) {
245 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
246 }
247 return kUnsignedSmall;
248 }
249
250
251 template<class Config>
252 int TypeImpl<Config>::BitsetType::Lub(Limits lim) {
253 DisallowHeapAllocation no_allocation;
254 double min = lim.min->Number();
255 double max = lim.max->Number();
256 int lub = kNone;
257
258 int atoms31[] = {
rossberg 2014/09/15 15:58:29 Can we zip these arrays into two tables over a str
neis1 2014/09/16 10:04:00 Yes, something like that. I also need to pull the
259 kOtherNumber, kOtherSigned32, kOtherSignedSmall, kUnsignedSmall,
260 kOtherUnsigned31, kOtherUnsigned32, kOtherNumber
261 };
262 double mins31[] = {
263 -V8_INFINITY, kMinInt, -0x40000000, 0,
264 0x40000000u, 0x80000000u, static_cast<double>(kMaxUInt32) + 1
265 };
266 int atoms32[] = {
267 kOtherNumber, kOtherSignedSmall, kUnsignedSmall,
268 kOtherUnsigned32, kOtherNumber
269 };
270 double mins32[] = {
271 -V8_INFINITY, kMinInt, 0,
272 0x80000000u, static_cast<double>(kMaxUInt32) + 1
273 };
274 int* atoms = i::SmiValuesAre31Bits() ? atoms31 : atoms32;
275 double* mins = i::SmiValuesAre31Bits() ? mins31 : mins32;
276 size_t n = i::SmiValuesAre31Bits() ? arraysize(atoms31) : arraysize(atoms32);
277
278 for (size_t i = 1; i < n; ++i) {
279 if (min < mins[i]) {
280 lub |= atoms[i-1];
281 if (max < mins[i]) return lub;
282 }
283 }
284 return lub |= atoms[n-1];
285 }
286
287
264 // ----------------------------------------------------------------------------- 288 // -----------------------------------------------------------------------------
265 // Predicates. 289 // Predicates.
266 290
267 // Check this <= that. 291
292 template<class Config>
293 bool TypeImpl<Config>::SimplyEquals(TypeImpl* that) {
294 DisallowHeapAllocation no_allocation;
295 if (this->IsClass()) {
296 return that->IsClass()
297 && *this->AsClass()->Map() == *that->AsClass()->Map();
298 }
299 if (this->IsConstant()) {
300 return that->IsConstant()
301 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
302 }
303 if (this->IsContext()) {
304 return that->IsContext()
305 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
306 }
307 if (this->IsArray()) {
308 return that->IsArray()
309 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
310 }
311 if (this->IsFunction()) {
312 if (!that->IsFunction()) return false;
313 FunctionType* this_fun = this->AsFunction();
314 FunctionType* that_fun = that->AsFunction();
315 if (this_fun->Arity() != that_fun->Arity() ||
316 !this_fun->Result()->Equals(that_fun->Result()) ||
317 !this_fun->Receiver()->Equals(that_fun->Receiver())) {
318 return false;
319 }
320 for (int i = 0; i < this_fun->Arity(); ++i) {
321 if (!this_fun->Parameter(i)->Equals(that_fun->Parameter(i))) return false;
322 }
323 return true;
324 }
325 UNREACHABLE();
326 return false;
327 }
328
329
330 // Check if [this] <= [that].
268 template<class Config> 331 template<class Config>
269 bool TypeImpl<Config>::SlowIs(TypeImpl* that) { 332 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
270 DisallowHeapAllocation no_allocation; 333 DisallowHeapAllocation no_allocation;
271 334
272 // Fast path for bitsets.
273 if (this->IsNone()) return true;
274 if (that->IsBitset()) { 335 if (that->IsBitset()) {
275 return BitsetType::Is(BitsetType::Lub(this), that->AsBitset()); 336 return BitsetType::Is(this->BitsetLub(), that->AsBitset());
337 }
338 if (this->IsBitset()) {
339 return BitsetType::Is(this->AsBitset(), that->BitsetGlb());
276 } 340 }
277 341
278 if (that->IsClass()) { 342 // (T1 \/ ... \/ Tn) <= T if (T1 <= T) /\ ... /\ (Tn <= T)
279 return this->IsClass()
280 && *this->AsClass()->Map() == *that->AsClass()->Map()
281 && ((Config::is_class(that) && Config::is_class(this)) ||
282 BitsetType::New(this->BitsetLub())->Is(
283 BitsetType::New(that->BitsetLub())));
284 }
285 if (that->IsConstant()) {
286 return this->IsConstant()
287 && *this->AsConstant()->Value() == *that->AsConstant()->Value()
288 && this->AsConstant()->Bound()->Is(that->AsConstant()->Bound());
289 }
290 if (that->IsRange()) {
291 return this->IsRange()
292 && this->AsRange()->Bound()->Is(that->AsRange()->Bound())
293 && dle(that->AsRange()->Min(), this->AsRange()->Min())
294 && dle(this->AsRange()->Max(), that->AsRange()->Max());
295 }
296 if (that->IsContext()) {
297 return this->IsContext()
298 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
299 }
300 if (that->IsArray()) {
301 return this->IsArray()
302 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
303 }
304 if (that->IsFunction()) {
305 // We currently do not allow for any variance here, in order to keep
306 // Union and Intersect operations simple.
307 if (!this->IsFunction()) return false;
308 FunctionType* this_fun = this->AsFunction();
309 FunctionType* that_fun = that->AsFunction();
310 if (this_fun->Arity() != that_fun->Arity() ||
311 !this_fun->Result()->Equals(that_fun->Result()) ||
312 !that_fun->Receiver()->Equals(this_fun->Receiver())) {
313 return false;
314 }
315 for (int i = 0; i < this_fun->Arity(); ++i) {
316 if (!that_fun->Parameter(i)->Equals(this_fun->Parameter(i))) return false;
317 }
318 return true;
319 }
320
321 // (T1 \/ ... \/ Tn) <= T <=> (T1 <= T) /\ ... /\ (Tn <= T)
322 if (this->IsUnion()) { 343 if (this->IsUnion()) {
323 UnionHandle unioned = handle(this->AsUnion()); 344 UnionHandle unioned = handle(this->AsUnion());
324 for (int i = 0; i < unioned->Length(); ++i) { 345 for (int i = 0; i < unioned->Length(); ++i) {
325 if (!unioned->Get(i)->Is(that)) return false; 346 if (!unioned->Get(i)->Is(that)) return false;
326 } 347 }
327 return true; 348 return true;
328 } 349 }
329 350
330 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn) 351 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn)
331 // (iff T is not a union) 352 if (that->IsUnion()) {
332 DCHECK(!this->IsUnion() && that->IsUnion()); 353 UnionHandle unioned = handle(that->AsUnion());
333 UnionHandle unioned = handle(that->AsUnion()); 354 for (int i = 0; i < unioned->Length(); ++i) {
334 for (int i = 0; i < unioned->Length(); ++i) { 355 if (this->Is(unioned->Get(i))) return true;
335 if (this->Is(unioned->Get(i))) return true; 356 if (i > 1 && this->IsRange()) return false; // Shortcut.
336 if (this->IsBitset()) break; // Fast fail, only first field is a bitset. 357 }
358 return false;
337 } 359 }
338 return false; 360
361 if (this->IsRange()) {
rossberg 2014/09/15 15:58:29 Do the that->IsRange() test first, to avoid the du
neis1 2014/09/16 10:03:59 Not sure what exactly you had in mind, but I rewro
rossberg 2014/09/16 14:04:40 That's what I had in mind. :)
362 return that->IsRange() && contains(that->AsRange(), this->AsRange());
363 }
364 if (this->IsConstant() && that->IsRange()) {
365 return contains(that->AsRange(), *this->AsConstant()->Value());
366 }
367 return this->SimplyEquals(that);
339 } 368 }
340 369
341 370
342 template<class Config> 371 template<class Config>
343 bool TypeImpl<Config>::NowIs(TypeImpl* that) { 372 bool TypeImpl<Config>::NowIs(TypeImpl* that) {
344 DisallowHeapAllocation no_allocation; 373 DisallowHeapAllocation no_allocation;
345 374
346 // TODO(rossberg): this is incorrect for 375 // TODO(rossberg): this is incorrect for
347 // Union(Constant(V), T)->NowIs(Class(M)) 376 // Union(Constant(V), T)->NowIs(Class(M))
348 // but fuzzing does not cover that! 377 // but fuzzing does not cover that!
349 if (this->IsConstant()) { 378 if (this->IsConstant()) {
350 i::Object* object = *this->AsConstant()->Value(); 379 i::Object* object = *this->AsConstant()->Value();
351 if (object->IsHeapObject()) { 380 if (object->IsHeapObject()) {
352 i::Map* map = i::HeapObject::cast(object)->map(); 381 i::Map* map = i::HeapObject::cast(object)->map();
353 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) { 382 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
354 if (*it.Current() == map) return true; 383 if (*it.Current() == map) return true;
355 } 384 }
356 } 385 }
357 } 386 }
358 return this->Is(that); 387 return this->Is(that);
359 } 388 }
360 389
361 390
362 // Check if this contains only (currently) stable classes. 391 // Check if [this] contains only (currently) stable classes.
363 template<class Config> 392 template<class Config>
364 bool TypeImpl<Config>::NowStable() { 393 bool TypeImpl<Config>::NowStable() {
365 DisallowHeapAllocation no_allocation; 394 DisallowHeapAllocation no_allocation;
366 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) { 395 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
367 if (!it.Current()->is_stable()) return false; 396 if (!it.Current()->is_stable()) return false;
368 } 397 }
369 return true; 398 return true;
370 } 399 }
371 400
372 401
373 // Check this overlaps that. 402 // Check if [this] and [that] overlap.
374 template<class Config> 403 template<class Config>
375 bool TypeImpl<Config>::Maybe(TypeImpl* that) { 404 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
376 DisallowHeapAllocation no_allocation; 405 DisallowHeapAllocation no_allocation;
377 406
378 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T) 407 // (T1 \/ ... \/ Tn) overlaps T if (T1 overlaps T) \/ ... \/ (Tn overlaps T)
379 if (this->IsUnion()) { 408 if (this->IsUnion()) {
380 UnionHandle unioned = handle(this->AsUnion()); 409 UnionHandle unioned = handle(this->AsUnion());
381 for (int i = 0; i < unioned->Length(); ++i) { 410 for (int i = 0; i < unioned->Length(); ++i) {
382 if (unioned->Get(i)->Maybe(that)) return true; 411 if (unioned->Get(i)->Maybe(that)) return true;
383 } 412 }
384 return false; 413 return false;
385 } 414 }
386 415
387 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn) 416 // T overlaps (T1 \/ ... \/ Tn) if (T overlaps T1) \/ ... \/ (T overlaps Tn)
388 if (that->IsUnion()) { 417 if (that->IsUnion()) {
389 UnionHandle unioned = handle(that->AsUnion()); 418 for (int i = 0; i < that->AsUnion()->Length(); ++i) {
390 for (int i = 0; i < unioned->Length(); ++i) { 419 if (this->Maybe(that->AsUnion()->Get(i))) return true;
391 if (this->Maybe(unioned->Get(i))) return true;
392 } 420 }
393 return false; 421 return false;
394 } 422 }
395 423
396 DCHECK(!this->IsUnion() && !that->IsUnion()); 424 if (!BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()))
rossberg 2014/09/15 15:58:29 I'm not sure I understand why this is the right th
neis1 2014/09/16 10:04:00 I allow the representation part to be empty, but n
rossberg 2014/09/16 14:04:40 Hm. The asymmetry is too suspicious for my taste.
neis1 2014/09/18 13:05:18 Marked as TODO.
397 if (this->IsBitset() || that->IsBitset()) { 425 return false;
398 return BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()); 426 if (this->IsBitset() || that->IsBitset()) return true;
427
428 if (this->IsClass() && !that->IsClass()) return true;
429 if (that->IsClass() && !this->IsClass()) return true;
rossberg 2014/09/15 15:58:29 You can combine these two into "if (this->IsClass(
neis1 2014/09/16 10:04:00 Done.
430
431 if (this->IsRange()) {
432 if (that->IsConstant()) {
433 return contains(this->AsRange(), *that->AsConstant()->Value());
434 }
435 return that->IsRange() && overlap(this->AsRange(), that->AsRange());
399 } 436 }
400 if (this->IsClass()) { 437 if (that->IsRange()) {
401 return that->IsClass() 438 if (this->IsConstant()) {
402 && *this->AsClass()->Map() == *that->AsClass()->Map(); 439 return contains(that->AsRange(), *this->AsConstant()->Value());
403 } 440 }
404 if (this->IsConstant()) { 441 return this->IsRange() && overlap(this->AsRange(), that->AsRange());
405 return that->IsConstant()
406 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
407 }
408 if (this->IsContext()) {
409 return this->Equals(that);
410 }
411 if (this->IsArray()) {
412 // There is no variance!
413 return this->Equals(that);
414 }
415 if (this->IsFunction()) {
416 // There is no variance!
417 return this->Equals(that);
418 } 442 }
419 443
420 return false; 444 return this->SimplyEquals(that);
421 } 445 }
422 446
423 447
424 // Check if value is contained in (inhabits) type. 448 // Return the range in [this], or [NULL].
449 template<class Config>
450 typename TypeImpl<Config>::RangeType* TypeImpl<Config>::GetRange() {
451 DisallowHeapAllocation no_allocation;
452 if (this->IsRange()) return this->AsRange();
453 if (this->IsUnion() && this->AsUnion()->Get(1)->IsRange()) {
454 return this->AsUnion()->Get(1)->AsRange();
455 }
456 return NULL;
457 }
458
459
425 template<class Config> 460 template<class Config>
426 bool TypeImpl<Config>::Contains(i::Object* value) { 461 bool TypeImpl<Config>::Contains(i::Object* value) {
427 DisallowHeapAllocation no_allocation; 462 DisallowHeapAllocation no_allocation;
428 if (this->IsRange()) {
429 return value->IsNumber() &&
430 dle(this->AsRange()->Min(), value->Number()) &&
431 dle(value->Number(), this->AsRange()->Max()) &&
432 BitsetType::Is(BitsetType::Lub(value), this->BitsetLub());
433 }
434 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) { 463 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
435 if (*it.Current() == value) return true; 464 if (*it.Current() == value) return true;
436 } 465 }
466 if (IsInteger(value)) {
467 RangeType* range = this->GetRange();
468 if (range != NULL && contains(range, value)) return true;
469 }
437 return BitsetType::New(BitsetType::Lub(value))->Is(this); 470 return BitsetType::New(BitsetType::Lub(value))->Is(this);
438 } 471 }
439 472
440 473
441 template<class Config> 474 template<class Config>
442 bool TypeImpl<Config>::UnionType::Wellformed() { 475 bool TypeImpl<Config>::UnionType::Wellformed() {
443 DCHECK(this->Length() >= 2); 476 DisallowHeapAllocation no_allocation;
477 // This checks the invariants of the union representation:
478 // 1. There are at least two elements.
479 // 2. At most one element is a bitset, and it must be the first one.
480 // 3. At most one element is a range, and it must be the second one.
rossberg 2014/09/15 15:58:29 Can't you have a range and no bitset?
neis1 2014/09/16 10:03:59 Yes, but in that case you either have another type
rossberg 2014/09/16 14:04:40 Right, figured that out later. Maybe say so explic
neis1 2014/09/18 13:05:18 Done.
481 // 4. No element is itself a union.
482 // 5. No element is a subtype of any other.
483 DCHECK(this->Length() >= 2); // (1)
444 for (int i = 0; i < this->Length(); ++i) { 484 for (int i = 0; i < this->Length(); ++i) {
445 DCHECK(!this->Get(i)->IsUnion()); 485 if (i != 0) DCHECK(!this->Get(i)->IsBitset()); // (2)
446 if (i > 0) DCHECK(!this->Get(i)->IsBitset()); 486 if (i != 1) DCHECK(!this->Get(i)->IsRange()); // (3)
487 DCHECK(!this->Get(i)->IsUnion()); // (4)
447 for (int j = 0; j < this->Length(); ++j) { 488 for (int j = 0; j < this->Length(); ++j) {
448 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j))); 489 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j))); // (5)
449 } 490 }
450 } 491 }
451 return true; 492 return true;
452 } 493 }
453 494
454 495
455 // ----------------------------------------------------------------------------- 496 // -----------------------------------------------------------------------------
456 // Union and intersection 497 // Union and intersection
457 498
458 template<class Config> 499
459 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound( 500 static bool AddIsSafe(int x, int y) {
460 int bitset, Region* region) { 501 return x >= 0 ?
461 TypeHandle bound = BitsetType::New(bitset, region); 502 y <= std::numeric_limits<int>::max() - x :
462 if (this->IsClass()) { 503 y >= std::numeric_limits<int>::min() - x;
463 return ClassType::New(this->AsClass()->Map(), bound, region);
464 } else if (this->IsConstant()) {
465 return ConstantType::New(this->AsConstant()->Value(), bound, region);
466 } else if (this->IsRange()) {
467 return RangeType::New(
468 this->AsRange()->Min(), this->AsRange()->Max(), bound, region);
469 } else if (this->IsContext()) {
470 return ContextType::New(this->AsContext()->Outer(), bound, region);
471 } else if (this->IsArray()) {
472 return ArrayType::New(this->AsArray()->Element(), bound, region);
473 } else if (this->IsFunction()) {
474 FunctionHandle function = Config::handle(this->AsFunction());
475 int arity = function->Arity();
476 FunctionHandle type = FunctionType::New(
477 function->Result(), function->Receiver(), bound, arity, region);
478 for (int i = 0; i < arity; ++i) {
479 type->InitParameter(i, function->Parameter(i));
480 }
481 return type;
482 }
483 UNREACHABLE();
484 return TypeHandle();
485 } 504 }
486 505
487 506
488 template<class Config> 507 template<class Config>
489 int TypeImpl<Config>::BoundBy(TypeImpl* that) { 508 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
490 DCHECK(!this->IsUnion()); 509 TypeHandle type1, TypeHandle type2, Region* region) {
491 if (that->IsUnion()) { 510 int bitset = type1->BitsetGlb() & type2->BitsetGlb();
492 UnionType* unioned = that->AsUnion(); 511 if (!BitsetType::IsInhabited(bitset)) bitset = BitsetType::kNone;
493 int length = unioned->Length(); 512
494 int bitset = BitsetType::kNone; 513 // Fast case: bit sets.
495 for (int i = 0; i < length; ++i) { 514 if (type1->IsBitset() && type2->IsBitset()) {
496 bitset |= BoundBy(unioned->Get(i)->unhandle()); 515 return BitsetType::New(bitset, region);
516 }
517
518 // Fast case: top or bottom types.
519 if (type1->IsNone() || type2->IsAny()) return type1;
520 if (type2->IsNone() || type2->IsAny()) return type2;
521
522 // Semi-fast case.
523 if (type1->Is(type2)) return type1;
524 if (type2->Is(type1)) return type2;
525
526 // Slow case: create union.
527 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
528 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
529 if (!AddIsSafe(size1, size2)) return Any(region);
530 int size = size1 + size2;
531 if (!AddIsSafe(size, 2)) return Any(region);
532 size += 2;
533 UnionHandle result = UnionType::New(size, region);
534 size = 0;
535
536 // Deal with bitsets.
537 result->Set(size++, BitsetType::New(bitset, region));
538
539 // Deal with ranges.
540 TypeHandle range = None(region);
541 RangeType* range1 = type1->GetRange();
542 RangeType* range2 = type2->GetRange();
543 if (range1 != NULL && range2 != NULL) {
544 Limits lim = intersect(Limits(range1), Limits(range2));
545 if (lim.min->Number() <= lim.max->Number()) {
546 range = RangeType::New(lim, region);
497 } 547 }
498 return bitset;
499 } else if (that->IsClass() && this->IsClass() &&
500 *this->AsClass()->Map() == *that->AsClass()->Map()) {
501 return that->BitsetLub();
502 } else if (that->IsConstant() && this->IsConstant() &&
503 *this->AsConstant()->Value() == *that->AsConstant()->Value()) {
504 return that->AsConstant()->Bound()->AsBitset();
505 } else if (that->IsContext() && this->IsContext() && this->Is(that)) {
506 return that->AsContext()->Bound()->AsBitset();
507 } else if (that->IsArray() && this->IsArray() && this->Is(that)) {
508 return that->AsArray()->Bound()->AsBitset();
509 } else if (that->IsFunction() && this->IsFunction() && this->Is(that)) {
510 return that->AsFunction()->Bound()->AsBitset();
511 } 548 }
512 return that->BitsetGlb(); 549 result->Set(size++, range);
550
551 size = IntersectAux(type1, type2, result, size, region);
552 return NormalizeUnion(result, size);
513 } 553 }
514 554
515 555
516 template<class Config> 556 template<class Config>
517 int TypeImpl<Config>::IndexInUnion( 557 int TypeImpl<Config>::UpdateRange(
518 int bound, UnionHandle unioned, int current_size) { 558 RangeHandle range, UnionHandle result, int size, Region* region) {
519 DCHECK(!this->IsUnion()); 559 if (range->Is(result->Get(1))) return size;
rossberg 2014/09/15 15:58:29 Maybe bind to a variable Get(1), instead of repeat
neis1 2014/09/16 10:04:00 Done.
rossberg 2014/09/16 14:04:40 I think it would actually be cleaner conceptually,
neis1 2014/09/18 13:05:18 As discussed, it might be None too. I documented
520 for (int i = 0; i < current_size; ++i) { 560 if (!result->Get(1)->Is(range->unhandle())) {
rossberg 2014/09/15 15:58:30 Is the unhandle needed?
neis1 2014/09/16 10:04:00 Yes, doesn't compile otherwise.
rossberg 2014/09/16 14:04:40 That is weird. What's the error? (I'd really like
521 TypeHandle that = unioned->Get(i); 561 range = RangeType::New(union_(
522 if (that->IsBitset()) { 562 Limits(range->AsRange()), Limits(result->Get(1)->AsRange())), region);
523 if (BitsetType::Is(bound, that->AsBitset())) return i;
524 } else if (that->IsClass() && this->IsClass()) {
525 if (*this->AsClass()->Map() == *that->AsClass()->Map()) return i;
526 } else if (that->IsConstant() && this->IsConstant()) {
527 if (*this->AsConstant()->Value() == *that->AsConstant()->Value())
528 return i;
529 } else if (that->IsContext() && this->IsContext()) {
530 if (this->Is(that)) return i;
531 } else if (that->IsArray() && this->IsArray()) {
532 if (this->Is(that)) return i;
533 } else if (that->IsFunction() && this->IsFunction()) {
534 if (this->Is(that)) return i;
535 }
536 } 563 }
537 return -1; 564 result->Set(1, range);
538 }
539 565
540 566 // Remove any components that just got subsumed.
541 // Get non-bitsets from type, bounded by upper. 567 for (int i = 2; i < size; ) {
542 // Store at result starting at index. Returns updated index. 568 if (result->Get(i)->Is(range->unhandle())) {
rossberg 2014/09/15 15:58:30 Likewise, here you only need to consider constants
neis1 2014/09/16 10:03:59 See above.
543 template<class Config> 569 result->Set(i, result->Get(--size));
544 int TypeImpl<Config>::ExtendUnion( 570 } else {
545 UnionHandle result, int size, TypeHandle type, 571 ++i;
546 TypeHandle other, bool is_intersect, Region* region) {
547 if (type->IsUnion()) {
548 UnionHandle unioned = handle(type->AsUnion());
549 for (int i = 0; i < unioned->Length(); ++i) {
550 TypeHandle type_i = unioned->Get(i);
551 DCHECK(i == 0 || !(type_i->IsBitset() || type_i->Is(unioned->Get(0))));
552 if (!type_i->IsBitset()) {
553 size = ExtendUnion(result, size, type_i, other, is_intersect, region);
554 }
555 }
556 } else if (!type->IsBitset()) {
557 DCHECK(type->IsClass() || type->IsConstant() || type->IsRange() ||
558 type->IsContext() || type->IsArray() || type->IsFunction());
559 int inherent_bound = type->InherentBitsetLub();
560 int old_bound = type->BitsetLub();
561 int other_bound = type->BoundBy(other->unhandle()) & inherent_bound;
562 int new_bound =
563 is_intersect ? (old_bound & other_bound) : (old_bound | other_bound);
564 if (new_bound != BitsetType::kNone) {
565 int i = type->IndexInUnion(new_bound, result, size);
566 if (i == -1) {
567 i = size++;
568 } else if (result->Get(i)->IsBitset()) {
569 return size; // Already fully subsumed.
570 } else {
571 int type_i_bound = result->Get(i)->BitsetLub();
572 new_bound |= type_i_bound;
573 if (new_bound == type_i_bound) return size;
574 }
575 if (new_bound != old_bound) type = type->Rebound(new_bound, region);
576 result->Set(i, type);
577 } 572 }
578 } 573 }
579 return size; 574 return size;
580 } 575 }
581 576
582 577
583 // Union is O(1) on simple bitsets, but O(n*m) on structured unions. 578 template<class Config>
579 int TypeImpl<Config>::IntersectAux(
580 TypeHandle lhs, TypeHandle rhs,
581 UnionHandle result, int size, Region* region) {
582 if (lhs->IsUnion()) {
583 for (int i = 0; i < lhs->AsUnion()->Length(); ++i) {
584 size = IntersectAux(lhs->AsUnion()->Get(i), rhs, result, size, region);
585 }
586 return size;
587 }
588 if (rhs->IsUnion()) {
589 for (int i = 0; i < rhs->AsUnion()->Length(); ++i) {
590 size = IntersectAux(lhs, rhs->AsUnion()->Get(i), result, size, region);
591 }
592 return size;
593 }
594
595 if (!BitsetType::IsInhabited(lhs->BitsetLub() & rhs->BitsetLub())) {
596 return size;
597 }
598
599 if (lhs->IsBitset()) {
600 if (rhs->IsRange()) {
601 return UpdateRange(
602 Config::template cast<RangeType>(rhs), result, size, region);
603 } else {
604 return AddToUnion(rhs, result, size, region);
605 }
606 }
607 if (rhs->IsBitset()) {
608 if (lhs->IsRange()) {
609 return UpdateRange(
610 Config::template cast<RangeType>(lhs), result, size, region);
611 } else {
612 return AddToUnion(lhs, result, size, region);
613 }
614 }
615 if (lhs->IsClass() && !rhs->IsClass()) {
616 if (rhs->IsRange()) {
617 return UpdateRange(
618 Config::template cast<RangeType>(rhs), result, size, region);
619 } else {
620 return AddToUnion(rhs, result, size, region);
621 }
622 }
623 if (rhs->IsClass() && !lhs->IsClass()) {
624 if (lhs->IsRange()) {
625 return UpdateRange(
626 Config::template cast<RangeType>(lhs), result, size, region);
627 } else {
628 return AddToUnion(lhs, result, size, region);
629 }
630 }
631 if (lhs->IsRange()) {
632 if (rhs->IsConstant() &&
633 contains(lhs->AsRange(), *rhs->AsConstant()->Value())) {
634 return AddToUnion(rhs, result, size, region);
635 }
636 return size; // Shortcut.
637 }
638 if (rhs->IsRange()) {
639 if (lhs->IsConstant() &&
640 contains(rhs->AsRange(), *lhs->AsConstant()->Value())) {
641 return AddToUnion(lhs, result, size, region);
642 }
643 return size; // Shortcut.
644 }
645
646 if (lhs->Is(rhs)) return AddToUnion(lhs, result, size, region);
647 if (rhs->Is(lhs)) return AddToUnion(rhs, result, size, region);
648 return size;
649 }
650
651
584 template<class Config> 652 template<class Config>
585 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union( 653 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union(
586 TypeHandle type1, TypeHandle type2, Region* region) { 654 TypeHandle type1, TypeHandle type2, Region* region) {
655
587 // Fast case: bit sets. 656 // Fast case: bit sets.
588 if (type1->IsBitset() && type2->IsBitset()) { 657 if (type1->IsBitset() && type2->IsBitset()) {
589 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region); 658 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region);
590 } 659 }
591 660
592 // Fast case: top or bottom types. 661 // Fast case: top or bottom types.
593 if (type1->IsAny() || type2->IsNone()) return type1; 662 if (type1->IsAny() || type2->IsNone()) return type1;
594 if (type2->IsAny() || type1->IsNone()) return type2; 663 if (type2->IsAny() || type1->IsNone()) return type2;
595 664
596 // Semi-fast case: Unioned objects are neither involved nor produced. 665 // Semi-fast case.
597 if (!(type1->IsUnion() || type2->IsUnion())) { 666 if (type1->Is(type2)) return type2;
598 if (type1->Is(type2)) return type2; 667 if (type2->Is(type1)) return type1;
599 if (type2->Is(type1)) return type1; 668
669 // Slow case: create union.
670 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
671 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
672 if (!AddIsSafe(size1, size2)) return Any(region);
673 int size = size1 + size2;
674 if (!AddIsSafe(size, 2)) return Any(region);
675 size += 2;
676 UnionHandle result = UnionType::New(size, region);
677 size = 0;
678
679 // Deal with bitsets.
680 TypeHandle bitset = BitsetType::New(
681 type1->BitsetGlb() | type2->BitsetGlb(), region);
682 result->Set(size++, bitset);
683
684 // Deal with ranges.
685 TypeHandle range = None(region);
686 RangeType* range1 = type1->GetRange();
687 RangeType* range2 = type2->GetRange();
688 if (range1 != NULL && range2 != NULL) {
689 range = RangeType::New(union_(Limits(range1), Limits(range2)), region);
690 } else if (range1 != NULL) {
691 range = handle(range1);
692 } else if (range2 != NULL) {
693 range = handle(range2);
600 } 694 }
695 result->Set(size++, range);
601 696
602 // Slow case: may need to produce a Unioned object. 697 size = AddToUnion(type1, result, size, region);
603 int size = 0; 698 size = AddToUnion(type2, result, size, region);
604 if (!type1->IsBitset()) { 699 return NormalizeUnion(result, size);
605 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
606 }
607 if (!type2->IsBitset()) {
608 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
609 }
610 int bitset = type1->BitsetGlb() | type2->BitsetGlb();
611 if (bitset != BitsetType::kNone) ++size;
612 DCHECK(size >= 1);
613
614 UnionHandle unioned = UnionType::New(size, region);
615 size = 0;
616 if (bitset != BitsetType::kNone) {
617 unioned->Set(size++, BitsetType::New(bitset, region));
618 }
619 size = ExtendUnion(unioned, size, type1, type2, false, region);
620 size = ExtendUnion(unioned, size, type2, type1, false, region);
621
622 if (size == 1) {
623 return unioned->Get(0);
624 } else {
625 unioned->Shrink(size);
626 DCHECK(unioned->Wellformed());
627 return unioned;
628 }
629 } 700 }
630 701
631 702
632 // Intersection is O(1) on simple bitsets, but O(n*m) on structured unions. 703 // Add [this] to [result] unless [this] is bitset, range, or already subsumed.
rossberg 2014/09/15 15:58:29 s/this/type/g
neis1 2014/09/16 10:03:59 Done.
704 // Return new size of [result].
633 template<class Config> 705 template<class Config>
634 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect( 706 int TypeImpl<Config>::AddToUnion(
635 TypeHandle type1, TypeHandle type2, Region* region) { 707 TypeHandle type, UnionHandle result, int size, Region* region) {
636 // Fast case: bit sets. 708 if (type->IsBitset() || type->IsRange()) return size;
637 if (type1->IsBitset() && type2->IsBitset()) { 709 if (type->IsUnion()) {
638 return BitsetType::New(type1->AsBitset() & type2->AsBitset(), region); 710 for (int i = 0; i < type->AsUnion()->Length(); ++i) {
711 size = AddToUnion(type->AsUnion()->Get(i), result, size, region);
712 }
713 return size;
639 } 714 }
640 715 for (int i = 0; i < size; ++i) {
641 // Fast case: top or bottom types. 716 if (type->Is(result->Get(i))) return size;
642 if (type1->IsNone() || type2->IsAny()) return type1;
643 if (type2->IsNone() || type1->IsAny()) return type2;
644
645 // Semi-fast case: Unioned objects are neither involved nor produced.
646 if (!(type1->IsUnion() || type2->IsUnion())) {
647 if (type1->Is(type2)) return type1;
648 if (type2->Is(type1)) return type2;
649 } 717 }
650 718 result->Set(size++, type);
651 // Slow case: may need to produce a Unioned object. 719 return size;
652 int size = 0;
653 if (!type1->IsBitset()) {
654 size += (type1->IsUnion() ? type1->AsUnion()->Length() : 1);
655 }
656 if (!type2->IsBitset()) {
657 size += (type2->IsUnion() ? type2->AsUnion()->Length() : 1);
658 }
659 int bitset = type1->BitsetGlb() & type2->BitsetGlb();
660 if (bitset != BitsetType::kNone) ++size;
661 DCHECK(size >= 1);
662
663 UnionHandle unioned = UnionType::New(size, region);
664 size = 0;
665 if (bitset != BitsetType::kNone) {
666 unioned->Set(size++, BitsetType::New(bitset, region));
667 }
668 size = ExtendUnion(unioned, size, type1, type2, true, region);
669 size = ExtendUnion(unioned, size, type2, type1, true, region);
670
671 if (size == 0) {
672 return None(region);
673 } else if (size == 1) {
674 return unioned->Get(0);
675 } else {
676 unioned->Shrink(size);
677 DCHECK(unioned->Wellformed());
678 return unioned;
679 }
680 } 720 }
681 721
682 722
723 template<class Config>
724 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::NormalizeUnion(
725 UnionHandle unioned, int size) {
726 DCHECK(size >= 2);
727 // If bitset or range are None, use their positions for different types.
728 if (unioned->Get(0)->IsNone()) unioned->Set(0, unioned->Get(--size));
729 if (size >= 2 && unioned->Get(1)->Is(unioned->Get(0))) {
rossberg 2014/09/15 15:58:29 This condition looks weird. Can't slot 0 contain w
neis1 2014/09/16 10:04:00 Yes. The code is correct but it's a little twiste
730 unioned->Set(1, unioned->Get(--size));
731 }
732 if (size == 1) return unioned->Get(0);
733 unioned->Shrink(size);
734 SLOW_DCHECK(unioned->Wellformed());
735 return unioned;
736 }
737
738
683 // ----------------------------------------------------------------------------- 739 // -----------------------------------------------------------------------------
684 // Iteration. 740 // Iteration.
685 741
686 template<class Config> 742 template<class Config>
687 int TypeImpl<Config>::NumClasses() { 743 int TypeImpl<Config>::NumClasses() {
688 DisallowHeapAllocation no_allocation; 744 DisallowHeapAllocation no_allocation;
689 if (this->IsClass()) { 745 if (this->IsClass()) {
690 return 1; 746 return 1;
691 } else if (this->IsUnion()) { 747 } else if (this->IsUnion()) {
692 UnionHandle unioned = handle(this->AsUnion()); 748 UnionHandle unioned = handle(this->AsUnion());
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 // ----------------------------------------------------------------------------- 842 // -----------------------------------------------------------------------------
787 // Conversion between low-level representations. 843 // Conversion between low-level representations.
788 844
789 template<class Config> 845 template<class Config>
790 template<class OtherType> 846 template<class OtherType>
791 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert( 847 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
792 typename OtherType::TypeHandle type, Region* region) { 848 typename OtherType::TypeHandle type, Region* region) {
793 if (type->IsBitset()) { 849 if (type->IsBitset()) {
794 return BitsetType::New(type->AsBitset(), region); 850 return BitsetType::New(type->AsBitset(), region);
795 } else if (type->IsClass()) { 851 } else if (type->IsClass()) {
796 TypeHandle bound = BitsetType::New(type->BitsetLub(), region); 852 return ClassType::New(type->AsClass()->Map(), region);
797 return ClassType::New(type->AsClass()->Map(), bound, region);
798 } else if (type->IsConstant()) { 853 } else if (type->IsConstant()) {
799 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region); 854 return ConstantType::New(type->AsConstant()->Value(), region);
800 return ConstantType::New(type->AsConstant()->Value(), bound, region);
801 } else if (type->IsRange()) { 855 } else if (type->IsRange()) {
802 TypeHandle bound = Convert<OtherType>(type->AsRange()->Bound(), region);
803 return RangeType::New( 856 return RangeType::New(
804 type->AsRange()->Min(), type->AsRange()->Max(), bound, region); 857 type->AsRange()->MinV(), type->AsRange()->MaxV(), region);
805 } else if (type->IsContext()) { 858 } else if (type->IsContext()) {
806 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region);
807 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region); 859 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region);
808 return ContextType::New(outer, bound, region); 860 return ContextType::New(outer, region);
809 } else if (type->IsUnion()) { 861 } else if (type->IsUnion()) {
810 int length = type->AsUnion()->Length(); 862 int length = type->AsUnion()->Length();
811 UnionHandle unioned = UnionType::New(length, region); 863 UnionHandle unioned = UnionType::New(length, region);
812 for (int i = 0; i < length; ++i) { 864 for (int i = 0; i < length; ++i) {
813 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region); 865 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region);
814 unioned->Set(i, t); 866 unioned->Set(i, t);
815 } 867 }
816 return unioned; 868 return unioned;
817 } else if (type->IsArray()) { 869 } else if (type->IsArray()) {
818 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region); 870 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region);
819 TypeHandle bound = Convert<OtherType>(type->AsArray()->Bound(), region); 871 return ArrayType::New(element, region);
820 return ArrayType::New(element, bound, region);
821 } else if (type->IsFunction()) { 872 } else if (type->IsFunction()) {
822 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region); 873 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region);
823 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region); 874 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region);
824 TypeHandle bound = Convert<OtherType>(type->AsFunction()->Bound(), region);
825 FunctionHandle function = FunctionType::New( 875 FunctionHandle function = FunctionType::New(
826 res, rcv, bound, type->AsFunction()->Arity(), region); 876 res, rcv, type->AsFunction()->Arity(), region);
827 for (int i = 0; i < function->Arity(); ++i) { 877 for (int i = 0; i < function->Arity(); ++i) {
828 TypeHandle param = Convert<OtherType>( 878 TypeHandle param = Convert<OtherType>(
829 type->AsFunction()->Parameter(i), region); 879 type->AsFunction()->Parameter(i), region);
830 function->InitParameter(i, param); 880 function->InitParameter(i, param);
831 } 881 }
832 return function; 882 return function;
833 } else { 883 } else {
834 UNREACHABLE(); 884 UNREACHABLE();
835 return None(region); 885 return None(region);
836 } 886 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 DisallowHeapAllocation no_allocation; 951 DisallowHeapAllocation no_allocation;
902 if (dim != REPRESENTATION_DIM) { 952 if (dim != REPRESENTATION_DIM) {
903 if (this->IsBitset()) { 953 if (this->IsBitset()) {
904 BitsetType::Print(os, SEMANTIC(this->AsBitset())); 954 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
905 } else if (this->IsClass()) { 955 } else if (this->IsClass()) {
906 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < "; 956 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
907 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim); 957 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
908 os << ")"; 958 os << ")";
909 } else if (this->IsConstant()) { 959 } else if (this->IsConstant()) {
910 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value()) 960 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value())
911 << " : "; 961 << ")";
912 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
913 os << ")";
914 } else if (this->IsRange()) { 962 } else if (this->IsRange()) {
915 os << "Range(" << this->AsRange()->Min() 963 os << "Range(" << this->AsRange()->Min()
916 << ".." << this->AsRange()->Max() << " : "; 964 << ", " << this->AsRange()->Max() << ")";
917 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
918 os << ")";
919 } else if (this->IsContext()) { 965 } else if (this->IsContext()) {
920 os << "Context("; 966 os << "Context(";
921 this->AsContext()->Outer()->PrintTo(os, dim); 967 this->AsContext()->Outer()->PrintTo(os, dim);
922 os << ")"; 968 os << ")";
923 } else if (this->IsUnion()) { 969 } else if (this->IsUnion()) {
924 os << "("; 970 os << "(";
925 UnionHandle unioned = handle(this->AsUnion()); 971 UnionHandle unioned = handle(this->AsUnion());
926 for (int i = 0; i < unioned->Length(); ++i) { 972 for (int i = 0; i < unioned->Length(); ++i) {
927 TypeHandle type_i = unioned->Get(i); 973 TypeHandle type_i = unioned->Get(i);
928 if (i > 0) os << " | "; 974 if (i > 0) os << " | ";
929 type_i->PrintTo(os, dim); 975 type_i->PrintTo(os, dim);
930 } 976 }
931 os << ")"; 977 os << ")";
932 } else if (this->IsArray()) { 978 } else if (this->IsArray()) {
933 os << "Array("; 979 os << "Array(";
934 AsArray()->Element()->PrintTo(os, dim); 980 AsArray()->Element()->PrintTo(os, dim);
935 os << ")"; 981 os << ")";
936 } else if (this->IsFunction()) { 982 } else if (this->IsFunction()) {
983 os << "Function[";
rossberg 2014/09/15 15:58:29 Hm?
neis1 2014/09/16 10:03:59 I found it impossible to parse non-trivial functio
rossberg 2014/09/16 14:04:40 I see, but this syntax is a bit too random (and po
neis1 2014/09/18 13:05:18 I got rid of it.
937 if (!this->AsFunction()->Receiver()->IsAny()) { 984 if (!this->AsFunction()->Receiver()->IsAny()) {
938 this->AsFunction()->Receiver()->PrintTo(os, dim); 985 this->AsFunction()->Receiver()->PrintTo(os, dim);
939 os << "."; 986 os << ".";
940 } 987 }
941 os << "("; 988 os << "(";
942 for (int i = 0; i < this->AsFunction()->Arity(); ++i) { 989 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
943 if (i > 0) os << ", "; 990 if (i > 0) os << ", ";
944 this->AsFunction()->Parameter(i)->PrintTo(os, dim); 991 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
945 } 992 }
946 os << ")->"; 993 os << ")->";
947 this->AsFunction()->Result()->PrintTo(os, dim); 994 this->AsFunction()->Result()->PrintTo(os, dim);
995 os << "]";
948 } else { 996 } else {
949 UNREACHABLE(); 997 UNREACHABLE();
950 } 998 }
951 } 999 }
952 if (dim == BOTH_DIMS) os << "/"; 1000 if (dim == BOTH_DIMS) os << "/";
953 if (dim != SEMANTIC_DIM) { 1001 if (dim != SEMANTIC_DIM) {
954 BitsetType::Print(os, REPRESENTATION(this->BitsetLub())); 1002 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
955 } 1003 }
956 } 1004 }
957 1005
958 1006
959 #ifdef DEBUG 1007 #ifdef DEBUG
960 template <class Config> 1008 template <class Config>
961 void TypeImpl<Config>::Print() { 1009 void TypeImpl<Config>::Print() {
962 OFStream os(stdout); 1010 OFStream os(stdout);
963 PrintTo(os); 1011 PrintTo(os);
964 os << endl; 1012 os << endl;
965 } 1013 }
1014 template <class Config>
1015 void TypeImpl<Config>::BitsetType::Print(int bitset) {
1016 OFStream os(stdout);
1017 Print(os, bitset);
1018 os << endl;
1019 }
966 #endif 1020 #endif
967 1021
968 1022
969 // ----------------------------------------------------------------------------- 1023 // -----------------------------------------------------------------------------
970 // Instantiations. 1024 // Instantiations.
971 1025
972 template class TypeImpl<ZoneTypeConfig>; 1026 template class TypeImpl<ZoneTypeConfig>;
973 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>; 1027 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
974 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>; 1028 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
975 1029
976 template class TypeImpl<HeapTypeConfig>; 1030 template class TypeImpl<HeapTypeConfig>;
977 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>; 1031 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
978 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; 1032 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
979 1033
980 template TypeImpl<ZoneTypeConfig>::TypeHandle 1034 template TypeImpl<ZoneTypeConfig>::TypeHandle
981 TypeImpl<ZoneTypeConfig>::Convert<HeapType>( 1035 TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
982 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*); 1036 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
983 template TypeImpl<HeapTypeConfig>::TypeHandle 1037 template TypeImpl<HeapTypeConfig>::TypeHandle
984 TypeImpl<HeapTypeConfig>::Convert<Type>( 1038 TypeImpl<HeapTypeConfig>::Convert<Type>(
985 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*); 1039 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
986 1040
987 } } // namespace v8::internal 1041 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698