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

Side by Side Diff: src/compiler/typer.cc

Issue 618803003: Refine expression typing, esp. by propagating range information. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 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
« no previous file with comments | « src/compiler/typer.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/compiler/graph-inl.h" 5 #include "src/compiler/graph-inl.h"
6 #include "src/compiler/js-operator.h" 6 #include "src/compiler/js-operator.h"
7 #include "src/compiler/node.h" 7 #include "src/compiler/node.h"
8 #include "src/compiler/node-properties-inl.h" 8 #include "src/compiler/node-properties-inl.h"
9 #include "src/compiler/node-properties.h" 9 #include "src/compiler/node-properties.h"
10 #include "src/compiler/simplified-operator.h" 10 #include "src/compiler/simplified-operator.h"
11 #include "src/compiler/typer.h" 11 #include "src/compiler/typer.h"
12 12
13 namespace v8 { 13 namespace v8 {
14 namespace internal { 14 namespace internal {
15 namespace compiler { 15 namespace compiler {
16 16
17 Typer::Typer(Zone* zone) : zone_(zone) { 17 Typer::Typer(Zone* zone) : zone_(zone) {
18 Factory* f = zone->isolate()->factory(); 18 Factory* f = zone->isolate()->factory();
19 19
20 Handle<Object> zero = f->NewNumber(0);
21 Handle<Object> one = f->NewNumber(1);
22 Handle<Object> positive_infinity = f->NewNumber(+V8_INFINITY);
23 Handle<Object> negative_infinity = f->NewNumber(-V8_INFINITY);
24
25 negative_signed32 = Type::Union(
26 Type::SignedSmall(), Type::OtherSigned32(), zone);
27 non_negative_signed32 = Type::Union(
28 Type::UnsignedSmall(), Type::OtherUnsigned31(), zone);
29 undefined_or_null = Type::Union(Type::Undefined(), Type::Null(), zone);
30 singleton_false = Type::Constant(f->false_value(), zone);
31 singleton_true = Type::Constant(f->true_value(), zone);
32 singleton_zero = Type::Range(zero, zero, zone);
33 singleton_one = Type::Range(one, one, zone);
34 zero_or_one = Type::Union(singleton_zero, singleton_one, zone);
35 zeroish = Type::Union(
36 singleton_zero, Type::Union(Type::NaN(), Type::MinusZero(), zone), zone);
37 falsish = Type::Union(Type::Undetectable(),
38 Type::Union(zeroish, undefined_or_null, zone), zone);
39 integer = Type::Range(negative_infinity, positive_infinity, zone);
40
20 Type* number = Type::Number(); 41 Type* number = Type::Number();
21 Type* signed32 = Type::Signed32(); 42 Type* signed32 = Type::Signed32();
22 Type* unsigned32 = Type::Unsigned32(); 43 Type* unsigned32 = Type::Unsigned32();
23 Type* integral32 = Type::Integral32(); 44 Type* integral32 = Type::Integral32();
24 Type* object = Type::Object(); 45 Type* object = Type::Object();
25 Type* undefined = Type::Undefined(); 46 Type* undefined = Type::Undefined();
26 Type* weakint = Type::Union( 47 Type* weakint = Type::Union(
27 Type::Range(f->NewNumber(-V8_INFINITY), f->NewNumber(+V8_INFINITY), zone), 48 integer, Type::Union(Type::NaN(), Type::MinusZero(), zone), zone);
28 Type::Union(Type::NaN(), Type::MinusZero(), zone), zone);
29 49
30 number_fun0_ = Type::Function(number, zone); 50 number_fun0_ = Type::Function(number, zone);
31 number_fun1_ = Type::Function(number, number, zone); 51 number_fun1_ = Type::Function(number, number, zone);
32 number_fun2_ = Type::Function(number, number, number, zone); 52 number_fun2_ = Type::Function(number, number, number, zone);
33 weakint_fun1_ = Type::Function(weakint, number, zone); 53 weakint_fun1_ = Type::Function(weakint, number, zone);
34 imul_fun_ = Type::Function(signed32, integral32, integral32, zone); 54 imul_fun_ = Type::Function(signed32, integral32, integral32, zone);
35 random_fun_ = Type::Function(Type::Union( 55 random_fun_ = Type::Function(Type::Union(
36 Type::UnsignedSmall(), Type::OtherNumber(), zone), zone); 56 Type::UnsignedSmall(), Type::OtherNumber(), zone), zone);
37 57
58 Type* int8 = Type::Intersect(
59 Type::Range(f->NewNumber(-0x7F), f->NewNumber(0x7F-1), zone),
60 Type::UntaggedInt8(), zone);
61 Type* int16 = Type::Intersect(
62 Type::Range(f->NewNumber(-0x7FFF), f->NewNumber(0x7FFF-1), zone),
63 Type::UntaggedInt16(), zone);
64 Type* uint8 = Type::Intersect(
65 Type::Range(zero, f->NewNumber(0xFF-1), zone),
66 Type::UntaggedInt8(), zone);
67 Type* uint16 = Type::Intersect(
68 Type::Range(zero, f->NewNumber(0xFFFF-1), zone),
69 Type::UntaggedInt16(), zone);
38 70
39 #define NATIVE_TYPE(sem, rep) \ 71 #define NATIVE_TYPE(sem, rep) \
40 Type::Intersect(Type::sem(zone), Type::rep(zone), zone) 72 Type::Intersect(Type::sem(), Type::rep(), zone)
41 // TODO(rossberg): Use range types for more precision, once we have them.
42 Type* int8 = NATIVE_TYPE(SignedSmall, UntaggedInt8);
43 Type* int16 = NATIVE_TYPE(SignedSmall, UntaggedInt16);
44 Type* int32 = NATIVE_TYPE(Signed32, UntaggedInt32); 73 Type* int32 = NATIVE_TYPE(Signed32, UntaggedInt32);
45 Type* uint8 = NATIVE_TYPE(UnsignedSmall, UntaggedInt8);
46 Type* uint16 = NATIVE_TYPE(UnsignedSmall, UntaggedInt16);
47 Type* uint32 = NATIVE_TYPE(Unsigned32, UntaggedInt32); 74 Type* uint32 = NATIVE_TYPE(Unsigned32, UntaggedInt32);
48 Type* float32 = NATIVE_TYPE(Number, UntaggedFloat32); 75 Type* float32 = NATIVE_TYPE(Number, UntaggedFloat32);
49 Type* float64 = NATIVE_TYPE(Number, UntaggedFloat64); 76 Type* float64 = NATIVE_TYPE(Number, UntaggedFloat64);
50 #undef NATIVE_TYPE 77 #undef NATIVE_TYPE
78
51 Type* buffer = Type::Buffer(zone); 79 Type* buffer = Type::Buffer(zone);
52 Type* int8_array = Type::Array(int8, zone); 80 Type* int8_array = Type::Array(int8, zone);
53 Type* int16_array = Type::Array(int16, zone); 81 Type* int16_array = Type::Array(int16, zone);
54 Type* int32_array = Type::Array(int32, zone); 82 Type* int32_array = Type::Array(int32, zone);
55 Type* uint8_array = Type::Array(uint8, zone); 83 Type* uint8_array = Type::Array(uint8, zone);
56 Type* uint16_array = Type::Array(uint16, zone); 84 Type* uint16_array = Type::Array(uint16, zone);
57 Type* uint32_array = Type::Array(uint32, zone); 85 Type* uint32_array = Type::Array(uint32, zone);
58 Type* float32_array = Type::Array(float32, zone); 86 Type* float32_array = Type::Array(float32, zone);
59 Type* float64_array = Type::Array(float64, zone); 87 Type* float64_array = Type::Array(float64, zone);
60 Type* arg1 = Type::Union(unsigned32, object, zone); 88 Type* arg1 = Type::Union(unsigned32, object, zone);
(...skipping 11 matching lines...) Expand all
72 } 100 }
73 101
74 102
75 class Typer::Visitor : public NullNodeVisitor { 103 class Typer::Visitor : public NullNodeVisitor {
76 public: 104 public:
77 Visitor(Typer* typer, MaybeHandle<Context> context) 105 Visitor(Typer* typer, MaybeHandle<Context> context)
78 : typer_(typer), context_(context) {} 106 : typer_(typer), context_(context) {}
79 107
80 Bounds TypeNode(Node* node) { 108 Bounds TypeNode(Node* node) {
81 switch (node->opcode()) { 109 switch (node->opcode()) {
110 #define DECLARE_CASE(x) \
111 case IrOpcode::k##x: return TypeBinaryOp(node, x##Typer);
112 JS_SIMPLE_BINOP_LIST(DECLARE_CASE)
113 #undef DECLARE_CASE
114
82 #define DECLARE_CASE(x) case IrOpcode::k##x: return Type##x(node); 115 #define DECLARE_CASE(x) case IrOpcode::k##x: return Type##x(node);
83 DECLARE_CASE(Start) 116 DECLARE_CASE(Start)
84 VALUE_OP_LIST(DECLARE_CASE) 117 // VALUE_OP_LIST without JS_SIMPLE_BINOP_LIST:
118 COMMON_OP_LIST(DECLARE_CASE)
119 SIMPLIFIED_OP_LIST(DECLARE_CASE)
120 MACHINE_OP_LIST(DECLARE_CASE)
121 JS_SIMPLE_UNOP_LIST(DECLARE_CASE)
122 JS_OBJECT_OP_LIST(DECLARE_CASE)
123 JS_CONTEXT_OP_LIST(DECLARE_CASE)
124 JS_OTHER_OP_LIST(DECLARE_CASE)
85 #undef DECLARE_CASE 125 #undef DECLARE_CASE
86 126
87 #define DECLARE_CASE(x) case IrOpcode::k##x: 127 #define DECLARE_CASE(x) case IrOpcode::k##x:
88 DECLARE_CASE(End) 128 DECLARE_CASE(End)
89 INNER_CONTROL_OP_LIST(DECLARE_CASE) 129 INNER_CONTROL_OP_LIST(DECLARE_CASE)
90 #undef DECLARE_CASE 130 #undef DECLARE_CASE
91 break; 131 break;
92 } 132 }
93 UNREACHABLE(); 133 UNREACHABLE();
94 return Bounds(); 134 return Bounds();
95 } 135 }
96 136
97 Type* TypeConstant(Handle<Object> value); 137 Type* TypeConstant(Handle<Object> value);
98 138
99 protected: 139 protected:
100 #define DECLARE_METHOD(x) inline Bounds Type##x(Node* node); 140 #define DECLARE_METHOD(x) inline Bounds Type##x(Node* node);
101 DECLARE_METHOD(Start) 141 DECLARE_METHOD(Start)
102 VALUE_OP_LIST(DECLARE_METHOD) 142 VALUE_OP_LIST(DECLARE_METHOD)
103 #undef DECLARE_METHOD 143 #undef DECLARE_METHOD
104 144
105 Bounds OperandType(Node* node, int i) { 145 static Bounds OperandType(Node* node, int i) {
106 return NodeProperties::GetBounds(NodeProperties::GetValueInput(node, i)); 146 return NodeProperties::GetBounds(NodeProperties::GetValueInput(node, i));
107 } 147 }
108 148
109 Type* ContextType(Node* node) { 149 static Type* ContextType(Node* node) {
110 Bounds result = 150 Bounds result =
111 NodeProperties::GetBounds(NodeProperties::GetContextInput(node)); 151 NodeProperties::GetBounds(NodeProperties::GetContextInput(node));
112 DCHECK(result.upper->Maybe(Type::Internal())); 152 DCHECK(result.upper->Maybe(Type::Internal()));
113 // TODO(rossberg): More precisely, instead of the above assertion, we should 153 // TODO(rossberg): More precisely, instead of the above assertion, we should
114 // back-propagate the constraint that it has to be a subtype of Internal. 154 // back-propagate the constraint that it has to be a subtype of Internal.
115 return result.upper; 155 return result.upper;
116 } 156 }
117 157
118 Zone* zone() { return typer_->zone(); } 158 Zone* zone() { return typer_->zone(); }
119 Isolate* isolate() { return typer_->isolate(); } 159 Isolate* isolate() { return typer_->isolate(); }
120 MaybeHandle<Context> context() { return context_; } 160 MaybeHandle<Context> context() { return context_; }
121 161
122 private: 162 private:
123 Typer* typer_; 163 Typer* typer_;
124 MaybeHandle<Context> context_; 164 MaybeHandle<Context> context_;
165
166 typedef Type* (*UnaryTyperFun)(Type*, Typer* t);
167 typedef Type* (*BinaryTyperFun)(Type*, Type*, Typer* t);
168
169 Bounds TypeUnaryOp(Node* node, UnaryTyperFun);
170 Bounds TypeBinaryOp(Node* node, BinaryTyperFun);
171
172 static Type* Invert(Type*, Typer*);
173 static Type* FalsifyUndefined(Type*, Typer*);
174
175 static Type* ToPrimitive(Type*, Typer*);
176 static Type* ToBoolean(Type*, Typer*);
177 static Type* ToNumber(Type*, Typer*);
178 static Type* ToString(Type*, Typer*);
179 static Type* NumberToInt32(Type*, Typer*);
180 static Type* NumberToUint32(Type*, Typer*);
181
182 static Type* JSAddRanger(Type::RangeType*, Type::RangeType*, Typer*);
183 static Type* JSSubtractRanger(Type::RangeType*, Type::RangeType*, Typer*);
184 static Type* JSMultiplyRanger(Type::RangeType*, Type::RangeType*, Typer*);
185 static Type* JSDivideRanger(Type::RangeType*, Type::RangeType*, Typer*);
186
187 static Type* JSCompareTyper(Type*, Type*, Typer*);
188
189 #define DECLARE_METHOD(x) static Type* x##Typer(Type*, Type*, Typer*);
190 JS_SIMPLE_BINOP_LIST(DECLARE_METHOD)
191 #undef DECLARE_METHOD
192
193 static Type* JSUnaryNotTyper(Type*, Typer*);
194 static Type* JSLoadPropertyTyper(Type*, Type*, Typer*);
195 static Type* JSCallFunctionTyper(Type*, Typer*);
125 }; 196 };
126 197
127 198
128 class Typer::RunVisitor : public Typer::Visitor { 199 class Typer::RunVisitor : public Typer::Visitor {
129 public: 200 public:
130 RunVisitor(Typer* typer, MaybeHandle<Context> context) 201 RunVisitor(Typer* typer, MaybeHandle<Context> context)
131 : Visitor(typer, context), 202 : Visitor(typer, context),
132 redo(NodeSet::key_compare(), NodeSet::allocator_type(typer->zone())) {} 203 redo(NodeSet::key_compare(), NodeSet::allocator_type(typer->zone())) {}
133 204
134 GenericGraphVisit::Control Post(Node* node) { 205 GenericGraphVisit::Control Post(Node* node) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 Visitor typing(this, MaybeHandle<Context>()); 302 Visitor typing(this, MaybeHandle<Context>());
232 Bounds bounds = typing.TypeNode(node); 303 Bounds bounds = typing.TypeNode(node);
233 NodeProperties::SetBounds(node, bounds); 304 NodeProperties::SetBounds(node, bounds);
234 } 305 }
235 } 306 }
236 307
237 308
238 // ----------------------------------------------------------------------------- 309 // -----------------------------------------------------------------------------
239 310
240 311
312 Bounds Typer::Visitor::TypeUnaryOp(Node* node, UnaryTyperFun f) {
313 Bounds input = OperandType(node, 0);
314 Type* upper = input.upper->Is(Type::None())
315 ? Type::None()
316 : f(input.upper, typer_);
317 Type* lower = input.lower->Is(Type::None())
318 ? Type::None()
319 : (input.lower == input.upper || upper->IsConstant())
320 ? upper // TODO(neis): Extend this to Range(x,x), NaN, MinusZero, ...?
321 : f(input.lower, typer_);
322 // TODO(neis): Figure out what to do with lower bound.
323 return Bounds(lower, upper);
324 }
325
326
327 Bounds Typer::Visitor::TypeBinaryOp(Node* node, BinaryTyperFun f) {
328 Bounds left = OperandType(node, 0);
329 Bounds right = OperandType(node, 1);
330 Type* upper = left.upper->Is(Type::None()) || right.upper->Is(Type::None())
331 ? Type::None()
332 : f(left.upper, right.upper, typer_);
333 Type* lower = left.lower->Is(Type::None()) || right.lower->Is(Type::None())
334 ? Type::None()
335 : ((left.lower == left.upper && right.lower == right.upper) ||
336 upper->IsConstant())
337 ? upper
338 : f(left.lower, right.lower, typer_);
339 // TODO(neis): Figure out what to do with lower bound.
340 return Bounds(lower, upper);
341 }
342
343
344 Type* Typer::Visitor::Invert(Type* type, Typer* t) {
345 if (type->Is(t->singleton_false)) return t->singleton_true;
346 if (type->Is(t->singleton_true)) return t->singleton_false;
347 return type;
348 }
349
350
351 Type* Typer::Visitor::FalsifyUndefined(Type* type, Typer* t) {
352 if (type->Is(Type::Undefined())) return t->singleton_false;
353 return type;
354 }
355
356
357 // Type conversion.
358
359
360 Type* Typer::Visitor::ToPrimitive(Type* type, Typer* t) {
361 if (type->Is(Type::Primitive()) && !type->Maybe(Type::Receiver())) {
362 return type;
363 }
364 return Type::Primitive();
365 }
366
367
368 Type* Typer::Visitor::ToBoolean(Type* type, Typer* t) {
369 if (type->Is(Type::Boolean())) return type;
370 if (type->Is(t->falsish)) return t->singleton_false;
371 if (type->Is(Type::DetectableReceiver())) return t->singleton_true;
372 if (type->Is(Type::OrderedNumber()) && (type->Max() < 0 || 0 < type->Min())) {
373 return t->singleton_true; // Ruled out nan, -0 and +0.
374 }
375 return Type::Boolean();
376 }
377
378
379 Type* Typer::Visitor::ToNumber(Type* type, Typer* t) {
380 if (type->Is(Type::Number())) return type;
381 if (type->Is(Type::Undefined())) return Type::NaN();
382 if (type->Is(t->singleton_false)) return t->singleton_zero;
383 if (type->Is(t->singleton_true)) return t->singleton_one;
384 if (type->Is(Type::Boolean())) return t->zero_or_one;
385 return Type::Number();
386 }
387
388
389 Type* Typer::Visitor::ToString(Type* type, Typer* t) {
390 if (type->Is(Type::String())) return type;
391 return Type::String();
392 }
393
394
395 Type* Typer::Visitor::NumberToInt32(Type* type, Typer* t) {
396 DCHECK(type->Is(Type::Number()));
397 if (type->Is(Type::Signed32())) return type;
398 if (type->Is(t->zeroish)) return t->singleton_zero;
399 return Type::Signed32();
400 }
401
402
403 Type* Typer::Visitor::NumberToUint32(Type* type, Typer* t) {
404 DCHECK(type->Is(Type::Number()));
405 if (type->Is(Type::Unsigned32())) return type;
406 if (type->Is(t->zeroish)) return t->singleton_zero;
407 return Type::Unsigned32();
408 }
409
410
411 // -----------------------------------------------------------------------------
412
413
241 // Control operators. 414 // Control operators.
242 415
416
243 Bounds Typer::Visitor::TypeStart(Node* node) { 417 Bounds Typer::Visitor::TypeStart(Node* node) {
244 return Bounds(Type::Internal(zone())); 418 return Bounds(Type::Internal());
245 } 419 }
246 420
247 421
248 // Common operators. 422 // Common operators.
249 423
424
250 Bounds Typer::Visitor::TypeParameter(Node* node) { 425 Bounds Typer::Visitor::TypeParameter(Node* node) {
251 return Bounds::Unbounded(zone()); 426 return Bounds::Unbounded(zone());
252 } 427 }
253 428
254 429
255 Bounds Typer::Visitor::TypeInt32Constant(Node* node) { 430 Bounds Typer::Visitor::TypeInt32Constant(Node* node) {
256 // TODO(titzer): only call Type::Of() if the type is not already known. 431 Factory* f = zone()->isolate()->factory();
257 return Bounds(Type::Of(OpParameter<int32_t>(node), zone())); 432 Handle<Object> number = f->NewNumber(OpParameter<int32_t>(node));
433 return Bounds(Type::Intersect(
434 Type::Range(number, number, zone()), Type::UntaggedInt32(), zone()));
258 } 435 }
259 436
260 437
261 Bounds Typer::Visitor::TypeInt64Constant(Node* node) { 438 Bounds Typer::Visitor::TypeInt64Constant(Node* node) {
262 // TODO(titzer): only call Type::Of() if the type is not already known. 439 return Bounds(Type::Internal()); // TODO(rossberg): Add int64 bitset type?
263 return Bounds(
264 Type::Of(static_cast<double>(OpParameter<int64_t>(node)), zone()));
265 } 440 }
266 441
267 442
268 Bounds Typer::Visitor::TypeFloat32Constant(Node* node) { 443 Bounds Typer::Visitor::TypeFloat32Constant(Node* node) {
269 // TODO(titzer): only call Type::Of() if the type is not already known. 444 return Bounds(Type::Intersect(
270 return Bounds(Type::Of(OpParameter<float>(node), zone())); 445 Type::Of(OpParameter<float>(node), zone()),
446 Type::UntaggedFloat32(), zone()));
271 } 447 }
272 448
273 449
274 Bounds Typer::Visitor::TypeFloat64Constant(Node* node) { 450 Bounds Typer::Visitor::TypeFloat64Constant(Node* node) {
275 // TODO(titzer): only call Type::Of() if the type is not already known. 451 return Bounds(Type::Intersect(
276 return Bounds(Type::Of(OpParameter<double>(node), zone())); 452 Type::Of(OpParameter<double>(node), zone()),
453 Type::UntaggedFloat64(), zone()));
277 } 454 }
278 455
279 456
280 Bounds Typer::Visitor::TypeNumberConstant(Node* node) { 457 Bounds Typer::Visitor::TypeNumberConstant(Node* node) {
281 // TODO(titzer): only call Type::Of() if the type is not already known. 458 Factory* f = zone()->isolate()->factory();
282 return Bounds(Type::Of(OpParameter<double>(node), zone())); 459 return Bounds(Type::Constant(
460 f->NewNumber(OpParameter<double>(node)), zone()));
283 } 461 }
284 462
285 463
286 Bounds Typer::Visitor::TypeHeapConstant(Node* node) { 464 Bounds Typer::Visitor::TypeHeapConstant(Node* node) {
287 return Bounds(TypeConstant(OpParameter<Unique<Object> >(node).handle())); 465 return Bounds(TypeConstant(OpParameter<Unique<Object> >(node).handle()));
288 } 466 }
289 467
290 468
291 Bounds Typer::Visitor::TypeExternalConstant(Node* node) { 469 Bounds Typer::Visitor::TypeExternalConstant(Node* node) {
292 return Bounds(Type::Internal(zone())); 470 return Bounds(Type::Internal());
293 } 471 }
294 472
295 473
296 Bounds Typer::Visitor::TypePhi(Node* node) { 474 Bounds Typer::Visitor::TypePhi(Node* node) {
297 int arity = OperatorProperties::GetValueInputCount(node->op()); 475 int arity = OperatorProperties::GetValueInputCount(node->op());
298 Bounds bounds = OperandType(node, 0); 476 Bounds bounds = OperandType(node, 0);
299 for (int i = 1; i < arity; ++i) { 477 for (int i = 1; i < arity; ++i) {
300 bounds = Bounds::Either(bounds, OperandType(node, i), zone()); 478 bounds = Bounds::Either(bounds, OperandType(node, i), zone());
301 } 479 }
302 return bounds; 480 return bounds;
(...skipping 18 matching lines...) Expand all
321 } 499 }
322 500
323 501
324 Bounds Typer::Visitor::TypeFinish(Node* node) { 502 Bounds Typer::Visitor::TypeFinish(Node* node) {
325 return OperandType(node, 0); 503 return OperandType(node, 0);
326 } 504 }
327 505
328 506
329 Bounds Typer::Visitor::TypeFrameState(Node* node) { 507 Bounds Typer::Visitor::TypeFrameState(Node* node) {
330 // TODO(rossberg): Ideally FrameState wouldn't have a value output. 508 // TODO(rossberg): Ideally FrameState wouldn't have a value output.
331 return Bounds(Type::Internal(zone())); 509 return Bounds(Type::Internal());
332 } 510 }
333 511
334 512
335 Bounds Typer::Visitor::TypeStateValues(Node* node) { 513 Bounds Typer::Visitor::TypeStateValues(Node* node) {
336 return Bounds(Type::Internal(zone())); 514 return Bounds(Type::Internal());
337 } 515 }
338 516
339 517
340 Bounds Typer::Visitor::TypeCall(Node* node) { 518 Bounds Typer::Visitor::TypeCall(Node* node) {
341 return Bounds::Unbounded(zone()); 519 return Bounds::Unbounded(zone());
342 } 520 }
343 521
344 522
345 Bounds Typer::Visitor::TypeProjection(Node* node) { 523 Bounds Typer::Visitor::TypeProjection(Node* node) {
346 // TODO(titzer): use the output type of the input to determine the bounds. 524 // TODO(titzer): use the output type of the input to determine the bounds.
347 return Bounds::Unbounded(zone()); 525 return Bounds::Unbounded(zone());
348 } 526 }
349 527
350 528
351 // JS comparison operators. 529 // JS comparison operators.
352 530
353 #define DEFINE_METHOD(x) \ 531
354 Bounds Typer::Visitor::Type##x(Node* node) { \ 532 Type* Typer::Visitor::JSEqualTyper(Type* lhs, Type* rhs, Typer* t) {
355 return Bounds(Type::Boolean(zone())); \ 533 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return t->singleton_false;
356 } 534 if (lhs->Is(t->undefined_or_null) && rhs->Is(t->undefined_or_null)) {
357 JS_COMPARE_BINOP_LIST(DEFINE_METHOD) 535 return t->singleton_true;
358 #undef DEFINE_METHOD 536 }
537 if (lhs->Is(Type::Number()) && rhs->Is(Type::Number()) &&
538 (lhs->Max() < rhs->Min() || lhs->Min() > rhs->Max())) {
539 return t->singleton_false;
540 }
541 if (lhs->IsConstant() && rhs->Is(lhs)) {
542 // Types are equal and are inhabited only by a single semantic value,
543 // which is not nan due to the earlier check.
544 // TODO(neis): Extend this to Range(x,x), MinusZero, ...?
545 return t->singleton_true;
546 }
547 return Type::Boolean();
548 }
549
550
551 Type* Typer::Visitor::JSNotEqualTyper(Type* lhs, Type* rhs, Typer* t) {
552 return Invert(JSEqualTyper(lhs, rhs, t), t);
553 }
554
555
556 static Type* JSType(Type* type) {
557 if (type->Is(Type::Boolean())) return Type::Boolean();
558 if (type->Is(Type::String())) return Type::String();
559 if (type->Is(Type::Number())) return Type::Number();
560 if (type->Is(Type::Undefined())) return Type::Undefined();
561 if (type->Is(Type::Null())) return Type::Null();
562 if (type->Is(Type::Symbol())) return Type::Symbol();
563 if (type->Is(Type::Receiver())) return Type::Receiver(); // JS "Object"
564 return Type::Any();
565 }
566
567
568 Type* Typer::Visitor::JSStrictEqualTyper(Type* lhs, Type* rhs, Typer* t) {
569 if (!JSType(lhs)->Maybe(JSType(rhs))) return t->singleton_false;
570 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return t->singleton_false;
571 if (lhs->Is(Type::Number()) && rhs->Is(Type::Number()) &&
572 (lhs->Max() < rhs->Min() || lhs->Min() > rhs->Max())) {
573 return t->singleton_false;
574 }
575 if (lhs->IsConstant() && rhs->Is(lhs)) {
576 // Types are equal and are inhabited only by a single semantic value,
577 // which is not nan due to the earlier check.
578 return t->singleton_true;
579 }
580 return Type::Boolean();
581 }
582
583
584 Type* Typer::Visitor::JSStrictNotEqualTyper(Type* lhs, Type* rhs, Typer* t) {
585 return Invert(JSStrictEqualTyper(lhs, rhs, t), t);
586 }
587
588
589 // The EcmaScript specification defines the four relational comparison operators
590 // (<, <=, >=, >) with the help of a single abstract one. It behaves like <
591 // but returns undefined when the inputs cannot be compared.
592 // We implement the typing analogously.
593 Type* Typer::Visitor::JSCompareTyper(Type* lhs, Type* rhs, Typer* t) {
594 lhs = ToPrimitive(lhs, t);
595 rhs = ToPrimitive(rhs, t);
596 if (lhs->Maybe(Type::String()) && rhs->Maybe(Type::String())) {
597 return Type::Boolean();
598 }
599 lhs = ToNumber(lhs, t);
600 rhs = ToNumber(rhs, t);
601 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::Undefined();
602 if (lhs->IsConstant() && rhs->Is(lhs)) {
603 // Types are equal and are inhabited only by a single semantic value,
604 // which is not NaN due to the previous check.
605 return t->singleton_false;
606 }
607 if (lhs->Min() >= rhs->Max()) return t->singleton_false;
608 if (lhs->Max() < rhs->Min() &&
609 !lhs->Maybe(Type::NaN()) && !rhs->Maybe(Type::NaN())) {
610 return t->singleton_true;
611 }
612 return Type::Boolean();
613 }
614
615
616 Type* Typer::Visitor::JSLessThanTyper(Type* lhs, Type* rhs, Typer* t) {
617 return FalsifyUndefined(JSCompareTyper(lhs, rhs, t), t);
618 }
619
620
621 Type* Typer::Visitor::JSGreaterThanTyper(Type* lhs, Type* rhs, Typer* t) {
622 return FalsifyUndefined(JSCompareTyper(rhs, lhs, t), t);
623 }
624
625
626 Type* Typer::Visitor::JSLessThanOrEqualTyper(Type* lhs, Type* rhs, Typer* t) {
627 return FalsifyUndefined(Invert(JSCompareTyper(rhs, lhs, t), t), t);
628 }
629
630
631 Type* Typer::Visitor::JSGreaterThanOrEqualTyper(
632 Type* lhs, Type* rhs, Typer* t) {
633 return FalsifyUndefined(Invert(JSCompareTyper(lhs, rhs, t), t), t);
634 }
359 635
360 636
361 // JS bitwise operators. 637 // JS bitwise operators.
362 638
363 Bounds Typer::Visitor::TypeJSBitwiseOr(Node* node) { 639
364 Bounds left = OperandType(node, 0); 640 Type* Typer::Visitor::JSBitwiseOrTyper(Type* lhs, Type* rhs, Typer* t) {
365 Bounds right = OperandType(node, 1); 641 Factory* f = t->zone()->isolate()->factory();
366 Type* upper = Type::Union(left.upper, right.upper, zone()); 642 lhs = NumberToInt32(ToNumber(lhs, t), t);
367 if (!upper->Is(Type::Signed32())) upper = Type::Signed32(zone()); 643 rhs = NumberToInt32(ToNumber(rhs, t), t);
368 Type* lower = Type::Intersect(Type::SignedSmall(zone()), upper, zone()); 644 double lmin = lhs->Min();
369 return Bounds(lower, upper); 645 double rmin = rhs->Min();
370 } 646 double lmax = lhs->Max();
371 647 double rmax = rhs->Max();
372 648 // Or-ing any two values results in a value no smaller than their minimum.
373 Bounds Typer::Visitor::TypeJSBitwiseAnd(Node* node) { 649 // Even no smaller than their maximum if both values are non-negative.
374 Bounds left = OperandType(node, 0); 650 Handle<Object> min = f->NewNumber(
375 Bounds right = OperandType(node, 1); 651 lmin >= 0 && rmin >= 0 ? std::max(lmin, rmin) : std::min(lmin, rmin));
376 Type* upper = Type::Union(left.upper, right.upper, zone()); 652 if (lmax < 0 || rmax < 0) {
377 if (!upper->Is(Type::Signed32())) upper = Type::Signed32(zone()); 653 // Or-ing two values of which at least one is negative results in a negative
378 Type* lower = Type::Intersect(Type::SignedSmall(zone()), upper, zone()); 654 // value.
379 return Bounds(lower, upper); 655 Handle<Object> max = f->NewNumber(-1);
380 } 656 return Type::Range(min, max, t->zone());
381 657 }
382 658 Handle<Object> max = f->NewNumber(Type::Signed32()->Max());
383 Bounds Typer::Visitor::TypeJSBitwiseXor(Node* node) { 659 return Type::Range(min, max, t->zone());
384 return Bounds(Type::SignedSmall(zone()), Type::Signed32(zone())); 660 // TODO(neis): Be precise for singleton inputs, here and elsewhere.
385 } 661 }
386 662
387 663
388 Bounds Typer::Visitor::TypeJSShiftLeft(Node* node) { 664 Type* Typer::Visitor::JSBitwiseAndTyper(Type* lhs, Type* rhs, Typer* t) {
389 return Bounds(Type::SignedSmall(zone()), Type::Signed32(zone())); 665 Factory* f = t->zone()->isolate()->factory();
390 } 666 lhs = NumberToInt32(ToNumber(lhs, t), t);
391 667 rhs = NumberToInt32(ToNumber(rhs, t), t);
392 668 double lmin = lhs->Min();
393 Bounds Typer::Visitor::TypeJSShiftRight(Node* node) { 669 double rmin = rhs->Min();
394 return Bounds(Type::SignedSmall(zone()), Type::Signed32(zone())); 670 double lmax = lhs->Max();
395 } 671 double rmax = rhs->Max();
396 672 // And-ing any two values results in a value no larger than their maximum.
397 673 // Even no larger than their minimum if both values are non-negative.
398 Bounds Typer::Visitor::TypeJSShiftRightLogical(Node* node) { 674 Handle<Object> max = f->NewNumber(
399 return Bounds(Type::UnsignedSmall(zone()), Type::Unsigned32(zone())); 675 lmin >= 0 && rmin >= 0 ? std::min(lmax, rmax) : std::max(lmax, rmax));
676 if (lmin >= 0 || rmin >= 0) {
677 // And-ing two values of which at least one is non-negative results in a
678 // non-negative value.
679 Handle<Object> min = f->NewNumber(0);
680 return Type::Range(min, max, t->zone());
681 }
682 Handle<Object> min = f->NewNumber(Type::Signed32()->Min());
683 return Type::Range(min, max, t->zone());
684 }
685
686
687 Type* Typer::Visitor::JSBitwiseXorTyper(Type* lhs, Type* rhs, Typer* t) {
688 lhs = NumberToInt32(ToNumber(lhs, t), t);
689 rhs = NumberToInt32(ToNumber(rhs, t), t);
690 double lmin = lhs->Min();
691 double rmin = rhs->Min();
692 double lmax = lhs->Max();
693 double rmax = rhs->Max();
694 if ((lmin >= 0 && rmin >= 0) || (lmax < 0 && rmax < 0)) {
695 // Xor-ing negative or non-negative values results in a non-negative value.
696 return t->non_negative_signed32;
697 }
698 if ((lmax < 0 && rmin >= 0) || (lmin >= 0 && rmax < 0)) {
699 // Xor-ing a negative and a non-negative value results in a negative value.
700 return t->negative_signed32;
701 }
702 return Type::Signed32();
703 }
704
705
706 Type* Typer::Visitor::JSShiftLeftTyper(Type* lhs, Type* rhs, Typer* t) {
707 return Type::Signed32();
708 }
709
710
711 Type* Typer::Visitor::JSShiftRightTyper(Type* lhs, Type* rhs, Typer* t) {
712 lhs = NumberToInt32(ToNumber(lhs, t), t);
713 Factory* f = t->zone()->isolate()->factory();
714 if (lhs->Min() >= 0) {
715 // Right-shifting a non-negative value cannot make it negative, nor larger.
716 Handle<Object> min = f->NewNumber(0);
717 Handle<Object> max = f->NewNumber(lhs->Max());
718 return Type::Range(min, max, t->zone());
719 }
720 if (lhs->Max() < 0) {
721 // Right-shifting a negative value cannot make it non-negative, nor smaller.
722 Handle<Object> min = f->NewNumber(lhs->Min());
723 Handle<Object> max = f->NewNumber(-1);
724 return Type::Range(min, max, t->zone());
725 }
726 return Type::Signed32();
727 }
728
729
730 Type* Typer::Visitor::JSShiftRightLogicalTyper(Type* lhs, Type* rhs, Typer* t) {
731 lhs = NumberToUint32(ToNumber(lhs, t), t);
732 Factory* f = t->zone()->isolate()->factory();
733 // Logical right-shifting any value cannot make it larger.
734 Handle<Object> min = f->NewNumber(0);
735 Handle<Object> max = f->NewNumber(lhs->Max());
736 return Type::Range(min, max, t->zone());
400 } 737 }
401 738
402 739
403 // JS arithmetic operators. 740 // JS arithmetic operators.
404 741
405 Bounds Typer::Visitor::TypeJSAdd(Node* node) { 742
406 Bounds left = OperandType(node, 0); 743 Type* Typer::Visitor::JSAddTyper(Type* lhs, Type* rhs, Typer* t) {
407 Bounds right = OperandType(node, 1); 744 lhs = ToPrimitive(lhs, t);
408 Type* lower = 745 rhs = ToPrimitive(rhs, t);
409 left.lower->Is(Type::None()) || right.lower->Is(Type::None()) ? 746 if (lhs->Maybe(Type::String()) || rhs->Maybe(Type::String())) {
410 Type::None(zone()) : 747 if (lhs->Is(Type::String()) || rhs->Is(Type::String())) {
411 left.lower->Is(Type::Number()) && right.lower->Is(Type::Number()) ? 748 return Type::String();
412 Type::SignedSmall(zone()) : 749 } else {
413 left.lower->Is(Type::String()) || right.lower->Is(Type::String()) ? 750 return Type::NumberOrString();
414 Type::String(zone()) : Type::None(zone()); 751 }
415 Type* upper = 752 }
416 left.upper->Is(Type::None()) && right.upper->Is(Type::None()) ? 753 lhs = ToNumber(lhs, t);
417 Type::None(zone()) : 754 rhs = ToNumber(rhs, t);
418 left.upper->Is(Type::Number()) && right.upper->Is(Type::Number()) ? 755 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
419 Type::Number(zone()) : 756 // TODO(neis): Do some analysis.
420 left.upper->Is(Type::String()) || right.upper->Is(Type::String()) ? 757 // TODO(neis): Deal with numeric bitsets here and elsewhere.
421 Type::String(zone()) : Type::NumberOrString(zone()); 758 return Type::Number();
422 return Bounds(lower, upper); 759 }
423 } 760
424 761
425 762 Type* Typer::Visitor::JSSubtractTyper(Type* lhs, Type* rhs, Typer* t) {
426 Bounds Typer::Visitor::TypeJSSubtract(Node* node) { 763 lhs = ToNumber(lhs, t);
427 return Bounds(Type::SignedSmall(zone()), Type::Number(zone())); 764 rhs = ToNumber(rhs, t);
428 } 765 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
429 766 // TODO(neis): Do some analysis.
430 767 return Type::Number();
431 Bounds Typer::Visitor::TypeJSMultiply(Node* node) { 768 }
432 return Bounds(Type::SignedSmall(zone()), Type::Number(zone())); 769
433 } 770
434 771 Type* Typer::Visitor::JSMultiplyTyper(Type* lhs, Type* rhs, Typer* t) {
435 772 lhs = ToNumber(lhs, t);
436 Bounds Typer::Visitor::TypeJSDivide(Node* node) { 773 rhs = ToNumber(rhs, t);
437 return Bounds(Type::SignedSmall(zone()), Type::Number(zone())); 774 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
438 } 775 // TODO(neis): Do some analysis.
439 776 return Type::Number();
440 777 }
441 Bounds Typer::Visitor::TypeJSModulus(Node* node) { 778
442 return Bounds(Type::SignedSmall(zone()), Type::Number(zone())); 779
780 Type* Typer::Visitor::JSDivideTyper(Type* lhs, Type* rhs, Typer* t) {
781 lhs = ToNumber(lhs, t);
782 rhs = ToNumber(rhs, t);
783 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
784 // TODO(neis): Do some analysis.
785 return Type::Number();
786 }
787
788
789 Type* Typer::Visitor::JSModulusTyper(Type* lhs, Type* rhs, Typer* t) {
790 lhs = ToNumber(lhs, t);
791 rhs = ToNumber(rhs, t);
792 if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
793 // TODO(neis): Do some analysis.
794 return Type::Number();
443 } 795 }
444 796
445 797
446 // JS unary operators. 798 // JS unary operators.
447 799
800
801 Type* Typer::Visitor::JSUnaryNotTyper(Type* type, Typer* t) {
802 return Invert(ToBoolean(type, t), t);
803 }
804
805
448 Bounds Typer::Visitor::TypeJSUnaryNot(Node* node) { 806 Bounds Typer::Visitor::TypeJSUnaryNot(Node* node) {
449 return Bounds(Type::Boolean(zone())); 807 return TypeUnaryOp(node, JSUnaryNotTyper);
450 } 808 }
451 809
452 810
453 Bounds Typer::Visitor::TypeJSTypeOf(Node* node) { 811 Bounds Typer::Visitor::TypeJSTypeOf(Node* node) {
454 return Bounds(Type::InternalizedString(zone())); 812 return Bounds(Type::InternalizedString());
455 } 813 }
456 814
457 815
458 // JS conversion operators. 816 // JS conversion operators.
459 817
818
460 Bounds Typer::Visitor::TypeJSToBoolean(Node* node) { 819 Bounds Typer::Visitor::TypeJSToBoolean(Node* node) {
461 return Bounds(Type::Boolean(zone())); 820 return TypeUnaryOp(node, ToBoolean);
462 } 821 }
463 822
464 823
465 Bounds Typer::Visitor::TypeJSToNumber(Node* node) { 824 Bounds Typer::Visitor::TypeJSToNumber(Node* node) {
466 return Bounds(Type::SignedSmall(zone()), Type::Number(zone())); 825 return TypeUnaryOp(node, ToNumber);
467 } 826 }
468 827
469 828
470 Bounds Typer::Visitor::TypeJSToString(Node* node) { 829 Bounds Typer::Visitor::TypeJSToString(Node* node) {
471 return Bounds(Type::None(zone()), Type::String(zone())); 830 return TypeUnaryOp(node, ToString);
472 } 831 }
473 832
474 833
475 Bounds Typer::Visitor::TypeJSToName(Node* node) { 834 Bounds Typer::Visitor::TypeJSToName(Node* node) {
476 return Bounds(Type::None(zone()), Type::Name(zone())); 835 return Bounds(Type::None(), Type::Name());
477 } 836 }
478 837
479 838
480 Bounds Typer::Visitor::TypeJSToObject(Node* node) { 839 Bounds Typer::Visitor::TypeJSToObject(Node* node) {
481 return Bounds(Type::None(zone()), Type::Receiver(zone())); 840 return Bounds(Type::None(), Type::Receiver());
482 } 841 }
483 842
484 843
485 // JS object operators. 844 // JS object operators.
486 845
846
487 Bounds Typer::Visitor::TypeJSCreate(Node* node) { 847 Bounds Typer::Visitor::TypeJSCreate(Node* node) {
488 return Bounds(Type::None(zone()), Type::Object(zone())); 848 return Bounds(Type::None(), Type::Object());
849 }
850
851
852 Type* Typer::Visitor::JSLoadPropertyTyper(Type* object, Type* name, Typer* t) {
853 // TODO(rossberg): Use range types and sized array types to filter undefined.
854 if (object->IsArray() && name->Is(Type::Integral32())) {
855 return Type::Union(
856 object->AsArray()->Element(), Type::Undefined(), t->zone());
857 }
858 return Type::Any();
489 } 859 }
490 860
491 861
492 Bounds Typer::Visitor::TypeJSLoadProperty(Node* node) { 862 Bounds Typer::Visitor::TypeJSLoadProperty(Node* node) {
493 Bounds object = OperandType(node, 0); 863 return TypeBinaryOp(node, JSLoadPropertyTyper);
494 Bounds name = OperandType(node, 1); 864 }
495 Bounds result = Bounds::Unbounded(zone()); 865
496 // TODO(rossberg): Use range types and sized array types to filter undefined. 866
497 if (object.lower->IsArray() && name.lower->Is(Type::Integral32())) {
498 result.lower = Type::Union(
499 object.lower->AsArray()->Element(), Type::Undefined(zone()), zone());
500 }
501 if (object.upper->IsArray() && name.upper->Is(Type::Integral32())) {
502 result.upper = Type::Union(
503 object.upper->AsArray()->Element(), Type::Undefined(zone()), zone());
504 }
505 return result;
506 }
507
508
509 Bounds Typer::Visitor::TypeJSLoadNamed(Node* node) { 867 Bounds Typer::Visitor::TypeJSLoadNamed(Node* node) {
510 return Bounds::Unbounded(zone()); 868 return Bounds::Unbounded(zone());
511 } 869 }
512 870
513 871
514 Bounds Typer::Visitor::TypeJSStoreProperty(Node* node) { 872 Bounds Typer::Visitor::TypeJSStoreProperty(Node* node) {
515 UNREACHABLE(); 873 UNREACHABLE();
516 return Bounds(); 874 return Bounds();
517 } 875 }
518 876
519 877
520 Bounds Typer::Visitor::TypeJSStoreNamed(Node* node) { 878 Bounds Typer::Visitor::TypeJSStoreNamed(Node* node) {
521 UNREACHABLE(); 879 UNREACHABLE();
522 return Bounds(); 880 return Bounds();
523 } 881 }
524 882
525 883
526 Bounds Typer::Visitor::TypeJSDeleteProperty(Node* node) { 884 Bounds Typer::Visitor::TypeJSDeleteProperty(Node* node) {
527 return Bounds(Type::Boolean(zone())); 885 return Bounds(Type::Boolean());
528 } 886 }
529 887
530 888
531 Bounds Typer::Visitor::TypeJSHasProperty(Node* node) { 889 Bounds Typer::Visitor::TypeJSHasProperty(Node* node) {
532 return Bounds(Type::Boolean(zone())); 890 return Bounds(Type::Boolean());
533 } 891 }
534 892
535 893
536 Bounds Typer::Visitor::TypeJSInstanceOf(Node* node) { 894 Bounds Typer::Visitor::TypeJSInstanceOf(Node* node) {
537 return Bounds(Type::Boolean(zone())); 895 return Bounds(Type::Boolean());
538 } 896 }
539 897
540 898
541 // JS context operators. 899 // JS context operators.
542 900
901
543 Bounds Typer::Visitor::TypeJSLoadContext(Node* node) { 902 Bounds Typer::Visitor::TypeJSLoadContext(Node* node) {
544 Bounds outer = OperandType(node, 0); 903 Bounds outer = OperandType(node, 0);
545 DCHECK(outer.upper->Maybe(Type::Internal())); 904 DCHECK(outer.upper->Maybe(Type::Internal()));
546 // TODO(rossberg): More precisely, instead of the above assertion, we should 905 // TODO(rossberg): More precisely, instead of the above assertion, we should
547 // back-propagate the constraint that it has to be a subtype of Internal. 906 // back-propagate the constraint that it has to be a subtype of Internal.
548 907
549 ContextAccess access = OpParameter<ContextAccess>(node); 908 ContextAccess access = OpParameter<ContextAccess>(node);
550 Type* context_type = outer.upper; 909 Type* context_type = outer.upper;
551 MaybeHandle<Context> context; 910 MaybeHandle<Context> context;
552 if (context_type->IsConstant()) { 911 if (context_type->IsConstant()) {
(...skipping 14 matching lines...) Expand all
567 context = handle(context.ToHandleChecked()->previous(), isolate()); 926 context = handle(context.ToHandleChecked()->previous(), isolate());
568 } 927 }
569 } 928 }
570 if (context.is_null()) { 929 if (context.is_null()) {
571 return Bounds::Unbounded(zone()); 930 return Bounds::Unbounded(zone());
572 } else { 931 } else {
573 Handle<Object> value = 932 Handle<Object> value =
574 handle(context.ToHandleChecked()->get(static_cast<int>(access.index())), 933 handle(context.ToHandleChecked()->get(static_cast<int>(access.index())),
575 isolate()); 934 isolate());
576 Type* lower = TypeConstant(value); 935 Type* lower = TypeConstant(value);
577 return Bounds(lower, Type::Any(zone())); 936 return Bounds(lower, Type::Any());
578 } 937 }
579 } 938 }
580 939
581 940
582 Bounds Typer::Visitor::TypeJSStoreContext(Node* node) { 941 Bounds Typer::Visitor::TypeJSStoreContext(Node* node) {
583 UNREACHABLE(); 942 UNREACHABLE();
584 return Bounds(); 943 return Bounds();
585 } 944 }
586 945
587 946
(...skipping 29 matching lines...) Expand all
617 976
618 977
619 Bounds Typer::Visitor::TypeJSCreateGlobalContext(Node* node) { 978 Bounds Typer::Visitor::TypeJSCreateGlobalContext(Node* node) {
620 Type* outer = ContextType(node); 979 Type* outer = ContextType(node);
621 return Bounds(Type::Context(outer, zone())); 980 return Bounds(Type::Context(outer, zone()));
622 } 981 }
623 982
624 983
625 // JS other operators. 984 // JS other operators.
626 985
986
627 Bounds Typer::Visitor::TypeJSYield(Node* node) { 987 Bounds Typer::Visitor::TypeJSYield(Node* node) {
628 return Bounds::Unbounded(zone()); 988 return Bounds::Unbounded(zone());
629 } 989 }
630 990
631 991
632 Bounds Typer::Visitor::TypeJSCallConstruct(Node* node) { 992 Bounds Typer::Visitor::TypeJSCallConstruct(Node* node) {
633 return Bounds(Type::None(zone()), Type::Receiver(zone())); 993 return Bounds(Type::None(), Type::Receiver());
994 }
995
996
997 Type* Typer::Visitor::JSCallFunctionTyper(Type* fun, Typer* t) {
998 return fun->IsFunction() ? fun->AsFunction()->Result() : Type::Any();
634 } 999 }
635 1000
636 1001
637 Bounds Typer::Visitor::TypeJSCallFunction(Node* node) { 1002 Bounds Typer::Visitor::TypeJSCallFunction(Node* node) {
638 Bounds fun = OperandType(node, 0); 1003 return TypeUnaryOp(node, JSCallFunctionTyper); // We ignore argument types.
639 Type* lower = fun.lower->IsFunction()
640 ? fun.lower->AsFunction()->Result() : Type::None(zone());
641 Type* upper = fun.upper->IsFunction()
642 ? fun.upper->AsFunction()->Result() : Type::Any(zone());
643 return Bounds(lower, upper);
644 } 1004 }
645 1005
646 1006
647 Bounds Typer::Visitor::TypeJSCallRuntime(Node* node) { 1007 Bounds Typer::Visitor::TypeJSCallRuntime(Node* node) {
648 return Bounds::Unbounded(zone()); 1008 return Bounds::Unbounded(zone());
649 } 1009 }
650 1010
651 1011
652 Bounds Typer::Visitor::TypeJSDebugger(Node* node) { 1012 Bounds Typer::Visitor::TypeJSDebugger(Node* node) {
653 return Bounds::Unbounded(zone()); 1013 return Bounds::Unbounded(zone());
654 } 1014 }
655 1015
656 1016
657 // Simplified operators. 1017 // Simplified operators.
658 1018
1019
659 Bounds Typer::Visitor::TypeBooleanNot(Node* node) { 1020 Bounds Typer::Visitor::TypeBooleanNot(Node* node) {
660 return Bounds(Type::Boolean(zone())); 1021 return Bounds(Type::Boolean());
661 } 1022 }
662 1023
663 1024
664 Bounds Typer::Visitor::TypeBooleanToNumber(Node* node) { 1025 Bounds Typer::Visitor::TypeBooleanToNumber(Node* node) {
665 return Bounds(Type::Number(zone())); 1026 return Bounds(Type::Number());
666 } 1027 }
667 1028
668 1029
669 Bounds Typer::Visitor::TypeNumberEqual(Node* node) { 1030 Bounds Typer::Visitor::TypeNumberEqual(Node* node) {
670 return Bounds(Type::Boolean(zone())); 1031 return Bounds(Type::Boolean());
671 } 1032 }
672 1033
673 1034
674 Bounds Typer::Visitor::TypeNumberLessThan(Node* node) { 1035 Bounds Typer::Visitor::TypeNumberLessThan(Node* node) {
675 return Bounds(Type::Boolean(zone())); 1036 return Bounds(Type::Boolean());
676 } 1037 }
677 1038
678 1039
679 Bounds Typer::Visitor::TypeNumberLessThanOrEqual(Node* node) { 1040 Bounds Typer::Visitor::TypeNumberLessThanOrEqual(Node* node) {
680 return Bounds(Type::Boolean(zone())); 1041 return Bounds(Type::Boolean());
681 } 1042 }
682 1043
683 1044
684 Bounds Typer::Visitor::TypeNumberAdd(Node* node) { 1045 Bounds Typer::Visitor::TypeNumberAdd(Node* node) {
685 return Bounds(Type::Number(zone())); 1046 return Bounds(Type::Number());
686 } 1047 }
687 1048
688 1049
689 Bounds Typer::Visitor::TypeNumberSubtract(Node* node) { 1050 Bounds Typer::Visitor::TypeNumberSubtract(Node* node) {
690 return Bounds(Type::Number(zone())); 1051 return Bounds(Type::Number());
691 } 1052 }
692 1053
693 1054
694 Bounds Typer::Visitor::TypeNumberMultiply(Node* node) { 1055 Bounds Typer::Visitor::TypeNumberMultiply(Node* node) {
695 return Bounds(Type::Number(zone())); 1056 return Bounds(Type::Number());
696 } 1057 }
697 1058
698 1059
699 Bounds Typer::Visitor::TypeNumberDivide(Node* node) { 1060 Bounds Typer::Visitor::TypeNumberDivide(Node* node) {
700 return Bounds(Type::Number(zone())); 1061 return Bounds(Type::Number());
701 } 1062 }
702 1063
703 1064
704 Bounds Typer::Visitor::TypeNumberModulus(Node* node) { 1065 Bounds Typer::Visitor::TypeNumberModulus(Node* node) {
705 return Bounds(Type::Number(zone())); 1066 return Bounds(Type::Number());
706 } 1067 }
707 1068
708 1069
709 Bounds Typer::Visitor::TypeNumberToInt32(Node* node) { 1070 Bounds Typer::Visitor::TypeNumberToInt32(Node* node) {
710 Bounds arg = OperandType(node, 0); 1071 return TypeUnaryOp(node, NumberToInt32);
711 Type* s32 = Type::Signed32(zone());
712 Type* lower = arg.lower->Is(s32) ? arg.lower : s32;
713 Type* upper = arg.upper->Is(s32) ? arg.upper : s32;
714 return Bounds(lower, upper);
715 } 1072 }
716 1073
717 1074
718 Bounds Typer::Visitor::TypeNumberToUint32(Node* node) { 1075 Bounds Typer::Visitor::TypeNumberToUint32(Node* node) {
719 Bounds arg = OperandType(node, 0); 1076 return TypeUnaryOp(node, NumberToUint32);
720 Type* u32 = Type::Unsigned32(zone());
721 Type* lower = arg.lower->Is(u32) ? arg.lower : u32;
722 Type* upper = arg.upper->Is(u32) ? arg.upper : u32;
723 return Bounds(lower, upper);
724 } 1077 }
725 1078
726 1079
727 Bounds Typer::Visitor::TypeReferenceEqual(Node* node) { 1080 Bounds Typer::Visitor::TypeReferenceEqual(Node* node) {
728 return Bounds(Type::Boolean(zone())); 1081 return Bounds(Type::Boolean());
729 } 1082 }
730 1083
731 1084
732 Bounds Typer::Visitor::TypeStringEqual(Node* node) { 1085 Bounds Typer::Visitor::TypeStringEqual(Node* node) {
733 return Bounds(Type::Boolean(zone())); 1086 return Bounds(Type::Boolean());
734 } 1087 }
735 1088
736 1089
737 Bounds Typer::Visitor::TypeStringLessThan(Node* node) { 1090 Bounds Typer::Visitor::TypeStringLessThan(Node* node) {
738 return Bounds(Type::Boolean(zone())); 1091 return Bounds(Type::Boolean());
739 } 1092 }
740 1093
741 1094
742 Bounds Typer::Visitor::TypeStringLessThanOrEqual(Node* node) { 1095 Bounds Typer::Visitor::TypeStringLessThanOrEqual(Node* node) {
743 return Bounds(Type::Boolean(zone())); 1096 return Bounds(Type::Boolean());
744 } 1097 }
745 1098
746 1099
747 Bounds Typer::Visitor::TypeStringAdd(Node* node) { 1100 Bounds Typer::Visitor::TypeStringAdd(Node* node) {
748 return Bounds(Type::String(zone())); 1101 return Bounds(Type::String());
1102 }
1103
1104
1105 static Type* ChangeRepresentation(Type* type, Type* rep, Zone* zone) {
1106 // TODO(neis): Enable when expressible.
1107 /*
1108 return Type::Union(
1109 Type::Intersect(type, Type::Semantic(), zone),
1110 Type::Intersect(rep, Type::Representation(), zone), zone);
1111 */
1112 return type;
749 } 1113 }
750 1114
751 1115
752 Bounds Typer::Visitor::TypeChangeTaggedToInt32(Node* node) { 1116 Bounds Typer::Visitor::TypeChangeTaggedToInt32(Node* node) {
753 // TODO(titzer): type is type of input, representation is Word32. 1117 Bounds arg = OperandType(node, 0);
754 return Bounds(Type::Integral32()); 1118 DCHECK(arg.upper->Is(Type::Signed32()));
1119 return Bounds(
1120 ChangeRepresentation(arg.lower, Type::UntaggedInt32(), zone()),
1121 ChangeRepresentation(arg.upper, Type::UntaggedInt32(), zone()));
755 } 1122 }
756 1123
757 1124
758 Bounds Typer::Visitor::TypeChangeTaggedToUint32(Node* node) { 1125 Bounds Typer::Visitor::TypeChangeTaggedToUint32(Node* node) {
759 return Bounds(Type::Integral32()); // TODO(titzer): add appropriate rep 1126 Bounds arg = OperandType(node, 0);
1127 DCHECK(arg.upper->Is(Type::Unsigned32()));
1128 return Bounds(
1129 ChangeRepresentation(arg.lower, Type::UntaggedInt32(), zone()),
1130 ChangeRepresentation(arg.upper, Type::UntaggedInt32(), zone()));
760 } 1131 }
761 1132
762 1133
763 Bounds Typer::Visitor::TypeChangeTaggedToFloat64(Node* node) { 1134 Bounds Typer::Visitor::TypeChangeTaggedToFloat64(Node* node) {
764 // TODO(titzer): type is type of input, representation is Float64. 1135 Bounds arg = OperandType(node, 0);
765 return Bounds(Type::Number()); 1136 DCHECK(arg.upper->Is(Type::Number()));
1137 return Bounds(
1138 ChangeRepresentation(arg.lower, Type::UntaggedFloat64(), zone()),
1139 ChangeRepresentation(arg.upper, Type::UntaggedFloat64(), zone()));
766 } 1140 }
767 1141
768 1142
769 Bounds Typer::Visitor::TypeChangeInt32ToTagged(Node* node) { 1143 Bounds Typer::Visitor::TypeChangeInt32ToTagged(Node* node) {
770 // TODO(titzer): type is type of input, representation is Tagged. 1144 Bounds arg = OperandType(node, 0);
771 return Bounds(Type::Integral32()); 1145 DCHECK(arg.upper->Is(Type::Signed32()));
1146 return Bounds(
1147 ChangeRepresentation(arg.lower, Type::Tagged(), zone()),
1148 ChangeRepresentation(arg.upper, Type::Tagged(), zone()));
772 } 1149 }
773 1150
774 1151
775 Bounds Typer::Visitor::TypeChangeUint32ToTagged(Node* node) { 1152 Bounds Typer::Visitor::TypeChangeUint32ToTagged(Node* node) {
776 // TODO(titzer): type is type of input, representation is Tagged. 1153 Bounds arg = OperandType(node, 0);
777 return Bounds(Type::Unsigned32()); 1154 DCHECK(arg.upper->Is(Type::Unsigned32()));
1155 return Bounds(
1156 ChangeRepresentation(arg.lower, Type::Tagged(), zone()),
1157 ChangeRepresentation(arg.upper, Type::Tagged(), zone()));
778 } 1158 }
779 1159
780 1160
781 Bounds Typer::Visitor::TypeChangeFloat64ToTagged(Node* node) { 1161 Bounds Typer::Visitor::TypeChangeFloat64ToTagged(Node* node) {
782 // TODO(titzer): type is type of input, representation is Tagged. 1162 Bounds arg = OperandType(node, 0);
783 return Bounds(Type::Number()); 1163 // CHECK(arg.upper->Is(Type::Number()));
1164 // TODO(neis): This check currently fails due to inconsistent typing.
1165 return Bounds(
1166 ChangeRepresentation(arg.lower, Type::Tagged(), zone()),
1167 ChangeRepresentation(arg.upper, Type::Tagged(), zone()));
784 } 1168 }
785 1169
786 1170
787 Bounds Typer::Visitor::TypeChangeBoolToBit(Node* node) { 1171 Bounds Typer::Visitor::TypeChangeBoolToBit(Node* node) {
788 // TODO(titzer): type is type of input, representation is Bit. 1172 Bounds arg = OperandType(node, 0);
789 return Bounds(Type::Boolean()); 1173 DCHECK(arg.upper->Is(Type::Boolean()));
1174 return Bounds(
1175 ChangeRepresentation(arg.lower, Type::UntaggedInt1(), zone()),
1176 ChangeRepresentation(arg.upper, Type::UntaggedInt1(), zone()));
790 } 1177 }
791 1178
792 1179
793 Bounds Typer::Visitor::TypeChangeBitToBool(Node* node) { 1180 Bounds Typer::Visitor::TypeChangeBitToBool(Node* node) {
794 // TODO(titzer): type is type of input, representation is Tagged. 1181 Bounds arg = OperandType(node, 0);
795 return Bounds(Type::Boolean()); 1182 DCHECK(arg.upper->Is(Type::Boolean()));
1183 return Bounds(
1184 ChangeRepresentation(arg.lower, Type::TaggedPtr(), zone()),
1185 ChangeRepresentation(arg.upper, Type::TaggedPtr(), zone()));
796 } 1186 }
797 1187
798 1188
799 Bounds Typer::Visitor::TypeLoadField(Node* node) { 1189 Bounds Typer::Visitor::TypeLoadField(Node* node) {
800 return Bounds(FieldAccessOf(node->op()).type); 1190 return Bounds(FieldAccessOf(node->op()).type);
801 } 1191 }
802 1192
803 1193
804 Bounds Typer::Visitor::TypeLoadElement(Node* node) { 1194 Bounds Typer::Visitor::TypeLoadElement(Node* node) {
805 return Bounds(ElementAccessOf(node->op()).type); 1195 return Bounds(ElementAccessOf(node->op()).type);
806 } 1196 }
807 1197
808 1198
809 Bounds Typer::Visitor::TypeStoreField(Node* node) { 1199 Bounds Typer::Visitor::TypeStoreField(Node* node) {
810 UNREACHABLE(); 1200 UNREACHABLE();
811 return Bounds(); 1201 return Bounds();
812 } 1202 }
813 1203
814 1204
815 Bounds Typer::Visitor::TypeStoreElement(Node* node) { 1205 Bounds Typer::Visitor::TypeStoreElement(Node* node) {
816 UNREACHABLE(); 1206 UNREACHABLE();
817 return Bounds(); 1207 return Bounds();
818 } 1208 }
819 1209
820 1210
821 // Machine operators. 1211 // Machine operators.
822 1212
1213
823 // TODO(rossberg): implement 1214 // TODO(rossberg): implement
824 #define DEFINE_METHOD(x) \ 1215 #define DEFINE_METHOD(x) \
825 Bounds Typer::Visitor::Type##x(Node* node) { return Bounds(Type::None()); } 1216 Bounds Typer::Visitor::Type##x(Node* node) { \
1217 return Bounds::Unbounded(zone()); \
1218 }
826 MACHINE_OP_LIST(DEFINE_METHOD) 1219 MACHINE_OP_LIST(DEFINE_METHOD)
827 #undef DEFINE_METHOD 1220 #undef DEFINE_METHOD
828 1221
829 1222
830 // Heap constants. 1223 // Heap constants.
831 1224
832 Type* Typer::Visitor::TypeConstant(Handle<Object> value) { 1225 Type* Typer::Visitor::TypeConstant(Handle<Object> value) {
833 if (value->IsJSFunction() && JSFunction::cast(*value)->IsBuiltin() && 1226 if (value->IsJSFunction() && JSFunction::cast(*value)->IsBuiltin() &&
834 !context().is_null()) { 1227 !context().is_null()) {
835 Handle<Context> native = 1228 Handle<Context> native =
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 } 1299 }
907 1300
908 1301
909 void Typer::DecorateGraph(Graph* graph) { 1302 void Typer::DecorateGraph(Graph* graph) {
910 graph->AddDecorator(new (zone()) TyperDecorator(this)); 1303 graph->AddDecorator(new (zone()) TyperDecorator(this));
911 } 1304 }
912 1305
913 } 1306 }
914 } 1307 }
915 } // namespace v8::internal::compiler 1308 } // namespace v8::internal::compiler
OLDNEW
« no previous file with comments | « src/compiler/typer.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698