1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <cmath> // For isfinite.
32 #include "code-stubs.h"
34 #include "conversions.h"
37 #include "property-details.h"
40 #include "string-stream.h"
41 #include "type-info.h"
46 // ----------------------------------------------------------------------------
47 // All the Accept member functions for each syntax tree node type.
49 #define DECL_ACCEPT(type) \
50 void type::Accept(AstVisitor* v) { v->Visit##type(this); }
51 AST_NODE_LIST(DECL_ACCEPT)
55 // ----------------------------------------------------------------------------
56 // Implementation of other node functionality.
59 bool Expression::IsSmiLiteral() {
60 return AsLiteral() != NULL && AsLiteral()->value()->IsSmi();
64 bool Expression::IsStringLiteral() {
65 return AsLiteral() != NULL && AsLiteral()->value()->IsString();
69 bool Expression::IsNullLiteral() {
70 return AsLiteral() != NULL && AsLiteral()->value()->IsNull();
74 bool Expression::IsUndefinedLiteral(Isolate* isolate) {
75 VariableProxy* var_proxy = AsVariableProxy();
76 if (var_proxy == NULL) return false;
77 Variable* var = var_proxy->var();
78 // The global identifier "undefined" is immutable. Everything
79 // else could be reassigned.
80 return var != NULL && var->location() == Variable::UNALLOCATED &&
81 var_proxy->name()->Equals(isolate->heap()->undefined_string());
85 VariableProxy::VariableProxy(Isolate* isolate, Variable* var, int position)
86 : Expression(isolate, position),
88 var_(NULL), // Will be set by the call to BindTo.
89 is_this_(var->is_this()),
92 interface_(var->interface()) {
97 VariableProxy::VariableProxy(Isolate* isolate,
100 Interface* interface,
102 : Expression(isolate, position),
108 interface_(interface) {
109 // Names must be canonicalized for fast equality checks.
110 ASSERT(name->IsInternalizedString());
114 void VariableProxy::BindTo(Variable* var) {
115 ASSERT(var_ == NULL); // must be bound only once
116 ASSERT(var != NULL); // must bind
117 ASSERT(!FLAG_harmony_modules || interface_->IsUnified(var->interface()));
118 ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name()));
119 // Ideally CONST-ness should match. However, this is very hard to achieve
120 // because we don't know the exact semantics of conflicting (const and
121 // non-const) multiple variable declarations, const vars introduced via
122 // eval() etc. Const-ness and variable declarations are a complete mess
125 var->set_is_used(true);
129 Assignment::Assignment(Isolate* isolate,
134 : Expression(isolate, pos),
138 binary_operation_(NULL),
139 assignment_id_(GetNextId(isolate)),
140 is_monomorphic_(false),
141 is_uninitialized_(false),
142 is_pre_monomorphic_(false),
143 store_mode_(STANDARD_STORE) { }
146 Token::Value Assignment::binary_op() const {
148 case Token::ASSIGN_BIT_OR: return Token::BIT_OR;
149 case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR;
150 case Token::ASSIGN_BIT_AND: return Token::BIT_AND;
151 case Token::ASSIGN_SHL: return Token::SHL;
152 case Token::ASSIGN_SAR: return Token::SAR;
153 case Token::ASSIGN_SHR: return Token::SHR;
154 case Token::ASSIGN_ADD: return Token::ADD;
155 case Token::ASSIGN_SUB: return Token::SUB;
156 case Token::ASSIGN_MUL: return Token::MUL;
157 case Token::ASSIGN_DIV: return Token::DIV;
158 case Token::ASSIGN_MOD: return Token::MOD;
159 default: UNREACHABLE();
161 return Token::ILLEGAL;
165 bool FunctionLiteral::AllowsLazyCompilation() {
166 return scope()->AllowsLazyCompilation();
170 bool FunctionLiteral::AllowsLazyCompilationWithoutContext() {
171 return scope()->AllowsLazyCompilationWithoutContext();
175 int FunctionLiteral::start_position() const {
176 return scope()->start_position();
180 int FunctionLiteral::end_position() const {
181 return scope()->end_position();
185 LanguageMode FunctionLiteral::language_mode() const {
186 return scope()->language_mode();
190 ObjectLiteralProperty::ObjectLiteralProperty(Literal* key,
196 Object* k = *key->value();
197 if (k->IsInternalizedString() &&
198 isolate->heap()->proto_string()->Equals(String::cast(k))) {
200 } else if (value_->AsMaterializedLiteral() != NULL) {
201 kind_ = MATERIALIZED_LITERAL;
202 } else if (value_->AsLiteral() != NULL) {
210 ObjectLiteralProperty::ObjectLiteralProperty(bool is_getter,
211 FunctionLiteral* value) {
214 kind_ = is_getter ? GETTER : SETTER;
218 bool ObjectLiteral::Property::IsCompileTimeValue() {
219 return kind_ == CONSTANT ||
220 (kind_ == MATERIALIZED_LITERAL &&
221 CompileTimeValue::IsCompileTimeValue(value_));
225 void ObjectLiteral::Property::set_emit_store(bool emit_store) {
226 emit_store_ = emit_store;
230 bool ObjectLiteral::Property::emit_store() {
235 void ObjectLiteral::CalculateEmitStore(Zone* zone) {
236 ZoneAllocationPolicy allocator(zone);
238 ZoneHashMap table(Literal::Match, ZoneHashMap::kDefaultHashMapCapacity,
240 for (int i = properties()->length() - 1; i >= 0; i--) {
241 ObjectLiteral::Property* property = properties()->at(i);
242 Literal* literal = property->key();
243 if (literal->value()->IsNull()) continue;
244 uint32_t hash = literal->Hash();
245 // If the key of a computed property is in the table, do not emit
246 // a store for the property later.
247 if ((property->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL ||
248 property->kind() == ObjectLiteral::Property::COMPUTED) &&
249 table.Lookup(literal, hash, false, allocator) != NULL) {
250 property->set_emit_store(false);
252 // Add key to the table.
253 table.Lookup(literal, hash, true, allocator);
259 void TargetCollector::AddTarget(Label* target, Zone* zone) {
260 // Add the label to the collector, but discard duplicates.
261 int length = targets_.length();
262 for (int i = 0; i < length; i++) {
263 if (targets_[i] == target) return;
265 targets_.Add(target, zone);
269 void UnaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
270 // TODO(olivf) If this Operation is used in a test context, then the
271 // expression has a ToBoolean stub and we want to collect the type
272 // information. However the GraphBuilder expects it to be on the instruction
273 // corresponding to the TestContext, therefore we have to store it here and
274 // not on the operand.
275 set_to_boolean_types(oracle->ToBooleanTypes(expression()->test_id()));
279 void BinaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
280 // TODO(olivf) If this Operation is used in a test context, then the right
281 // hand side has a ToBoolean stub and we want to collect the type information.
282 // However the GraphBuilder expects it to be on the instruction corresponding
283 // to the TestContext, therefore we have to store it here and not on the
284 // right hand operand.
285 set_to_boolean_types(oracle->ToBooleanTypes(right()->test_id()));
289 bool BinaryOperation::ResultOverwriteAllowed() {
314 static bool IsTypeof(Expression* expr) {
315 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
316 return maybe_unary != NULL && maybe_unary->op() == Token::TYPEOF;
320 // Check for the pattern: typeof <expression> equals <string literal>.
321 static bool MatchLiteralCompareTypeof(Expression* left,
325 Handle<String>* check) {
326 if (IsTypeof(left) && right->IsStringLiteral() && Token::IsEqualityOp(op)) {
327 *expr = left->AsUnaryOperation()->expression();
328 *check = Handle<String>::cast(right->AsLiteral()->value());
335 bool CompareOperation::IsLiteralCompareTypeof(Expression** expr,
336 Handle<String>* check) {
337 return MatchLiteralCompareTypeof(left_, op_, right_, expr, check) ||
338 MatchLiteralCompareTypeof(right_, op_, left_, expr, check);
342 static bool IsVoidOfLiteral(Expression* expr) {
343 UnaryOperation* maybe_unary = expr->AsUnaryOperation();
344 return maybe_unary != NULL &&
345 maybe_unary->op() == Token::VOID &&
346 maybe_unary->expression()->AsLiteral() != NULL;
350 // Check for the pattern: void <literal> equals <expression> or
351 // undefined equals <expression>
352 static bool MatchLiteralCompareUndefined(Expression* left,
357 if (IsVoidOfLiteral(left) && Token::IsEqualityOp(op)) {
361 if (left->IsUndefinedLiteral(isolate) && Token::IsEqualityOp(op)) {
369 bool CompareOperation::IsLiteralCompareUndefined(
370 Expression** expr, Isolate* isolate) {
371 return MatchLiteralCompareUndefined(left_, op_, right_, expr, isolate) ||
372 MatchLiteralCompareUndefined(right_, op_, left_, expr, isolate);
376 // Check for the pattern: null equals <expression>
377 static bool MatchLiteralCompareNull(Expression* left,
381 if (left->IsNullLiteral() && Token::IsEqualityOp(op)) {
389 bool CompareOperation::IsLiteralCompareNull(Expression** expr) {
390 return MatchLiteralCompareNull(left_, op_, right_, expr) ||
391 MatchLiteralCompareNull(right_, op_, left_, expr);
395 // ----------------------------------------------------------------------------
398 bool Declaration::IsInlineable() const {
399 return proxy()->var()->IsStackAllocated();
402 bool FunctionDeclaration::IsInlineable() const {
407 // ----------------------------------------------------------------------------
408 // Recording of type feedback
410 // TODO(rossberg): all RecordTypeFeedback functions should disappear
411 // once we use the common type field in the AST consistently.
414 void ForInStatement::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
415 for_in_type_ = static_cast<ForInType>(oracle->ForInType(this));
419 void Expression::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
420 to_boolean_types_ = oracle->ToBooleanTypes(test_id());
424 void Property::RecordTypeFeedback(TypeFeedbackOracle* oracle,
426 // Record type feedback from the oracle in the AST.
427 is_uninitialized_ = oracle->LoadIsUninitialized(this);
428 if (is_uninitialized_) return;
430 is_pre_monomorphic_ = oracle->LoadIsPreMonomorphic(this);
431 is_monomorphic_ = oracle->LoadIsMonomorphicNormal(this);
432 ASSERT(!is_pre_monomorphic_ || !is_monomorphic_);
433 receiver_types_.Clear();
434 if (key()->IsPropertyName()) {
435 FunctionPrototypeStub proto_stub(Code::LOAD_IC);
436 if (oracle->LoadIsStub(this, &proto_stub)) {
437 is_function_prototype_ = true;
439 Literal* lit_key = key()->AsLiteral();
440 ASSERT(lit_key != NULL && lit_key->value()->IsString());
441 Handle<String> name = Handle<String>::cast(lit_key->value());
442 oracle->LoadReceiverTypes(this, name, &receiver_types_);
444 } else if (oracle->LoadIsBuiltin(this, Builtins::kKeyedLoadIC_String)) {
445 is_string_access_ = true;
446 } else if (is_monomorphic_) {
447 receiver_types_.Add(oracle->LoadMonomorphicReceiverType(this), zone);
448 } else if (oracle->LoadIsPolymorphic(this)) {
449 receiver_types_.Reserve(kMaxKeyedPolymorphism, zone);
450 oracle->CollectKeyedReceiverTypes(PropertyFeedbackId(), &receiver_types_);
455 void Assignment::RecordTypeFeedback(TypeFeedbackOracle* oracle,
457 Property* prop = target()->AsProperty();
458 ASSERT(prop != NULL);
459 TypeFeedbackId id = AssignmentFeedbackId();
460 is_uninitialized_ = oracle->StoreIsUninitialized(id);
461 if (is_uninitialized_) return;
463 is_pre_monomorphic_ = oracle->StoreIsPreMonomorphic(id);
464 is_monomorphic_ = oracle->StoreIsMonomorphicNormal(id);
465 ASSERT(!is_pre_monomorphic_ || !is_monomorphic_);
466 receiver_types_.Clear();
467 if (prop->key()->IsPropertyName()) {
468 Literal* lit_key = prop->key()->AsLiteral();
469 ASSERT(lit_key != NULL && lit_key->value()->IsString());
470 Handle<String> name = Handle<String>::cast(lit_key->value());
471 oracle->StoreReceiverTypes(this, name, &receiver_types_);
472 } else if (is_monomorphic_) {
473 // Record receiver type for monomorphic keyed stores.
474 receiver_types_.Add(oracle->StoreMonomorphicReceiverType(id), zone);
475 store_mode_ = oracle->GetStoreMode(id);
476 } else if (oracle->StoreIsKeyedPolymorphic(id)) {
477 receiver_types_.Reserve(kMaxKeyedPolymorphism, zone);
478 oracle->CollectKeyedReceiverTypes(id, &receiver_types_);
479 store_mode_ = oracle->GetStoreMode(id);
484 void CountOperation::RecordTypeFeedback(TypeFeedbackOracle* oracle,
486 TypeFeedbackId id = CountStoreFeedbackId();
487 is_monomorphic_ = oracle->StoreIsMonomorphicNormal(id);
488 receiver_types_.Clear();
489 if (is_monomorphic_) {
490 // Record receiver type for monomorphic keyed stores.
492 oracle->StoreMonomorphicReceiverType(id), zone);
493 } else if (oracle->StoreIsKeyedPolymorphic(id)) {
494 receiver_types_.Reserve(kMaxKeyedPolymorphism, zone);
495 oracle->CollectKeyedReceiverTypes(id, &receiver_types_);
497 oracle->CollectPolymorphicStoreReceiverTypes(id, &receiver_types_);
499 store_mode_ = oracle->GetStoreMode(id);
500 type_ = oracle->IncrementType(this);
504 void CaseClause::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
505 compare_type_ = oracle->ClauseType(CompareId());
509 bool Call::ComputeTarget(Handle<Map> type, Handle<String> name) {
510 // If there is an interceptor, we can't compute the target for a direct call.
511 if (type->has_named_interceptor()) return false;
513 if (check_type_ == RECEIVER_MAP_CHECK) {
514 // For primitive checks the holder is set up to point to the corresponding
515 // prototype object, i.e. one step of the algorithm below has been already
516 // performed. For non-primitive checks we clear it to allow computing
517 // targets for polymorphic calls.
518 holder_ = Handle<JSObject>::null();
520 LookupResult lookup(type->GetIsolate());
522 // If a dictionary map is found in the prototype chain before the actual
523 // target, a new target can always appear. In that case, bail out.
524 // TODO(verwaest): Alternatively a runtime negative lookup on the normal
525 // receiver or prototype could be added.
526 if (type->is_dictionary_map()) return false;
527 type->LookupDescriptor(NULL, *name, &lookup);
528 if (lookup.IsFound()) {
529 switch (lookup.type()) {
531 // We surely know the target for a constant function.
532 Handle<Object> constant(lookup.GetConstantFromMap(*type),
534 if (constant->IsJSFunction()) {
535 target_ = Handle<JSFunction>::cast(constant);
545 // We don't know the target.
553 // If we reach the end of the prototype chain, we don't know the target.
554 if (!type->prototype()->IsJSObject()) return false;
555 // Go up the prototype chain, recording where we are currently.
556 holder_ = Handle<JSObject>(JSObject::cast(type->prototype()));
557 type = Handle<Map>(holder()->map());
562 bool Call::ComputeGlobalTarget(Handle<GlobalObject> global,
563 LookupResult* lookup) {
564 target_ = Handle<JSFunction>::null();
565 cell_ = Handle<Cell>::null();
566 ASSERT(lookup->IsFound() &&
567 lookup->type() == NORMAL &&
568 lookup->holder() == *global);
569 cell_ = Handle<Cell>(global->GetPropertyCell(lookup));
570 if (cell_->value()->IsJSFunction()) {
571 Handle<JSFunction> candidate(JSFunction::cast(cell_->value()));
572 // If the function is in new space we assume it's more likely to
573 // change and thus prefer the general IC code.
574 if (!lookup->isolate()->heap()->InNewSpace(*candidate)) {
583 Handle<JSObject> Call::GetPrototypeForPrimitiveCheck(
584 CheckType check, Isolate* isolate) {
585 v8::internal::Context* native_context = isolate->context()->native_context();
586 JSFunction* function = NULL;
588 case RECEIVER_MAP_CHECK:
592 function = native_context->string_function();
595 function = native_context->symbol_function();
598 function = native_context->number_function();
601 function = native_context->boolean_function();
604 ASSERT(function != NULL);
605 return Handle<JSObject>(JSObject::cast(function->instance_prototype()));
609 void Call::RecordTypeFeedback(TypeFeedbackOracle* oracle,
610 CallKind call_kind) {
611 is_monomorphic_ = oracle->CallIsMonomorphic(this);
612 Property* property = expression()->AsProperty();
613 if (property == NULL) {
614 // Function call. Specialize for monomorphic calls.
615 if (is_monomorphic_) target_ = oracle->GetCallTarget(this);
617 // Method call. Specialize for the receiver types seen at runtime.
618 Literal* key = property->key()->AsLiteral();
619 ASSERT(key != NULL && key->value()->IsString());
620 Handle<String> name = Handle<String>::cast(key->value());
621 check_type_ = oracle->GetCallCheckType(this);
622 receiver_types_.Clear();
623 if (check_type_ == RECEIVER_MAP_CHECK) {
624 oracle->CallReceiverTypes(this, name, call_kind, &receiver_types_);
625 is_monomorphic_ = is_monomorphic_ && receiver_types_.length() > 0;
627 holder_ = GetPrototypeForPrimitiveCheck(check_type_, oracle->isolate());
628 receiver_types_.Add(handle(holder_->map()), oracle->zone());
630 #ifdef ENABLE_SLOW_ASSERTS
631 if (FLAG_enable_slow_asserts) {
632 int length = receiver_types_.length();
633 for (int i = 0; i < length; i++) {
634 Handle<Map> map = receiver_types_.at(i);
635 ASSERT(!map.is_null() && *map != NULL);
639 if (is_monomorphic_) {
640 Handle<Map> map = receiver_types_.first();
641 is_monomorphic_ = ComputeTarget(map, name);
647 void CallNew::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
648 allocation_info_cell_ = oracle->GetCallNewAllocationInfoCell(this);
649 is_monomorphic_ = oracle->CallNewIsMonomorphic(this);
650 if (is_monomorphic_) {
651 target_ = oracle->GetCallNewTarget(this);
652 Object* value = allocation_info_cell_->value();
653 ASSERT(!value->IsTheHole());
654 if (value->IsAllocationSite()) {
655 AllocationSite* site = AllocationSite::cast(value);
656 elements_kind_ = site->GetElementsKind();
662 void ObjectLiteral::Property::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
663 receiver_type_ = oracle->ObjectLiteralStoreIsMonomorphic(this)
664 ? oracle->GetObjectLiteralStoreMap(this)
665 : Handle<Map>::null();
669 // ----------------------------------------------------------------------------
670 // Implementation of AstVisitor
672 void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) {
673 for (int i = 0; i < declarations->length(); i++) {
674 Visit(declarations->at(i));
679 void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) {
680 for (int i = 0; i < statements->length(); i++) {
681 Statement* stmt = statements->at(i);
683 if (stmt->IsJump()) break;
688 void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) {
689 for (int i = 0; i < expressions->length(); i++) {
690 // The variable statement visiting code may pass NULL expressions
691 // to this code. Maybe this should be handled by introducing an
692 // undefined expression or literal? Revisit this code if this
694 Expression* expression = expressions->at(i);
695 if (expression != NULL) Visit(expression);
700 // ----------------------------------------------------------------------------
701 // Regular expressions
703 #define MAKE_ACCEPT(Name) \
704 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \
705 return visitor->Visit##Name(this, data); \
707 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT)
710 #define MAKE_TYPE_CASE(Name) \
711 RegExp##Name* RegExpTree::As##Name() { \
714 bool RegExpTree::Is##Name() { return false; }
715 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
716 #undef MAKE_TYPE_CASE
718 #define MAKE_TYPE_CASE(Name) \
719 RegExp##Name* RegExp##Name::As##Name() { \
722 bool RegExp##Name::Is##Name() { return true; }
723 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
724 #undef MAKE_TYPE_CASE
727 static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) {
728 Interval result = Interval::Empty();
729 for (int i = 0; i < children->length(); i++)
730 result = result.Union(children->at(i)->CaptureRegisters());
735 Interval RegExpAlternative::CaptureRegisters() {
736 return ListCaptureRegisters(nodes());
740 Interval RegExpDisjunction::CaptureRegisters() {
741 return ListCaptureRegisters(alternatives());
745 Interval RegExpLookahead::CaptureRegisters() {
746 return body()->CaptureRegisters();
750 Interval RegExpCapture::CaptureRegisters() {
751 Interval self(StartRegister(index()), EndRegister(index()));
752 return self.Union(body()->CaptureRegisters());
756 Interval RegExpQuantifier::CaptureRegisters() {
757 return body()->CaptureRegisters();
761 bool RegExpAssertion::IsAnchoredAtStart() {
762 return assertion_type() == RegExpAssertion::START_OF_INPUT;
766 bool RegExpAssertion::IsAnchoredAtEnd() {
767 return assertion_type() == RegExpAssertion::END_OF_INPUT;
771 bool RegExpAlternative::IsAnchoredAtStart() {
772 ZoneList<RegExpTree*>* nodes = this->nodes();
773 for (int i = 0; i < nodes->length(); i++) {
774 RegExpTree* node = nodes->at(i);
775 if (node->IsAnchoredAtStart()) { return true; }
776 if (node->max_match() > 0) { return false; }
782 bool RegExpAlternative::IsAnchoredAtEnd() {
783 ZoneList<RegExpTree*>* nodes = this->nodes();
784 for (int i = nodes->length() - 1; i >= 0; i--) {
785 RegExpTree* node = nodes->at(i);
786 if (node->IsAnchoredAtEnd()) { return true; }
787 if (node->max_match() > 0) { return false; }
793 bool RegExpDisjunction::IsAnchoredAtStart() {
794 ZoneList<RegExpTree*>* alternatives = this->alternatives();
795 for (int i = 0; i < alternatives->length(); i++) {
796 if (!alternatives->at(i)->IsAnchoredAtStart())
803 bool RegExpDisjunction::IsAnchoredAtEnd() {
804 ZoneList<RegExpTree*>* alternatives = this->alternatives();
805 for (int i = 0; i < alternatives->length(); i++) {
806 if (!alternatives->at(i)->IsAnchoredAtEnd())
813 bool RegExpLookahead::IsAnchoredAtStart() {
814 return is_positive() && body()->IsAnchoredAtStart();
818 bool RegExpCapture::IsAnchoredAtStart() {
819 return body()->IsAnchoredAtStart();
823 bool RegExpCapture::IsAnchoredAtEnd() {
824 return body()->IsAnchoredAtEnd();
828 // Convert regular expression trees to a simple sexp representation.
829 // This representation should be different from the input grammar
830 // in as many cases as possible, to make it more difficult for incorrect
831 // parses to look as correct ones which is likely if the input and
832 // output formats are alike.
833 class RegExpUnparser V8_FINAL : public RegExpVisitor {
835 explicit RegExpUnparser(Zone* zone);
836 void VisitCharacterRange(CharacterRange that);
837 SmartArrayPointer<const char> ToString() { return stream_.ToCString(); }
838 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, \
839 void* data) V8_OVERRIDE;
840 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
843 StringStream* stream() { return &stream_; }
844 HeapStringAllocator alloc_;
845 StringStream stream_;
850 RegExpUnparser::RegExpUnparser(Zone* zone) : stream_(&alloc_), zone_(zone) {
854 void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) {
856 for (int i = 0; i < that->alternatives()->length(); i++) {
858 that->alternatives()->at(i)->Accept(this, data);
865 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
867 for (int i = 0; i < that->nodes()->length(); i++) {
869 that->nodes()->at(i)->Accept(this, data);
876 void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
877 stream()->Add("%k", that.from());
878 if (!that.IsSingleton()) {
879 stream()->Add("-%k", that.to());
885 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
887 if (that->is_negated())
890 for (int i = 0; i < that->ranges(zone_)->length(); i++) {
891 if (i > 0) stream()->Add(" ");
892 VisitCharacterRange(that->ranges(zone_)->at(i));
899 void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) {
900 switch (that->assertion_type()) {
901 case RegExpAssertion::START_OF_INPUT:
902 stream()->Add("@^i");
904 case RegExpAssertion::END_OF_INPUT:
905 stream()->Add("@$i");
907 case RegExpAssertion::START_OF_LINE:
908 stream()->Add("@^l");
910 case RegExpAssertion::END_OF_LINE:
911 stream()->Add("@$l");
913 case RegExpAssertion::BOUNDARY:
916 case RegExpAssertion::NON_BOUNDARY:
924 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
926 Vector<const uc16> chardata = that->data();
927 for (int i = 0; i < chardata.length(); i++) {
928 stream()->Add("%k", chardata[i]);
935 void* RegExpUnparser::VisitText(RegExpText* that, void* data) {
936 if (that->elements()->length() == 1) {
937 that->elements()->at(0).tree()->Accept(this, data);
940 for (int i = 0; i < that->elements()->length(); i++) {
942 that->elements()->at(i).tree()->Accept(this, data);
950 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) {
951 stream()->Add("(# %i ", that->min());
952 if (that->max() == RegExpTree::kInfinity) {
955 stream()->Add("%i ", that->max());
957 stream()->Add(that->is_greedy() ? "g " : that->is_possessive() ? "p " : "n ");
958 that->body()->Accept(this, data);
964 void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) {
965 stream()->Add("(^ ");
966 that->body()->Accept(this, data);
972 void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) {
973 stream()->Add("(-> ");
974 stream()->Add(that->is_positive() ? "+ " : "- ");
975 that->body()->Accept(this, data);
981 void* RegExpUnparser::VisitBackReference(RegExpBackReference* that,
983 stream()->Add("(<- %i)", that->index());
988 void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) {
994 SmartArrayPointer<const char> RegExpTree::ToString(Zone* zone) {
995 RegExpUnparser unparser(zone);
996 Accept(&unparser, NULL);
997 return unparser.ToString();
1001 RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives)
1002 : alternatives_(alternatives) {
1003 ASSERT(alternatives->length() > 1);
1004 RegExpTree* first_alternative = alternatives->at(0);
1005 min_match_ = first_alternative->min_match();
1006 max_match_ = first_alternative->max_match();
1007 for (int i = 1; i < alternatives->length(); i++) {
1008 RegExpTree* alternative = alternatives->at(i);
1009 min_match_ = Min(min_match_, alternative->min_match());
1010 max_match_ = Max(max_match_, alternative->max_match());
1015 static int IncreaseBy(int previous, int increase) {
1016 if (RegExpTree::kInfinity - previous < increase) {
1017 return RegExpTree::kInfinity;
1019 return previous + increase;
1023 RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes)
1025 ASSERT(nodes->length() > 1);
1028 for (int i = 0; i < nodes->length(); i++) {
1029 RegExpTree* node = nodes->at(i);
1030 int node_min_match = node->min_match();
1031 min_match_ = IncreaseBy(min_match_, node_min_match);
1032 int node_max_match = node->max_match();
1033 max_match_ = IncreaseBy(max_match_, node_max_match);
1038 CaseClause::CaseClause(Isolate* isolate,
1040 ZoneList<Statement*>* statements,
1044 statements_(statements),
1045 compare_type_(Type::None(), isolate),
1046 compare_id_(AstNode::GetNextId(isolate)),
1047 entry_id_(AstNode::GetNextId(isolate)) {
1051 #define REGULAR_NODE(NodeType) \
1052 void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1053 increase_node_count(); \
1055 #define DONT_OPTIMIZE_NODE(NodeType) \
1056 void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1057 increase_node_count(); \
1058 set_dont_optimize_reason(k##NodeType); \
1059 add_flag(kDontInline); \
1060 add_flag(kDontSelfOptimize); \
1062 #define DONT_SELFOPTIMIZE_NODE(NodeType) \
1063 void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1064 increase_node_count(); \
1065 add_flag(kDontSelfOptimize); \
1067 #define DONT_CACHE_NODE(NodeType) \
1068 void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1069 increase_node_count(); \
1070 set_dont_optimize_reason(k##NodeType); \
1071 add_flag(kDontInline); \
1072 add_flag(kDontSelfOptimize); \
1073 add_flag(kDontCache); \
1076 REGULAR_NODE(VariableDeclaration)
1077 REGULAR_NODE(FunctionDeclaration)
1079 REGULAR_NODE(ExpressionStatement)
1080 REGULAR_NODE(EmptyStatement)
1081 REGULAR_NODE(IfStatement)
1082 REGULAR_NODE(ContinueStatement)
1083 REGULAR_NODE(BreakStatement)
1084 REGULAR_NODE(ReturnStatement)
1085 REGULAR_NODE(SwitchStatement)
1086 REGULAR_NODE(CaseClause)
1087 REGULAR_NODE(Conditional)
1088 REGULAR_NODE(Literal)
1089 REGULAR_NODE(ArrayLiteral)
1090 REGULAR_NODE(ObjectLiteral)
1091 REGULAR_NODE(RegExpLiteral)
1092 REGULAR_NODE(FunctionLiteral)
1093 REGULAR_NODE(Assignment)
1095 REGULAR_NODE(Property)
1096 REGULAR_NODE(UnaryOperation)
1097 REGULAR_NODE(CountOperation)
1098 REGULAR_NODE(BinaryOperation)
1099 REGULAR_NODE(CompareOperation)
1100 REGULAR_NODE(ThisFunction)
1102 REGULAR_NODE(CallNew)
1103 // In theory, for VariableProxy we'd have to add:
1104 // if (node->var()->IsLookupSlot()) add_flag(kDontInline);
1105 // But node->var() is usually not bound yet at VariableProxy creation time, and
1106 // LOOKUP variables only result from constructs that cannot be inlined anyway.
1107 REGULAR_NODE(VariableProxy)
1109 // We currently do not optimize any modules.
1110 DONT_OPTIMIZE_NODE(ModuleDeclaration)
1111 DONT_OPTIMIZE_NODE(ImportDeclaration)
1112 DONT_OPTIMIZE_NODE(ExportDeclaration)
1113 DONT_OPTIMIZE_NODE(ModuleVariable)
1114 DONT_OPTIMIZE_NODE(ModulePath)
1115 DONT_OPTIMIZE_NODE(ModuleUrl)
1116 DONT_OPTIMIZE_NODE(ModuleStatement)
1117 DONT_OPTIMIZE_NODE(Yield)
1118 DONT_OPTIMIZE_NODE(WithStatement)
1119 DONT_OPTIMIZE_NODE(TryCatchStatement)
1120 DONT_OPTIMIZE_NODE(TryFinallyStatement)
1121 DONT_OPTIMIZE_NODE(DebuggerStatement)
1122 DONT_OPTIMIZE_NODE(NativeFunctionLiteral)
1124 DONT_SELFOPTIMIZE_NODE(DoWhileStatement)
1125 DONT_SELFOPTIMIZE_NODE(WhileStatement)
1126 DONT_SELFOPTIMIZE_NODE(ForStatement)
1127 DONT_SELFOPTIMIZE_NODE(ForInStatement)
1128 DONT_SELFOPTIMIZE_NODE(ForOfStatement)
1130 DONT_CACHE_NODE(ModuleLiteral)
1132 void AstConstructionVisitor::VisitCallRuntime(CallRuntime* node) {
1133 increase_node_count();
1134 if (node->is_jsruntime()) {
1135 // Don't try to inline JS runtime calls because we don't (currently) even
1137 add_flag(kDontInline);
1138 } else if (node->function()->intrinsic_type == Runtime::INLINE &&
1139 (node->name()->IsOneByteEqualTo(
1140 STATIC_ASCII_VECTOR("_ArgumentsLength")) ||
1141 node->name()->IsOneByteEqualTo(STATIC_ASCII_VECTOR("_Arguments")))) {
1142 // Don't inline the %_ArgumentsLength or %_Arguments because their
1143 // implementation will not work. There is no stack frame to get them
1145 add_flag(kDontInline);
1150 #undef DONT_OPTIMIZE_NODE
1151 #undef DONT_SELFOPTIMIZE_NODE
1152 #undef DONT_CACHE_NODE
1155 Handle<String> Literal::ToString() {
1156 if (value_->IsString()) return Handle<String>::cast(value_);
1157 ASSERT(value_->IsNumber());
1159 Vector<char> buffer(arr, ARRAY_SIZE(arr));
1161 if (value_->IsSmi()) {
1162 // Optimization only, the heap number case would subsume this.
1163 OS::SNPrintF(buffer, "%d", Smi::cast(*value_)->value());
1166 str = DoubleToCString(value_->Number(), buffer);
1168 return isolate_->factory()->NewStringFromAscii(CStrVector(str));
1172 } } // namespace v8::internal