blob: 9b2c6388c1626c4bbe975dde7a01794acfc716e4 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/ast/ast.h"
6
7#include <cmath> // For isfinite.
Ben Murdoch097c5b22016-05-18 11:27:45 +01008
9#include "src/ast/prettyprinter.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/ast/scopes.h"
11#include "src/builtins.h"
12#include "src/code-stubs.h"
13#include "src/contexts.h"
14#include "src/conversions.h"
15#include "src/hashmap.h"
16#include "src/parsing/parser.h"
17#include "src/property.h"
18#include "src/property-details.h"
19#include "src/string-stream.h"
20#include "src/type-info.h"
21
22namespace v8 {
23namespace internal {
24
25// ----------------------------------------------------------------------------
26// All the Accept member functions for each syntax tree node type.
27
28#define DECL_ACCEPT(type) \
29 void type::Accept(AstVisitor* v) { v->Visit##type(this); }
30AST_NODE_LIST(DECL_ACCEPT)
31#undef DECL_ACCEPT
32
33
34// ----------------------------------------------------------------------------
35// Implementation of other node functionality.
36
Ben Murdoch097c5b22016-05-18 11:27:45 +010037#ifdef DEBUG
38
39void AstNode::Print() { Print(Isolate::Current()); }
40
41
42void AstNode::Print(Isolate* isolate) {
43 AstPrinter::PrintOut(isolate, this);
44}
45
46
47void AstNode::PrettyPrint() { PrettyPrint(Isolate::Current()); }
48
49
50void AstNode::PrettyPrint(Isolate* isolate) {
51 PrettyPrinter::PrintOut(isolate, this);
52}
53
54#endif // DEBUG
55
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056
57bool Expression::IsSmiLiteral() const {
58 return IsLiteral() && AsLiteral()->value()->IsSmi();
59}
60
61
62bool Expression::IsStringLiteral() const {
63 return IsLiteral() && AsLiteral()->value()->IsString();
64}
65
66
67bool Expression::IsNullLiteral() const {
68 return IsLiteral() && AsLiteral()->value()->IsNull();
69}
70
71
72bool Expression::IsUndefinedLiteral(Isolate* isolate) const {
73 const VariableProxy* var_proxy = AsVariableProxy();
74 if (var_proxy == NULL) return false;
75 Variable* var = var_proxy->var();
76 // The global identifier "undefined" is immutable. Everything
77 // else could be reassigned.
78 return var != NULL && var->IsUnallocatedOrGlobalSlot() &&
79 var_proxy->raw_name()->IsOneByteEqualTo("undefined");
80}
81
82
83bool Expression::IsValidReferenceExpressionOrThis() const {
84 return IsValidReferenceExpression() ||
85 (IsVariableProxy() && AsVariableProxy()->is_this());
86}
87
88
89VariableProxy::VariableProxy(Zone* zone, Variable* var, int start_position,
90 int end_position)
91 : Expression(zone, start_position),
92 bit_field_(IsThisField::encode(var->is_this()) |
93 IsAssignedField::encode(false) |
94 IsResolvedField::encode(false)),
95 raw_name_(var->raw_name()),
96 end_position_(end_position) {
97 BindTo(var);
98}
99
100
101VariableProxy::VariableProxy(Zone* zone, const AstRawString* name,
102 Variable::Kind variable_kind, int start_position,
103 int end_position)
104 : Expression(zone, start_position),
105 bit_field_(IsThisField::encode(variable_kind == Variable::THIS) |
106 IsAssignedField::encode(false) |
107 IsResolvedField::encode(false)),
108 raw_name_(name),
109 end_position_(end_position) {}
110
111
112void VariableProxy::BindTo(Variable* var) {
113 DCHECK((is_this() && var->is_this()) || raw_name() == var->raw_name());
114 set_var(var);
115 set_is_resolved();
116 var->set_is_used();
117}
118
119
120void VariableProxy::AssignFeedbackVectorSlots(Isolate* isolate,
121 FeedbackVectorSpec* spec,
122 FeedbackVectorSlotCache* cache) {
123 if (UsesVariableFeedbackSlot()) {
124 // VariableProxies that point to the same Variable within a function can
125 // make their loads from the same IC slot.
126 if (var()->IsUnallocated()) {
127 ZoneHashMap::Entry* entry = cache->Get(var());
128 if (entry != NULL) {
129 variable_feedback_slot_ = FeedbackVectorSlot(
130 static_cast<int>(reinterpret_cast<intptr_t>(entry->value)));
131 return;
132 }
133 }
134 variable_feedback_slot_ = spec->AddLoadICSlot();
135 if (var()->IsUnallocated()) {
136 cache->Put(var(), variable_feedback_slot_);
137 }
138 }
139}
140
141
142static void AssignVectorSlots(Expression* expr, FeedbackVectorSpec* spec,
143 FeedbackVectorSlot* out_slot) {
144 Property* property = expr->AsProperty();
145 LhsKind assign_type = Property::GetAssignType(property);
146 if ((assign_type == VARIABLE &&
147 expr->AsVariableProxy()->var()->IsUnallocated()) ||
148 assign_type == NAMED_PROPERTY || assign_type == KEYED_PROPERTY) {
149 // TODO(ishell): consider using ICSlotCache for variables here.
150 FeedbackVectorSlotKind kind = assign_type == KEYED_PROPERTY
151 ? FeedbackVectorSlotKind::KEYED_STORE_IC
152 : FeedbackVectorSlotKind::STORE_IC;
153 *out_slot = spec->AddSlot(kind);
154 }
155}
156
157
158void ForEachStatement::AssignFeedbackVectorSlots(
159 Isolate* isolate, FeedbackVectorSpec* spec,
160 FeedbackVectorSlotCache* cache) {
161 // TODO(adamk): for-of statements do not make use of this feedback slot.
162 // The each_slot_ should be specific to ForInStatement, and this work moved
163 // there.
164 if (IsForOfStatement()) return;
165 AssignVectorSlots(each(), spec, &each_slot_);
166}
167
168
169Assignment::Assignment(Zone* zone, Token::Value op, Expression* target,
170 Expression* value, int pos)
171 : Expression(zone, pos),
172 bit_field_(
173 IsUninitializedField::encode(false) | KeyTypeField::encode(ELEMENT) |
174 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
175 target_(target),
176 value_(value),
177 binary_operation_(NULL) {}
178
179
180void Assignment::AssignFeedbackVectorSlots(Isolate* isolate,
181 FeedbackVectorSpec* spec,
182 FeedbackVectorSlotCache* cache) {
183 AssignVectorSlots(target(), spec, &slot_);
184}
185
186
187void CountOperation::AssignFeedbackVectorSlots(Isolate* isolate,
188 FeedbackVectorSpec* spec,
189 FeedbackVectorSlotCache* cache) {
190 AssignVectorSlots(expression(), spec, &slot_);
191}
192
193
194Token::Value Assignment::binary_op() const {
195 switch (op()) {
196 case Token::ASSIGN_BIT_OR: return Token::BIT_OR;
197 case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR;
198 case Token::ASSIGN_BIT_AND: return Token::BIT_AND;
199 case Token::ASSIGN_SHL: return Token::SHL;
200 case Token::ASSIGN_SAR: return Token::SAR;
201 case Token::ASSIGN_SHR: return Token::SHR;
202 case Token::ASSIGN_ADD: return Token::ADD;
203 case Token::ASSIGN_SUB: return Token::SUB;
204 case Token::ASSIGN_MUL: return Token::MUL;
205 case Token::ASSIGN_DIV: return Token::DIV;
206 case Token::ASSIGN_MOD: return Token::MOD;
207 default: UNREACHABLE();
208 }
209 return Token::ILLEGAL;
210}
211
212
213bool FunctionLiteral::AllowsLazyCompilation() {
214 return scope()->AllowsLazyCompilation();
215}
216
217
218bool FunctionLiteral::AllowsLazyCompilationWithoutContext() {
219 return scope()->AllowsLazyCompilationWithoutContext();
220}
221
222
223int FunctionLiteral::start_position() const {
224 return scope()->start_position();
225}
226
227
228int FunctionLiteral::end_position() const {
229 return scope()->end_position();
230}
231
232
233LanguageMode FunctionLiteral::language_mode() const {
234 return scope()->language_mode();
235}
236
237
238bool FunctionLiteral::NeedsHomeObject(Expression* expr) {
239 if (expr == nullptr || !expr->IsFunctionLiteral()) return false;
240 DCHECK_NOT_NULL(expr->AsFunctionLiteral()->scope());
241 return expr->AsFunctionLiteral()->scope()->NeedsHomeObject();
242}
243
244
245ObjectLiteralProperty::ObjectLiteralProperty(Expression* key, Expression* value,
246 Kind kind, bool is_static,
247 bool is_computed_name)
248 : key_(key),
249 value_(value),
250 kind_(kind),
251 emit_store_(true),
252 is_static_(is_static),
253 is_computed_name_(is_computed_name) {}
254
255
256ObjectLiteralProperty::ObjectLiteralProperty(AstValueFactory* ast_value_factory,
257 Expression* key, Expression* value,
258 bool is_static,
259 bool is_computed_name)
260 : key_(key),
261 value_(value),
262 emit_store_(true),
263 is_static_(is_static),
264 is_computed_name_(is_computed_name) {
265 if (!is_computed_name &&
266 key->AsLiteral()->raw_value()->EqualsString(
267 ast_value_factory->proto_string())) {
268 kind_ = PROTOTYPE;
269 } else if (value_->AsMaterializedLiteral() != NULL) {
270 kind_ = MATERIALIZED_LITERAL;
271 } else if (value_->IsLiteral()) {
272 kind_ = CONSTANT;
273 } else {
274 kind_ = COMPUTED;
275 }
276}
277
Ben Murdoch097c5b22016-05-18 11:27:45 +0100278bool ObjectLiteralProperty::NeedsSetFunctionName() const {
279 return is_computed_name_ &&
280 (value_->IsAnonymousFunctionDefinition() ||
281 (value_->IsFunctionLiteral() &&
282 IsConciseMethod(value_->AsFunctionLiteral()->kind())));
283}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284
285void ClassLiteral::AssignFeedbackVectorSlots(Isolate* isolate,
286 FeedbackVectorSpec* spec,
287 FeedbackVectorSlotCache* cache) {
288 // This logic that computes the number of slots needed for vector store
289 // ICs must mirror FullCodeGenerator::VisitClassLiteral.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100290 prototype_slot_ = spec->AddLoadICSlot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 if (NeedsProxySlot()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100292 proxy_slot_ = spec->AddStoreICSlot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 }
294
295 for (int i = 0; i < properties()->length(); i++) {
296 ObjectLiteral::Property* property = properties()->at(i);
297 Expression* value = property->value();
298 if (FunctionLiteral::NeedsHomeObject(value)) {
299 property->SetSlot(spec->AddStoreICSlot());
300 }
301 }
302}
303
304
305bool ObjectLiteral::Property::IsCompileTimeValue() {
306 return kind_ == CONSTANT ||
307 (kind_ == MATERIALIZED_LITERAL &&
308 CompileTimeValue::IsCompileTimeValue(value_));
309}
310
311
312void ObjectLiteral::Property::set_emit_store(bool emit_store) {
313 emit_store_ = emit_store;
314}
315
316
317bool ObjectLiteral::Property::emit_store() {
318 return emit_store_;
319}
320
321
322void ObjectLiteral::AssignFeedbackVectorSlots(Isolate* isolate,
323 FeedbackVectorSpec* spec,
324 FeedbackVectorSlotCache* cache) {
325 // This logic that computes the number of slots needed for vector store
326 // ics must mirror FullCodeGenerator::VisitObjectLiteral.
327 int property_index = 0;
328 for (; property_index < properties()->length(); property_index++) {
329 ObjectLiteral::Property* property = properties()->at(property_index);
330 if (property->is_computed_name()) break;
331 if (property->IsCompileTimeValue()) continue;
332
333 Literal* key = property->key()->AsLiteral();
334 Expression* value = property->value();
335 switch (property->kind()) {
336 case ObjectLiteral::Property::CONSTANT:
337 UNREACHABLE();
338 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
339 // Fall through.
340 case ObjectLiteral::Property::COMPUTED:
341 // It is safe to use [[Put]] here because the boilerplate already
342 // contains computed properties with an uninitialized value.
343 if (key->value()->IsInternalizedString()) {
344 if (property->emit_store()) {
345 property->SetSlot(spec->AddStoreICSlot());
346 if (FunctionLiteral::NeedsHomeObject(value)) {
347 property->SetSlot(spec->AddStoreICSlot(), 1);
348 }
349 }
350 break;
351 }
352 if (property->emit_store() && FunctionLiteral::NeedsHomeObject(value)) {
353 property->SetSlot(spec->AddStoreICSlot());
354 }
355 break;
356 case ObjectLiteral::Property::PROTOTYPE:
357 break;
358 case ObjectLiteral::Property::GETTER:
359 if (property->emit_store() && FunctionLiteral::NeedsHomeObject(value)) {
360 property->SetSlot(spec->AddStoreICSlot());
361 }
362 break;
363 case ObjectLiteral::Property::SETTER:
364 if (property->emit_store() && FunctionLiteral::NeedsHomeObject(value)) {
365 property->SetSlot(spec->AddStoreICSlot());
366 }
367 break;
368 }
369 }
370
371 for (; property_index < properties()->length(); property_index++) {
372 ObjectLiteral::Property* property = properties()->at(property_index);
373
374 Expression* value = property->value();
375 if (property->kind() != ObjectLiteral::Property::PROTOTYPE) {
376 if (FunctionLiteral::NeedsHomeObject(value)) {
377 property->SetSlot(spec->AddStoreICSlot());
378 }
379 }
380 }
381}
382
383
384void ObjectLiteral::CalculateEmitStore(Zone* zone) {
385 const auto GETTER = ObjectLiteral::Property::GETTER;
386 const auto SETTER = ObjectLiteral::Property::SETTER;
387
388 ZoneAllocationPolicy allocator(zone);
389
390 ZoneHashMap table(Literal::Match, ZoneHashMap::kDefaultHashMapCapacity,
391 allocator);
392 for (int i = properties()->length() - 1; i >= 0; i--) {
393 ObjectLiteral::Property* property = properties()->at(i);
394 if (property->is_computed_name()) continue;
395 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) continue;
396 Literal* literal = property->key()->AsLiteral();
397 DCHECK(!literal->value()->IsNull());
398
399 // If there is an existing entry do not emit a store unless the previous
400 // entry was also an accessor.
401 uint32_t hash = literal->Hash();
402 ZoneHashMap::Entry* entry = table.LookupOrInsert(literal, hash, allocator);
403 if (entry->value != NULL) {
404 auto previous_kind =
405 static_cast<ObjectLiteral::Property*>(entry->value)->kind();
406 if (!((property->kind() == GETTER && previous_kind == SETTER) ||
407 (property->kind() == SETTER && previous_kind == GETTER))) {
408 property->set_emit_store(false);
409 }
410 }
411 entry->value = property;
412 }
413}
414
415
416bool ObjectLiteral::IsBoilerplateProperty(ObjectLiteral::Property* property) {
417 return property != NULL &&
418 property->kind() != ObjectLiteral::Property::PROTOTYPE;
419}
420
421
422void ObjectLiteral::BuildConstantProperties(Isolate* isolate) {
423 if (!constant_properties_.is_null()) return;
424
425 // Allocate a fixed array to hold all the constant properties.
426 Handle<FixedArray> constant_properties = isolate->factory()->NewFixedArray(
427 boilerplate_properties_ * 2, TENURED);
428
429 int position = 0;
430 // Accumulate the value in local variables and store it at the end.
431 bool is_simple = true;
432 int depth_acc = 1;
433 uint32_t max_element_index = 0;
434 uint32_t elements = 0;
435 for (int i = 0; i < properties()->length(); i++) {
436 ObjectLiteral::Property* property = properties()->at(i);
437 if (!IsBoilerplateProperty(property)) {
438 is_simple = false;
439 continue;
440 }
441
442 if (position == boilerplate_properties_ * 2) {
443 DCHECK(property->is_computed_name());
444 is_simple = false;
445 break;
446 }
447 DCHECK(!property->is_computed_name());
448
449 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
450 if (m_literal != NULL) {
451 m_literal->BuildConstants(isolate);
452 if (m_literal->depth() >= depth_acc) depth_acc = m_literal->depth() + 1;
453 }
454
455 // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined
456 // value for COMPUTED properties, the real value is filled in at
457 // runtime. The enumeration order is maintained.
458 Handle<Object> key = property->key()->AsLiteral()->value();
459 Handle<Object> value = GetBoilerplateValue(property->value(), isolate);
460
461 // Ensure objects that may, at any point in time, contain fields with double
462 // representation are always treated as nested objects. This is true for
463 // computed fields (value is undefined), and smi and double literals
464 // (value->IsNumber()).
465 // TODO(verwaest): Remove once we can store them inline.
466 if (FLAG_track_double_fields &&
467 (value->IsNumber() || value->IsUninitialized())) {
468 may_store_doubles_ = true;
469 }
470
471 is_simple = is_simple && !value->IsUninitialized();
472
473 // Keep track of the number of elements in the object literal and
474 // the largest element index. If the largest element index is
475 // much larger than the number of elements, creating an object
476 // literal with fast elements will be a waste of space.
477 uint32_t element_index = 0;
478 if (key->IsString()
479 && Handle<String>::cast(key)->AsArrayIndex(&element_index)
480 && element_index > max_element_index) {
481 max_element_index = element_index;
482 elements++;
483 } else if (key->IsSmi()) {
484 int key_value = Smi::cast(*key)->value();
485 if (key_value > 0
486 && static_cast<uint32_t>(key_value) > max_element_index) {
487 max_element_index = key_value;
488 }
489 elements++;
490 }
491
492 // Add name, value pair to the fixed array.
493 constant_properties->set(position++, *key);
494 constant_properties->set(position++, *value);
495 }
496
497 constant_properties_ = constant_properties;
498 fast_elements_ =
499 (max_element_index <= 32) || ((2 * elements) >= max_element_index);
500 has_elements_ = elements > 0;
501 set_is_simple(is_simple);
502 set_depth(depth_acc);
503}
504
505
506void ArrayLiteral::BuildConstantElements(Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100507 DCHECK_LT(first_spread_index_, 0);
508
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000509 if (!constant_elements_.is_null()) return;
510
Ben Murdoch097c5b22016-05-18 11:27:45 +0100511 int constants_length = values()->length();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000512
513 // Allocate a fixed array to hold all the object literals.
514 Handle<JSArray> array = isolate->factory()->NewJSArray(
515 FAST_HOLEY_SMI_ELEMENTS, constants_length, constants_length,
516 Strength::WEAK, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
517
518 // Fill in the literals.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100519 bool is_simple = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000520 int depth_acc = 1;
521 bool is_holey = false;
522 int array_index = 0;
523 for (; array_index < constants_length; array_index++) {
524 Expression* element = values()->at(array_index);
525 DCHECK(!element->IsSpread());
526 MaterializedLiteral* m_literal = element->AsMaterializedLiteral();
527 if (m_literal != NULL) {
528 m_literal->BuildConstants(isolate);
529 if (m_literal->depth() + 1 > depth_acc) {
530 depth_acc = m_literal->depth() + 1;
531 }
532 }
533
534 // New handle scope here, needs to be after BuildContants().
535 HandleScope scope(isolate);
536 Handle<Object> boilerplate_value = GetBoilerplateValue(element, isolate);
537 if (boilerplate_value->IsTheHole()) {
538 is_holey = true;
539 continue;
540 }
541
542 if (boilerplate_value->IsUninitialized()) {
543 boilerplate_value = handle(Smi::FromInt(0), isolate);
544 is_simple = false;
545 }
546
547 JSObject::AddDataElement(array, array_index, boilerplate_value, NONE)
548 .Assert();
549 }
550
551 JSObject::ValidateElements(array);
552 Handle<FixedArrayBase> element_values(array->elements());
553
554 // Simple and shallow arrays can be lazily copied, we transform the
555 // elements array to a copy-on-write array.
556 if (is_simple && depth_acc == 1 && array_index > 0 &&
557 array->HasFastSmiOrObjectElements()) {
558 element_values->set_map(isolate->heap()->fixed_cow_array_map());
559 }
560
561 // Remember both the literal's constant values as well as the ElementsKind
562 // in a 2-element FixedArray.
563 Handle<FixedArray> literals = isolate->factory()->NewFixedArray(2, TENURED);
564
565 ElementsKind kind = array->GetElementsKind();
566 kind = is_holey ? GetHoleyElementsKind(kind) : GetPackedElementsKind(kind);
567
568 literals->set(0, Smi::FromInt(kind));
569 literals->set(1, *element_values);
570
571 constant_elements_ = literals;
572 set_is_simple(is_simple);
573 set_depth(depth_acc);
574}
575
576
577void ArrayLiteral::AssignFeedbackVectorSlots(Isolate* isolate,
578 FeedbackVectorSpec* spec,
579 FeedbackVectorSlotCache* cache) {
580 // This logic that computes the number of slots needed for vector store
581 // ics must mirror FullCodeGenerator::VisitArrayLiteral.
582 int array_index = 0;
583 for (; array_index < values()->length(); array_index++) {
584 Expression* subexpr = values()->at(array_index);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100585 DCHECK(!subexpr->IsSpread());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000586 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
587
588 // We'll reuse the same literal slot for all of the non-constant
589 // subexpressions that use a keyed store IC.
590 literal_slot_ = spec->AddKeyedStoreICSlot();
591 return;
592 }
593}
594
595
596Handle<Object> MaterializedLiteral::GetBoilerplateValue(Expression* expression,
597 Isolate* isolate) {
598 if (expression->IsLiteral()) {
599 return expression->AsLiteral()->value();
600 }
601 if (CompileTimeValue::IsCompileTimeValue(expression)) {
602 return CompileTimeValue::GetValue(isolate, expression);
603 }
604 return isolate->factory()->uninitialized_value();
605}
606
607
608void MaterializedLiteral::BuildConstants(Isolate* isolate) {
609 if (IsArrayLiteral()) {
610 return AsArrayLiteral()->BuildConstantElements(isolate);
611 }
612 if (IsObjectLiteral()) {
613 return AsObjectLiteral()->BuildConstantProperties(isolate);
614 }
615 DCHECK(IsRegExpLiteral());
616 DCHECK(depth() >= 1); // Depth should be initialized.
617}
618
619
620void UnaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
621 // TODO(olivf) If this Operation is used in a test context, then the
622 // expression has a ToBoolean stub and we want to collect the type
623 // information. However the GraphBuilder expects it to be on the instruction
624 // corresponding to the TestContext, therefore we have to store it here and
625 // not on the operand.
626 set_to_boolean_types(oracle->ToBooleanTypes(expression()->test_id()));
627}
628
629
630void BinaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
631 // TODO(olivf) If this Operation is used in a test context, then the right
632 // hand side has a ToBoolean stub and we want to collect the type information.
633 // However the GraphBuilder expects it to be on the instruction corresponding
634 // to the TestContext, therefore we have to store it here and not on the
635 // right hand operand.
636 set_to_boolean_types(oracle->ToBooleanTypes(right()->test_id()));
637}
638
639
640static bool IsTypeof(Expression* expr) {
641 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
642 return maybe_unary != NULL && maybe_unary->op() == Token::TYPEOF;
643}
644
645
646// Check for the pattern: typeof <expression> equals <string literal>.
647static bool MatchLiteralCompareTypeof(Expression* left,
648 Token::Value op,
649 Expression* right,
650 Expression** expr,
651 Handle<String>* check) {
652 if (IsTypeof(left) && right->IsStringLiteral() && Token::IsEqualityOp(op)) {
653 *expr = left->AsUnaryOperation()->expression();
654 *check = Handle<String>::cast(right->AsLiteral()->value());
655 return true;
656 }
657 return false;
658}
659
660
661bool CompareOperation::IsLiteralCompareTypeof(Expression** expr,
662 Handle<String>* check) {
663 return MatchLiteralCompareTypeof(left_, op_, right_, expr, check) ||
664 MatchLiteralCompareTypeof(right_, op_, left_, expr, check);
665}
666
667
668static bool IsVoidOfLiteral(Expression* expr) {
669 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
670 return maybe_unary != NULL &&
671 maybe_unary->op() == Token::VOID &&
672 maybe_unary->expression()->IsLiteral();
673}
674
675
676// Check for the pattern: void <literal> equals <expression> or
677// undefined equals <expression>
678static bool MatchLiteralCompareUndefined(Expression* left,
679 Token::Value op,
680 Expression* right,
681 Expression** expr,
682 Isolate* isolate) {
683 if (IsVoidOfLiteral(left) && Token::IsEqualityOp(op)) {
684 *expr = right;
685 return true;
686 }
687 if (left->IsUndefinedLiteral(isolate) && Token::IsEqualityOp(op)) {
688 *expr = right;
689 return true;
690 }
691 return false;
692}
693
694
695bool CompareOperation::IsLiteralCompareUndefined(
696 Expression** expr, Isolate* isolate) {
697 return MatchLiteralCompareUndefined(left_, op_, right_, expr, isolate) ||
698 MatchLiteralCompareUndefined(right_, op_, left_, expr, isolate);
699}
700
701
702// Check for the pattern: null equals <expression>
703static bool MatchLiteralCompareNull(Expression* left,
704 Token::Value op,
705 Expression* right,
706 Expression** expr) {
707 if (left->IsNullLiteral() && Token::IsEqualityOp(op)) {
708 *expr = right;
709 return true;
710 }
711 return false;
712}
713
714
715bool CompareOperation::IsLiteralCompareNull(Expression** expr) {
716 return MatchLiteralCompareNull(left_, op_, right_, expr) ||
717 MatchLiteralCompareNull(right_, op_, left_, expr);
718}
719
720
721// ----------------------------------------------------------------------------
722// Inlining support
723
724bool Declaration::IsInlineable() const {
725 return proxy()->var()->IsStackAllocated();
726}
727
728bool FunctionDeclaration::IsInlineable() const {
729 return false;
730}
731
732
733// ----------------------------------------------------------------------------
734// Recording of type feedback
735
736// TODO(rossberg): all RecordTypeFeedback functions should disappear
737// once we use the common type field in the AST consistently.
738
739void Expression::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
740 set_to_boolean_types(oracle->ToBooleanTypes(test_id()));
741}
742
743
744bool Call::IsUsingCallFeedbackICSlot(Isolate* isolate) const {
745 CallType call_type = GetCallType(isolate);
746 if (call_type == POSSIBLY_EVAL_CALL) {
747 return false;
748 }
749 return true;
750}
751
752
753bool Call::IsUsingCallFeedbackSlot(Isolate* isolate) const {
754 // SuperConstructorCall uses a CallConstructStub, which wants
755 // a Slot, in addition to any IC slots requested elsewhere.
756 return GetCallType(isolate) == SUPER_CALL;
757}
758
759
760void Call::AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
761 FeedbackVectorSlotCache* cache) {
762 if (IsUsingCallFeedbackICSlot(isolate)) {
763 ic_slot_ = spec->AddCallICSlot();
764 }
765 if (IsUsingCallFeedbackSlot(isolate)) {
766 stub_slot_ = spec->AddGeneralSlot();
767 }
768}
769
770
771Call::CallType Call::GetCallType(Isolate* isolate) const {
772 VariableProxy* proxy = expression()->AsVariableProxy();
773 if (proxy != NULL) {
774 if (proxy->var()->is_possibly_eval(isolate)) {
775 return POSSIBLY_EVAL_CALL;
776 } else if (proxy->var()->IsUnallocatedOrGlobalSlot()) {
777 return GLOBAL_CALL;
778 } else if (proxy->var()->IsLookupSlot()) {
779 return LOOKUP_SLOT_CALL;
780 }
781 }
782
783 if (expression()->IsSuperCallReference()) return SUPER_CALL;
784
785 Property* property = expression()->AsProperty();
786 if (property != nullptr) {
787 bool is_super = property->IsSuperAccess();
788 if (property->key()->IsPropertyName()) {
789 return is_super ? NAMED_SUPER_PROPERTY_CALL : NAMED_PROPERTY_CALL;
790 } else {
791 return is_super ? KEYED_SUPER_PROPERTY_CALL : KEYED_PROPERTY_CALL;
792 }
793 }
794
795 return OTHER_CALL;
796}
797
798
799// ----------------------------------------------------------------------------
800// Implementation of AstVisitor
801
802void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) {
803 for (int i = 0; i < declarations->length(); i++) {
804 Visit(declarations->at(i));
805 }
806}
807
808
809void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) {
810 for (int i = 0; i < statements->length(); i++) {
811 Statement* stmt = statements->at(i);
812 Visit(stmt);
813 if (stmt->IsJump()) break;
814 }
815}
816
817
818void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) {
819 for (int i = 0; i < expressions->length(); i++) {
820 // The variable statement visiting code may pass NULL expressions
821 // to this code. Maybe this should be handled by introducing an
822 // undefined expression or literal? Revisit this code if this
823 // changes
824 Expression* expression = expressions->at(i);
825 if (expression != NULL) Visit(expression);
826 }
827}
828
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829CaseClause::CaseClause(Zone* zone, Expression* label,
830 ZoneList<Statement*>* statements, int pos)
831 : Expression(zone, pos),
832 label_(label),
833 statements_(statements),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100834 compare_type_(Type::None()) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000835
836uint32_t Literal::Hash() {
837 return raw_value()->IsString()
838 ? raw_value()->AsString()->hash()
839 : ComputeLongHash(double_to_uint64(raw_value()->AsNumber()));
840}
841
842
843// static
844bool Literal::Match(void* literal1, void* literal2) {
845 const AstValue* x = static_cast<Literal*>(literal1)->raw_value();
846 const AstValue* y = static_cast<Literal*>(literal2)->raw_value();
847 return (x->IsString() && y->IsString() && x->AsString() == y->AsString()) ||
848 (x->IsNumber() && y->IsNumber() && x->AsNumber() == y->AsNumber());
849}
850
851
852} // namespace internal
853} // namespace v8