1 // 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.
8 #include "src/assembler.h"
9 #include "src/ast-value-factory.h"
10 #include "src/bailout-reason.h"
11 #include "src/base/flags.h"
12 #include "src/base/smart-pointers.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
16 #include "src/modules.h"
17 #include "src/regexp/jsregexp.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/token.h"
21 #include "src/types.h"
22 #include "src/utils.h"
23 #include "src/variables.h"
28 // The abstract syntax tree is an intermediate, light-weight
29 // representation of the parsed JavaScript code suitable for
30 // compilation to native code.
32 // Nodes are allocated in a separate zone, which allows faster
33 // allocation and constant-time deallocation of the entire syntax
37 // ----------------------------------------------------------------------------
38 // Nodes of the abstract syntax tree. Only concrete classes are
41 #define DECLARATION_NODE_LIST(V) \
42 V(VariableDeclaration) \
43 V(FunctionDeclaration) \
44 V(ImportDeclaration) \
47 #define STATEMENT_NODE_LIST(V) \
49 V(ExpressionStatement) \
52 V(ContinueStatement) \
62 V(TryCatchStatement) \
63 V(TryFinallyStatement) \
66 #define EXPRESSION_NODE_LIST(V) \
69 V(NativeFunctionLiteral) \
89 V(SuperPropertyReference) \
90 V(SuperCallReference) \
93 #define AST_NODE_LIST(V) \
94 DECLARATION_NODE_LIST(V) \
95 STATEMENT_NODE_LIST(V) \
96 EXPRESSION_NODE_LIST(V)
98 // Forward declarations
103 class BreakableStatement;
105 class IterationStatement;
106 class MaterializedLiteral;
108 class TypeFeedbackOracle;
110 class RegExpAlternative;
111 class RegExpAssertion;
113 class RegExpBackReference;
115 class RegExpCharacterClass;
116 class RegExpCompiler;
117 class RegExpDisjunction;
119 class RegExpLookahead;
120 class RegExpQuantifier;
123 #define DEF_FORWARD_DECLARATION(type) class type;
124 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
125 #undef DEF_FORWARD_DECLARATION
128 // Typedef only introduced to avoid unreadable code.
129 typedef ZoneList<Handle<String>> ZoneStringList;
130 typedef ZoneList<Handle<Object>> ZoneObjectList;
133 #define DECLARE_NODE_TYPE(type) \
134 void Accept(AstVisitor* v) override; \
135 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
136 friend class AstNodeFactory;
139 class FeedbackVectorRequirements {
141 FeedbackVectorRequirements(int slots, int ic_slots)
142 : slots_(slots), ic_slots_(ic_slots) {}
144 int slots() const { return slots_; }
145 int ic_slots() const { return ic_slots_; }
155 explicit ICSlotCache(Zone* zone)
157 hash_map_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
158 ZoneAllocationPolicy(zone)) {}
160 void Put(Variable* variable, FeedbackVectorICSlot slot) {
161 ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
162 variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
163 entry->value = reinterpret_cast<void*>(slot.ToInt());
166 ZoneHashMap::Entry* Get(Variable* variable) const {
167 return hash_map_.Lookup(variable, ComputePointerHash(variable));
172 ZoneHashMap hash_map_;
176 class AstProperties final BASE_EMBEDDED {
180 kDontSelfOptimize = 1 << 0,
181 kDontCrankshaft = 1 << 1
184 typedef base::Flags<Flag> Flags;
186 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
188 Flags& flags() { return flags_; }
189 Flags flags() const { return flags_; }
190 int node_count() { return node_count_; }
191 void add_node_count(int count) { node_count_ += count; }
193 int slots() const { return spec_.slots(); }
194 void increase_slots(int count) { spec_.increase_slots(count); }
196 int ic_slots() const { return spec_.ic_slots(); }
197 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
198 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
199 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
204 ZoneFeedbackVectorSpec spec_;
207 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
210 class AstNode: public ZoneObject {
212 #define DECLARE_TYPE_ENUM(type) k##type,
214 AST_NODE_LIST(DECLARE_TYPE_ENUM)
217 #undef DECLARE_TYPE_ENUM
219 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
221 explicit AstNode(int position): position_(position) {}
222 virtual ~AstNode() {}
224 virtual void Accept(AstVisitor* v) = 0;
225 virtual NodeType node_type() const = 0;
226 int position() const { return position_; }
228 // Type testing & conversion functions overridden by concrete subclasses.
229 #define DECLARE_NODE_FUNCTIONS(type) \
230 bool Is##type() const { return node_type() == AstNode::k##type; } \
232 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
234 const type* As##type() const { \
235 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
237 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
238 #undef DECLARE_NODE_FUNCTIONS
240 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
241 virtual IterationStatement* AsIterationStatement() { return NULL; }
242 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
244 // The interface for feedback slots, with default no-op implementations for
245 // node types which don't actually have this. Note that this is conceptually
246 // not really nice, but multiple inheritance would introduce yet another
247 // vtable entry per node, something we don't want for space reasons.
248 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
249 Isolate* isolate, const ICSlotCache* cache) {
250 return FeedbackVectorRequirements(0, 0);
252 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
253 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
254 ICSlotCache* cache) {
257 // Each ICSlot stores a kind of IC which the participating node should know.
258 virtual Code::Kind FeedbackICSlotKind(int index) {
260 return Code::NUMBER_OF_KINDS;
264 // Hidden to prevent accidental usage. It would have to load the
265 // current zone from the TLS.
266 void* operator new(size_t size);
268 friend class CaseClause; // Generates AST IDs.
274 class Statement : public AstNode {
276 explicit Statement(Zone* zone, int position) : AstNode(position) {}
278 bool IsEmpty() { return AsEmptyStatement() != NULL; }
279 virtual bool IsJump() const { return false; }
283 class SmallMapList final {
286 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
288 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
289 void Clear() { list_.Clear(); }
290 void Sort() { list_.Sort(); }
292 bool is_empty() const { return list_.is_empty(); }
293 int length() const { return list_.length(); }
295 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
296 if (!Map::TryUpdate(map).ToHandle(&map)) return;
297 for (int i = 0; i < length(); ++i) {
298 if (at(i).is_identical_to(map)) return;
303 void FilterForPossibleTransitions(Map* root_map) {
304 for (int i = list_.length() - 1; i >= 0; i--) {
305 if (at(i)->FindRootMap() != root_map) {
306 list_.RemoveElement(list_.at(i));
311 void Add(Handle<Map> handle, Zone* zone) {
312 list_.Add(handle.location(), zone);
315 Handle<Map> at(int i) const {
316 return Handle<Map>(list_.at(i));
319 Handle<Map> first() const { return at(0); }
320 Handle<Map> last() const { return at(length() - 1); }
323 // The list stores pointers to Map*, that is Map**, so it's GC safe.
324 SmallPointerList<Map*> list_;
326 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
330 class Expression : public AstNode {
333 // Not assigned a context yet, or else will not be visited during
336 // Evaluated for its side effects.
338 // Evaluated for its value (and side effects).
340 // Evaluated for control flow (and side effects).
344 // True iff the expression is a valid reference expression.
345 virtual bool IsValidReferenceExpression() const { return false; }
347 // Helpers for ToBoolean conversion.
348 virtual bool ToBooleanIsTrue() const { return false; }
349 virtual bool ToBooleanIsFalse() const { return false; }
351 // Symbols that cannot be parsed as array indices are considered property
352 // names. We do not treat symbols that can be array indexes as property
353 // names because [] for string objects is handled only by keyed ICs.
354 virtual bool IsPropertyName() const { return false; }
356 // True iff the expression is a literal represented as a smi.
357 bool IsSmiLiteral() const;
359 // True iff the expression is a string literal.
360 bool IsStringLiteral() const;
362 // True iff the expression is the null literal.
363 bool IsNullLiteral() const;
365 // True if we can prove that the expression is the undefined literal.
366 bool IsUndefinedLiteral(Isolate* isolate) const;
368 // True iff the expression is a valid target for an assignment.
369 bool IsValidReferenceExpressionOrThis() const;
371 // Expression type bounds
372 Bounds bounds() const { return bounds_; }
373 void set_bounds(Bounds bounds) { bounds_ = bounds; }
375 // Type feedback information for assignments and properties.
376 virtual bool IsMonomorphic() {
380 virtual SmallMapList* GetReceiverTypes() {
384 virtual KeyedAccessStoreMode GetStoreMode() const {
386 return STANDARD_STORE;
388 virtual IcCheckType GetKeyType() const {
393 // TODO(rossberg): this should move to its own AST node eventually.
394 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
395 uint16_t to_boolean_types() const {
396 return ToBooleanTypesField::decode(bit_field_);
399 void set_base_id(int id) { base_id_ = id; }
400 static int num_ids() { return parent_num_ids() + 2; }
401 BailoutId id() const { return BailoutId(local_id(0)); }
402 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
405 Expression(Zone* zone, int pos)
407 base_id_(BailoutId::None().ToInt()),
408 bounds_(Bounds::Unbounded(zone)),
410 static int parent_num_ids() { return 0; }
411 void set_to_boolean_types(uint16_t types) {
412 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
415 int base_id() const {
416 DCHECK(!BailoutId(base_id_).IsNone());
421 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
425 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
427 // Ends with 16-bit field; deriving classes in turn begin with
428 // 16-bit fields for optimum packing efficiency.
432 class BreakableStatement : public Statement {
435 TARGET_FOR_ANONYMOUS,
436 TARGET_FOR_NAMED_ONLY
439 // The labels associated with this statement. May be NULL;
440 // if it is != NULL, guaranteed to contain at least one entry.
441 ZoneList<const AstRawString*>* labels() const { return labels_; }
443 // Type testing & conversion.
444 BreakableStatement* AsBreakableStatement() final { return this; }
447 Label* break_target() { return &break_target_; }
450 bool is_target_for_anonymous() const {
451 return breakable_type_ == TARGET_FOR_ANONYMOUS;
454 void set_base_id(int id) { base_id_ = id; }
455 static int num_ids() { return parent_num_ids() + 2; }
456 BailoutId EntryId() const { return BailoutId(local_id(0)); }
457 BailoutId ExitId() const { return BailoutId(local_id(1)); }
460 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
461 BreakableType breakable_type, int position)
462 : Statement(zone, position),
464 breakable_type_(breakable_type),
465 base_id_(BailoutId::None().ToInt()) {
466 DCHECK(labels == NULL || labels->length() > 0);
468 static int parent_num_ids() { return 0; }
470 int base_id() const {
471 DCHECK(!BailoutId(base_id_).IsNone());
476 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
478 ZoneList<const AstRawString*>* labels_;
479 BreakableType breakable_type_;
485 class Block final : public BreakableStatement {
487 DECLARE_NODE_TYPE(Block)
489 void AddStatement(Statement* statement, Zone* zone) {
490 statements_.Add(statement, zone);
493 ZoneList<Statement*>* statements() { return &statements_; }
494 bool ignore_completion_value() const { return ignore_completion_value_; }
496 static int num_ids() { return parent_num_ids() + 1; }
497 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
499 bool IsJump() const override {
500 return !statements_.is_empty() && statements_.last()->IsJump()
501 && labels() == NULL; // Good enough as an approximation...
504 Scope* scope() const { return scope_; }
505 void set_scope(Scope* scope) { scope_ = scope; }
508 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
509 bool ignore_completion_value, int pos)
510 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
511 statements_(capacity, zone),
512 ignore_completion_value_(ignore_completion_value),
514 static int parent_num_ids() { return BreakableStatement::num_ids(); }
517 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
519 ZoneList<Statement*> statements_;
520 bool ignore_completion_value_;
525 class Declaration : public AstNode {
527 VariableProxy* proxy() const { return proxy_; }
528 VariableMode mode() const { return mode_; }
529 Scope* scope() const { return scope_; }
530 virtual InitializationFlag initialization() const = 0;
531 virtual bool IsInlineable() const;
534 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
536 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
537 DCHECK(IsDeclaredVariableMode(mode));
542 VariableProxy* proxy_;
544 // Nested scope from which the declaration originated.
549 class VariableDeclaration final : public Declaration {
551 DECLARE_NODE_TYPE(VariableDeclaration)
553 InitializationFlag initialization() const override {
554 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
557 bool is_class_declaration() const { return is_class_declaration_; }
559 // VariableDeclarations can be grouped into consecutive declaration
560 // groups. Each VariableDeclaration is associated with the start position of
561 // the group it belongs to. The positions are used for strong mode scope
562 // checks for classes and functions.
563 int declaration_group_start() const { return declaration_group_start_; }
566 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
567 Scope* scope, int pos, bool is_class_declaration = false,
568 int declaration_group_start = -1)
569 : Declaration(zone, proxy, mode, scope, pos),
570 is_class_declaration_(is_class_declaration),
571 declaration_group_start_(declaration_group_start) {}
573 bool is_class_declaration_;
574 int declaration_group_start_;
578 class FunctionDeclaration final : public Declaration {
580 DECLARE_NODE_TYPE(FunctionDeclaration)
582 FunctionLiteral* fun() const { return fun_; }
583 InitializationFlag initialization() const override {
584 return kCreatedInitialized;
586 bool IsInlineable() const override;
589 FunctionDeclaration(Zone* zone,
590 VariableProxy* proxy,
592 FunctionLiteral* fun,
595 : Declaration(zone, proxy, mode, scope, pos),
597 DCHECK(mode == VAR || mode == LET || mode == CONST);
602 FunctionLiteral* fun_;
606 class ImportDeclaration final : public Declaration {
608 DECLARE_NODE_TYPE(ImportDeclaration)
610 const AstRawString* import_name() const { return import_name_; }
611 const AstRawString* module_specifier() const { return module_specifier_; }
612 void set_module_specifier(const AstRawString* module_specifier) {
613 DCHECK(module_specifier_ == NULL);
614 module_specifier_ = module_specifier;
616 InitializationFlag initialization() const override {
617 return kNeedsInitialization;
621 ImportDeclaration(Zone* zone, VariableProxy* proxy,
622 const AstRawString* import_name,
623 const AstRawString* module_specifier, Scope* scope, int pos)
624 : Declaration(zone, proxy, IMPORT, scope, pos),
625 import_name_(import_name),
626 module_specifier_(module_specifier) {}
629 const AstRawString* import_name_;
630 const AstRawString* module_specifier_;
634 class ExportDeclaration final : public Declaration {
636 DECLARE_NODE_TYPE(ExportDeclaration)
638 InitializationFlag initialization() const override {
639 return kCreatedInitialized;
643 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
644 : Declaration(zone, proxy, LET, scope, pos) {}
648 class Module : public AstNode {
650 ModuleDescriptor* descriptor() const { return descriptor_; }
651 Block* body() const { return body_; }
654 Module(Zone* zone, int pos)
655 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
656 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
657 : AstNode(pos), descriptor_(descriptor), body_(body) {}
660 ModuleDescriptor* descriptor_;
665 class IterationStatement : public BreakableStatement {
667 // Type testing & conversion.
668 IterationStatement* AsIterationStatement() final { return this; }
670 Statement* body() const { return body_; }
672 static int num_ids() { return parent_num_ids() + 1; }
673 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
674 virtual BailoutId ContinueId() const = 0;
675 virtual BailoutId StackCheckId() const = 0;
678 Label* continue_target() { return &continue_target_; }
681 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
682 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
684 static int parent_num_ids() { return BreakableStatement::num_ids(); }
685 void Initialize(Statement* body) { body_ = body; }
688 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
691 Label continue_target_;
695 class DoWhileStatement final : public IterationStatement {
697 DECLARE_NODE_TYPE(DoWhileStatement)
699 void Initialize(Expression* cond, Statement* body) {
700 IterationStatement::Initialize(body);
704 Expression* cond() const { return cond_; }
706 static int num_ids() { return parent_num_ids() + 2; }
707 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
708 BailoutId StackCheckId() const override { return BackEdgeId(); }
709 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
712 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
713 : IterationStatement(zone, labels, pos), cond_(NULL) {}
714 static int parent_num_ids() { return IterationStatement::num_ids(); }
717 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
723 class WhileStatement final : public IterationStatement {
725 DECLARE_NODE_TYPE(WhileStatement)
727 void Initialize(Expression* cond, Statement* body) {
728 IterationStatement::Initialize(body);
732 Expression* cond() const { return cond_; }
734 static int num_ids() { return parent_num_ids() + 1; }
735 BailoutId ContinueId() const override { return EntryId(); }
736 BailoutId StackCheckId() const override { return BodyId(); }
737 BailoutId BodyId() const { return BailoutId(local_id(0)); }
740 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
741 : IterationStatement(zone, labels, pos), cond_(NULL) {}
742 static int parent_num_ids() { return IterationStatement::num_ids(); }
745 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
751 class ForStatement final : public IterationStatement {
753 DECLARE_NODE_TYPE(ForStatement)
755 void Initialize(Statement* init,
759 IterationStatement::Initialize(body);
765 Statement* init() const { return init_; }
766 Expression* cond() const { return cond_; }
767 Statement* next() const { return next_; }
769 static int num_ids() { return parent_num_ids() + 2; }
770 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
771 BailoutId StackCheckId() const override { return BodyId(); }
772 BailoutId BodyId() const { return BailoutId(local_id(1)); }
775 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
776 : IterationStatement(zone, labels, pos),
780 static int parent_num_ids() { return IterationStatement::num_ids(); }
783 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
791 class ForEachStatement : public IterationStatement {
794 ENUMERATE, // for (each in subject) body;
795 ITERATE // for (each of subject) body;
798 void Initialize(Expression* each, Expression* subject, Statement* body) {
799 IterationStatement::Initialize(body);
804 Expression* each() const { return each_; }
805 Expression* subject() const { return subject_; }
807 FeedbackVectorRequirements ComputeFeedbackRequirements(
808 Isolate* isolate, const ICSlotCache* cache) override;
809 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
810 ICSlotCache* cache) override {
813 Code::Kind FeedbackICSlotKind(int index) override;
814 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
817 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
818 : IterationStatement(zone, labels, pos),
821 each_slot_(FeedbackVectorICSlot::Invalid()) {}
825 Expression* subject_;
826 FeedbackVectorICSlot each_slot_;
830 class ForInStatement final : public ForEachStatement {
832 DECLARE_NODE_TYPE(ForInStatement)
834 Expression* enumerable() const {
838 // Type feedback information.
839 FeedbackVectorRequirements ComputeFeedbackRequirements(
840 Isolate* isolate, const ICSlotCache* cache) override {
841 FeedbackVectorRequirements base =
842 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
843 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
844 return FeedbackVectorRequirements(1, base.ic_slots());
846 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
847 for_in_feedback_slot_ = slot;
850 FeedbackVectorSlot ForInFeedbackSlot() {
851 DCHECK(!for_in_feedback_slot_.IsInvalid());
852 return for_in_feedback_slot_;
855 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
856 ForInType for_in_type() const { return for_in_type_; }
857 void set_for_in_type(ForInType type) { for_in_type_ = type; }
859 static int num_ids() { return parent_num_ids() + 6; }
860 BailoutId BodyId() const { return BailoutId(local_id(0)); }
861 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
862 BailoutId EnumId() const { return BailoutId(local_id(2)); }
863 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
864 BailoutId FilterId() const { return BailoutId(local_id(4)); }
865 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
866 BailoutId ContinueId() const override { return EntryId(); }
867 BailoutId StackCheckId() const override { return BodyId(); }
870 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
871 : ForEachStatement(zone, labels, pos),
872 for_in_type_(SLOW_FOR_IN),
873 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
874 static int parent_num_ids() { return ForEachStatement::num_ids(); }
877 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
879 ForInType for_in_type_;
880 FeedbackVectorSlot for_in_feedback_slot_;
884 class ForOfStatement final : public ForEachStatement {
886 DECLARE_NODE_TYPE(ForOfStatement)
888 void Initialize(Expression* each,
891 Expression* assign_iterator,
892 Expression* next_result,
893 Expression* result_done,
894 Expression* assign_each) {
895 ForEachStatement::Initialize(each, subject, body);
896 assign_iterator_ = assign_iterator;
897 next_result_ = next_result;
898 result_done_ = result_done;
899 assign_each_ = assign_each;
902 Expression* iterable() const {
906 // iterator = subject[Symbol.iterator]()
907 Expression* assign_iterator() const {
908 return assign_iterator_;
911 // result = iterator.next() // with type check
912 Expression* next_result() const {
917 Expression* result_done() const {
921 // each = result.value
922 Expression* assign_each() const {
926 BailoutId ContinueId() const override { return EntryId(); }
927 BailoutId StackCheckId() const override { return BackEdgeId(); }
929 static int num_ids() { return parent_num_ids() + 1; }
930 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
933 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
934 : ForEachStatement(zone, labels, pos),
935 assign_iterator_(NULL),
938 assign_each_(NULL) {}
939 static int parent_num_ids() { return ForEachStatement::num_ids(); }
942 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
944 Expression* assign_iterator_;
945 Expression* next_result_;
946 Expression* result_done_;
947 Expression* assign_each_;
951 class ExpressionStatement final : public Statement {
953 DECLARE_NODE_TYPE(ExpressionStatement)
955 void set_expression(Expression* e) { expression_ = e; }
956 Expression* expression() const { return expression_; }
957 bool IsJump() const override { return expression_->IsThrow(); }
960 ExpressionStatement(Zone* zone, Expression* expression, int pos)
961 : Statement(zone, pos), expression_(expression) { }
964 Expression* expression_;
968 class JumpStatement : public Statement {
970 bool IsJump() const final { return true; }
973 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
977 class ContinueStatement final : public JumpStatement {
979 DECLARE_NODE_TYPE(ContinueStatement)
981 IterationStatement* target() const { return target_; }
984 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
985 : JumpStatement(zone, pos), target_(target) { }
988 IterationStatement* target_;
992 class BreakStatement final : public JumpStatement {
994 DECLARE_NODE_TYPE(BreakStatement)
996 BreakableStatement* target() const { return target_; }
999 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1000 : JumpStatement(zone, pos), target_(target) { }
1003 BreakableStatement* target_;
1007 class ReturnStatement final : public JumpStatement {
1009 DECLARE_NODE_TYPE(ReturnStatement)
1011 Expression* expression() const { return expression_; }
1014 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1015 : JumpStatement(zone, pos), expression_(expression) { }
1018 Expression* expression_;
1022 class WithStatement final : public Statement {
1024 DECLARE_NODE_TYPE(WithStatement)
1026 Scope* scope() { return scope_; }
1027 Expression* expression() const { return expression_; }
1028 Statement* statement() const { return statement_; }
1030 void set_base_id(int id) { base_id_ = id; }
1031 static int num_ids() { return parent_num_ids() + 1; }
1032 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1035 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1036 Statement* statement, int pos)
1037 : Statement(zone, pos),
1039 expression_(expression),
1040 statement_(statement),
1041 base_id_(BailoutId::None().ToInt()) {}
1042 static int parent_num_ids() { return 0; }
1044 int base_id() const {
1045 DCHECK(!BailoutId(base_id_).IsNone());
1050 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1053 Expression* expression_;
1054 Statement* statement_;
1059 class CaseClause final : public Expression {
1061 DECLARE_NODE_TYPE(CaseClause)
1063 bool is_default() const { return label_ == NULL; }
1064 Expression* label() const {
1065 CHECK(!is_default());
1068 Label* body_target() { return &body_target_; }
1069 ZoneList<Statement*>* statements() const { return statements_; }
1071 static int num_ids() { return parent_num_ids() + 2; }
1072 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1073 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1075 Type* compare_type() { return compare_type_; }
1076 void set_compare_type(Type* type) { compare_type_ = type; }
1079 static int parent_num_ids() { return Expression::num_ids(); }
1082 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1084 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1088 ZoneList<Statement*>* statements_;
1089 Type* compare_type_;
1093 class SwitchStatement final : public BreakableStatement {
1095 DECLARE_NODE_TYPE(SwitchStatement)
1097 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1102 Expression* tag() const { return tag_; }
1103 ZoneList<CaseClause*>* cases() const { return cases_; }
1106 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1107 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1113 ZoneList<CaseClause*>* cases_;
1117 // If-statements always have non-null references to their then- and
1118 // else-parts. When parsing if-statements with no explicit else-part,
1119 // the parser implicitly creates an empty statement. Use the
1120 // HasThenStatement() and HasElseStatement() functions to check if a
1121 // given if-statement has a then- or an else-part containing code.
1122 class IfStatement final : public Statement {
1124 DECLARE_NODE_TYPE(IfStatement)
1126 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1127 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1129 Expression* condition() const { return condition_; }
1130 Statement* then_statement() const { return then_statement_; }
1131 Statement* else_statement() const { return else_statement_; }
1133 bool IsJump() const override {
1134 return HasThenStatement() && then_statement()->IsJump()
1135 && HasElseStatement() && else_statement()->IsJump();
1138 void set_base_id(int id) { base_id_ = id; }
1139 static int num_ids() { return parent_num_ids() + 3; }
1140 BailoutId IfId() const { return BailoutId(local_id(0)); }
1141 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1142 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1145 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1146 Statement* else_statement, int pos)
1147 : Statement(zone, pos),
1148 condition_(condition),
1149 then_statement_(then_statement),
1150 else_statement_(else_statement),
1151 base_id_(BailoutId::None().ToInt()) {}
1152 static int parent_num_ids() { return 0; }
1154 int base_id() const {
1155 DCHECK(!BailoutId(base_id_).IsNone());
1160 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1162 Expression* condition_;
1163 Statement* then_statement_;
1164 Statement* else_statement_;
1169 class TryStatement : public Statement {
1171 Block* try_block() const { return try_block_; }
1173 void set_base_id(int id) { base_id_ = id; }
1174 static int num_ids() { return parent_num_ids() + 1; }
1175 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1178 TryStatement(Zone* zone, Block* try_block, int pos)
1179 : Statement(zone, pos),
1180 try_block_(try_block),
1181 base_id_(BailoutId::None().ToInt()) {}
1182 static int parent_num_ids() { return 0; }
1184 int base_id() const {
1185 DCHECK(!BailoutId(base_id_).IsNone());
1190 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1197 class TryCatchStatement final : public TryStatement {
1199 DECLARE_NODE_TYPE(TryCatchStatement)
1201 Scope* scope() { return scope_; }
1202 Variable* variable() { return variable_; }
1203 Block* catch_block() const { return catch_block_; }
1206 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1207 Variable* variable, Block* catch_block, int pos)
1208 : TryStatement(zone, try_block, pos),
1210 variable_(variable),
1211 catch_block_(catch_block) {}
1215 Variable* variable_;
1216 Block* catch_block_;
1220 class TryFinallyStatement final : public TryStatement {
1222 DECLARE_NODE_TYPE(TryFinallyStatement)
1224 Block* finally_block() const { return finally_block_; }
1227 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1229 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1232 Block* finally_block_;
1236 class DebuggerStatement final : public Statement {
1238 DECLARE_NODE_TYPE(DebuggerStatement)
1240 void set_base_id(int id) { base_id_ = id; }
1241 static int num_ids() { return parent_num_ids() + 1; }
1242 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1245 explicit DebuggerStatement(Zone* zone, int pos)
1246 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1247 static int parent_num_ids() { return 0; }
1249 int base_id() const {
1250 DCHECK(!BailoutId(base_id_).IsNone());
1255 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1261 class EmptyStatement final : public Statement {
1263 DECLARE_NODE_TYPE(EmptyStatement)
1266 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1270 class Literal final : public Expression {
1272 DECLARE_NODE_TYPE(Literal)
1274 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1276 Handle<String> AsPropertyName() {
1277 DCHECK(IsPropertyName());
1278 return Handle<String>::cast(value());
1281 const AstRawString* AsRawPropertyName() {
1282 DCHECK(IsPropertyName());
1283 return value_->AsString();
1286 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1287 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1289 Handle<Object> value() const { return value_->value(); }
1290 const AstValue* raw_value() const { return value_; }
1292 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1293 // only for string and number literals!
1295 static bool Match(void* literal1, void* literal2);
1297 static int num_ids() { return parent_num_ids() + 1; }
1298 TypeFeedbackId LiteralFeedbackId() const {
1299 return TypeFeedbackId(local_id(0));
1303 Literal(Zone* zone, const AstValue* value, int position)
1304 : Expression(zone, position), value_(value) {}
1305 static int parent_num_ids() { return Expression::num_ids(); }
1308 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1310 const AstValue* value_;
1314 class AstLiteralReindexer;
1316 // Base class for literals that needs space in the corresponding JSFunction.
1317 class MaterializedLiteral : public Expression {
1319 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1321 int literal_index() { return literal_index_; }
1324 // only callable after initialization.
1325 DCHECK(depth_ >= 1);
1329 bool is_strong() const { return is_strong_; }
1332 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1333 : Expression(zone, pos),
1334 literal_index_(literal_index),
1336 is_strong_(is_strong),
1339 // A materialized literal is simple if the values consist of only
1340 // constants and simple object and array literals.
1341 bool is_simple() const { return is_simple_; }
1342 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1343 friend class CompileTimeValue;
1345 void set_depth(int depth) {
1350 // Populate the constant properties/elements fixed array.
1351 void BuildConstants(Isolate* isolate);
1352 friend class ArrayLiteral;
1353 friend class ObjectLiteral;
1355 // If the expression is a literal, return the literal value;
1356 // if the expression is a materialized literal and is simple return a
1357 // compile time value as encoded by CompileTimeValue::GetValue().
1358 // Otherwise, return undefined literal as the placeholder
1359 // in the object literal boilerplate.
1360 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1368 friend class AstLiteralReindexer;
1372 // Property is used for passing information
1373 // about an object literal's properties from the parser
1374 // to the code generator.
1375 class ObjectLiteralProperty final : public ZoneObject {
1378 CONSTANT, // Property with constant value (compile time).
1379 COMPUTED, // Property with computed value (execution time).
1380 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1381 GETTER, SETTER, // Property is an accessor function.
1382 PROTOTYPE // Property is __proto__.
1385 Expression* key() { return key_; }
1386 Expression* value() { return value_; }
1387 Kind kind() { return kind_; }
1389 // Type feedback information.
1390 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1391 Handle<Map> GetReceiverType() { return receiver_type_; }
1393 bool IsCompileTimeValue();
1395 void set_emit_store(bool emit_store);
1398 bool is_static() const { return is_static_; }
1399 bool is_computed_name() const { return is_computed_name_; }
1401 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1404 friend class AstNodeFactory;
1406 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1407 bool is_static, bool is_computed_name);
1408 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1409 Expression* value, bool is_static,
1410 bool is_computed_name);
1418 bool is_computed_name_;
1419 Handle<Map> receiver_type_;
1423 // An object literal has a boilerplate object that is used
1424 // for minimizing the work when constructing it at runtime.
1425 class ObjectLiteral final : public MaterializedLiteral {
1427 typedef ObjectLiteralProperty Property;
1429 DECLARE_NODE_TYPE(ObjectLiteral)
1431 Handle<FixedArray> constant_properties() const {
1432 return constant_properties_;
1434 int properties_count() const { return constant_properties_->length() / 2; }
1435 ZoneList<Property*>* properties() const { return properties_; }
1436 bool fast_elements() const { return fast_elements_; }
1437 bool may_store_doubles() const { return may_store_doubles_; }
1438 bool has_function() const { return has_function_; }
1439 bool has_elements() const { return has_elements_; }
1441 // Decide if a property should be in the object boilerplate.
1442 static bool IsBoilerplateProperty(Property* property);
1444 // Populate the constant properties fixed array.
1445 void BuildConstantProperties(Isolate* isolate);
1447 // Mark all computed expressions that are bound to a key that
1448 // is shadowed by a later occurrence of the same key. For the
1449 // marked expressions, no store code is emitted.
1450 void CalculateEmitStore(Zone* zone);
1452 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1453 int ComputeFlags(bool disable_mementos = false) const {
1454 int flags = fast_elements() ? kFastElements : kNoFlags;
1455 flags |= has_function() ? kHasFunction : kNoFlags;
1456 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1457 flags |= kShallowProperties;
1459 if (disable_mementos) {
1460 flags |= kDisableMementos;
1471 kHasFunction = 1 << 1,
1472 kShallowProperties = 1 << 2,
1473 kDisableMementos = 1 << 3,
1477 struct Accessors: public ZoneObject {
1478 Accessors() : getter(NULL), setter(NULL) {}
1483 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1485 // Return an AST id for a property that is used in simulate instructions.
1486 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1488 // Unlike other AST nodes, this number of bailout IDs allocated for an
1489 // ObjectLiteral can vary, so num_ids() is not a static method.
1490 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1492 // Object literals need one feedback slot for each non-trivial value, as well
1493 // as some slots for home objects.
1494 FeedbackVectorRequirements ComputeFeedbackRequirements(
1495 Isolate* isolate, const ICSlotCache* cache) override;
1496 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1497 ICSlotCache* cache) override {
1500 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1501 FeedbackVectorICSlot GetNthSlot(int n) const {
1502 return FeedbackVectorICSlot(slot_.ToInt() + n);
1505 // If value needs a home object, returns a valid feedback vector ic slot
1506 // given by slot_index, and increments slot_index.
1507 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1508 int* slot_index) const;
1511 int slot_count() const { return slot_count_; }
1515 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1516 int boilerplate_properties, bool has_function, bool is_strong,
1518 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1519 properties_(properties),
1520 boilerplate_properties_(boilerplate_properties),
1521 fast_elements_(false),
1522 has_elements_(false),
1523 may_store_doubles_(false),
1524 has_function_(has_function),
1528 slot_(FeedbackVectorICSlot::Invalid()) {
1530 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1533 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1534 Handle<FixedArray> constant_properties_;
1535 ZoneList<Property*>* properties_;
1536 int boilerplate_properties_;
1537 bool fast_elements_;
1539 bool may_store_doubles_;
1542 // slot_count_ helps validate that the logic to allocate ic slots and the
1543 // logic to use them are in sync.
1546 FeedbackVectorICSlot slot_;
1550 // Node for capturing a regexp literal.
1551 class RegExpLiteral final : public MaterializedLiteral {
1553 DECLARE_NODE_TYPE(RegExpLiteral)
1555 Handle<String> pattern() const { return pattern_->string(); }
1556 Handle<String> flags() const { return flags_->string(); }
1559 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1560 const AstRawString* flags, int literal_index, bool is_strong,
1562 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1569 const AstRawString* pattern_;
1570 const AstRawString* flags_;
1574 // An array literal has a literals object that is used
1575 // for minimizing the work when constructing it at runtime.
1576 class ArrayLiteral final : public MaterializedLiteral {
1578 DECLARE_NODE_TYPE(ArrayLiteral)
1580 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1581 ElementsKind constant_elements_kind() const {
1582 DCHECK_EQ(2, constant_elements_->length());
1583 return static_cast<ElementsKind>(
1584 Smi::cast(constant_elements_->get(0))->value());
1587 ZoneList<Expression*>* values() const { return values_; }
1589 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1591 // Return an AST id for an element that is used in simulate instructions.
1592 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1594 // Unlike other AST nodes, this number of bailout IDs allocated for an
1595 // ArrayLiteral can vary, so num_ids() is not a static method.
1596 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1598 // Populate the constant elements fixed array.
1599 void BuildConstantElements(Isolate* isolate);
1601 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1602 int ComputeFlags(bool disable_mementos = false) const {
1603 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1604 if (disable_mementos) {
1605 flags |= kDisableMementos;
1615 kShallowElements = 1,
1616 kDisableMementos = 1 << 1,
1621 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1622 int first_spread_index, int literal_index, bool is_strong,
1624 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1626 first_spread_index_(first_spread_index) {}
1627 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1630 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1632 Handle<FixedArray> constant_elements_;
1633 ZoneList<Expression*>* values_;
1634 int first_spread_index_;
1638 class VariableProxy final : public Expression {
1640 DECLARE_NODE_TYPE(VariableProxy)
1642 bool IsValidReferenceExpression() const override {
1643 return !is_this() && !is_new_target();
1646 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1648 Handle<String> name() const { return raw_name()->string(); }
1649 const AstRawString* raw_name() const {
1650 return is_resolved() ? var_->raw_name() : raw_name_;
1653 Variable* var() const {
1654 DCHECK(is_resolved());
1657 void set_var(Variable* v) {
1658 DCHECK(!is_resolved());
1663 bool is_this() const { return IsThisField::decode(bit_field_); }
1665 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1666 void set_is_assigned() {
1667 bit_field_ = IsAssignedField::update(bit_field_, true);
1670 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1671 void set_is_resolved() {
1672 bit_field_ = IsResolvedField::update(bit_field_, true);
1675 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1676 void set_is_new_target() {
1677 bit_field_ = IsNewTargetField::update(bit_field_, true);
1680 int end_position() const { return end_position_; }
1682 // Bind this proxy to the variable var.
1683 void BindTo(Variable* var);
1685 bool UsesVariableFeedbackSlot() const {
1686 return var()->IsUnallocated() || var()->IsLookupSlot();
1689 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1690 Isolate* isolate, const ICSlotCache* cache) override;
1692 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1693 ICSlotCache* cache) override;
1694 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1695 FeedbackVectorICSlot VariableFeedbackSlot() {
1696 return variable_feedback_slot_;
1699 static int num_ids() { return parent_num_ids() + 1; }
1700 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1703 VariableProxy(Zone* zone, Variable* var, int start_position,
1706 VariableProxy(Zone* zone, const AstRawString* name,
1707 Variable::Kind variable_kind, int start_position,
1709 static int parent_num_ids() { return Expression::num_ids(); }
1710 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1712 class IsThisField : public BitField8<bool, 0, 1> {};
1713 class IsAssignedField : public BitField8<bool, 1, 1> {};
1714 class IsResolvedField : public BitField8<bool, 2, 1> {};
1715 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1717 // Start with 16-bit (or smaller) field, which should get packed together
1718 // with Expression's trailing 16-bit field.
1720 FeedbackVectorICSlot variable_feedback_slot_;
1722 const AstRawString* raw_name_; // if !is_resolved_
1723 Variable* var_; // if is_resolved_
1725 // Position is stored in the AstNode superclass, but VariableProxy needs to
1726 // know its end position too (for error messages). It cannot be inferred from
1727 // the variable name length because it can contain escapes.
1732 // Left-hand side can only be a property, a global or a (parameter or local)
1738 NAMED_SUPER_PROPERTY,
1739 KEYED_SUPER_PROPERTY
1743 class Property final : public Expression {
1745 DECLARE_NODE_TYPE(Property)
1747 bool IsValidReferenceExpression() const override { return true; }
1749 Expression* obj() const { return obj_; }
1750 Expression* key() const { return key_; }
1752 static int num_ids() { return parent_num_ids() + 1; }
1753 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1755 bool IsStringAccess() const {
1756 return IsStringAccessField::decode(bit_field_);
1759 // Type feedback information.
1760 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1761 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1762 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1763 IcCheckType GetKeyType() const override {
1764 return KeyTypeField::decode(bit_field_);
1766 bool IsUninitialized() const {
1767 return !is_for_call() && HasNoTypeInformation();
1769 bool HasNoTypeInformation() const {
1770 return GetInlineCacheState() == UNINITIALIZED;
1772 InlineCacheState GetInlineCacheState() const {
1773 return InlineCacheStateField::decode(bit_field_);
1775 void set_is_string_access(bool b) {
1776 bit_field_ = IsStringAccessField::update(bit_field_, b);
1778 void set_key_type(IcCheckType key_type) {
1779 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1781 void set_inline_cache_state(InlineCacheState state) {
1782 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1784 void mark_for_call() {
1785 bit_field_ = IsForCallField::update(bit_field_, true);
1787 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1789 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1791 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1792 Isolate* isolate, const ICSlotCache* cache) override {
1793 return FeedbackVectorRequirements(0, 1);
1795 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1796 ICSlotCache* cache) override {
1797 property_feedback_slot_ = slot;
1799 Code::Kind FeedbackICSlotKind(int index) override {
1800 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1803 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1804 return property_feedback_slot_;
1807 static LhsKind GetAssignType(Property* property) {
1808 if (property == NULL) return VARIABLE;
1809 bool super_access = property->IsSuperAccess();
1810 return (property->key()->IsPropertyName())
1811 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1812 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1816 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1817 : Expression(zone, pos),
1818 bit_field_(IsForCallField::encode(false) |
1819 IsStringAccessField::encode(false) |
1820 InlineCacheStateField::encode(UNINITIALIZED)),
1821 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1824 static int parent_num_ids() { return Expression::num_ids(); }
1827 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1829 class IsForCallField : public BitField8<bool, 0, 1> {};
1830 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1831 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1832 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1834 FeedbackVectorICSlot property_feedback_slot_;
1837 SmallMapList receiver_types_;
1841 class Call final : public Expression {
1843 DECLARE_NODE_TYPE(Call)
1845 Expression* expression() const { return expression_; }
1846 ZoneList<Expression*>* arguments() const { return arguments_; }
1848 // Type feedback information.
1849 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1850 Isolate* isolate, const ICSlotCache* cache) override;
1851 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1852 ICSlotCache* cache) override {
1855 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1856 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1858 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1860 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1862 SmallMapList* GetReceiverTypes() override {
1863 if (expression()->IsProperty()) {
1864 return expression()->AsProperty()->GetReceiverTypes();
1869 bool IsMonomorphic() override {
1870 if (expression()->IsProperty()) {
1871 return expression()->AsProperty()->IsMonomorphic();
1873 return !target_.is_null();
1876 bool global_call() const {
1877 VariableProxy* proxy = expression_->AsVariableProxy();
1878 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1881 bool known_global_function() const {
1882 return global_call() && !target_.is_null();
1885 Handle<JSFunction> target() { return target_; }
1887 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1889 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1891 set_is_uninitialized(false);
1893 void set_target(Handle<JSFunction> target) { target_ = target; }
1894 void set_allocation_site(Handle<AllocationSite> site) {
1895 allocation_site_ = site;
1898 static int num_ids() { return parent_num_ids() + 3; }
1899 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1900 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1901 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1903 bool is_uninitialized() const {
1904 return IsUninitializedField::decode(bit_field_);
1906 void set_is_uninitialized(bool b) {
1907 bit_field_ = IsUninitializedField::update(bit_field_, b);
1919 // Helpers to determine how to handle the call.
1920 CallType GetCallType(Isolate* isolate) const;
1921 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1922 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1925 // Used to assert that the FullCodeGenerator records the return site.
1926 bool return_is_recorded_;
1930 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1932 : Expression(zone, pos),
1933 ic_slot_(FeedbackVectorICSlot::Invalid()),
1934 slot_(FeedbackVectorSlot::Invalid()),
1935 expression_(expression),
1936 arguments_(arguments),
1937 bit_field_(IsUninitializedField::encode(false)) {
1938 if (expression->IsProperty()) {
1939 expression->AsProperty()->mark_for_call();
1942 static int parent_num_ids() { return Expression::num_ids(); }
1945 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1947 FeedbackVectorICSlot ic_slot_;
1948 FeedbackVectorSlot slot_;
1949 Expression* expression_;
1950 ZoneList<Expression*>* arguments_;
1951 Handle<JSFunction> target_;
1952 Handle<AllocationSite> allocation_site_;
1953 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1958 class CallNew final : public Expression {
1960 DECLARE_NODE_TYPE(CallNew)
1962 Expression* expression() const { return expression_; }
1963 ZoneList<Expression*>* arguments() const { return arguments_; }
1965 // Type feedback information.
1966 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1967 Isolate* isolate, const ICSlotCache* cache) override {
1968 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1970 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1971 callnew_feedback_slot_ = slot;
1974 FeedbackVectorSlot CallNewFeedbackSlot() {
1975 DCHECK(!callnew_feedback_slot_.IsInvalid());
1976 return callnew_feedback_slot_;
1978 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1979 DCHECK(FLAG_pretenuring_call_new);
1980 return CallNewFeedbackSlot().next();
1983 bool IsMonomorphic() override { return is_monomorphic_; }
1984 Handle<JSFunction> target() const { return target_; }
1985 Handle<AllocationSite> allocation_site() const {
1986 return allocation_site_;
1989 static int num_ids() { return parent_num_ids() + 1; }
1990 static int feedback_slots() { return 1; }
1991 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1993 void set_allocation_site(Handle<AllocationSite> site) {
1994 allocation_site_ = site;
1996 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1997 void set_target(Handle<JSFunction> target) { target_ = target; }
1998 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2000 is_monomorphic_ = true;
2004 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2006 : Expression(zone, pos),
2007 expression_(expression),
2008 arguments_(arguments),
2009 is_monomorphic_(false),
2010 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2012 static int parent_num_ids() { return Expression::num_ids(); }
2015 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2017 Expression* expression_;
2018 ZoneList<Expression*>* arguments_;
2019 bool is_monomorphic_;
2020 Handle<JSFunction> target_;
2021 Handle<AllocationSite> allocation_site_;
2022 FeedbackVectorSlot callnew_feedback_slot_;
2026 // The CallRuntime class does not represent any official JavaScript
2027 // language construct. Instead it is used to call a C or JS function
2028 // with a set of arguments. This is used from the builtins that are
2029 // implemented in JavaScript (see "v8natives.js").
2030 class CallRuntime final : public Expression {
2032 DECLARE_NODE_TYPE(CallRuntime)
2034 Handle<String> name() const { return raw_name_->string(); }
2035 const AstRawString* raw_name() const { return raw_name_; }
2036 const Runtime::Function* function() const { return function_; }
2037 ZoneList<Expression*>* arguments() const { return arguments_; }
2038 bool is_jsruntime() const { return function_ == NULL; }
2040 // Type feedback information.
2041 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2042 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2043 Isolate* isolate, const ICSlotCache* cache) override {
2044 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2046 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2047 ICSlotCache* cache) override {
2048 callruntime_feedback_slot_ = slot;
2050 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2052 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2053 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2054 !callruntime_feedback_slot_.IsInvalid());
2055 return callruntime_feedback_slot_;
2058 static int num_ids() { return parent_num_ids() + 1; }
2059 BailoutId CallId() { return BailoutId(local_id(0)); }
2062 CallRuntime(Zone* zone, const AstRawString* name,
2063 const Runtime::Function* function,
2064 ZoneList<Expression*>* arguments, int pos)
2065 : Expression(zone, pos),
2067 function_(function),
2068 arguments_(arguments),
2069 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2070 static int parent_num_ids() { return Expression::num_ids(); }
2073 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2075 const AstRawString* raw_name_;
2076 const Runtime::Function* function_;
2077 ZoneList<Expression*>* arguments_;
2078 FeedbackVectorICSlot callruntime_feedback_slot_;
2082 class UnaryOperation final : public Expression {
2084 DECLARE_NODE_TYPE(UnaryOperation)
2086 Token::Value op() const { return op_; }
2087 Expression* expression() const { return expression_; }
2089 // For unary not (Token::NOT), the AST ids where true and false will
2090 // actually be materialized, respectively.
2091 static int num_ids() { return parent_num_ids() + 2; }
2092 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2093 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2095 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2098 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2099 : Expression(zone, pos), op_(op), expression_(expression) {
2100 DCHECK(Token::IsUnaryOp(op));
2102 static int parent_num_ids() { return Expression::num_ids(); }
2105 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2108 Expression* expression_;
2112 class BinaryOperation final : public Expression {
2114 DECLARE_NODE_TYPE(BinaryOperation)
2116 Token::Value op() const { return static_cast<Token::Value>(op_); }
2117 Expression* left() const { return left_; }
2118 Expression* right() const { return right_; }
2119 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2120 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2121 allocation_site_ = allocation_site;
2124 // The short-circuit logical operations need an AST ID for their
2125 // right-hand subexpression.
2126 static int num_ids() { return parent_num_ids() + 2; }
2127 BailoutId RightId() const { return BailoutId(local_id(0)); }
2129 TypeFeedbackId BinaryOperationFeedbackId() const {
2130 return TypeFeedbackId(local_id(1));
2132 Maybe<int> fixed_right_arg() const {
2133 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2135 void set_fixed_right_arg(Maybe<int> arg) {
2136 has_fixed_right_arg_ = arg.IsJust();
2137 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2140 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2143 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2144 Expression* right, int pos)
2145 : Expression(zone, pos),
2146 op_(static_cast<byte>(op)),
2147 has_fixed_right_arg_(false),
2148 fixed_right_arg_value_(0),
2151 DCHECK(Token::IsBinaryOp(op));
2153 static int parent_num_ids() { return Expression::num_ids(); }
2156 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2158 const byte op_; // actually Token::Value
2159 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2160 // type for the RHS. Currenty it's actually a Maybe<int>
2161 bool has_fixed_right_arg_;
2162 int fixed_right_arg_value_;
2165 Handle<AllocationSite> allocation_site_;
2169 class CountOperation final : public Expression {
2171 DECLARE_NODE_TYPE(CountOperation)
2173 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2174 bool is_postfix() const { return !is_prefix(); }
2176 Token::Value op() const { return TokenField::decode(bit_field_); }
2177 Token::Value binary_op() {
2178 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2181 Expression* expression() const { return expression_; }
2183 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2184 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2185 IcCheckType GetKeyType() const override {
2186 return KeyTypeField::decode(bit_field_);
2188 KeyedAccessStoreMode GetStoreMode() const override {
2189 return StoreModeField::decode(bit_field_);
2191 Type* type() const { return type_; }
2192 void set_key_type(IcCheckType type) {
2193 bit_field_ = KeyTypeField::update(bit_field_, type);
2195 void set_store_mode(KeyedAccessStoreMode mode) {
2196 bit_field_ = StoreModeField::update(bit_field_, mode);
2198 void set_type(Type* type) { type_ = type; }
2200 static int num_ids() { return parent_num_ids() + 4; }
2201 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2202 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2203 TypeFeedbackId CountBinOpFeedbackId() const {
2204 return TypeFeedbackId(local_id(2));
2206 TypeFeedbackId CountStoreFeedbackId() const {
2207 return TypeFeedbackId(local_id(3));
2210 FeedbackVectorRequirements ComputeFeedbackRequirements(
2211 Isolate* isolate, const ICSlotCache* cache) override;
2212 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2213 ICSlotCache* cache) override {
2216 Code::Kind FeedbackICSlotKind(int index) override;
2217 FeedbackVectorICSlot CountSlot() const { return slot_; }
2220 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2222 : Expression(zone, pos),
2224 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2225 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2228 slot_(FeedbackVectorICSlot::Invalid()) {}
2229 static int parent_num_ids() { return Expression::num_ids(); }
2232 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2234 class IsPrefixField : public BitField16<bool, 0, 1> {};
2235 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2236 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2237 class TokenField : public BitField16<Token::Value, 6, 8> {};
2239 // Starts with 16-bit field, which should get packed together with
2240 // Expression's trailing 16-bit field.
2241 uint16_t bit_field_;
2243 Expression* expression_;
2244 SmallMapList receiver_types_;
2245 FeedbackVectorICSlot slot_;
2249 class CompareOperation final : public Expression {
2251 DECLARE_NODE_TYPE(CompareOperation)
2253 Token::Value op() const { return op_; }
2254 Expression* left() const { return left_; }
2255 Expression* right() const { return right_; }
2257 // Type feedback information.
2258 static int num_ids() { return parent_num_ids() + 1; }
2259 TypeFeedbackId CompareOperationFeedbackId() const {
2260 return TypeFeedbackId(local_id(0));
2262 Type* combined_type() const { return combined_type_; }
2263 void set_combined_type(Type* type) { combined_type_ = type; }
2265 // Match special cases.
2266 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2267 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2268 bool IsLiteralCompareNull(Expression** expr);
2271 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2272 Expression* right, int pos)
2273 : Expression(zone, pos),
2277 combined_type_(Type::None(zone)) {
2278 DCHECK(Token::IsCompareOp(op));
2280 static int parent_num_ids() { return Expression::num_ids(); }
2283 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2289 Type* combined_type_;
2293 class Spread final : public Expression {
2295 DECLARE_NODE_TYPE(Spread)
2297 Expression* expression() const { return expression_; }
2299 static int num_ids() { return parent_num_ids(); }
2302 Spread(Zone* zone, Expression* expression, int pos)
2303 : Expression(zone, pos), expression_(expression) {}
2304 static int parent_num_ids() { return Expression::num_ids(); }
2307 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2309 Expression* expression_;
2313 class Conditional final : public Expression {
2315 DECLARE_NODE_TYPE(Conditional)
2317 Expression* condition() const { return condition_; }
2318 Expression* then_expression() const { return then_expression_; }
2319 Expression* else_expression() const { return else_expression_; }
2321 static int num_ids() { return parent_num_ids() + 2; }
2322 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2323 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2326 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2327 Expression* else_expression, int position)
2328 : Expression(zone, position),
2329 condition_(condition),
2330 then_expression_(then_expression),
2331 else_expression_(else_expression) {}
2332 static int parent_num_ids() { return Expression::num_ids(); }
2335 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2337 Expression* condition_;
2338 Expression* then_expression_;
2339 Expression* else_expression_;
2343 class Assignment final : public Expression {
2345 DECLARE_NODE_TYPE(Assignment)
2347 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2349 Token::Value binary_op() const;
2351 Token::Value op() const { return TokenField::decode(bit_field_); }
2352 Expression* target() const { return target_; }
2353 Expression* value() const { return value_; }
2354 BinaryOperation* binary_operation() const { return binary_operation_; }
2356 // This check relies on the definition order of token in token.h.
2357 bool is_compound() const { return op() > Token::ASSIGN; }
2359 static int num_ids() { return parent_num_ids() + 2; }
2360 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2362 // Type feedback information.
2363 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2364 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2365 bool IsUninitialized() const {
2366 return IsUninitializedField::decode(bit_field_);
2368 bool HasNoTypeInformation() {
2369 return IsUninitializedField::decode(bit_field_);
2371 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2372 IcCheckType GetKeyType() const override {
2373 return KeyTypeField::decode(bit_field_);
2375 KeyedAccessStoreMode GetStoreMode() const override {
2376 return StoreModeField::decode(bit_field_);
2378 void set_is_uninitialized(bool b) {
2379 bit_field_ = IsUninitializedField::update(bit_field_, b);
2381 void set_key_type(IcCheckType key_type) {
2382 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2384 void set_store_mode(KeyedAccessStoreMode mode) {
2385 bit_field_ = StoreModeField::update(bit_field_, mode);
2388 FeedbackVectorRequirements ComputeFeedbackRequirements(
2389 Isolate* isolate, const ICSlotCache* cache) override;
2390 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2391 ICSlotCache* cache) override {
2394 Code::Kind FeedbackICSlotKind(int index) override;
2395 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2398 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2400 static int parent_num_ids() { return Expression::num_ids(); }
2403 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2405 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2406 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2407 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2408 class TokenField : public BitField16<Token::Value, 6, 8> {};
2410 // Starts with 16-bit field, which should get packed together with
2411 // Expression's trailing 16-bit field.
2412 uint16_t bit_field_;
2413 Expression* target_;
2415 BinaryOperation* binary_operation_;
2416 SmallMapList receiver_types_;
2417 FeedbackVectorICSlot slot_;
2421 class Yield final : public Expression {
2423 DECLARE_NODE_TYPE(Yield)
2426 kInitial, // The initial yield that returns the unboxed generator object.
2427 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2428 kDelegating, // A yield*.
2429 kFinal // A return: { value: EXPRESSION, done: true }
2432 Expression* generator_object() const { return generator_object_; }
2433 Expression* expression() const { return expression_; }
2434 Kind yield_kind() const { return yield_kind_; }
2436 // Type feedback information.
2437 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2438 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2439 Isolate* isolate, const ICSlotCache* cache) override {
2440 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2442 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2443 ICSlotCache* cache) override {
2444 yield_first_feedback_slot_ = slot;
2446 Code::Kind FeedbackICSlotKind(int index) override {
2447 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2450 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2451 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2452 return yield_first_feedback_slot_;
2455 FeedbackVectorICSlot DoneFeedbackSlot() {
2456 return KeyedLoadFeedbackSlot().next();
2459 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2462 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2463 Kind yield_kind, int pos)
2464 : Expression(zone, pos),
2465 generator_object_(generator_object),
2466 expression_(expression),
2467 yield_kind_(yield_kind),
2468 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2471 Expression* generator_object_;
2472 Expression* expression_;
2474 FeedbackVectorICSlot yield_first_feedback_slot_;
2478 class Throw final : public Expression {
2480 DECLARE_NODE_TYPE(Throw)
2482 Expression* exception() const { return exception_; }
2485 Throw(Zone* zone, Expression* exception, int pos)
2486 : Expression(zone, pos), exception_(exception) {}
2489 Expression* exception_;
2493 class FunctionLiteral final : public Expression {
2496 ANONYMOUS_EXPRESSION,
2501 enum ParameterFlag {
2502 kNoDuplicateParameters = 0,
2503 kHasDuplicateParameters = 1
2506 enum IsFunctionFlag {
2511 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2513 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2515 enum ArityRestriction {
2521 DECLARE_NODE_TYPE(FunctionLiteral)
2523 Handle<String> name() const { return raw_name_->string(); }
2524 const AstRawString* raw_name() const { return raw_name_; }
2525 Scope* scope() const { return scope_; }
2526 ZoneList<Statement*>* body() const { return body_; }
2527 void set_function_token_position(int pos) { function_token_position_ = pos; }
2528 int function_token_position() const { return function_token_position_; }
2529 int start_position() const;
2530 int end_position() const;
2531 int SourceSize() const { return end_position() - start_position(); }
2532 bool is_expression() const { return IsExpression::decode(bitfield_); }
2533 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2534 LanguageMode language_mode() const;
2536 static bool NeedsHomeObject(Expression* expr);
2538 int materialized_literal_count() { return materialized_literal_count_; }
2539 int expected_property_count() { return expected_property_count_; }
2540 int parameter_count() { return parameter_count_; }
2542 bool AllowsLazyCompilation();
2543 bool AllowsLazyCompilationWithoutContext();
2545 Handle<String> debug_name() const {
2546 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2547 return raw_name_->string();
2549 return inferred_name();
2552 Handle<String> inferred_name() const {
2553 if (!inferred_name_.is_null()) {
2554 DCHECK(raw_inferred_name_ == NULL);
2555 return inferred_name_;
2557 if (raw_inferred_name_ != NULL) {
2558 return raw_inferred_name_->string();
2561 return Handle<String>();
2564 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2565 void set_inferred_name(Handle<String> inferred_name) {
2566 DCHECK(!inferred_name.is_null());
2567 inferred_name_ = inferred_name;
2568 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2569 raw_inferred_name_ = NULL;
2572 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2573 DCHECK(raw_inferred_name != NULL);
2574 raw_inferred_name_ = raw_inferred_name;
2575 DCHECK(inferred_name_.is_null());
2576 inferred_name_ = Handle<String>();
2579 bool pretenure() { return Pretenure::decode(bitfield_); }
2580 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2582 bool has_duplicate_parameters() {
2583 return HasDuplicateParameters::decode(bitfield_);
2586 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2588 // This is used as a heuristic on when to eagerly compile a function
2589 // literal. We consider the following constructs as hints that the
2590 // function will be called immediately:
2591 // - (function() { ... })();
2592 // - var x = function() { ... }();
2593 bool should_eager_compile() const {
2594 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2596 void set_should_eager_compile() {
2597 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2600 // A hint that we expect this function to be called (exactly) once,
2601 // i.e. we suspect it's an initialization function.
2602 bool should_be_used_once_hint() const {
2603 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2605 void set_should_be_used_once_hint() {
2606 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2609 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2611 int ast_node_count() { return ast_properties_.node_count(); }
2612 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2613 void set_ast_properties(AstProperties* ast_properties) {
2614 ast_properties_ = *ast_properties;
2616 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2617 return ast_properties_.get_spec();
2619 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2620 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2621 void set_dont_optimize_reason(BailoutReason reason) {
2622 dont_optimize_reason_ = reason;
2626 FunctionLiteral(Zone* zone, const AstRawString* name,
2627 AstValueFactory* ast_value_factory, Scope* scope,
2628 ZoneList<Statement*>* body, int materialized_literal_count,
2629 int expected_property_count, int parameter_count,
2630 FunctionType function_type,
2631 ParameterFlag has_duplicate_parameters,
2632 IsFunctionFlag is_function,
2633 EagerCompileHint eager_compile_hint, FunctionKind kind,
2635 : Expression(zone, position),
2639 raw_inferred_name_(ast_value_factory->empty_string()),
2640 ast_properties_(zone),
2641 dont_optimize_reason_(kNoReason),
2642 materialized_literal_count_(materialized_literal_count),
2643 expected_property_count_(expected_property_count),
2644 parameter_count_(parameter_count),
2645 function_token_position_(RelocInfo::kNoPosition) {
2646 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2647 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2648 Pretenure::encode(false) |
2649 HasDuplicateParameters::encode(has_duplicate_parameters) |
2650 IsFunction::encode(is_function) |
2651 EagerCompileHintBit::encode(eager_compile_hint) |
2652 FunctionKindBits::encode(kind) |
2653 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2654 DCHECK(IsValidFunctionKind(kind));
2658 const AstRawString* raw_name_;
2659 Handle<String> name_;
2661 ZoneList<Statement*>* body_;
2662 const AstString* raw_inferred_name_;
2663 Handle<String> inferred_name_;
2664 AstProperties ast_properties_;
2665 BailoutReason dont_optimize_reason_;
2667 int materialized_literal_count_;
2668 int expected_property_count_;
2669 int parameter_count_;
2670 int function_token_position_;
2673 class IsExpression : public BitField<bool, 0, 1> {};
2674 class IsAnonymous : public BitField<bool, 1, 1> {};
2675 class Pretenure : public BitField<bool, 2, 1> {};
2676 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2677 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2678 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2679 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2680 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2685 class ClassLiteral final : public Expression {
2687 typedef ObjectLiteralProperty Property;
2689 DECLARE_NODE_TYPE(ClassLiteral)
2691 Handle<String> name() const { return raw_name_->string(); }
2692 const AstRawString* raw_name() const { return raw_name_; }
2693 Scope* scope() const { return scope_; }
2694 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2695 Expression* extends() const { return extends_; }
2696 FunctionLiteral* constructor() const { return constructor_; }
2697 ZoneList<Property*>* properties() const { return properties_; }
2698 int start_position() const { return position(); }
2699 int end_position() const { return end_position_; }
2701 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2702 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2703 BailoutId ExitId() { return BailoutId(local_id(2)); }
2704 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2706 // Return an AST id for a property that is used in simulate instructions.
2707 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2709 // Unlike other AST nodes, this number of bailout IDs allocated for an
2710 // ClassLiteral can vary, so num_ids() is not a static method.
2711 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2713 // Object literals need one feedback slot for each non-trivial value, as well
2714 // as some slots for home objects.
2715 FeedbackVectorRequirements ComputeFeedbackRequirements(
2716 Isolate* isolate, const ICSlotCache* cache) override;
2717 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2718 ICSlotCache* cache) override {
2721 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2722 FeedbackVectorICSlot GetNthSlot(int n) const {
2723 return FeedbackVectorICSlot(slot_.ToInt() + n);
2726 // If value needs a home object, returns a valid feedback vector ic slot
2727 // given by slot_index, and increments slot_index.
2728 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2729 int* slot_index) const;
2732 int slot_count() const { return slot_count_; }
2736 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2737 VariableProxy* class_variable_proxy, Expression* extends,
2738 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2739 int start_position, int end_position)
2740 : Expression(zone, start_position),
2743 class_variable_proxy_(class_variable_proxy),
2745 constructor_(constructor),
2746 properties_(properties),
2747 end_position_(end_position),
2751 slot_(FeedbackVectorICSlot::Invalid()) {
2754 static int parent_num_ids() { return Expression::num_ids(); }
2757 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2759 const AstRawString* raw_name_;
2761 VariableProxy* class_variable_proxy_;
2762 Expression* extends_;
2763 FunctionLiteral* constructor_;
2764 ZoneList<Property*>* properties_;
2767 // slot_count_ helps validate that the logic to allocate ic slots and the
2768 // logic to use them are in sync.
2771 FeedbackVectorICSlot slot_;
2775 class NativeFunctionLiteral final : public Expression {
2777 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2779 Handle<String> name() const { return name_->string(); }
2780 v8::Extension* extension() const { return extension_; }
2783 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2784 v8::Extension* extension, int pos)
2785 : Expression(zone, pos), name_(name), extension_(extension) {}
2788 const AstRawString* name_;
2789 v8::Extension* extension_;
2793 class ThisFunction final : public Expression {
2795 DECLARE_NODE_TYPE(ThisFunction)
2798 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2802 class SuperPropertyReference final : public Expression {
2804 DECLARE_NODE_TYPE(SuperPropertyReference)
2806 VariableProxy* this_var() const { return this_var_; }
2807 Expression* home_object() const { return home_object_; }
2810 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2811 Expression* home_object, int pos)
2812 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2813 DCHECK(this_var->is_this());
2814 DCHECK(home_object->IsProperty());
2818 VariableProxy* this_var_;
2819 Expression* home_object_;
2823 class SuperCallReference final : public Expression {
2825 DECLARE_NODE_TYPE(SuperCallReference)
2827 VariableProxy* this_var() const { return this_var_; }
2828 VariableProxy* new_target_var() const { return new_target_var_; }
2829 VariableProxy* this_function_var() const { return this_function_var_; }
2832 SuperCallReference(Zone* zone, VariableProxy* this_var,
2833 VariableProxy* new_target_var,
2834 VariableProxy* this_function_var, int pos)
2835 : Expression(zone, pos),
2836 this_var_(this_var),
2837 new_target_var_(new_target_var),
2838 this_function_var_(this_function_var) {
2839 DCHECK(this_var->is_this());
2840 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2841 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2845 VariableProxy* this_var_;
2846 VariableProxy* new_target_var_;
2847 VariableProxy* this_function_var_;
2851 #undef DECLARE_NODE_TYPE
2854 // ----------------------------------------------------------------------------
2855 // Regular expressions
2858 class RegExpVisitor BASE_EMBEDDED {
2860 virtual ~RegExpVisitor() { }
2861 #define MAKE_CASE(Name) \
2862 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2863 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2868 class RegExpTree : public ZoneObject {
2870 static const int kInfinity = kMaxInt;
2871 virtual ~RegExpTree() {}
2872 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2873 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2874 RegExpNode* on_success) = 0;
2875 virtual bool IsTextElement() { return false; }
2876 virtual bool IsAnchoredAtStart() { return false; }
2877 virtual bool IsAnchoredAtEnd() { return false; }
2878 virtual int min_match() = 0;
2879 virtual int max_match() = 0;
2880 // Returns the interval of registers used for captures within this
2882 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2883 virtual void AppendToText(RegExpText* text, Zone* zone);
2884 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2885 #define MAKE_ASTYPE(Name) \
2886 virtual RegExp##Name* As##Name(); \
2887 virtual bool Is##Name();
2888 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2893 class RegExpDisjunction final : public RegExpTree {
2895 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2896 void* Accept(RegExpVisitor* visitor, void* data) override;
2897 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2898 RegExpNode* on_success) override;
2899 RegExpDisjunction* AsDisjunction() override;
2900 Interval CaptureRegisters() override;
2901 bool IsDisjunction() override;
2902 bool IsAnchoredAtStart() override;
2903 bool IsAnchoredAtEnd() override;
2904 int min_match() override { return min_match_; }
2905 int max_match() override { return max_match_; }
2906 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2908 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2909 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2910 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2911 ZoneList<RegExpTree*>* alternatives_;
2917 class RegExpAlternative final : public RegExpTree {
2919 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2920 void* Accept(RegExpVisitor* visitor, void* data) override;
2921 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2922 RegExpNode* on_success) override;
2923 RegExpAlternative* AsAlternative() override;
2924 Interval CaptureRegisters() override;
2925 bool IsAlternative() override;
2926 bool IsAnchoredAtStart() override;
2927 bool IsAnchoredAtEnd() override;
2928 int min_match() override { return min_match_; }
2929 int max_match() override { return max_match_; }
2930 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2932 ZoneList<RegExpTree*>* nodes_;
2938 class RegExpAssertion final : public RegExpTree {
2940 enum AssertionType {
2948 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2949 void* Accept(RegExpVisitor* visitor, void* data) override;
2950 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2951 RegExpNode* on_success) override;
2952 RegExpAssertion* AsAssertion() override;
2953 bool IsAssertion() override;
2954 bool IsAnchoredAtStart() override;
2955 bool IsAnchoredAtEnd() override;
2956 int min_match() override { return 0; }
2957 int max_match() override { return 0; }
2958 AssertionType assertion_type() { return assertion_type_; }
2960 AssertionType assertion_type_;
2964 class CharacterSet final BASE_EMBEDDED {
2966 explicit CharacterSet(uc16 standard_set_type)
2968 standard_set_type_(standard_set_type) {}
2969 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2971 standard_set_type_(0) {}
2972 ZoneList<CharacterRange>* ranges(Zone* zone);
2973 uc16 standard_set_type() { return standard_set_type_; }
2974 void set_standard_set_type(uc16 special_set_type) {
2975 standard_set_type_ = special_set_type;
2977 bool is_standard() { return standard_set_type_ != 0; }
2978 void Canonicalize();
2980 ZoneList<CharacterRange>* ranges_;
2981 // If non-zero, the value represents a standard set (e.g., all whitespace
2982 // characters) without having to expand the ranges.
2983 uc16 standard_set_type_;
2987 class RegExpCharacterClass final : public RegExpTree {
2989 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2991 is_negated_(is_negated) { }
2992 explicit RegExpCharacterClass(uc16 type)
2994 is_negated_(false) { }
2995 void* Accept(RegExpVisitor* visitor, void* data) override;
2996 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2997 RegExpNode* on_success) override;
2998 RegExpCharacterClass* AsCharacterClass() override;
2999 bool IsCharacterClass() override;
3000 bool IsTextElement() override { return true; }
3001 int min_match() override { return 1; }
3002 int max_match() override { return 1; }
3003 void AppendToText(RegExpText* text, Zone* zone) override;
3004 CharacterSet character_set() { return set_; }
3005 // TODO(lrn): Remove need for complex version if is_standard that
3006 // recognizes a mangled standard set and just do { return set_.is_special(); }
3007 bool is_standard(Zone* zone);
3008 // Returns a value representing the standard character set if is_standard()
3010 // Currently used values are:
3011 // s : unicode whitespace
3012 // S : unicode non-whitespace
3013 // w : ASCII word character (digit, letter, underscore)
3014 // W : non-ASCII word character
3016 // D : non-ASCII digit
3017 // . : non-unicode non-newline
3018 // * : All characters
3019 uc16 standard_type() { return set_.standard_set_type(); }
3020 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3021 bool is_negated() { return is_negated_; }
3029 class RegExpAtom final : public RegExpTree {
3031 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3032 void* Accept(RegExpVisitor* visitor, void* data) override;
3033 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3034 RegExpNode* on_success) override;
3035 RegExpAtom* AsAtom() override;
3036 bool IsAtom() override;
3037 bool IsTextElement() override { return true; }
3038 int min_match() override { return data_.length(); }
3039 int max_match() override { return data_.length(); }
3040 void AppendToText(RegExpText* text, Zone* zone) override;
3041 Vector<const uc16> data() { return data_; }
3042 int length() { return data_.length(); }
3044 Vector<const uc16> data_;
3048 class RegExpText final : public RegExpTree {
3050 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3051 void* Accept(RegExpVisitor* visitor, void* data) override;
3052 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3053 RegExpNode* on_success) override;
3054 RegExpText* AsText() override;
3055 bool IsText() override;
3056 bool IsTextElement() override { return true; }
3057 int min_match() override { return length_; }
3058 int max_match() override { return length_; }
3059 void AppendToText(RegExpText* text, Zone* zone) override;
3060 void AddElement(TextElement elm, Zone* zone) {
3061 elements_.Add(elm, zone);
3062 length_ += elm.length();
3064 ZoneList<TextElement>* elements() { return &elements_; }
3066 ZoneList<TextElement> elements_;
3071 class RegExpQuantifier final : public RegExpTree {
3073 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3074 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3078 min_match_(min * body->min_match()),
3079 quantifier_type_(type) {
3080 if (max > 0 && body->max_match() > kInfinity / max) {
3081 max_match_ = kInfinity;
3083 max_match_ = max * body->max_match();
3086 void* Accept(RegExpVisitor* visitor, void* data) override;
3087 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3088 RegExpNode* on_success) override;
3089 static RegExpNode* ToNode(int min,
3093 RegExpCompiler* compiler,
3094 RegExpNode* on_success,
3095 bool not_at_start = false);
3096 RegExpQuantifier* AsQuantifier() override;
3097 Interval CaptureRegisters() override;
3098 bool IsQuantifier() override;
3099 int min_match() override { return min_match_; }
3100 int max_match() override { return max_match_; }
3101 int min() { return min_; }
3102 int max() { return max_; }
3103 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3104 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3105 bool is_greedy() { return quantifier_type_ == GREEDY; }
3106 RegExpTree* body() { return body_; }
3114 QuantifierType quantifier_type_;
3118 class RegExpCapture final : public RegExpTree {
3120 explicit RegExpCapture(RegExpTree* body, int index)
3121 : body_(body), index_(index) { }
3122 void* Accept(RegExpVisitor* visitor, void* data) override;
3123 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3124 RegExpNode* on_success) override;
3125 static RegExpNode* ToNode(RegExpTree* body,
3127 RegExpCompiler* compiler,
3128 RegExpNode* on_success);
3129 RegExpCapture* AsCapture() override;
3130 bool IsAnchoredAtStart() override;
3131 bool IsAnchoredAtEnd() override;
3132 Interval CaptureRegisters() override;
3133 bool IsCapture() override;
3134 int min_match() override { return body_->min_match(); }
3135 int max_match() override { return body_->max_match(); }
3136 RegExpTree* body() { return body_; }
3137 int index() { return index_; }
3138 static int StartRegister(int index) { return index * 2; }
3139 static int EndRegister(int index) { return index * 2 + 1; }
3147 class RegExpLookahead final : public RegExpTree {
3149 RegExpLookahead(RegExpTree* body,
3154 is_positive_(is_positive),
3155 capture_count_(capture_count),
3156 capture_from_(capture_from) { }
3158 void* Accept(RegExpVisitor* visitor, void* data) override;
3159 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3160 RegExpNode* on_success) override;
3161 RegExpLookahead* AsLookahead() override;
3162 Interval CaptureRegisters() override;
3163 bool IsLookahead() override;
3164 bool IsAnchoredAtStart() override;
3165 int min_match() override { return 0; }
3166 int max_match() override { return 0; }
3167 RegExpTree* body() { return body_; }
3168 bool is_positive() { return is_positive_; }
3169 int capture_count() { return capture_count_; }
3170 int capture_from() { return capture_from_; }
3180 class RegExpBackReference final : public RegExpTree {
3182 explicit RegExpBackReference(RegExpCapture* capture)
3183 : capture_(capture) { }
3184 void* Accept(RegExpVisitor* visitor, void* data) override;
3185 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3186 RegExpNode* on_success) override;
3187 RegExpBackReference* AsBackReference() override;
3188 bool IsBackReference() override;
3189 int min_match() override { return 0; }
3190 int max_match() override { return capture_->max_match(); }
3191 int index() { return capture_->index(); }
3192 RegExpCapture* capture() { return capture_; }
3194 RegExpCapture* capture_;
3198 class RegExpEmpty final : public RegExpTree {
3201 void* Accept(RegExpVisitor* visitor, void* data) override;
3202 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3203 RegExpNode* on_success) override;
3204 RegExpEmpty* AsEmpty() override;
3205 bool IsEmpty() override;
3206 int min_match() override { return 0; }
3207 int max_match() override { return 0; }
3211 // ----------------------------------------------------------------------------
3213 // - leaf node visitors are abstract.
3215 class AstVisitor BASE_EMBEDDED {
3218 virtual ~AstVisitor() {}
3220 // Stack overflow check and dynamic dispatch.
3221 virtual void Visit(AstNode* node) = 0;
3223 // Iteration left-to-right.
3224 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3225 virtual void VisitStatements(ZoneList<Statement*>* statements);
3226 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3228 // Individual AST nodes.
3229 #define DEF_VISIT(type) \
3230 virtual void Visit##type(type* node) = 0;
3231 AST_NODE_LIST(DEF_VISIT)
3236 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3238 void Visit(AstNode* node) final { \
3239 if (!CheckStackOverflow()) node->Accept(this); \
3242 void SetStackOverflow() { stack_overflow_ = true; } \
3243 void ClearStackOverflow() { stack_overflow_ = false; } \
3244 bool HasStackOverflow() const { return stack_overflow_; } \
3246 bool CheckStackOverflow() { \
3247 if (stack_overflow_) return true; \
3248 StackLimitCheck check(isolate_); \
3249 if (!check.HasOverflowed()) return false; \
3250 stack_overflow_ = true; \
3255 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3256 isolate_ = isolate; \
3258 stack_overflow_ = false; \
3260 Zone* zone() { return zone_; } \
3261 Isolate* isolate() { return isolate_; } \
3263 Isolate* isolate_; \
3265 bool stack_overflow_
3268 // ----------------------------------------------------------------------------
3271 class AstNodeFactory final BASE_EMBEDDED {
3273 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3274 : zone_(ast_value_factory->zone()),
3275 ast_value_factory_(ast_value_factory) {}
3277 VariableDeclaration* NewVariableDeclaration(
3278 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3279 bool is_class_declaration = false, int declaration_group_start = -1) {
3281 VariableDeclaration(zone_, proxy, mode, scope, pos,
3282 is_class_declaration, declaration_group_start);
3285 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3287 FunctionLiteral* fun,
3290 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3293 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3294 const AstRawString* import_name,
3295 const AstRawString* module_specifier,
3296 Scope* scope, int pos) {
3297 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3298 module_specifier, scope, pos);
3301 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3304 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3307 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3308 bool ignore_completion_value, int pos) {
3310 Block(zone_, labels, capacity, ignore_completion_value, pos);
3313 #define STATEMENT_WITH_LABELS(NodeType) \
3314 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3315 return new (zone_) NodeType(zone_, labels, pos); \
3317 STATEMENT_WITH_LABELS(DoWhileStatement)
3318 STATEMENT_WITH_LABELS(WhileStatement)
3319 STATEMENT_WITH_LABELS(ForStatement)
3320 STATEMENT_WITH_LABELS(SwitchStatement)
3321 #undef STATEMENT_WITH_LABELS
3323 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3324 ZoneList<const AstRawString*>* labels,
3326 switch (visit_mode) {
3327 case ForEachStatement::ENUMERATE: {
3328 return new (zone_) ForInStatement(zone_, labels, pos);
3330 case ForEachStatement::ITERATE: {
3331 return new (zone_) ForOfStatement(zone_, labels, pos);
3338 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3339 return new (zone_) ExpressionStatement(zone_, expression, pos);
3342 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3343 return new (zone_) ContinueStatement(zone_, target, pos);
3346 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3347 return new (zone_) BreakStatement(zone_, target, pos);
3350 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3351 return new (zone_) ReturnStatement(zone_, expression, pos);
3354 WithStatement* NewWithStatement(Scope* scope,
3355 Expression* expression,
3356 Statement* statement,
3358 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3361 IfStatement* NewIfStatement(Expression* condition,
3362 Statement* then_statement,
3363 Statement* else_statement,
3366 IfStatement(zone_, condition, then_statement, else_statement, pos);
3369 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3371 Block* catch_block, int pos) {
3373 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3376 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3377 Block* finally_block, int pos) {
3379 TryFinallyStatement(zone_, try_block, finally_block, pos);
3382 DebuggerStatement* NewDebuggerStatement(int pos) {
3383 return new (zone_) DebuggerStatement(zone_, pos);
3386 EmptyStatement* NewEmptyStatement(int pos) {
3387 return new(zone_) EmptyStatement(zone_, pos);
3390 CaseClause* NewCaseClause(
3391 Expression* label, ZoneList<Statement*>* statements, int pos) {
3392 return new (zone_) CaseClause(zone_, label, statements, pos);
3395 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3397 Literal(zone_, ast_value_factory_->NewString(string), pos);
3400 // A JavaScript symbol (ECMA-262 edition 6).
3401 Literal* NewSymbolLiteral(const char* name, int pos) {
3402 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3405 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3407 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3410 Literal* NewSmiLiteral(int number, int pos) {
3411 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3414 Literal* NewBooleanLiteral(bool b, int pos) {
3415 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3418 Literal* NewNullLiteral(int pos) {
3419 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3422 Literal* NewUndefinedLiteral(int pos) {
3423 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3426 Literal* NewTheHoleLiteral(int pos) {
3427 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3430 ObjectLiteral* NewObjectLiteral(
3431 ZoneList<ObjectLiteral::Property*>* properties,
3433 int boilerplate_properties,
3437 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3438 boilerplate_properties, has_function,
3442 ObjectLiteral::Property* NewObjectLiteralProperty(
3443 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3444 bool is_static, bool is_computed_name) {
3446 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3449 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3452 bool is_computed_name) {
3453 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3454 is_static, is_computed_name);
3457 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3458 const AstRawString* flags,
3462 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3466 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3471 ArrayLiteral(zone_, values, -1, literal_index, is_strong, pos);
3474 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3475 int first_spread_index, int literal_index,
3476 bool is_strong, int pos) {
3477 return new (zone_) ArrayLiteral(zone_, values, first_spread_index,
3478 literal_index, is_strong, pos);
3481 VariableProxy* NewVariableProxy(Variable* var,
3482 int start_position = RelocInfo::kNoPosition,
3483 int end_position = RelocInfo::kNoPosition) {
3484 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3487 VariableProxy* NewVariableProxy(const AstRawString* name,
3488 Variable::Kind variable_kind,
3489 int start_position = RelocInfo::kNoPosition,
3490 int end_position = RelocInfo::kNoPosition) {
3491 DCHECK_NOT_NULL(name);
3493 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3496 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3497 return new (zone_) Property(zone_, obj, key, pos);
3500 Call* NewCall(Expression* expression,
3501 ZoneList<Expression*>* arguments,
3503 return new (zone_) Call(zone_, expression, arguments, pos);
3506 CallNew* NewCallNew(Expression* expression,
3507 ZoneList<Expression*>* arguments,
3509 return new (zone_) CallNew(zone_, expression, arguments, pos);
3512 CallRuntime* NewCallRuntime(const AstRawString* name,
3513 const Runtime::Function* function,
3514 ZoneList<Expression*>* arguments,
3516 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3519 UnaryOperation* NewUnaryOperation(Token::Value op,
3520 Expression* expression,
3522 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3525 BinaryOperation* NewBinaryOperation(Token::Value op,
3529 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3532 CountOperation* NewCountOperation(Token::Value op,
3536 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3539 CompareOperation* NewCompareOperation(Token::Value op,
3543 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3546 Spread* NewSpread(Expression* expression, int pos) {
3547 return new (zone_) Spread(zone_, expression, pos);
3550 Conditional* NewConditional(Expression* condition,
3551 Expression* then_expression,
3552 Expression* else_expression,
3554 return new (zone_) Conditional(zone_, condition, then_expression,
3555 else_expression, position);
3558 Assignment* NewAssignment(Token::Value op,
3562 DCHECK(Token::IsAssignmentOp(op));
3563 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3564 if (assign->is_compound()) {
3565 DCHECK(Token::IsAssignmentOp(op));
3566 assign->binary_operation_ =
3567 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3572 Yield* NewYield(Expression *generator_object,
3573 Expression* expression,
3574 Yield::Kind yield_kind,
3576 if (!expression) expression = NewUndefinedLiteral(pos);
3578 Yield(zone_, generator_object, expression, yield_kind, pos);
3581 Throw* NewThrow(Expression* exception, int pos) {
3582 return new (zone_) Throw(zone_, exception, pos);
3585 FunctionLiteral* NewFunctionLiteral(
3586 const AstRawString* name, AstValueFactory* ast_value_factory,
3587 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3588 int expected_property_count, int parameter_count,
3589 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3590 FunctionLiteral::FunctionType function_type,
3591 FunctionLiteral::IsFunctionFlag is_function,
3592 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3594 return new (zone_) FunctionLiteral(
3595 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3596 expected_property_count, parameter_count, function_type,
3597 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3601 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3602 VariableProxy* proxy, Expression* extends,
3603 FunctionLiteral* constructor,
3604 ZoneList<ObjectLiteral::Property*>* properties,
3605 int start_position, int end_position) {
3607 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3608 properties, start_position, end_position);
3611 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3612 v8::Extension* extension,
3614 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3617 ThisFunction* NewThisFunction(int pos) {
3618 return new (zone_) ThisFunction(zone_, pos);
3621 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3622 Expression* home_object,
3625 SuperPropertyReference(zone_, this_var, home_object, pos);
3628 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3629 VariableProxy* new_target_var,
3630 VariableProxy* this_function_var,
3632 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3633 this_function_var, pos);
3638 AstValueFactory* ast_value_factory_;
3642 } } // namespace v8::internal