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

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: 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;
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.
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->IsUnion()) {
52 return type->AsBitset();
53 } else if (type->IsUnion()) {
54 UnionHandle unioned = handle(type->AsUnion());
55 int bitset = kNone; 92 int bitset = kNone;
56 for (int i = 0; i < unioned->Length(); ++i) { 93 for (int i = 0; i < type->AsUnion()->Length(); ++i) {
57 bitset |= unioned->Get(i)->BitsetLub(); 94 bitset |= type->AsUnion()->Get(i)->BitsetLub();
58 } 95 }
59 return bitset; 96 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 } 97 }
98 if (type->IsBitset()) return type->AsBitset();
rossberg 2014/09/10 15:44:13 Put this case first, since it is probably the most
neis1 2014/09/11 12:58:12 Done.
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) {
rossberg 2014/09/10 15:44:14 I'm glad that we can get rid of this one...
neis1 2014/09/11 12:58:12 Well, it's basically still there. But we only hav
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> 110 template<class Config>
114 int TypeImpl<Config>::BitsetType::Lub(i::Object* value) { 111 int TypeImpl<Config>::BitsetType::Lub(i::Object* value) {
115 DisallowHeapAllocation no_allocation; 112 DisallowHeapAllocation no_allocation;
116 if (value->IsNumber()) { 113 if (value->IsNumber()) {
117 return Lub(value->Number()) & (value->IsSmi() ? kTaggedInt : kTaggedPtr); 114 return Lub(value->Number()) & (value->IsSmi() ? kTaggedInt : kTaggedPtr);
118 } 115 }
119 return Lub(i::HeapObject::cast(value)->map()); 116 return Lub(i::HeapObject::cast(value)->map());
120 } 117 }
121 118
122 119
123 template<class Config> 120 template<class Config>
124 int TypeImpl<Config>::BitsetType::Lub(double value) { 121 int TypeImpl<Config>::BitsetType::Lub(double value) {
125 DisallowHeapAllocation no_allocation; 122 DisallowHeapAllocation no_allocation;
126 if (i::IsMinusZero(value)) return kMinusZero; 123 if (i::IsMinusZero(value)) return kMinusZero;
127 if (std::isnan(value)) return kNaN; 124 if (std::isnan(value)) return kNaN;
128 if (IsUint32Double(value)) return Lub(FastD2UI(value)); 125 if (IsUint32Double(value)) return Lub(FastD2UI(value));
129 if (IsInt32Double(value)) return Lub(FastD2I(value)); 126 if (IsInt32Double(value)) return Lub(FastD2I(value));
130 return kOtherNumber; 127 return kOtherNumber;
131 } 128 }
132 129
133 130
134 template<class Config> 131 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) { 132 int TypeImpl<Config>::BitsetType::Lub(int32_t value) {
133 DisallowHeapAllocation no_allocation;
148 if (value >= 0x40000000) { 134 if (value >= 0x40000000) {
149 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall; 135 return i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
150 } 136 }
151 if (value >= 0) return kUnsignedSmall; 137 if (value >= 0) return kUnsignedSmall;
152 if (value >= -0x40000000) return kOtherSignedSmall; 138 if (value >= -0x40000000) return kOtherSignedSmall;
153 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall; 139 return i::SmiValuesAre31Bits() ? kOtherSigned32 : kOtherSignedSmall;
154 } 140 }
155 141
156 142
157 template<class Config> 143 template<class Config>
(...skipping 16 matching lines...) Expand all
174 case CONS_STRING_TYPE: 160 case CONS_STRING_TYPE:
175 case CONS_ONE_BYTE_STRING_TYPE: 161 case CONS_ONE_BYTE_STRING_TYPE:
176 case SLICED_STRING_TYPE: 162 case SLICED_STRING_TYPE:
177 case SLICED_ONE_BYTE_STRING_TYPE: 163 case SLICED_ONE_BYTE_STRING_TYPE:
178 case EXTERNAL_STRING_TYPE: 164 case EXTERNAL_STRING_TYPE:
179 case EXTERNAL_ONE_BYTE_STRING_TYPE: 165 case EXTERNAL_ONE_BYTE_STRING_TYPE:
180 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: 166 case EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
181 case SHORT_EXTERNAL_STRING_TYPE: 167 case SHORT_EXTERNAL_STRING_TYPE:
182 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE: 168 case SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE:
183 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE: 169 case SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE:
170 return kOtherString;
184 case INTERNALIZED_STRING_TYPE: 171 case INTERNALIZED_STRING_TYPE:
185 case ONE_BYTE_INTERNALIZED_STRING_TYPE: 172 case ONE_BYTE_INTERNALIZED_STRING_TYPE:
186 case EXTERNAL_INTERNALIZED_STRING_TYPE: 173 case EXTERNAL_INTERNALIZED_STRING_TYPE:
187 case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: 174 case EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
188 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: 175 case EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
189 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE: 176 case SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE:
190 case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE: 177 case SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE:
191 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE: 178 case SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE:
192 return kString; 179 return kInternalizedString;
193 case SYMBOL_TYPE: 180 case SYMBOL_TYPE:
194 return kSymbol; 181 return kSymbol;
195 case ODDBALL_TYPE: { 182 case ODDBALL_TYPE: {
196 Heap* heap = map->GetHeap(); 183 Heap* heap = map->GetHeap();
197 if (map == heap->undefined_map()) return kUndefined; 184 if (map == heap->undefined_map()) return kUndefined;
198 if (map == heap->null_map()) return kNull; 185 if (map == heap->null_map()) return kNull;
199 if (map == heap->boolean_map()) return kBoolean; 186 if (map == heap->boolean_map()) return kBoolean;
200 DCHECK(map == heap->the_hole_map() || 187 DCHECK(map == heap->the_hole_map() ||
201 map == heap->uninitialized_map() || 188 map == heap->uninitialized_map() ||
202 map == heap->no_interceptor_result_sentinel_map() || 189 map == heap->no_interceptor_result_sentinel_map() ||
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 case FOREIGN_TYPE: 241 case FOREIGN_TYPE:
255 case CODE_TYPE: 242 case CODE_TYPE:
256 return kInternal & kTaggedPtr; 243 return kInternal & kTaggedPtr;
257 default: 244 default:
258 UNREACHABLE(); 245 UNREACHABLE();
259 return kNone; 246 return kNone;
260 } 247 }
261 } 248 }
262 249
263 250
251 template<class Config>
252 double TypeImpl<Config>::BitsetType::Min(int bitset) {
253 DisallowHeapAllocation no_allocation;
254 switch (bitset) {
255 case kUnsignedSmall:
256 return 0;
257 case kOtherSignedSmall:
258 return i::SmiValuesAre31Bits() ? -0x40000000 : kMinInt;
259 case kOtherUnsigned31: // Only used if SmiValuesAre31Bits().
260 return 0x40000000u;
261 case kOtherUnsigned32:
262 return 0x80000000u;
263 case kOtherSigned32: // Only used if SmiValuesAre31Bits().
264 return kMinInt;
265 case kOtherNumber: // The positive integer part of OtherNumber.
266 return kMaxUInt32;
rossberg 2014/09/10 15:44:14 Why not static_cast<double>(kMaxUint32) + 1, and a
neis1 2014/09/11 12:58:12 Right, that's what it should have been in the firs
267 default:
268 UNREACHABLE();
269 return 0;
270 }
271 }
272
273
274 template<class Config>
275 int TypeImpl<Config>::BitsetType::Lub(Limits lim) {
rossberg 2014/09/10 15:44:14 Nit: move this decl closer to the other numeric Lu
neis1 2014/09/11 12:58:12 I moved the Lub for maps up. The order now matche
276 DisallowHeapAllocation no_allocation;
277 double min = lim.min->Number();
278 double max = lim.max->Number();
279 int lub = kNone;
280
281 if (min < Min(kOtherSigned32)) {
rossberg 2014/09/10 15:44:13 Hm, can't this written more concisely using a loop
neis1 2014/09/11 12:58:12 I rewrote it and also got rid of the Min() functio
282 lub |= kOtherNumber;
283 if (max < Min(kOtherSigned32)) return lub;
284 }
285 if (i::SmiValuesAre31Bits() && min < Min(kOtherSignedSmall)) {
286 lub |= kOtherSigned32;
287 if (max < Min(kOtherSignedSmall)) return lub;
288 }
289 if (min < Min(kUnsignedSmall)) {
290 lub |= kOtherSignedSmall;
291 if (max < Min(kUnsignedSmall)) return lub;
292 }
293 if (min < Min(kOtherUnsigned31)) {
294 lub |= kUnsignedSmall;
295 if (max < Min(kOtherUnsigned31)) return lub;
296 }
297 if (min < Min(kOtherUnsigned32)) {
298 lub |= i::SmiValuesAre31Bits() ? kOtherUnsigned31 : kUnsignedSmall;
299 if (max < Min(kOtherUnsigned32)) return lub;
300 }
301 if (min < Min(kOtherNumber)) {
rossberg 2014/09/10 15:44:14 Shouldn't this be <= ?
neis1 2014/09/11 12:58:12 The <= below should have been < but due to the bug
302 lub |= kOtherUnsigned32;
303 if (max <= Min(kOtherNumber)) return lub;
304 }
305 return lub | kOtherNumber;
306 }
307
308
264 // ----------------------------------------------------------------------------- 309 // -----------------------------------------------------------------------------
265 // Predicates. 310 // Predicates.
266 311
267 // Check this <= that. 312
313 template<class Config>
314 bool TypeImpl<Config>::SimplyEquals(TypeImpl* that) {
rossberg 2014/09/10 15:44:14 Is this not supposed to handle bitsets? If so, add
neis1 2014/09/11 12:58:12 Done.
315 DisallowHeapAllocation no_allocation;
316 if (this->IsClass()) {
317 return that->IsClass()
318 && *this->AsClass()->Map() == *that->AsClass()->Map();
319 }
320 if (this->IsConstant()) {
321 return that->IsConstant()
322 && *this->AsConstant()->Value() == *that->AsConstant()->Value();
323 }
324 if (this->IsContext()) {
325 return that->IsContext()
326 && this->AsContext()->Outer()->Equals(that->AsContext()->Outer());
327 }
328 if (this->IsArray()) {
329 return that->IsArray()
330 && this->AsArray()->Element()->Equals(that->AsArray()->Element());
331 }
332 if (this->IsFunction()) {
333 if (!that->IsFunction()) return false;
334 FunctionType* this_fun = this->AsFunction();
335 FunctionType* that_fun = that->AsFunction();
336 if (this_fun->Arity() != that_fun->Arity() ||
337 !this_fun->Result()->Equals(that_fun->Result()) ||
338 !this_fun->Receiver()->Equals(that_fun->Receiver())) {
339 return false;
340 }
341 for (int i = 0; i < this_fun->Arity(); ++i) {
342 if (!this_fun->Parameter(i)->Equals(that_fun->Parameter(i))) return false;
343 }
344 return true;
345 }
346 return false;
347 }
348
349
350 // Check if [this] <= [that].
268 template<class Config> 351 template<class Config>
269 bool TypeImpl<Config>::SlowIs(TypeImpl* that) { 352 bool TypeImpl<Config>::SlowIs(TypeImpl* that) {
270 DisallowHeapAllocation no_allocation; 353 DisallowHeapAllocation no_allocation;
271 354
272 // Fast path for bitsets. 355 // (T1 \/ ... \/ Tn) <= T if (T1 <= T) /\ ... /\ (Tn <= T)
273 if (this->IsNone()) return true;
274 if (that->IsBitset()) {
275 return BitsetType::Is(BitsetType::Lub(this), that->AsBitset());
276 }
277
278 if (that->IsClass()) {
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()) { 356 if (this->IsUnion()) {
323 UnionHandle unioned = handle(this->AsUnion()); 357 UnionHandle unioned = handle(this->AsUnion());
324 for (int i = 0; i < unioned->Length(); ++i) { 358 for (int i = 0; i < unioned->Length(); ++i) {
325 if (!unioned->Get(i)->Is(that)) return false; 359 if (!unioned->Get(i)->Is(that)) return false;
326 } 360 }
327 return true; 361 return true;
328 } 362 }
329 363
330 // T <= (T1 \/ ... \/ Tn) <=> (T <= T1) \/ ... \/ (T <= Tn) 364 // T <= (T1 \/ ... \/ Tn) if (T <= T1) \/ ... \/ (T <= Tn)
331 // (iff T is not a union) 365 if (that->IsUnion()) {
332 DCHECK(!this->IsUnion() && that->IsUnion()); 366 UnionHandle unioned = handle(that->AsUnion());
333 UnionHandle unioned = handle(that->AsUnion()); 367 for (int i = 0; i < unioned->Length(); ++i) {
334 for (int i = 0; i < unioned->Length(); ++i) { 368 if (this->Is(unioned->Get(i))) return true;
335 if (this->Is(unioned->Get(i))) return true; 369 if (this->IsBitset()) return false; // Shortcut.
336 if (this->IsBitset()) break; // Fast fail, only first field is a bitset. 370 if (i > 1 && this->IsRange()) return false; // Shortcut.
371 }
372 return false;
337 } 373 }
338 return false; 374
375 if (that->IsBitset()) {
rossberg 2014/09/10 15:44:14 Always handle the bitset case first where possible
neis1 2014/09/11 12:58:12 Ok. I rewrote this slightly.
376 return BitsetType::Is(this->BitsetLub(), that->AsBitset());
377 }
378 if (this->IsBitset()) return this->IsNone();
379 if (this->IsRange()) {
380 return that->IsRange() && contains(that->AsRange(), this->AsRange());
381 }
382 if (this->IsConstant() && that->IsRange()) {
383 return contains(that->AsRange(), *this->AsConstant()->Value());
384 }
385 return this->SimplyEquals(that);
339 } 386 }
340 387
341 388
342 template<class Config> 389 template<class Config>
343 bool TypeImpl<Config>::NowIs(TypeImpl* that) { 390 bool TypeImpl<Config>::NowIs(TypeImpl* that) {
344 DisallowHeapAllocation no_allocation; 391 DisallowHeapAllocation no_allocation;
345 392
346 // TODO(rossberg): this is incorrect for 393 // TODO(rossberg): this is incorrect for
347 // Union(Constant(V), T)->NowIs(Class(M)) 394 // Union(Constant(V), T)->NowIs(Class(M))
348 // but fuzzing does not cover that! 395 // but fuzzing does not cover that!
349 if (this->IsConstant()) { 396 if (this->IsConstant()) {
350 i::Object* object = *this->AsConstant()->Value(); 397 i::Object* object = *this->AsConstant()->Value();
351 if (object->IsHeapObject()) { 398 if (object->IsHeapObject()) {
352 i::Map* map = i::HeapObject::cast(object)->map(); 399 i::Map* map = i::HeapObject::cast(object)->map();
353 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) { 400 for (Iterator<i::Map> it = that->Classes(); !it.Done(); it.Advance()) {
354 if (*it.Current() == map) return true; 401 if (*it.Current() == map) return true;
355 } 402 }
356 } 403 }
357 } 404 }
358 return this->Is(that); 405 return this->Is(that);
359 } 406 }
360 407
361 408
362 // Check if this contains only (currently) stable classes. 409 // Check if [this] contains only (currently) stable classes.
363 template<class Config> 410 template<class Config>
364 bool TypeImpl<Config>::NowStable() { 411 bool TypeImpl<Config>::NowStable() {
365 DisallowHeapAllocation no_allocation; 412 DisallowHeapAllocation no_allocation;
366 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) { 413 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) {
367 if (!it.Current()->is_stable()) return false; 414 if (!it.Current()->is_stable()) return false;
368 } 415 }
369 return true; 416 return true;
370 } 417 }
371 418
372 419
373 // Check this overlaps that. 420 // Check if [this] and [that] overlap.
374 template<class Config> 421 template<class Config>
375 bool TypeImpl<Config>::Maybe(TypeImpl* that) { 422 bool TypeImpl<Config>::Maybe(TypeImpl* that) {
376 DisallowHeapAllocation no_allocation; 423 DisallowHeapAllocation no_allocation;
377 424
378 // (T1 \/ ... \/ Tn) overlaps T <=> (T1 overlaps T) \/ ... \/ (Tn overlaps T) 425 // (T1 \/ ... \/ Tn) overlaps T if (T1 overlaps T) \/ ... \/ (Tn overlaps T)
379 if (this->IsUnion()) { 426 if (this->IsUnion()) {
380 UnionHandle unioned = handle(this->AsUnion()); 427 UnionHandle unioned = handle(this->AsUnion());
381 for (int i = 0; i < unioned->Length(); ++i) { 428 for (int i = 0; i < unioned->Length(); ++i) {
382 if (unioned->Get(i)->Maybe(that)) return true; 429 if (unioned->Get(i)->Maybe(that)) return true;
383 } 430 }
384 return false; 431 return false;
385 } 432 }
386 433
387 // T overlaps (T1 \/ ... \/ Tn) <=> (T overlaps T1) \/ ... \/ (T overlaps Tn) 434 // T overlaps (T1 \/ ... \/ Tn) if (T overlaps T1) \/ ... \/ (T overlaps Tn)
435 // (iff T is not a union)
rossberg 2014/09/10 15:44:14 I'm confused...
neis1 2014/09/11 12:58:12 Hehe. Line 435 is a leftover from older code.
388 if (that->IsUnion()) { 436 if (that->IsUnion()) {
389 UnionHandle unioned = handle(that->AsUnion()); 437 for (int i = 0; i < that->AsUnion()->Length(); ++i) {
390 for (int i = 0; i < unioned->Length(); ++i) { 438 if (this->Maybe(that->AsUnion()->Get(i))) return true;
391 if (this->Maybe(unioned->Get(i))) return true;
392 } 439 }
393 return false; 440 return false;
394 } 441 }
395 442
396 DCHECK(!this->IsUnion() && !that->IsUnion()); 443 if (!BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()))
397 if (this->IsBitset() || that->IsBitset()) { 444 return false;
398 return BitsetType::IsInhabited(this->BitsetLub() & that->BitsetLub()); 445 if (this->IsBitset() || that->IsBitset()) return true;
446
447 if (this->IsClass() && !that->IsClass()) return true;
448 if (that->IsClass() && !this->IsClass()) return true;
449
450 if (this->IsRange()) {
451 if (that->IsConstant()) {
452 return contains(this->AsRange(), *that->AsConstant()->Value());
453 }
454 return that->IsRange() && overlap(this->AsRange(), that->AsRange());
399 } 455 }
400 if (this->IsClass()) { 456 if (that->IsRange()) {
401 return that->IsClass() 457 if (this->IsConstant()) {
402 && *this->AsClass()->Map() == *that->AsClass()->Map(); 458 return contains(that->AsRange(), *this->AsConstant()->Value());
403 } 459 }
404 if (this->IsConstant()) { 460 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 } 461 }
419 462
420 return false; 463 return this->SimplyEquals(that);
421 } 464 }
422 465
423 466
424 // Check if value is contained in (inhabits) type. 467 // Return the range in [this], or [NULL].
468 template<class Config>
469 typename TypeImpl<Config>::RangeType* TypeImpl<Config>::GetRange() {
470 DisallowHeapAllocation no_allocation;
471 if (this->IsRange()) return this->AsRange();
472 if (this->IsUnion() && this->AsUnion()->Get(1)->IsRange()) {
473 return this->AsUnion()->Get(1)->AsRange();
474 }
475 return NULL;
476 }
477
478
425 template<class Config> 479 template<class Config>
426 bool TypeImpl<Config>::Contains(i::Object* value) { 480 bool TypeImpl<Config>::Contains(i::Object* value) {
427 DisallowHeapAllocation no_allocation; 481 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()) { 482 for (Iterator<i::Object> it = this->Constants(); !it.Done(); it.Advance()) {
435 if (*it.Current() == value) return true; 483 if (*it.Current() == value) return true;
436 } 484 }
485 if (IsInteger(value)) {
486 RangeType* range = this->GetRange();
487 if (range != NULL && contains(range, value)) return true;
488 }
437 return BitsetType::New(BitsetType::Lub(value))->Is(this); 489 return BitsetType::New(BitsetType::Lub(value))->Is(this);
438 } 490 }
439 491
440 492
441 template<class Config> 493 template<class Config>
442 bool TypeImpl<Config>::UnionType::Wellformed() { 494 bool TypeImpl<Config>::UnionType::Wellformed() {
443 DCHECK(this->Length() >= 2); 495 DisallowHeapAllocation no_allocation;
496 // This checks the invariants of the union representation:
497 // 1. There are at least two elements.
498 // 2. At most one element is a bitset, and it must be the first one.
499 // 3. At most one element is a range, and it must be the second one.
500 // 4. No element is itself a union.
501 // 5. No element is a subtype of any other.
502 DCHECK(this->Length() >= 2); // (1)
444 for (int i = 0; i < this->Length(); ++i) { 503 for (int i = 0; i < this->Length(); ++i) {
445 DCHECK(!this->Get(i)->IsUnion()); 504 if (i != 0) DCHECK(!this->Get(i)->IsBitset()); // (2)
446 if (i > 0) DCHECK(!this->Get(i)->IsBitset()); 505 if (i != 1) DCHECK(!this->Get(i)->IsRange()); // (3)
506 DCHECK(!this->Get(i)->IsUnion()); // (4)
447 for (int j = 0; j < this->Length(); ++j) { 507 for (int j = 0; j < this->Length(); ++j) {
448 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j))); 508 if (i != j) DCHECK(!this->Get(i)->Is(this->Get(j))); // (5)
449 } 509 }
450 } 510 }
451 return true; 511 return true;
452 } 512 }
453 513
454 514
455 // ----------------------------------------------------------------------------- 515 // -----------------------------------------------------------------------------
456 // Union and intersection 516 // Union and intersection
457 517
458 template<class Config> 518
459 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Rebound( 519 static bool AddIsSafe(int x, int y) {
460 int bitset, Region* region) { 520 return x >= 0 ?
461 TypeHandle bound = BitsetType::New(bitset, region); 521 y <= std::numeric_limits<int>::max() - x :
462 if (this->IsClass()) { 522 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 } 523 }
486 524
487 525
488 template<class Config> 526 template<class Config>
489 int TypeImpl<Config>::BoundBy(TypeImpl* that) { 527 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect(
490 DCHECK(!this->IsUnion()); 528 TypeHandle type1, TypeHandle type2, Region* region) {
491 if (that->IsUnion()) { 529 int bitset = type1->BitsetGlb() & type2->BitsetGlb();
492 UnionType* unioned = that->AsUnion(); 530 if (!BitsetType::IsInhabited(bitset)) bitset = BitsetType::kNone;
493 int length = unioned->Length(); 531
494 int bitset = BitsetType::kNone; 532 // Fast case: bit sets.
495 for (int i = 0; i < length; ++i) { 533 if (type1->IsBitset() && type2->IsBitset()) {
496 bitset |= BoundBy(unioned->Get(i)->unhandle()); 534 return BitsetType::New(bitset, region);
535 }
536
537 // Fast case: top or bottom types.
538 if (type1->IsNone() || type2->IsAny()) return type1;
539 if (type2->IsNone() || type2->IsAny()) return type2;
540
541 // Semi-fast case.
542 if (type1->Is(type2)) return type1;
543 if (type2->Is(type1)) return type2;
544
545 // Slow case: create union.
546 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
547 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
548 if (!AddIsSafe(size1, size2)) return Any(region);
549 int size = size1 + size2;
550 if (!AddIsSafe(size, 2)) return Any(region);
551 size += 2;
552 UnionHandle result = UnionType::New(size, region);
553 size = 0;
554
555 // Deal with bitsets.
556 result->Set(size++, BitsetType::New(bitset, region));
557
558 // Deal with ranges.
559 TypeHandle range = None(region);
560 RangeType* range1 = type1->GetRange();
561 RangeType* range2 = type2->GetRange();
562 if (range1 != NULL && range2 != NULL) {
563 Limits lim = intersect(Limits(range1), Limits(range2));
564 if (lim.min->Number() <= lim.max->Number()) {
565 range = RangeType::New(lim, region);
497 } 566 }
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 } 567 }
512 return that->BitsetGlb(); 568 result->Set(size++, range);
569
570 size = IntersectAux(type1, type2, result, size, region);
571 return NormalizeUnion(result, size);
513 } 572 }
514 573
515 574
516 template<class Config> 575 template<class Config>
517 int TypeImpl<Config>::IndexInUnion( 576 int TypeImpl<Config>::UpdateRange(
518 int bound, UnionHandle unioned, int current_size) { 577 RangeHandle range, UnionHandle result, int size, Region* region) {
519 DCHECK(!this->IsUnion()); 578 if (range->Is(result->Get(1))) return size;
520 for (int i = 0; i < current_size; ++i) { 579 if (!result->Get(1)->Is(range->unhandle())) {
521 TypeHandle that = unioned->Get(i); 580 range = RangeType::New(union_(
522 if (that->IsBitset()) { 581 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 } 582 }
537 return -1; 583 result->Set(1, range);
538 }
539 584
540 585 // Remove any components that just got subsumed.
541 // Get non-bitsets from type, bounded by upper. 586 for (int i = 2; i < size; ) {
542 // Store at result starting at index. Returns updated index. 587 if (result->Get(i)->Is(range->unhandle())) {
543 template<class Config> 588 result->Set(i, result->Get(--size));
544 int TypeImpl<Config>::ExtendUnion( 589 } else {
545 UnionHandle result, int size, TypeHandle type, 590 ++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 } 591 }
578 } 592 }
579 return size; 593 return size;
580 } 594 }
581 595
582 596
583 // Union is O(1) on simple bitsets, but O(n*m) on structured unions. 597 template<class Config>
598 int TypeImpl<Config>::IntersectAux(
599 TypeHandle lhs, TypeHandle rhs,
600 UnionHandle result, int size, Region* region) {
601 if (lhs->IsUnion()) {
602 for (int i = 0; i < lhs->AsUnion()->Length(); ++i) {
603 size = IntersectAux(lhs->AsUnion()->Get(i), rhs, result, size, region);
604 }
605 return size;
606 }
607 if (rhs->IsUnion()) {
608 for (int i = 0; i < rhs->AsUnion()->Length(); ++i) {
609 size = IntersectAux(lhs, rhs->AsUnion()->Get(i), result, size, region);
610 }
611 return size;
612 }
613
614 if (!BitsetType::IsInhabited(lhs->BitsetLub() & rhs->BitsetLub())) {
615 return size;
616 }
617
618 if (lhs->IsBitset()) {
619 if (rhs->IsRange()) {
620 return UpdateRange(
621 Config::template cast<RangeType>(rhs), result, size, region);
622 } else {
623 return AddToUnion(rhs, result, size, region);
624 }
625 }
626 if (rhs->IsBitset()) {
627 if (lhs->IsRange()) {
628 return UpdateRange(
629 Config::template cast<RangeType>(lhs), result, size, region);
630 } else {
631 return AddToUnion(lhs, result, size, region);
632 }
633 }
634 if (lhs->IsClass() && !rhs->IsClass()) {
635 if (rhs->IsRange()) {
636 return UpdateRange(
637 Config::template cast<RangeType>(rhs), result, size, region);
638 } else {
639 return AddToUnion(rhs, result, size, region);
640 }
641 }
642 if (rhs->IsClass() && !lhs->IsClass()) {
643 if (lhs->IsRange()) {
644 return UpdateRange(
645 Config::template cast<RangeType>(lhs), result, size, region);
646 } else {
647 return AddToUnion(lhs, result, size, region);
648 }
649 }
650 if (lhs->IsRange()) {
651 if (rhs->IsConstant() &&
652 contains(lhs->AsRange(), *rhs->AsConstant()->Value())) {
653 return AddToUnion(rhs, result, size, region);
654 }
655 return size; // Shortcut.
656 }
657 if (rhs->IsRange()) {
658 if (lhs->IsConstant() &&
659 contains(rhs->AsRange(), *lhs->AsConstant()->Value())) {
660 return AddToUnion(lhs, result, size, region);
661 }
662 return size; // Shortcut.
663 }
664
665 if (lhs->Is(rhs)) return AddToUnion(lhs, result, size, region);
666 if (rhs->Is(lhs)) return AddToUnion(rhs, result, size, region);
667 return size;
668 }
669
670
584 template<class Config> 671 template<class Config>
585 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union( 672 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Union(
586 TypeHandle type1, TypeHandle type2, Region* region) { 673 TypeHandle type1, TypeHandle type2, Region* region) {
674
587 // Fast case: bit sets. 675 // Fast case: bit sets.
588 if (type1->IsBitset() && type2->IsBitset()) { 676 if (type1->IsBitset() && type2->IsBitset()) {
589 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region); 677 return BitsetType::New(type1->AsBitset() | type2->AsBitset(), region);
590 } 678 }
591 679
592 // Fast case: top or bottom types. 680 // Fast case: top or bottom types.
593 if (type1->IsAny() || type2->IsNone()) return type1; 681 if (type1->IsAny() || type2->IsNone()) return type1;
594 if (type2->IsAny() || type1->IsNone()) return type2; 682 if (type2->IsAny() || type1->IsNone()) return type2;
595 683
596 // Semi-fast case: Unioned objects are neither involved nor produced. 684 // Semi-fast case.
597 if (!(type1->IsUnion() || type2->IsUnion())) { 685 if (type1->Is(type2)) return type2;
598 if (type1->Is(type2)) return type2; 686 if (type2->Is(type1)) return type1;
599 if (type2->Is(type1)) return type1; 687
688 // Slow case: create union.
689 int size1 = type1->IsUnion() ? type1->AsUnion()->Length() : 1;
690 int size2 = type2->IsUnion() ? type2->AsUnion()->Length() : 1;
691 if (!AddIsSafe(size1, size2)) return Any(region);
692 int size = size1 + size2;
693 if (!AddIsSafe(size, 2)) return Any(region);
694 size += 2;
695 UnionHandle result = UnionType::New(size, region);
696 size = 0;
697
698 // Deal with bitsets.
699 TypeHandle bitset = BitsetType::New(
700 type1->BitsetGlb() | type2->BitsetGlb(), region);
701 result->Set(size++, bitset);
702
703 // Deal with ranges.
704 TypeHandle range = None(region);
705 RangeType* range1 = type1->GetRange();
706 RangeType* range2 = type2->GetRange();
707 if (range1 != NULL && range2 != NULL) {
708 range = RangeType::New(union_(Limits(range1), Limits(range2)), region);
709 } else if (range1 != NULL) {
710 range = handle(range1);
711 } else if (range2 != NULL) {
712 range = handle(range2);
600 } 713 }
714 result->Set(size++, range);
601 715
602 // Slow case: may need to produce a Unioned object. 716 size = AddToUnion(type1, result, size, region);
603 int size = 0; 717 size = AddToUnion(type2, result, size, region);
604 if (!type1->IsBitset()) { 718 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 } 719 }
630 720
631 721
632 // Intersection is O(1) on simple bitsets, but O(n*m) on structured unions. 722 // Add [this] to [result] unless [this] is bitset, range, or already subsumed.
723 // Return new size of [result].
633 template<class Config> 724 template<class Config>
634 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Intersect( 725 int TypeImpl<Config>::AddToUnion(
635 TypeHandle type1, TypeHandle type2, Region* region) { 726 TypeHandle type, UnionHandle result, int size, Region* region) {
636 // Fast case: bit sets. 727 if (type->IsBitset() || type->IsRange()) return size;
637 if (type1->IsBitset() && type2->IsBitset()) { 728 if (type->IsUnion()) {
638 return BitsetType::New(type1->AsBitset() & type2->AsBitset(), region); 729 for (int i = 0; i < type->AsUnion()->Length(); ++i) {
730 size = AddToUnion(type->AsUnion()->Get(i), result, size, region);
731 }
732 return size;
639 } 733 }
640 734 for (int i = 0; i < size; ++i) {
641 // Fast case: top or bottom types. 735 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 } 736 }
650 737 result->Set(size++, type);
651 // Slow case: may need to produce a Unioned object. 738 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 } 739 }
681 740
682 741
742 template<class Config>
743 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::NormalizeUnion(
744 UnionHandle unioned, int size) {
745 DCHECK(size >= 2);
746 // If bitset or range are None, use their positions for different types.
747 if (unioned->Get(0)->IsNone()) unioned->Set(0, unioned->Get(--size));
748 if (size >= 2 && unioned->Get(1)->Is(unioned->Get(0))) {
749 unioned->Set(1, unioned->Get(--size));
750 }
751 if (size == 1) return unioned->Get(0);
752 unioned->Shrink(size);
753 SLOW_DCHECK(unioned->Wellformed());
754 return unioned;
755 }
756
757
683 // ----------------------------------------------------------------------------- 758 // -----------------------------------------------------------------------------
684 // Iteration. 759 // Iteration.
685 760
686 template<class Config> 761 template<class Config>
687 int TypeImpl<Config>::NumClasses() { 762 int TypeImpl<Config>::NumClasses() {
688 DisallowHeapAllocation no_allocation; 763 DisallowHeapAllocation no_allocation;
689 if (this->IsClass()) { 764 if (this->IsClass()) {
690 return 1; 765 return 1;
691 } else if (this->IsUnion()) { 766 } else if (this->IsUnion()) {
692 UnionHandle unioned = handle(this->AsUnion()); 767 UnionHandle unioned = handle(this->AsUnion());
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 // ----------------------------------------------------------------------------- 861 // -----------------------------------------------------------------------------
787 // Conversion between low-level representations. 862 // Conversion between low-level representations.
788 863
789 template<class Config> 864 template<class Config>
790 template<class OtherType> 865 template<class OtherType>
791 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert( 866 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Convert(
792 typename OtherType::TypeHandle type, Region* region) { 867 typename OtherType::TypeHandle type, Region* region) {
793 if (type->IsBitset()) { 868 if (type->IsBitset()) {
794 return BitsetType::New(type->AsBitset(), region); 869 return BitsetType::New(type->AsBitset(), region);
795 } else if (type->IsClass()) { 870 } else if (type->IsClass()) {
796 TypeHandle bound = BitsetType::New(type->BitsetLub(), region); 871 return ClassType::New(type->AsClass()->Map(), region);
797 return ClassType::New(type->AsClass()->Map(), bound, region);
798 } else if (type->IsConstant()) { 872 } else if (type->IsConstant()) {
799 TypeHandle bound = Convert<OtherType>(type->AsConstant()->Bound(), region); 873 return ConstantType::New(type->AsConstant()->Value(), region);
800 return ConstantType::New(type->AsConstant()->Value(), bound, region);
801 } else if (type->IsRange()) { 874 } else if (type->IsRange()) {
802 TypeHandle bound = Convert<OtherType>(type->AsRange()->Bound(), region);
803 return RangeType::New( 875 return RangeType::New(
804 type->AsRange()->Min(), type->AsRange()->Max(), bound, region); 876 type->AsRange()->MinV(), type->AsRange()->MaxV(), region);
805 } else if (type->IsContext()) { 877 } else if (type->IsContext()) {
806 TypeHandle bound = Convert<OtherType>(type->AsContext()->Bound(), region);
807 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region); 878 TypeHandle outer = Convert<OtherType>(type->AsContext()->Outer(), region);
808 return ContextType::New(outer, bound, region); 879 return ContextType::New(outer, region);
809 } else if (type->IsUnion()) { 880 } else if (type->IsUnion()) {
810 int length = type->AsUnion()->Length(); 881 int length = type->AsUnion()->Length();
811 UnionHandle unioned = UnionType::New(length, region); 882 UnionHandle unioned = UnionType::New(length, region);
812 for (int i = 0; i < length; ++i) { 883 for (int i = 0; i < length; ++i) {
813 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region); 884 TypeHandle t = Convert<OtherType>(type->AsUnion()->Get(i), region);
814 unioned->Set(i, t); 885 unioned->Set(i, t);
815 } 886 }
816 return unioned; 887 return unioned;
817 } else if (type->IsArray()) { 888 } else if (type->IsArray()) {
818 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region); 889 TypeHandle element = Convert<OtherType>(type->AsArray()->Element(), region);
819 TypeHandle bound = Convert<OtherType>(type->AsArray()->Bound(), region); 890 return ArrayType::New(element, region);
820 return ArrayType::New(element, bound, region);
821 } else if (type->IsFunction()) { 891 } else if (type->IsFunction()) {
822 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region); 892 TypeHandle res = Convert<OtherType>(type->AsFunction()->Result(), region);
823 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region); 893 TypeHandle rcv = Convert<OtherType>(type->AsFunction()->Receiver(), region);
824 TypeHandle bound = Convert<OtherType>(type->AsFunction()->Bound(), region);
825 FunctionHandle function = FunctionType::New( 894 FunctionHandle function = FunctionType::New(
826 res, rcv, bound, type->AsFunction()->Arity(), region); 895 res, rcv, type->AsFunction()->Arity(), region);
827 for (int i = 0; i < function->Arity(); ++i) { 896 for (int i = 0; i < function->Arity(); ++i) {
828 TypeHandle param = Convert<OtherType>( 897 TypeHandle param = Convert<OtherType>(
829 type->AsFunction()->Parameter(i), region); 898 type->AsFunction()->Parameter(i), region);
830 function->InitParameter(i, param); 899 function->InitParameter(i, param);
831 } 900 }
832 return function; 901 return function;
833 } else { 902 } else {
834 UNREACHABLE(); 903 UNREACHABLE();
835 return None(region); 904 return None(region);
836 } 905 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 DisallowHeapAllocation no_allocation; 970 DisallowHeapAllocation no_allocation;
902 if (dim != REPRESENTATION_DIM) { 971 if (dim != REPRESENTATION_DIM) {
903 if (this->IsBitset()) { 972 if (this->IsBitset()) {
904 BitsetType::Print(os, SEMANTIC(this->AsBitset())); 973 BitsetType::Print(os, SEMANTIC(this->AsBitset()));
905 } else if (this->IsClass()) { 974 } else if (this->IsClass()) {
906 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < "; 975 os << "Class(" << static_cast<void*>(*this->AsClass()->Map()) << " < ";
907 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim); 976 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
908 os << ")"; 977 os << ")";
909 } else if (this->IsConstant()) { 978 } else if (this->IsConstant()) {
910 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value()) 979 os << "Constant(" << static_cast<void*>(*this->AsConstant()->Value())
911 << " : "; 980 << ")";
912 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
913 os << ")";
914 } else if (this->IsRange()) { 981 } else if (this->IsRange()) {
915 os << "Range(" << this->AsRange()->Min() 982 os << "Range(" << this->AsRange()->Min()
916 << ".." << this->AsRange()->Max() << " : "; 983 << ", " << this->AsRange()->Max() << ")";
917 BitsetType::New(BitsetType::Lub(this))->PrintTo(os, dim);
918 os << ")";
919 } else if (this->IsContext()) { 984 } else if (this->IsContext()) {
920 os << "Context("; 985 os << "Context(";
921 this->AsContext()->Outer()->PrintTo(os, dim); 986 this->AsContext()->Outer()->PrintTo(os, dim);
922 os << ")"; 987 os << ")";
923 } else if (this->IsUnion()) { 988 } else if (this->IsUnion()) {
924 os << "("; 989 os << "(";
925 UnionHandle unioned = handle(this->AsUnion()); 990 UnionHandle unioned = handle(this->AsUnion());
926 for (int i = 0; i < unioned->Length(); ++i) { 991 for (int i = 0; i < unioned->Length(); ++i) {
927 TypeHandle type_i = unioned->Get(i); 992 TypeHandle type_i = unioned->Get(i);
928 if (i > 0) os << " | "; 993 if (i > 0) os << " | ";
929 type_i->PrintTo(os, dim); 994 type_i->PrintTo(os, dim);
930 } 995 }
931 os << ")"; 996 os << ")";
932 } else if (this->IsArray()) { 997 } else if (this->IsArray()) {
933 os << "Array("; 998 os << "Array(";
934 AsArray()->Element()->PrintTo(os, dim); 999 AsArray()->Element()->PrintTo(os, dim);
935 os << ")"; 1000 os << ")";
936 } else if (this->IsFunction()) { 1001 } else if (this->IsFunction()) {
1002 os << "Function[";
937 if (!this->AsFunction()->Receiver()->IsAny()) { 1003 if (!this->AsFunction()->Receiver()->IsAny()) {
938 this->AsFunction()->Receiver()->PrintTo(os, dim); 1004 this->AsFunction()->Receiver()->PrintTo(os, dim);
939 os << "."; 1005 os << ".";
940 } 1006 }
941 os << "("; 1007 os << "(";
942 for (int i = 0; i < this->AsFunction()->Arity(); ++i) { 1008 for (int i = 0; i < this->AsFunction()->Arity(); ++i) {
943 if (i > 0) os << ", "; 1009 if (i > 0) os << ", ";
944 this->AsFunction()->Parameter(i)->PrintTo(os, dim); 1010 this->AsFunction()->Parameter(i)->PrintTo(os, dim);
945 } 1011 }
946 os << ")->"; 1012 os << ")->";
947 this->AsFunction()->Result()->PrintTo(os, dim); 1013 this->AsFunction()->Result()->PrintTo(os, dim);
1014 os << "]";
948 } else { 1015 } else {
949 UNREACHABLE(); 1016 UNREACHABLE();
950 } 1017 }
951 } 1018 }
952 if (dim == BOTH_DIMS) os << "/"; 1019 if (dim == BOTH_DIMS) os << "/";
953 if (dim != SEMANTIC_DIM) { 1020 if (dim != SEMANTIC_DIM) {
954 BitsetType::Print(os, REPRESENTATION(this->BitsetLub())); 1021 BitsetType::Print(os, REPRESENTATION(this->BitsetLub()));
955 } 1022 }
956 } 1023 }
957 1024
958 1025
959 #ifdef DEBUG 1026 #ifdef DEBUG
960 template <class Config> 1027 template <class Config>
961 void TypeImpl<Config>::Print() { 1028 void TypeImpl<Config>::Print() {
962 OFStream os(stdout); 1029 OFStream os(stdout);
963 PrintTo(os); 1030 PrintTo(os);
964 os << endl; 1031 os << endl;
965 } 1032 }
1033 template <class Config>
1034 void TypeImpl<Config>::BitsetType::Print(int bitset) {
1035 OFStream os(stdout);
1036 Print(os, bitset);
1037 os << endl;
1038 }
966 #endif 1039 #endif
967 1040
968 1041
969 // ----------------------------------------------------------------------------- 1042 // -----------------------------------------------------------------------------
970 // Instantiations. 1043 // Instantiations.
971 1044
972 template class TypeImpl<ZoneTypeConfig>; 1045 template class TypeImpl<ZoneTypeConfig>;
973 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>; 1046 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Map>;
974 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>; 1047 template class TypeImpl<ZoneTypeConfig>::Iterator<i::Object>;
975 1048
976 template class TypeImpl<HeapTypeConfig>; 1049 template class TypeImpl<HeapTypeConfig>;
977 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>; 1050 template class TypeImpl<HeapTypeConfig>::Iterator<i::Map>;
978 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>; 1051 template class TypeImpl<HeapTypeConfig>::Iterator<i::Object>;
979 1052
980 template TypeImpl<ZoneTypeConfig>::TypeHandle 1053 template TypeImpl<ZoneTypeConfig>::TypeHandle
981 TypeImpl<ZoneTypeConfig>::Convert<HeapType>( 1054 TypeImpl<ZoneTypeConfig>::Convert<HeapType>(
982 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*); 1055 TypeImpl<HeapTypeConfig>::TypeHandle, TypeImpl<ZoneTypeConfig>::Region*);
983 template TypeImpl<HeapTypeConfig>::TypeHandle 1056 template TypeImpl<HeapTypeConfig>::TypeHandle
984 TypeImpl<HeapTypeConfig>::Convert<Type>( 1057 TypeImpl<HeapTypeConfig>::Convert<Type>(
985 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*); 1058 TypeImpl<ZoneTypeConfig>::TypeHandle, TypeImpl<HeapTypeConfig>::Region*);
986 1059
987 } } // namespace v8::internal 1060 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698