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.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/base/flags.h"
14 #include "src/base/smart-pointers.h"
15 #include "src/factory.h"
16 #include "src/isolate.h"
17 #include "src/jsregexp.h"
18 #include "src/list-inl.h"
19 #include "src/modules.h"
20 #include "src/runtime/runtime.h"
21 #include "src/small-pointer-list.h"
22 #include "src/token.h"
23 #include "src/types.h"
24 #include "src/utils.h"
25 #include "src/variables.h"
30 // The abstract syntax tree is an intermediate, light-weight
31 // representation of the parsed JavaScript code suitable for
32 // compilation to native code.
34 // Nodes are allocated in a separate zone, which allows faster
35 // allocation and constant-time deallocation of the entire syntax
39 // ----------------------------------------------------------------------------
40 // Nodes of the abstract syntax tree. Only concrete classes are
43 #define DECLARATION_NODE_LIST(V) \
44 V(VariableDeclaration) \
45 V(FunctionDeclaration) \
46 V(ImportDeclaration) \
49 #define STATEMENT_NODE_LIST(V) \
51 V(ExpressionStatement) \
54 V(ContinueStatement) \
64 V(TryCatchStatement) \
65 V(TryFinallyStatement) \
68 #define EXPRESSION_NODE_LIST(V) \
71 V(NativeFunctionLiteral) \
91 V(SuperPropertyReference) \
92 V(SuperCallReference) \
95 #define AST_NODE_LIST(V) \
96 DECLARATION_NODE_LIST(V) \
97 STATEMENT_NODE_LIST(V) \
98 EXPRESSION_NODE_LIST(V)
100 // Forward declarations
101 class AstNodeFactory;
105 class BreakableStatement;
107 class IterationStatement;
108 class MaterializedLiteral;
110 class TypeFeedbackOracle;
112 class RegExpAlternative;
113 class RegExpAssertion;
115 class RegExpBackReference;
117 class RegExpCharacterClass;
118 class RegExpCompiler;
119 class RegExpDisjunction;
121 class RegExpLookahead;
122 class RegExpQuantifier;
125 #define DEF_FORWARD_DECLARATION(type) class type;
126 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
127 #undef DEF_FORWARD_DECLARATION
130 // Typedef only introduced to avoid unreadable code.
131 typedef ZoneList<Handle<String>> ZoneStringList;
132 typedef ZoneList<Handle<Object>> ZoneObjectList;
135 #define DECLARE_NODE_TYPE(type) \
136 void Accept(AstVisitor* v) override; \
137 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
138 friend class AstNodeFactory;
141 class FeedbackVectorRequirements {
143 FeedbackVectorRequirements(int slots, int ic_slots)
144 : slots_(slots), ic_slots_(ic_slots) {}
146 int slots() const { return slots_; }
147 int ic_slots() const { return ic_slots_; }
155 class VariableICSlotPair final {
157 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
158 : variable_(variable), slot_(slot) {}
160 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
162 Variable* variable() const { return variable_; }
163 FeedbackVectorICSlot slot() const { return slot_; }
167 FeedbackVectorICSlot slot_;
171 typedef List<VariableICSlotPair> ICSlotCache;
174 class AstProperties final BASE_EMBEDDED {
178 kDontSelfOptimize = 1 << 0,
179 kDontCrankshaft = 1 << 1
182 typedef base::Flags<Flag> Flags;
184 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
186 Flags& flags() { return flags_; }
187 Flags flags() const { return flags_; }
188 int node_count() { return node_count_; }
189 void add_node_count(int count) { node_count_ += count; }
191 int slots() const { return spec_.slots(); }
192 void increase_slots(int count) { spec_.increase_slots(count); }
194 int ic_slots() const { return spec_.ic_slots(); }
195 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
196 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
197 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
202 ZoneFeedbackVectorSpec spec_;
205 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
208 class AstNode: public ZoneObject {
210 #define DECLARE_TYPE_ENUM(type) k##type,
212 AST_NODE_LIST(DECLARE_TYPE_ENUM)
215 #undef DECLARE_TYPE_ENUM
217 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
219 explicit AstNode(int position): position_(position) {}
220 virtual ~AstNode() {}
222 virtual void Accept(AstVisitor* v) = 0;
223 virtual NodeType node_type() const = 0;
224 int position() const { return position_; }
226 // Type testing & conversion functions overridden by concrete subclasses.
227 #define DECLARE_NODE_FUNCTIONS(type) \
228 bool Is##type() const { return node_type() == AstNode::k##type; } \
230 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
232 const type* As##type() const { \
233 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
235 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
236 #undef DECLARE_NODE_FUNCTIONS
238 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
239 virtual IterationStatement* AsIterationStatement() { return NULL; }
240 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
242 // The interface for feedback slots, with default no-op implementations for
243 // node types which don't actually have this. Note that this is conceptually
244 // not really nice, but multiple inheritance would introduce yet another
245 // vtable entry per node, something we don't want for space reasons.
246 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
247 Isolate* isolate, const ICSlotCache* cache) {
248 return FeedbackVectorRequirements(0, 0);
250 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
251 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
252 ICSlotCache* cache) {
255 // Each ICSlot stores a kind of IC which the participating node should know.
256 virtual Code::Kind FeedbackICSlotKind(int index) {
258 return Code::NUMBER_OF_KINDS;
262 // Hidden to prevent accidental usage. It would have to load the
263 // current zone from the TLS.
264 void* operator new(size_t size);
266 friend class CaseClause; // Generates AST IDs.
272 class Statement : public AstNode {
274 explicit Statement(Zone* zone, int position) : AstNode(position) {}
276 bool IsEmpty() { return AsEmptyStatement() != NULL; }
277 virtual bool IsJump() const { return false; }
281 class SmallMapList final {
284 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
286 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
287 void Clear() { list_.Clear(); }
288 void Sort() { list_.Sort(); }
290 bool is_empty() const { return list_.is_empty(); }
291 int length() const { return list_.length(); }
293 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
294 if (!Map::TryUpdate(map).ToHandle(&map)) return;
295 for (int i = 0; i < length(); ++i) {
296 if (at(i).is_identical_to(map)) return;
301 void FilterForPossibleTransitions(Map* root_map) {
302 for (int i = list_.length() - 1; i >= 0; i--) {
303 if (at(i)->FindRootMap() != root_map) {
304 list_.RemoveElement(list_.at(i));
309 void Add(Handle<Map> handle, Zone* zone) {
310 list_.Add(handle.location(), zone);
313 Handle<Map> at(int i) const {
314 return Handle<Map>(list_.at(i));
317 Handle<Map> first() const { return at(0); }
318 Handle<Map> last() const { return at(length() - 1); }
321 // The list stores pointers to Map*, that is Map**, so it's GC safe.
322 SmallPointerList<Map*> list_;
324 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
328 class Expression : public AstNode {
331 // Not assigned a context yet, or else will not be visited during
334 // Evaluated for its side effects.
336 // Evaluated for its value (and side effects).
338 // Evaluated for control flow (and side effects).
342 // True iff the expression is a valid reference expression.
343 virtual bool IsValidReferenceExpression() const { return false; }
345 // Helpers for ToBoolean conversion.
346 virtual bool ToBooleanIsTrue() const { return false; }
347 virtual bool ToBooleanIsFalse() const { return false; }
349 // Symbols that cannot be parsed as array indices are considered property
350 // names. We do not treat symbols that can be array indexes as property
351 // names because [] for string objects is handled only by keyed ICs.
352 virtual bool IsPropertyName() const { return false; }
354 // True iff the expression is a literal represented as a smi.
355 bool IsSmiLiteral() const;
357 // True iff the expression is a string literal.
358 bool IsStringLiteral() const;
360 // True iff the expression is the null literal.
361 bool IsNullLiteral() const;
363 // True if we can prove that the expression is the undefined literal.
364 bool IsUndefinedLiteral(Isolate* isolate) const;
366 // True iff the expression is a valid target for an assignment.
367 bool IsValidReferenceExpressionOrThis() const;
369 // Expression type bounds
370 Bounds bounds() const { return bounds_; }
371 void set_bounds(Bounds bounds) { bounds_ = bounds; }
373 // Type feedback information for assignments and properties.
374 virtual bool IsMonomorphic() {
378 virtual SmallMapList* GetReceiverTypes() {
382 virtual KeyedAccessStoreMode GetStoreMode() const {
384 return STANDARD_STORE;
386 virtual IcCheckType GetKeyType() const {
391 // TODO(rossberg): this should move to its own AST node eventually.
392 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
393 uint16_t to_boolean_types() const {
394 return ToBooleanTypesField::decode(bit_field_);
397 void set_base_id(int id) { base_id_ = id; }
398 static int num_ids() { return parent_num_ids() + 2; }
399 BailoutId id() const { return BailoutId(local_id(0)); }
400 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
403 Expression(Zone* zone, int pos)
405 base_id_(BailoutId::None().ToInt()),
406 bounds_(Bounds::Unbounded(zone)),
408 static int parent_num_ids() { return 0; }
409 void set_to_boolean_types(uint16_t types) {
410 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
413 int base_id() const {
414 DCHECK(!BailoutId(base_id_).IsNone());
419 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
423 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
425 // Ends with 16-bit field; deriving classes in turn begin with
426 // 16-bit fields for optimum packing efficiency.
430 class BreakableStatement : public Statement {
433 TARGET_FOR_ANONYMOUS,
434 TARGET_FOR_NAMED_ONLY
437 // The labels associated with this statement. May be NULL;
438 // if it is != NULL, guaranteed to contain at least one entry.
439 ZoneList<const AstRawString*>* labels() const { return labels_; }
441 // Type testing & conversion.
442 BreakableStatement* AsBreakableStatement() final { return this; }
445 Label* break_target() { return &break_target_; }
448 bool is_target_for_anonymous() const {
449 return breakable_type_ == TARGET_FOR_ANONYMOUS;
452 void set_base_id(int id) { base_id_ = id; }
453 static int num_ids() { return parent_num_ids() + 2; }
454 BailoutId EntryId() const { return BailoutId(local_id(0)); }
455 BailoutId ExitId() const { return BailoutId(local_id(1)); }
458 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
459 BreakableType breakable_type, int position)
460 : Statement(zone, position),
462 breakable_type_(breakable_type),
463 base_id_(BailoutId::None().ToInt()) {
464 DCHECK(labels == NULL || labels->length() > 0);
466 static int parent_num_ids() { return 0; }
468 int base_id() const {
469 DCHECK(!BailoutId(base_id_).IsNone());
474 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
476 ZoneList<const AstRawString*>* labels_;
477 BreakableType breakable_type_;
483 class Block final : public BreakableStatement {
485 DECLARE_NODE_TYPE(Block)
487 void AddStatement(Statement* statement, Zone* zone) {
488 statements_.Add(statement, zone);
491 ZoneList<Statement*>* statements() { return &statements_; }
492 bool ignore_completion_value() const { return ignore_completion_value_; }
494 static int num_ids() { return parent_num_ids() + 1; }
495 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
497 bool IsJump() const override {
498 return !statements_.is_empty() && statements_.last()->IsJump()
499 && labels() == NULL; // Good enough as an approximation...
502 Scope* scope() const { return scope_; }
503 void set_scope(Scope* scope) { scope_ = scope; }
506 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
507 bool ignore_completion_value, int pos)
508 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
509 statements_(capacity, zone),
510 ignore_completion_value_(ignore_completion_value),
512 static int parent_num_ids() { return BreakableStatement::num_ids(); }
515 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
517 ZoneList<Statement*> statements_;
518 bool ignore_completion_value_;
523 class Declaration : public AstNode {
525 VariableProxy* proxy() const { return proxy_; }
526 VariableMode mode() const { return mode_; }
527 Scope* scope() const { return scope_; }
528 virtual InitializationFlag initialization() const = 0;
529 virtual bool IsInlineable() const;
532 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
534 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
535 DCHECK(IsDeclaredVariableMode(mode));
540 VariableProxy* proxy_;
542 // Nested scope from which the declaration originated.
547 class VariableDeclaration final : public Declaration {
549 DECLARE_NODE_TYPE(VariableDeclaration)
551 InitializationFlag initialization() const override {
552 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
555 bool is_class_declaration() const { return is_class_declaration_; }
557 // VariableDeclarations can be grouped into consecutive declaration
558 // groups. Each VariableDeclaration is associated with the start position of
559 // the group it belongs to. The positions are used for strong mode scope
560 // checks for classes and functions.
561 int declaration_group_start() const { return declaration_group_start_; }
564 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
565 Scope* scope, int pos, bool is_class_declaration = false,
566 int declaration_group_start = -1)
567 : Declaration(zone, proxy, mode, scope, pos),
568 is_class_declaration_(is_class_declaration),
569 declaration_group_start_(declaration_group_start) {}
571 bool is_class_declaration_;
572 int declaration_group_start_;
576 class FunctionDeclaration final : public Declaration {
578 DECLARE_NODE_TYPE(FunctionDeclaration)
580 FunctionLiteral* fun() const { return fun_; }
581 InitializationFlag initialization() const override {
582 return kCreatedInitialized;
584 bool IsInlineable() const override;
587 FunctionDeclaration(Zone* zone,
588 VariableProxy* proxy,
590 FunctionLiteral* fun,
593 : Declaration(zone, proxy, mode, scope, pos),
595 DCHECK(mode == VAR || mode == LET || mode == CONST);
600 FunctionLiteral* fun_;
604 class ImportDeclaration final : public Declaration {
606 DECLARE_NODE_TYPE(ImportDeclaration)
608 const AstRawString* import_name() const { return import_name_; }
609 const AstRawString* module_specifier() const { return module_specifier_; }
610 void set_module_specifier(const AstRawString* module_specifier) {
611 DCHECK(module_specifier_ == NULL);
612 module_specifier_ = module_specifier;
614 InitializationFlag initialization() const override {
615 return kNeedsInitialization;
619 ImportDeclaration(Zone* zone, VariableProxy* proxy,
620 const AstRawString* import_name,
621 const AstRawString* module_specifier, Scope* scope, int pos)
622 : Declaration(zone, proxy, IMPORT, scope, pos),
623 import_name_(import_name),
624 module_specifier_(module_specifier) {}
627 const AstRawString* import_name_;
628 const AstRawString* module_specifier_;
632 class ExportDeclaration final : public Declaration {
634 DECLARE_NODE_TYPE(ExportDeclaration)
636 InitializationFlag initialization() const override {
637 return kCreatedInitialized;
641 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
642 : Declaration(zone, proxy, LET, scope, pos) {}
646 class Module : public AstNode {
648 ModuleDescriptor* descriptor() const { return descriptor_; }
649 Block* body() const { return body_; }
652 Module(Zone* zone, int pos)
653 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
654 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
655 : AstNode(pos), descriptor_(descriptor), body_(body) {}
658 ModuleDescriptor* descriptor_;
663 class IterationStatement : public BreakableStatement {
665 // Type testing & conversion.
666 IterationStatement* AsIterationStatement() final { return this; }
668 Statement* body() const { return body_; }
670 static int num_ids() { return parent_num_ids() + 1; }
671 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
672 virtual BailoutId ContinueId() const = 0;
673 virtual BailoutId StackCheckId() const = 0;
676 Label* continue_target() { return &continue_target_; }
679 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
680 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
682 static int parent_num_ids() { return BreakableStatement::num_ids(); }
683 void Initialize(Statement* body) { body_ = body; }
686 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
689 Label continue_target_;
693 class DoWhileStatement final : public IterationStatement {
695 DECLARE_NODE_TYPE(DoWhileStatement)
697 void Initialize(Expression* cond, Statement* body) {
698 IterationStatement::Initialize(body);
702 Expression* cond() const { return cond_; }
704 static int num_ids() { return parent_num_ids() + 2; }
705 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
706 BailoutId StackCheckId() const override { return BackEdgeId(); }
707 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
710 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
711 : IterationStatement(zone, labels, pos), cond_(NULL) {}
712 static int parent_num_ids() { return IterationStatement::num_ids(); }
715 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
721 class WhileStatement final : public IterationStatement {
723 DECLARE_NODE_TYPE(WhileStatement)
725 void Initialize(Expression* cond, Statement* body) {
726 IterationStatement::Initialize(body);
730 Expression* cond() const { return cond_; }
732 static int num_ids() { return parent_num_ids() + 1; }
733 BailoutId ContinueId() const override { return EntryId(); }
734 BailoutId StackCheckId() const override { return BodyId(); }
735 BailoutId BodyId() const { return BailoutId(local_id(0)); }
738 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
739 : IterationStatement(zone, labels, pos), cond_(NULL) {}
740 static int parent_num_ids() { return IterationStatement::num_ids(); }
743 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
749 class ForStatement final : public IterationStatement {
751 DECLARE_NODE_TYPE(ForStatement)
753 void Initialize(Statement* init,
757 IterationStatement::Initialize(body);
763 Statement* init() const { return init_; }
764 Expression* cond() const { return cond_; }
765 Statement* next() const { return next_; }
767 static int num_ids() { return parent_num_ids() + 2; }
768 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
769 BailoutId StackCheckId() const override { return BodyId(); }
770 BailoutId BodyId() const { return BailoutId(local_id(1)); }
773 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
774 : IterationStatement(zone, labels, pos),
778 static int parent_num_ids() { return IterationStatement::num_ids(); }
781 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
789 class ForEachStatement : public IterationStatement {
792 ENUMERATE, // for (each in subject) body;
793 ITERATE // for (each of subject) body;
796 void Initialize(Expression* each, Expression* subject, Statement* body) {
797 IterationStatement::Initialize(body);
802 Expression* each() const { return each_; }
803 Expression* subject() const { return subject_; }
805 FeedbackVectorRequirements ComputeFeedbackRequirements(
806 Isolate* isolate, const ICSlotCache* cache) override;
807 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
808 ICSlotCache* cache) override {
811 Code::Kind FeedbackICSlotKind(int index) override;
812 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
815 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
816 : IterationStatement(zone, labels, pos),
819 each_slot_(FeedbackVectorICSlot::Invalid()) {}
823 Expression* subject_;
824 FeedbackVectorICSlot each_slot_;
828 class ForInStatement final : public ForEachStatement {
830 DECLARE_NODE_TYPE(ForInStatement)
832 Expression* enumerable() const {
836 // Type feedback information.
837 FeedbackVectorRequirements ComputeFeedbackRequirements(
838 Isolate* isolate, const ICSlotCache* cache) override {
839 FeedbackVectorRequirements base =
840 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
841 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
842 return FeedbackVectorRequirements(1, base.ic_slots());
844 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
845 for_in_feedback_slot_ = slot;
848 FeedbackVectorSlot ForInFeedbackSlot() {
849 DCHECK(!for_in_feedback_slot_.IsInvalid());
850 return for_in_feedback_slot_;
853 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
854 ForInType for_in_type() const { return for_in_type_; }
855 void set_for_in_type(ForInType type) { for_in_type_ = type; }
857 static int num_ids() { return parent_num_ids() + 6; }
858 BailoutId BodyId() const { return BailoutId(local_id(0)); }
859 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
860 BailoutId EnumId() const { return BailoutId(local_id(2)); }
861 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
862 BailoutId FilterId() const { return BailoutId(local_id(4)); }
863 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
864 BailoutId ContinueId() const override { return EntryId(); }
865 BailoutId StackCheckId() const override { return BodyId(); }
868 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
869 : ForEachStatement(zone, labels, pos),
870 for_in_type_(SLOW_FOR_IN),
871 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
872 static int parent_num_ids() { return ForEachStatement::num_ids(); }
875 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
877 ForInType for_in_type_;
878 FeedbackVectorSlot for_in_feedback_slot_;
882 class ForOfStatement final : public ForEachStatement {
884 DECLARE_NODE_TYPE(ForOfStatement)
886 void Initialize(Expression* each,
889 Expression* assign_iterator,
890 Expression* next_result,
891 Expression* result_done,
892 Expression* assign_each) {
893 ForEachStatement::Initialize(each, subject, body);
894 assign_iterator_ = assign_iterator;
895 next_result_ = next_result;
896 result_done_ = result_done;
897 assign_each_ = assign_each;
900 Expression* iterable() const {
904 // iterator = subject[Symbol.iterator]()
905 Expression* assign_iterator() const {
906 return assign_iterator_;
909 // result = iterator.next() // with type check
910 Expression* next_result() const {
915 Expression* result_done() const {
919 // each = result.value
920 Expression* assign_each() const {
924 BailoutId ContinueId() const override { return EntryId(); }
925 BailoutId StackCheckId() const override { return BackEdgeId(); }
927 static int num_ids() { return parent_num_ids() + 1; }
928 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
931 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
932 : ForEachStatement(zone, labels, pos),
933 assign_iterator_(NULL),
936 assign_each_(NULL) {}
937 static int parent_num_ids() { return ForEachStatement::num_ids(); }
940 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
942 Expression* assign_iterator_;
943 Expression* next_result_;
944 Expression* result_done_;
945 Expression* assign_each_;
949 class ExpressionStatement final : public Statement {
951 DECLARE_NODE_TYPE(ExpressionStatement)
953 void set_expression(Expression* e) { expression_ = e; }
954 Expression* expression() const { return expression_; }
955 bool IsJump() const override { return expression_->IsThrow(); }
958 ExpressionStatement(Zone* zone, Expression* expression, int pos)
959 : Statement(zone, pos), expression_(expression) { }
962 Expression* expression_;
966 class JumpStatement : public Statement {
968 bool IsJump() const final { return true; }
971 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
975 class ContinueStatement final : public JumpStatement {
977 DECLARE_NODE_TYPE(ContinueStatement)
979 IterationStatement* target() const { return target_; }
982 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
983 : JumpStatement(zone, pos), target_(target) { }
986 IterationStatement* target_;
990 class BreakStatement final : public JumpStatement {
992 DECLARE_NODE_TYPE(BreakStatement)
994 BreakableStatement* target() const { return target_; }
997 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
998 : JumpStatement(zone, pos), target_(target) { }
1001 BreakableStatement* target_;
1005 class ReturnStatement final : public JumpStatement {
1007 DECLARE_NODE_TYPE(ReturnStatement)
1009 Expression* expression() const { return expression_; }
1012 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1013 : JumpStatement(zone, pos), expression_(expression) { }
1016 Expression* expression_;
1020 class WithStatement final : public Statement {
1022 DECLARE_NODE_TYPE(WithStatement)
1024 Scope* scope() { return scope_; }
1025 Expression* expression() const { return expression_; }
1026 Statement* statement() const { return statement_; }
1028 void set_base_id(int id) { base_id_ = id; }
1029 static int num_ids() { return parent_num_ids() + 1; }
1030 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1033 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1034 Statement* statement, int pos)
1035 : Statement(zone, pos),
1037 expression_(expression),
1038 statement_(statement),
1039 base_id_(BailoutId::None().ToInt()) {}
1040 static int parent_num_ids() { return 0; }
1042 int base_id() const {
1043 DCHECK(!BailoutId(base_id_).IsNone());
1048 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1051 Expression* expression_;
1052 Statement* statement_;
1057 class CaseClause final : public Expression {
1059 DECLARE_NODE_TYPE(CaseClause)
1061 bool is_default() const { return label_ == NULL; }
1062 Expression* label() const {
1063 CHECK(!is_default());
1066 Label* body_target() { return &body_target_; }
1067 ZoneList<Statement*>* statements() const { return statements_; }
1069 static int num_ids() { return parent_num_ids() + 2; }
1070 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1071 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1073 Type* compare_type() { return compare_type_; }
1074 void set_compare_type(Type* type) { compare_type_ = type; }
1077 static int parent_num_ids() { return Expression::num_ids(); }
1080 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1082 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1086 ZoneList<Statement*>* statements_;
1087 Type* compare_type_;
1091 class SwitchStatement final : public BreakableStatement {
1093 DECLARE_NODE_TYPE(SwitchStatement)
1095 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1100 Expression* tag() const { return tag_; }
1101 ZoneList<CaseClause*>* cases() const { return cases_; }
1104 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1105 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1111 ZoneList<CaseClause*>* cases_;
1115 // If-statements always have non-null references to their then- and
1116 // else-parts. When parsing if-statements with no explicit else-part,
1117 // the parser implicitly creates an empty statement. Use the
1118 // HasThenStatement() and HasElseStatement() functions to check if a
1119 // given if-statement has a then- or an else-part containing code.
1120 class IfStatement final : public Statement {
1122 DECLARE_NODE_TYPE(IfStatement)
1124 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1125 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1127 Expression* condition() const { return condition_; }
1128 Statement* then_statement() const { return then_statement_; }
1129 Statement* else_statement() const { return else_statement_; }
1131 bool IsJump() const override {
1132 return HasThenStatement() && then_statement()->IsJump()
1133 && HasElseStatement() && else_statement()->IsJump();
1136 void set_base_id(int id) { base_id_ = id; }
1137 static int num_ids() { return parent_num_ids() + 3; }
1138 BailoutId IfId() const { return BailoutId(local_id(0)); }
1139 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1140 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1143 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1144 Statement* else_statement, int pos)
1145 : Statement(zone, pos),
1146 condition_(condition),
1147 then_statement_(then_statement),
1148 else_statement_(else_statement),
1149 base_id_(BailoutId::None().ToInt()) {}
1150 static int parent_num_ids() { return 0; }
1152 int base_id() const {
1153 DCHECK(!BailoutId(base_id_).IsNone());
1158 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1160 Expression* condition_;
1161 Statement* then_statement_;
1162 Statement* else_statement_;
1167 class TryStatement : public Statement {
1169 Block* try_block() const { return try_block_; }
1171 void set_base_id(int id) { base_id_ = id; }
1172 static int num_ids() { return parent_num_ids() + 1; }
1173 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1176 TryStatement(Zone* zone, Block* try_block, int pos)
1177 : Statement(zone, pos),
1178 try_block_(try_block),
1179 base_id_(BailoutId::None().ToInt()) {}
1180 static int parent_num_ids() { return 0; }
1182 int base_id() const {
1183 DCHECK(!BailoutId(base_id_).IsNone());
1188 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1195 class TryCatchStatement final : public TryStatement {
1197 DECLARE_NODE_TYPE(TryCatchStatement)
1199 Scope* scope() { return scope_; }
1200 Variable* variable() { return variable_; }
1201 Block* catch_block() const { return catch_block_; }
1204 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1205 Variable* variable, Block* catch_block, int pos)
1206 : TryStatement(zone, try_block, pos),
1208 variable_(variable),
1209 catch_block_(catch_block) {}
1213 Variable* variable_;
1214 Block* catch_block_;
1218 class TryFinallyStatement final : public TryStatement {
1220 DECLARE_NODE_TYPE(TryFinallyStatement)
1222 Block* finally_block() const { return finally_block_; }
1225 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1227 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1230 Block* finally_block_;
1234 class DebuggerStatement final : public Statement {
1236 DECLARE_NODE_TYPE(DebuggerStatement)
1238 void set_base_id(int id) { base_id_ = id; }
1239 static int num_ids() { return parent_num_ids() + 1; }
1240 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1243 explicit DebuggerStatement(Zone* zone, int pos)
1244 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1245 static int parent_num_ids() { return 0; }
1247 int base_id() const {
1248 DCHECK(!BailoutId(base_id_).IsNone());
1253 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1259 class EmptyStatement final : public Statement {
1261 DECLARE_NODE_TYPE(EmptyStatement)
1264 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1268 class Literal final : public Expression {
1270 DECLARE_NODE_TYPE(Literal)
1272 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1274 Handle<String> AsPropertyName() {
1275 DCHECK(IsPropertyName());
1276 return Handle<String>::cast(value());
1279 const AstRawString* AsRawPropertyName() {
1280 DCHECK(IsPropertyName());
1281 return value_->AsString();
1284 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1285 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1287 Handle<Object> value() const { return value_->value(); }
1288 const AstValue* raw_value() const { return value_; }
1290 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1291 // only for string and number literals!
1293 static bool Match(void* literal1, void* literal2);
1295 static int num_ids() { return parent_num_ids() + 1; }
1296 TypeFeedbackId LiteralFeedbackId() const {
1297 return TypeFeedbackId(local_id(0));
1301 Literal(Zone* zone, const AstValue* value, int position)
1302 : Expression(zone, position), value_(value) {}
1303 static int parent_num_ids() { return Expression::num_ids(); }
1306 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1308 const AstValue* value_;
1312 class AstLiteralReindexer;
1314 // Base class for literals that needs space in the corresponding JSFunction.
1315 class MaterializedLiteral : public Expression {
1317 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1319 int literal_index() { return literal_index_; }
1322 // only callable after initialization.
1323 DCHECK(depth_ >= 1);
1327 bool is_strong() const { return is_strong_; }
1330 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1331 : Expression(zone, pos),
1332 literal_index_(literal_index),
1334 is_strong_(is_strong),
1337 // A materialized literal is simple if the values consist of only
1338 // constants and simple object and array literals.
1339 bool is_simple() const { return is_simple_; }
1340 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1341 friend class CompileTimeValue;
1343 void set_depth(int depth) {
1348 // Populate the constant properties/elements fixed array.
1349 void BuildConstants(Isolate* isolate);
1350 friend class ArrayLiteral;
1351 friend class ObjectLiteral;
1353 // If the expression is a literal, return the literal value;
1354 // if the expression is a materialized literal and is simple return a
1355 // compile time value as encoded by CompileTimeValue::GetValue().
1356 // Otherwise, return undefined literal as the placeholder
1357 // in the object literal boilerplate.
1358 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1366 friend class AstLiteralReindexer;
1370 // Property is used for passing information
1371 // about an object literal's properties from the parser
1372 // to the code generator.
1373 class ObjectLiteralProperty final : public ZoneObject {
1376 CONSTANT, // Property with constant value (compile time).
1377 COMPUTED, // Property with computed value (execution time).
1378 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1379 GETTER, SETTER, // Property is an accessor function.
1380 PROTOTYPE // Property is __proto__.
1383 Expression* key() { return key_; }
1384 Expression* value() { return value_; }
1385 Kind kind() { return kind_; }
1387 // Type feedback information.
1388 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1389 Handle<Map> GetReceiverType() { return receiver_type_; }
1391 bool IsCompileTimeValue();
1393 void set_emit_store(bool emit_store);
1396 bool is_static() const { return is_static_; }
1397 bool is_computed_name() const { return is_computed_name_; }
1399 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1402 friend class AstNodeFactory;
1404 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1405 bool is_static, bool is_computed_name);
1406 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1407 Expression* value, bool is_static,
1408 bool is_computed_name);
1416 bool is_computed_name_;
1417 Handle<Map> receiver_type_;
1421 // An object literal has a boilerplate object that is used
1422 // for minimizing the work when constructing it at runtime.
1423 class ObjectLiteral final : public MaterializedLiteral {
1425 typedef ObjectLiteralProperty Property;
1427 DECLARE_NODE_TYPE(ObjectLiteral)
1429 Handle<FixedArray> constant_properties() const {
1430 return constant_properties_;
1432 int properties_count() const { return constant_properties_->length() / 2; }
1433 ZoneList<Property*>* properties() const { return properties_; }
1434 bool fast_elements() const { return fast_elements_; }
1435 bool may_store_doubles() const { return may_store_doubles_; }
1436 bool has_function() const { return has_function_; }
1437 bool has_elements() const { return has_elements_; }
1439 // Decide if a property should be in the object boilerplate.
1440 static bool IsBoilerplateProperty(Property* property);
1442 // Populate the constant properties fixed array.
1443 void BuildConstantProperties(Isolate* isolate);
1445 // Mark all computed expressions that are bound to a key that
1446 // is shadowed by a later occurrence of the same key. For the
1447 // marked expressions, no store code is emitted.
1448 void CalculateEmitStore(Zone* zone);
1450 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1451 int ComputeFlags(bool disable_mementos = false) const {
1452 int flags = fast_elements() ? kFastElements : kNoFlags;
1453 flags |= has_function() ? kHasFunction : kNoFlags;
1454 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1455 flags |= kShallowProperties;
1457 if (disable_mementos) {
1458 flags |= kDisableMementos;
1469 kHasFunction = 1 << 1,
1470 kShallowProperties = 1 << 2,
1471 kDisableMementos = 1 << 3,
1475 struct Accessors: public ZoneObject {
1476 Accessors() : getter(NULL), setter(NULL) {}
1481 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1483 // Return an AST id for a property that is used in simulate instructions.
1484 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1486 // Unlike other AST nodes, this number of bailout IDs allocated for an
1487 // ObjectLiteral can vary, so num_ids() is not a static method.
1488 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1490 // Object literals need one feedback slot for each non-trivial value, as well
1491 // as some slots for home objects.
1492 FeedbackVectorRequirements ComputeFeedbackRequirements(
1493 Isolate* isolate, const ICSlotCache* cache) override;
1494 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1495 ICSlotCache* cache) override {
1498 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1499 FeedbackVectorICSlot GetNthSlot(int n) const {
1500 return FeedbackVectorICSlot(slot_.ToInt() + n);
1503 // If value needs a home object, returns a valid feedback vector ic slot
1504 // given by slot_index, and increments slot_index.
1505 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1506 int* slot_index) const;
1509 int slot_count() const { return slot_count_; }
1513 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1514 int boilerplate_properties, bool has_function, bool is_strong,
1516 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1517 properties_(properties),
1518 boilerplate_properties_(boilerplate_properties),
1519 fast_elements_(false),
1520 has_elements_(false),
1521 may_store_doubles_(false),
1522 has_function_(has_function),
1526 slot_(FeedbackVectorICSlot::Invalid()) {
1528 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1531 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1532 Handle<FixedArray> constant_properties_;
1533 ZoneList<Property*>* properties_;
1534 int boilerplate_properties_;
1535 bool fast_elements_;
1537 bool may_store_doubles_;
1540 // slot_count_ helps validate that the logic to allocate ic slots and the
1541 // logic to use them are in sync.
1544 FeedbackVectorICSlot slot_;
1548 // Node for capturing a regexp literal.
1549 class RegExpLiteral final : public MaterializedLiteral {
1551 DECLARE_NODE_TYPE(RegExpLiteral)
1553 Handle<String> pattern() const { return pattern_->string(); }
1554 Handle<String> flags() const { return flags_->string(); }
1557 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1558 const AstRawString* flags, int literal_index, bool is_strong,
1560 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1567 const AstRawString* pattern_;
1568 const AstRawString* flags_;
1572 // An array literal has a literals object that is used
1573 // for minimizing the work when constructing it at runtime.
1574 class ArrayLiteral final : public MaterializedLiteral {
1576 DECLARE_NODE_TYPE(ArrayLiteral)
1578 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1579 ElementsKind constant_elements_kind() const {
1580 DCHECK_EQ(2, constant_elements_->length());
1581 return static_cast<ElementsKind>(
1582 Smi::cast(constant_elements_->get(0))->value());
1585 ZoneList<Expression*>* values() const { return values_; }
1587 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1589 // Return an AST id for an element that is used in simulate instructions.
1590 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1592 // Unlike other AST nodes, this number of bailout IDs allocated for an
1593 // ArrayLiteral can vary, so num_ids() is not a static method.
1594 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1596 // Populate the constant elements fixed array.
1597 void BuildConstantElements(Isolate* isolate);
1599 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1600 int ComputeFlags(bool disable_mementos = false) const {
1601 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1602 if (disable_mementos) {
1603 flags |= kDisableMementos;
1613 kShallowElements = 1,
1614 kDisableMementos = 1 << 1,
1619 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1620 int first_spread_index, int literal_index, bool is_strong,
1622 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1624 first_spread_index_(first_spread_index) {}
1625 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1628 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1630 Handle<FixedArray> constant_elements_;
1631 ZoneList<Expression*>* values_;
1632 int first_spread_index_;
1636 class VariableProxy final : public Expression {
1638 DECLARE_NODE_TYPE(VariableProxy)
1640 bool IsValidReferenceExpression() const override { return !is_this(); }
1642 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1644 Handle<String> name() const { return raw_name()->string(); }
1645 const AstRawString* raw_name() const {
1646 return is_resolved() ? var_->raw_name() : raw_name_;
1649 Variable* var() const {
1650 DCHECK(is_resolved());
1653 void set_var(Variable* v) {
1654 DCHECK(!is_resolved());
1659 bool is_this() const { return IsThisField::decode(bit_field_); }
1661 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1662 void set_is_assigned() {
1663 bit_field_ = IsAssignedField::update(bit_field_, true);
1666 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1667 void set_is_resolved() {
1668 bit_field_ = IsResolvedField::update(bit_field_, true);
1671 int end_position() const { return end_position_; }
1673 // Bind this proxy to the variable var.
1674 void BindTo(Variable* var);
1676 bool UsesVariableFeedbackSlot() const {
1677 return var()->IsUnallocated() || var()->IsLookupSlot();
1680 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1681 Isolate* isolate, const ICSlotCache* cache) override;
1683 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1684 ICSlotCache* cache) override;
1685 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1686 FeedbackVectorICSlot VariableFeedbackSlot() {
1687 return variable_feedback_slot_;
1690 static int num_ids() { return parent_num_ids() + 1; }
1691 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1694 VariableProxy(Zone* zone, Variable* var, int start_position,
1697 VariableProxy(Zone* zone, const AstRawString* name,
1698 Variable::Kind variable_kind, int start_position,
1700 static int parent_num_ids() { return Expression::num_ids(); }
1701 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1703 class IsThisField : public BitField8<bool, 0, 1> {};
1704 class IsAssignedField : public BitField8<bool, 1, 1> {};
1705 class IsResolvedField : public BitField8<bool, 2, 1> {};
1707 // Start with 16-bit (or smaller) field, which should get packed together
1708 // with Expression's trailing 16-bit field.
1710 FeedbackVectorICSlot variable_feedback_slot_;
1712 const AstRawString* raw_name_; // if !is_resolved_
1713 Variable* var_; // if is_resolved_
1715 // Position is stored in the AstNode superclass, but VariableProxy needs to
1716 // know its end position too (for error messages). It cannot be inferred from
1717 // the variable name length because it can contain escapes.
1722 // Left-hand side can only be a property, a global or a (parameter or local)
1728 NAMED_SUPER_PROPERTY,
1729 KEYED_SUPER_PROPERTY
1733 class Property final : public Expression {
1735 DECLARE_NODE_TYPE(Property)
1737 bool IsValidReferenceExpression() const override { return true; }
1739 Expression* obj() const { return obj_; }
1740 Expression* key() const { return key_; }
1742 static int num_ids() { return parent_num_ids() + 1; }
1743 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1745 bool IsStringAccess() const {
1746 return IsStringAccessField::decode(bit_field_);
1749 // Type feedback information.
1750 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1751 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1752 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1753 IcCheckType GetKeyType() const override {
1754 return KeyTypeField::decode(bit_field_);
1756 bool IsUninitialized() const {
1757 return !is_for_call() && HasNoTypeInformation();
1759 bool HasNoTypeInformation() const {
1760 return GetInlineCacheState() == UNINITIALIZED;
1762 InlineCacheState GetInlineCacheState() const {
1763 return InlineCacheStateField::decode(bit_field_);
1765 void set_is_string_access(bool b) {
1766 bit_field_ = IsStringAccessField::update(bit_field_, b);
1768 void set_key_type(IcCheckType key_type) {
1769 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1771 void set_inline_cache_state(InlineCacheState state) {
1772 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1774 void mark_for_call() {
1775 bit_field_ = IsForCallField::update(bit_field_, true);
1777 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1779 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1781 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1782 Isolate* isolate, const ICSlotCache* cache) override {
1783 return FeedbackVectorRequirements(0, 1);
1785 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1786 ICSlotCache* cache) override {
1787 property_feedback_slot_ = slot;
1789 Code::Kind FeedbackICSlotKind(int index) override {
1790 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1793 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1794 return property_feedback_slot_;
1797 static LhsKind GetAssignType(Property* property) {
1798 if (property == NULL) return VARIABLE;
1799 bool super_access = property->IsSuperAccess();
1800 return (property->key()->IsPropertyName())
1801 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1802 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1806 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1807 : Expression(zone, pos),
1808 bit_field_(IsForCallField::encode(false) |
1809 IsStringAccessField::encode(false) |
1810 InlineCacheStateField::encode(UNINITIALIZED)),
1811 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1814 static int parent_num_ids() { return Expression::num_ids(); }
1817 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1819 class IsForCallField : public BitField8<bool, 0, 1> {};
1820 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1821 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1822 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1824 FeedbackVectorICSlot property_feedback_slot_;
1827 SmallMapList receiver_types_;
1831 class Call final : public Expression {
1833 DECLARE_NODE_TYPE(Call)
1835 Expression* expression() const { return expression_; }
1836 ZoneList<Expression*>* arguments() const { return arguments_; }
1838 // Type feedback information.
1839 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1840 Isolate* isolate, const ICSlotCache* cache) override;
1841 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1842 ICSlotCache* cache) override {
1845 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1846 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1848 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1850 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1852 SmallMapList* GetReceiverTypes() override {
1853 if (expression()->IsProperty()) {
1854 return expression()->AsProperty()->GetReceiverTypes();
1859 bool IsMonomorphic() override {
1860 if (expression()->IsProperty()) {
1861 return expression()->AsProperty()->IsMonomorphic();
1863 return !target_.is_null();
1866 bool global_call() const {
1867 VariableProxy* proxy = expression_->AsVariableProxy();
1868 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1871 bool known_global_function() const {
1872 return global_call() && !target_.is_null();
1875 Handle<JSFunction> target() { return target_; }
1877 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1879 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1881 set_is_uninitialized(false);
1883 void set_target(Handle<JSFunction> target) { target_ = target; }
1884 void set_allocation_site(Handle<AllocationSite> site) {
1885 allocation_site_ = site;
1888 static int num_ids() { return parent_num_ids() + 3; }
1889 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1890 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1891 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1893 bool is_uninitialized() const {
1894 return IsUninitializedField::decode(bit_field_);
1896 void set_is_uninitialized(bool b) {
1897 bit_field_ = IsUninitializedField::update(bit_field_, b);
1909 // Helpers to determine how to handle the call.
1910 CallType GetCallType(Isolate* isolate) const;
1911 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1912 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1915 // Used to assert that the FullCodeGenerator records the return site.
1916 bool return_is_recorded_;
1920 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1922 : Expression(zone, pos),
1923 ic_slot_(FeedbackVectorICSlot::Invalid()),
1924 slot_(FeedbackVectorSlot::Invalid()),
1925 expression_(expression),
1926 arguments_(arguments),
1927 bit_field_(IsUninitializedField::encode(false)) {
1928 if (expression->IsProperty()) {
1929 expression->AsProperty()->mark_for_call();
1932 static int parent_num_ids() { return Expression::num_ids(); }
1935 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1937 FeedbackVectorICSlot ic_slot_;
1938 FeedbackVectorSlot slot_;
1939 Expression* expression_;
1940 ZoneList<Expression*>* arguments_;
1941 Handle<JSFunction> target_;
1942 Handle<AllocationSite> allocation_site_;
1943 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1948 class CallNew final : public Expression {
1950 DECLARE_NODE_TYPE(CallNew)
1952 Expression* expression() const { return expression_; }
1953 ZoneList<Expression*>* arguments() const { return arguments_; }
1955 // Type feedback information.
1956 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1957 Isolate* isolate, const ICSlotCache* cache) override {
1958 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1960 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1961 callnew_feedback_slot_ = slot;
1964 FeedbackVectorSlot CallNewFeedbackSlot() {
1965 DCHECK(!callnew_feedback_slot_.IsInvalid());
1966 return callnew_feedback_slot_;
1968 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1969 DCHECK(FLAG_pretenuring_call_new);
1970 return CallNewFeedbackSlot().next();
1973 bool IsMonomorphic() override { return is_monomorphic_; }
1974 Handle<JSFunction> target() const { return target_; }
1975 Handle<AllocationSite> allocation_site() const {
1976 return allocation_site_;
1979 static int num_ids() { return parent_num_ids() + 1; }
1980 static int feedback_slots() { return 1; }
1981 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1983 void set_allocation_site(Handle<AllocationSite> site) {
1984 allocation_site_ = site;
1986 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1987 void set_target(Handle<JSFunction> target) { target_ = target; }
1988 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1990 is_monomorphic_ = true;
1994 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1996 : Expression(zone, pos),
1997 expression_(expression),
1998 arguments_(arguments),
1999 is_monomorphic_(false),
2000 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2002 static int parent_num_ids() { return Expression::num_ids(); }
2005 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2007 Expression* expression_;
2008 ZoneList<Expression*>* arguments_;
2009 bool is_monomorphic_;
2010 Handle<JSFunction> target_;
2011 Handle<AllocationSite> allocation_site_;
2012 FeedbackVectorSlot callnew_feedback_slot_;
2016 // The CallRuntime class does not represent any official JavaScript
2017 // language construct. Instead it is used to call a C or JS function
2018 // with a set of arguments. This is used from the builtins that are
2019 // implemented in JavaScript (see "v8natives.js").
2020 class CallRuntime final : public Expression {
2022 DECLARE_NODE_TYPE(CallRuntime)
2024 Handle<String> name() const { return raw_name_->string(); }
2025 const AstRawString* raw_name() const { return raw_name_; }
2026 const Runtime::Function* function() const { return function_; }
2027 ZoneList<Expression*>* arguments() const { return arguments_; }
2028 bool is_jsruntime() const { return function_ == NULL; }
2030 // Type feedback information.
2031 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2032 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2033 Isolate* isolate, const ICSlotCache* cache) override {
2034 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2036 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2037 ICSlotCache* cache) override {
2038 callruntime_feedback_slot_ = slot;
2040 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2042 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2043 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2044 !callruntime_feedback_slot_.IsInvalid());
2045 return callruntime_feedback_slot_;
2048 static int num_ids() { return parent_num_ids() + 1; }
2049 BailoutId CallId() { return BailoutId(local_id(0)); }
2052 CallRuntime(Zone* zone, const AstRawString* name,
2053 const Runtime::Function* function,
2054 ZoneList<Expression*>* arguments, int pos)
2055 : Expression(zone, pos),
2057 function_(function),
2058 arguments_(arguments),
2059 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2060 static int parent_num_ids() { return Expression::num_ids(); }
2063 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2065 const AstRawString* raw_name_;
2066 const Runtime::Function* function_;
2067 ZoneList<Expression*>* arguments_;
2068 FeedbackVectorICSlot callruntime_feedback_slot_;
2072 class UnaryOperation final : public Expression {
2074 DECLARE_NODE_TYPE(UnaryOperation)
2076 Token::Value op() const { return op_; }
2077 Expression* expression() const { return expression_; }
2079 // For unary not (Token::NOT), the AST ids where true and false will
2080 // actually be materialized, respectively.
2081 static int num_ids() { return parent_num_ids() + 2; }
2082 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2083 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2085 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2088 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2089 : Expression(zone, pos), op_(op), expression_(expression) {
2090 DCHECK(Token::IsUnaryOp(op));
2092 static int parent_num_ids() { return Expression::num_ids(); }
2095 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2098 Expression* expression_;
2102 class BinaryOperation final : public Expression {
2104 DECLARE_NODE_TYPE(BinaryOperation)
2106 Token::Value op() const { return static_cast<Token::Value>(op_); }
2107 Expression* left() const { return left_; }
2108 Expression* right() const { return right_; }
2109 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2110 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2111 allocation_site_ = allocation_site;
2114 // The short-circuit logical operations need an AST ID for their
2115 // right-hand subexpression.
2116 static int num_ids() { return parent_num_ids() + 2; }
2117 BailoutId RightId() const { return BailoutId(local_id(0)); }
2119 TypeFeedbackId BinaryOperationFeedbackId() const {
2120 return TypeFeedbackId(local_id(1));
2122 Maybe<int> fixed_right_arg() const {
2123 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2125 void set_fixed_right_arg(Maybe<int> arg) {
2126 has_fixed_right_arg_ = arg.IsJust();
2127 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2130 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2133 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2134 Expression* right, int pos)
2135 : Expression(zone, pos),
2136 op_(static_cast<byte>(op)),
2137 has_fixed_right_arg_(false),
2138 fixed_right_arg_value_(0),
2141 DCHECK(Token::IsBinaryOp(op));
2143 static int parent_num_ids() { return Expression::num_ids(); }
2146 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2148 const byte op_; // actually Token::Value
2149 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2150 // type for the RHS. Currenty it's actually a Maybe<int>
2151 bool has_fixed_right_arg_;
2152 int fixed_right_arg_value_;
2155 Handle<AllocationSite> allocation_site_;
2159 class CountOperation final : public Expression {
2161 DECLARE_NODE_TYPE(CountOperation)
2163 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2164 bool is_postfix() const { return !is_prefix(); }
2166 Token::Value op() const { return TokenField::decode(bit_field_); }
2167 Token::Value binary_op() {
2168 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2171 Expression* expression() const { return expression_; }
2173 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2174 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2175 IcCheckType GetKeyType() const override {
2176 return KeyTypeField::decode(bit_field_);
2178 KeyedAccessStoreMode GetStoreMode() const override {
2179 return StoreModeField::decode(bit_field_);
2181 Type* type() const { return type_; }
2182 void set_key_type(IcCheckType type) {
2183 bit_field_ = KeyTypeField::update(bit_field_, type);
2185 void set_store_mode(KeyedAccessStoreMode mode) {
2186 bit_field_ = StoreModeField::update(bit_field_, mode);
2188 void set_type(Type* type) { type_ = type; }
2190 static int num_ids() { return parent_num_ids() + 4; }
2191 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2192 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2193 TypeFeedbackId CountBinOpFeedbackId() const {
2194 return TypeFeedbackId(local_id(2));
2196 TypeFeedbackId CountStoreFeedbackId() const {
2197 return TypeFeedbackId(local_id(3));
2200 FeedbackVectorRequirements ComputeFeedbackRequirements(
2201 Isolate* isolate, const ICSlotCache* cache) override;
2202 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2203 ICSlotCache* cache) override {
2206 Code::Kind FeedbackICSlotKind(int index) override;
2207 FeedbackVectorICSlot CountSlot() const { return slot_; }
2210 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2212 : Expression(zone, pos),
2214 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2215 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2218 slot_(FeedbackVectorICSlot::Invalid()) {}
2219 static int parent_num_ids() { return Expression::num_ids(); }
2222 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2224 class IsPrefixField : public BitField16<bool, 0, 1> {};
2225 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2226 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2227 class TokenField : public BitField16<Token::Value, 6, 8> {};
2229 // Starts with 16-bit field, which should get packed together with
2230 // Expression's trailing 16-bit field.
2231 uint16_t bit_field_;
2233 Expression* expression_;
2234 SmallMapList receiver_types_;
2235 FeedbackVectorICSlot slot_;
2239 class CompareOperation final : public Expression {
2241 DECLARE_NODE_TYPE(CompareOperation)
2243 Token::Value op() const { return op_; }
2244 Expression* left() const { return left_; }
2245 Expression* right() const { return right_; }
2247 // Type feedback information.
2248 static int num_ids() { return parent_num_ids() + 1; }
2249 TypeFeedbackId CompareOperationFeedbackId() const {
2250 return TypeFeedbackId(local_id(0));
2252 Type* combined_type() const { return combined_type_; }
2253 void set_combined_type(Type* type) { combined_type_ = type; }
2255 // Match special cases.
2256 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2257 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2258 bool IsLiteralCompareNull(Expression** expr);
2261 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2262 Expression* right, int pos)
2263 : Expression(zone, pos),
2267 combined_type_(Type::None(zone)) {
2268 DCHECK(Token::IsCompareOp(op));
2270 static int parent_num_ids() { return Expression::num_ids(); }
2273 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2279 Type* combined_type_;
2283 class Spread final : public Expression {
2285 DECLARE_NODE_TYPE(Spread)
2287 Expression* expression() const { return expression_; }
2289 static int num_ids() { return parent_num_ids(); }
2292 Spread(Zone* zone, Expression* expression, int pos)
2293 : Expression(zone, pos), expression_(expression) {}
2294 static int parent_num_ids() { return Expression::num_ids(); }
2297 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2299 Expression* expression_;
2303 class Conditional final : public Expression {
2305 DECLARE_NODE_TYPE(Conditional)
2307 Expression* condition() const { return condition_; }
2308 Expression* then_expression() const { return then_expression_; }
2309 Expression* else_expression() const { return else_expression_; }
2311 static int num_ids() { return parent_num_ids() + 2; }
2312 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2313 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2316 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2317 Expression* else_expression, int position)
2318 : Expression(zone, position),
2319 condition_(condition),
2320 then_expression_(then_expression),
2321 else_expression_(else_expression) {}
2322 static int parent_num_ids() { return Expression::num_ids(); }
2325 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2327 Expression* condition_;
2328 Expression* then_expression_;
2329 Expression* else_expression_;
2333 class Assignment final : public Expression {
2335 DECLARE_NODE_TYPE(Assignment)
2337 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2339 Token::Value binary_op() const;
2341 Token::Value op() const { return TokenField::decode(bit_field_); }
2342 Expression* target() const { return target_; }
2343 Expression* value() const { return value_; }
2344 BinaryOperation* binary_operation() const { return binary_operation_; }
2346 // This check relies on the definition order of token in token.h.
2347 bool is_compound() const { return op() > Token::ASSIGN; }
2349 static int num_ids() { return parent_num_ids() + 2; }
2350 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2352 // Type feedback information.
2353 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2354 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2355 bool IsUninitialized() const {
2356 return IsUninitializedField::decode(bit_field_);
2358 bool HasNoTypeInformation() {
2359 return IsUninitializedField::decode(bit_field_);
2361 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2362 IcCheckType GetKeyType() const override {
2363 return KeyTypeField::decode(bit_field_);
2365 KeyedAccessStoreMode GetStoreMode() const override {
2366 return StoreModeField::decode(bit_field_);
2368 void set_is_uninitialized(bool b) {
2369 bit_field_ = IsUninitializedField::update(bit_field_, b);
2371 void set_key_type(IcCheckType key_type) {
2372 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2374 void set_store_mode(KeyedAccessStoreMode mode) {
2375 bit_field_ = StoreModeField::update(bit_field_, mode);
2378 FeedbackVectorRequirements ComputeFeedbackRequirements(
2379 Isolate* isolate, const ICSlotCache* cache) override;
2380 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2381 ICSlotCache* cache) override {
2384 Code::Kind FeedbackICSlotKind(int index) override;
2385 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2388 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2390 static int parent_num_ids() { return Expression::num_ids(); }
2393 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2395 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2396 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2397 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2398 class TokenField : public BitField16<Token::Value, 6, 8> {};
2400 // Starts with 16-bit field, which should get packed together with
2401 // Expression's trailing 16-bit field.
2402 uint16_t bit_field_;
2403 Expression* target_;
2405 BinaryOperation* binary_operation_;
2406 SmallMapList receiver_types_;
2407 FeedbackVectorICSlot slot_;
2411 class Yield final : public Expression {
2413 DECLARE_NODE_TYPE(Yield)
2416 kInitial, // The initial yield that returns the unboxed generator object.
2417 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2418 kDelegating, // A yield*.
2419 kFinal // A return: { value: EXPRESSION, done: true }
2422 Expression* generator_object() const { return generator_object_; }
2423 Expression* expression() const { return expression_; }
2424 Kind yield_kind() const { return yield_kind_; }
2426 // Type feedback information.
2427 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2428 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2429 Isolate* isolate, const ICSlotCache* cache) override {
2430 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2432 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2433 ICSlotCache* cache) override {
2434 yield_first_feedback_slot_ = slot;
2436 Code::Kind FeedbackICSlotKind(int index) override {
2437 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2440 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2441 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2442 return yield_first_feedback_slot_;
2445 FeedbackVectorICSlot DoneFeedbackSlot() {
2446 return KeyedLoadFeedbackSlot().next();
2449 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2452 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2453 Kind yield_kind, int pos)
2454 : Expression(zone, pos),
2455 generator_object_(generator_object),
2456 expression_(expression),
2457 yield_kind_(yield_kind),
2458 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2461 Expression* generator_object_;
2462 Expression* expression_;
2464 FeedbackVectorICSlot yield_first_feedback_slot_;
2468 class Throw final : public Expression {
2470 DECLARE_NODE_TYPE(Throw)
2472 Expression* exception() const { return exception_; }
2475 Throw(Zone* zone, Expression* exception, int pos)
2476 : Expression(zone, pos), exception_(exception) {}
2479 Expression* exception_;
2483 class FunctionLiteral final : public Expression {
2486 ANONYMOUS_EXPRESSION,
2491 enum ParameterFlag {
2492 kNoDuplicateParameters = 0,
2493 kHasDuplicateParameters = 1
2496 enum IsFunctionFlag {
2501 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2503 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2505 enum ArityRestriction {
2511 DECLARE_NODE_TYPE(FunctionLiteral)
2513 Handle<String> name() const { return raw_name_->string(); }
2514 const AstRawString* raw_name() const { return raw_name_; }
2515 Scope* scope() const { return scope_; }
2516 ZoneList<Statement*>* body() const { return body_; }
2517 void set_function_token_position(int pos) { function_token_position_ = pos; }
2518 int function_token_position() const { return function_token_position_; }
2519 int start_position() const;
2520 int end_position() const;
2521 int SourceSize() const { return end_position() - start_position(); }
2522 bool is_expression() const { return IsExpression::decode(bitfield_); }
2523 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2524 LanguageMode language_mode() const;
2526 static bool NeedsHomeObject(Expression* expr);
2528 int materialized_literal_count() { return materialized_literal_count_; }
2529 int expected_property_count() { return expected_property_count_; }
2530 int parameter_count() { return parameter_count_; }
2532 bool AllowsLazyCompilation();
2533 bool AllowsLazyCompilationWithoutContext();
2535 Handle<String> debug_name() const {
2536 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2537 return raw_name_->string();
2539 return inferred_name();
2542 Handle<String> inferred_name() const {
2543 if (!inferred_name_.is_null()) {
2544 DCHECK(raw_inferred_name_ == NULL);
2545 return inferred_name_;
2547 if (raw_inferred_name_ != NULL) {
2548 return raw_inferred_name_->string();
2551 return Handle<String>();
2554 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2555 void set_inferred_name(Handle<String> inferred_name) {
2556 DCHECK(!inferred_name.is_null());
2557 inferred_name_ = inferred_name;
2558 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2559 raw_inferred_name_ = NULL;
2562 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2563 DCHECK(raw_inferred_name != NULL);
2564 raw_inferred_name_ = raw_inferred_name;
2565 DCHECK(inferred_name_.is_null());
2566 inferred_name_ = Handle<String>();
2569 bool pretenure() { return Pretenure::decode(bitfield_); }
2570 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2572 bool has_duplicate_parameters() {
2573 return HasDuplicateParameters::decode(bitfield_);
2576 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2578 // This is used as a heuristic on when to eagerly compile a function
2579 // literal. We consider the following constructs as hints that the
2580 // function will be called immediately:
2581 // - (function() { ... })();
2582 // - var x = function() { ... }();
2583 bool should_eager_compile() const {
2584 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2586 void set_should_eager_compile() {
2587 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2590 // A hint that we expect this function to be called (exactly) once,
2591 // i.e. we suspect it's an initialization function.
2592 bool should_be_used_once_hint() const {
2593 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2595 void set_should_be_used_once_hint() {
2596 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2599 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2601 int ast_node_count() { return ast_properties_.node_count(); }
2602 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2603 void set_ast_properties(AstProperties* ast_properties) {
2604 ast_properties_ = *ast_properties;
2606 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2607 return ast_properties_.get_spec();
2609 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2610 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2611 void set_dont_optimize_reason(BailoutReason reason) {
2612 dont_optimize_reason_ = reason;
2616 FunctionLiteral(Zone* zone, const AstRawString* name,
2617 AstValueFactory* ast_value_factory, Scope* scope,
2618 ZoneList<Statement*>* body, int materialized_literal_count,
2619 int expected_property_count, int parameter_count,
2620 FunctionType function_type,
2621 ParameterFlag has_duplicate_parameters,
2622 IsFunctionFlag is_function,
2623 EagerCompileHint eager_compile_hint, FunctionKind kind,
2625 : Expression(zone, position),
2629 raw_inferred_name_(ast_value_factory->empty_string()),
2630 ast_properties_(zone),
2631 dont_optimize_reason_(kNoReason),
2632 materialized_literal_count_(materialized_literal_count),
2633 expected_property_count_(expected_property_count),
2634 parameter_count_(parameter_count),
2635 function_token_position_(RelocInfo::kNoPosition) {
2636 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2637 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2638 Pretenure::encode(false) |
2639 HasDuplicateParameters::encode(has_duplicate_parameters) |
2640 IsFunction::encode(is_function) |
2641 EagerCompileHintBit::encode(eager_compile_hint) |
2642 FunctionKindBits::encode(kind) |
2643 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2644 DCHECK(IsValidFunctionKind(kind));
2648 const AstRawString* raw_name_;
2649 Handle<String> name_;
2651 ZoneList<Statement*>* body_;
2652 const AstString* raw_inferred_name_;
2653 Handle<String> inferred_name_;
2654 AstProperties ast_properties_;
2655 BailoutReason dont_optimize_reason_;
2657 int materialized_literal_count_;
2658 int expected_property_count_;
2659 int parameter_count_;
2660 int function_token_position_;
2663 class IsExpression : public BitField<bool, 0, 1> {};
2664 class IsAnonymous : public BitField<bool, 1, 1> {};
2665 class Pretenure : public BitField<bool, 2, 1> {};
2666 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2667 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2668 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2669 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2670 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2675 class ClassLiteral final : public Expression {
2677 typedef ObjectLiteralProperty Property;
2679 DECLARE_NODE_TYPE(ClassLiteral)
2681 Handle<String> name() const { return raw_name_->string(); }
2682 const AstRawString* raw_name() const { return raw_name_; }
2683 Scope* scope() const { return scope_; }
2684 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2685 Expression* extends() const { return extends_; }
2686 FunctionLiteral* constructor() const { return constructor_; }
2687 ZoneList<Property*>* properties() const { return properties_; }
2688 int start_position() const { return position(); }
2689 int end_position() const { return end_position_; }
2691 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2692 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2693 BailoutId ExitId() { return BailoutId(local_id(2)); }
2694 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2696 // Return an AST id for a property that is used in simulate instructions.
2697 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2699 // Unlike other AST nodes, this number of bailout IDs allocated for an
2700 // ClassLiteral can vary, so num_ids() is not a static method.
2701 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2703 // Object literals need one feedback slot for each non-trivial value, as well
2704 // as some slots for home objects.
2705 FeedbackVectorRequirements ComputeFeedbackRequirements(
2706 Isolate* isolate, const ICSlotCache* cache) override;
2707 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2708 ICSlotCache* cache) override {
2711 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2712 FeedbackVectorICSlot GetNthSlot(int n) const {
2713 return FeedbackVectorICSlot(slot_.ToInt() + n);
2716 // If value needs a home object, returns a valid feedback vector ic slot
2717 // given by slot_index, and increments slot_index.
2718 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2719 int* slot_index) const;
2722 int slot_count() const { return slot_count_; }
2726 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2727 VariableProxy* class_variable_proxy, Expression* extends,
2728 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2729 int start_position, int end_position)
2730 : Expression(zone, start_position),
2733 class_variable_proxy_(class_variable_proxy),
2735 constructor_(constructor),
2736 properties_(properties),
2737 end_position_(end_position),
2741 slot_(FeedbackVectorICSlot::Invalid()) {
2744 static int parent_num_ids() { return Expression::num_ids(); }
2747 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2749 const AstRawString* raw_name_;
2751 VariableProxy* class_variable_proxy_;
2752 Expression* extends_;
2753 FunctionLiteral* constructor_;
2754 ZoneList<Property*>* properties_;
2757 // slot_count_ helps validate that the logic to allocate ic slots and the
2758 // logic to use them are in sync.
2761 FeedbackVectorICSlot slot_;
2765 class NativeFunctionLiteral final : public Expression {
2767 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2769 Handle<String> name() const { return name_->string(); }
2770 v8::Extension* extension() const { return extension_; }
2773 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2774 v8::Extension* extension, int pos)
2775 : Expression(zone, pos), name_(name), extension_(extension) {}
2778 const AstRawString* name_;
2779 v8::Extension* extension_;
2783 class ThisFunction final : public Expression {
2785 DECLARE_NODE_TYPE(ThisFunction)
2788 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2792 class SuperPropertyReference final : public Expression {
2794 DECLARE_NODE_TYPE(SuperPropertyReference)
2796 VariableProxy* this_var() const { return this_var_; }
2797 Expression* home_object() const { return home_object_; }
2800 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2801 Expression* home_object, int pos)
2802 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2803 DCHECK(this_var->is_this());
2804 DCHECK(home_object->IsProperty());
2808 VariableProxy* this_var_;
2809 Expression* home_object_;
2813 class SuperCallReference final : public Expression {
2815 DECLARE_NODE_TYPE(SuperCallReference)
2817 VariableProxy* this_var() const { return this_var_; }
2818 VariableProxy* new_target_var() const { return new_target_var_; }
2819 VariableProxy* this_function_var() const { return this_function_var_; }
2822 SuperCallReference(Zone* zone, VariableProxy* this_var,
2823 VariableProxy* new_target_var,
2824 VariableProxy* this_function_var, int pos)
2825 : Expression(zone, pos),
2826 this_var_(this_var),
2827 new_target_var_(new_target_var),
2828 this_function_var_(this_function_var) {
2829 DCHECK(this_var->is_this());
2830 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2831 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2835 VariableProxy* this_var_;
2836 VariableProxy* new_target_var_;
2837 VariableProxy* this_function_var_;
2841 #undef DECLARE_NODE_TYPE
2844 // ----------------------------------------------------------------------------
2845 // Regular expressions
2848 class RegExpVisitor BASE_EMBEDDED {
2850 virtual ~RegExpVisitor() { }
2851 #define MAKE_CASE(Name) \
2852 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2853 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2858 class RegExpTree : public ZoneObject {
2860 static const int kInfinity = kMaxInt;
2861 virtual ~RegExpTree() {}
2862 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2863 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2864 RegExpNode* on_success) = 0;
2865 virtual bool IsTextElement() { return false; }
2866 virtual bool IsAnchoredAtStart() { return false; }
2867 virtual bool IsAnchoredAtEnd() { return false; }
2868 virtual int min_match() = 0;
2869 virtual int max_match() = 0;
2870 // Returns the interval of registers used for captures within this
2872 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2873 virtual void AppendToText(RegExpText* text, Zone* zone);
2874 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2875 #define MAKE_ASTYPE(Name) \
2876 virtual RegExp##Name* As##Name(); \
2877 virtual bool Is##Name();
2878 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2883 class RegExpDisjunction final : public RegExpTree {
2885 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2886 void* Accept(RegExpVisitor* visitor, void* data) override;
2887 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2888 RegExpNode* on_success) override;
2889 RegExpDisjunction* AsDisjunction() override;
2890 Interval CaptureRegisters() override;
2891 bool IsDisjunction() override;
2892 bool IsAnchoredAtStart() override;
2893 bool IsAnchoredAtEnd() override;
2894 int min_match() override { return min_match_; }
2895 int max_match() override { return max_match_; }
2896 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2898 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2899 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2900 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2901 ZoneList<RegExpTree*>* alternatives_;
2907 class RegExpAlternative final : public RegExpTree {
2909 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2910 void* Accept(RegExpVisitor* visitor, void* data) override;
2911 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2912 RegExpNode* on_success) override;
2913 RegExpAlternative* AsAlternative() override;
2914 Interval CaptureRegisters() override;
2915 bool IsAlternative() override;
2916 bool IsAnchoredAtStart() override;
2917 bool IsAnchoredAtEnd() override;
2918 int min_match() override { return min_match_; }
2919 int max_match() override { return max_match_; }
2920 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2922 ZoneList<RegExpTree*>* nodes_;
2928 class RegExpAssertion final : public RegExpTree {
2930 enum AssertionType {
2938 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2939 void* Accept(RegExpVisitor* visitor, void* data) override;
2940 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2941 RegExpNode* on_success) override;
2942 RegExpAssertion* AsAssertion() override;
2943 bool IsAssertion() override;
2944 bool IsAnchoredAtStart() override;
2945 bool IsAnchoredAtEnd() override;
2946 int min_match() override { return 0; }
2947 int max_match() override { return 0; }
2948 AssertionType assertion_type() { return assertion_type_; }
2950 AssertionType assertion_type_;
2954 class CharacterSet final BASE_EMBEDDED {
2956 explicit CharacterSet(uc16 standard_set_type)
2958 standard_set_type_(standard_set_type) {}
2959 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2961 standard_set_type_(0) {}
2962 ZoneList<CharacterRange>* ranges(Zone* zone);
2963 uc16 standard_set_type() { return standard_set_type_; }
2964 void set_standard_set_type(uc16 special_set_type) {
2965 standard_set_type_ = special_set_type;
2967 bool is_standard() { return standard_set_type_ != 0; }
2968 void Canonicalize();
2970 ZoneList<CharacterRange>* ranges_;
2971 // If non-zero, the value represents a standard set (e.g., all whitespace
2972 // characters) without having to expand the ranges.
2973 uc16 standard_set_type_;
2977 class RegExpCharacterClass final : public RegExpTree {
2979 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2981 is_negated_(is_negated) { }
2982 explicit RegExpCharacterClass(uc16 type)
2984 is_negated_(false) { }
2985 void* Accept(RegExpVisitor* visitor, void* data) override;
2986 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2987 RegExpNode* on_success) override;
2988 RegExpCharacterClass* AsCharacterClass() override;
2989 bool IsCharacterClass() override;
2990 bool IsTextElement() override { return true; }
2991 int min_match() override { return 1; }
2992 int max_match() override { return 1; }
2993 void AppendToText(RegExpText* text, Zone* zone) override;
2994 CharacterSet character_set() { return set_; }
2995 // TODO(lrn): Remove need for complex version if is_standard that
2996 // recognizes a mangled standard set and just do { return set_.is_special(); }
2997 bool is_standard(Zone* zone);
2998 // Returns a value representing the standard character set if is_standard()
3000 // Currently used values are:
3001 // s : unicode whitespace
3002 // S : unicode non-whitespace
3003 // w : ASCII word character (digit, letter, underscore)
3004 // W : non-ASCII word character
3006 // D : non-ASCII digit
3007 // . : non-unicode non-newline
3008 // * : All characters
3009 uc16 standard_type() { return set_.standard_set_type(); }
3010 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3011 bool is_negated() { return is_negated_; }
3019 class RegExpAtom final : public RegExpTree {
3021 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3022 void* Accept(RegExpVisitor* visitor, void* data) override;
3023 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3024 RegExpNode* on_success) override;
3025 RegExpAtom* AsAtom() override;
3026 bool IsAtom() override;
3027 bool IsTextElement() override { return true; }
3028 int min_match() override { return data_.length(); }
3029 int max_match() override { return data_.length(); }
3030 void AppendToText(RegExpText* text, Zone* zone) override;
3031 Vector<const uc16> data() { return data_; }
3032 int length() { return data_.length(); }
3034 Vector<const uc16> data_;
3038 class RegExpText final : public RegExpTree {
3040 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3041 void* Accept(RegExpVisitor* visitor, void* data) override;
3042 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3043 RegExpNode* on_success) override;
3044 RegExpText* AsText() override;
3045 bool IsText() override;
3046 bool IsTextElement() override { return true; }
3047 int min_match() override { return length_; }
3048 int max_match() override { return length_; }
3049 void AppendToText(RegExpText* text, Zone* zone) override;
3050 void AddElement(TextElement elm, Zone* zone) {
3051 elements_.Add(elm, zone);
3052 length_ += elm.length();
3054 ZoneList<TextElement>* elements() { return &elements_; }
3056 ZoneList<TextElement> elements_;
3061 class RegExpQuantifier final : public RegExpTree {
3063 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3064 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3068 min_match_(min * body->min_match()),
3069 quantifier_type_(type) {
3070 if (max > 0 && body->max_match() > kInfinity / max) {
3071 max_match_ = kInfinity;
3073 max_match_ = max * body->max_match();
3076 void* Accept(RegExpVisitor* visitor, void* data) override;
3077 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3078 RegExpNode* on_success) override;
3079 static RegExpNode* ToNode(int min,
3083 RegExpCompiler* compiler,
3084 RegExpNode* on_success,
3085 bool not_at_start = false);
3086 RegExpQuantifier* AsQuantifier() override;
3087 Interval CaptureRegisters() override;
3088 bool IsQuantifier() override;
3089 int min_match() override { return min_match_; }
3090 int max_match() override { return max_match_; }
3091 int min() { return min_; }
3092 int max() { return max_; }
3093 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3094 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3095 bool is_greedy() { return quantifier_type_ == GREEDY; }
3096 RegExpTree* body() { return body_; }
3104 QuantifierType quantifier_type_;
3108 class RegExpCapture final : public RegExpTree {
3110 explicit RegExpCapture(RegExpTree* body, int index)
3111 : body_(body), index_(index) { }
3112 void* Accept(RegExpVisitor* visitor, void* data) override;
3113 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3114 RegExpNode* on_success) override;
3115 static RegExpNode* ToNode(RegExpTree* body,
3117 RegExpCompiler* compiler,
3118 RegExpNode* on_success);
3119 RegExpCapture* AsCapture() override;
3120 bool IsAnchoredAtStart() override;
3121 bool IsAnchoredAtEnd() override;
3122 Interval CaptureRegisters() override;
3123 bool IsCapture() override;
3124 int min_match() override { return body_->min_match(); }
3125 int max_match() override { return body_->max_match(); }
3126 RegExpTree* body() { return body_; }
3127 int index() { return index_; }
3128 static int StartRegister(int index) { return index * 2; }
3129 static int EndRegister(int index) { return index * 2 + 1; }
3137 class RegExpLookahead final : public RegExpTree {
3139 RegExpLookahead(RegExpTree* body,
3144 is_positive_(is_positive),
3145 capture_count_(capture_count),
3146 capture_from_(capture_from) { }
3148 void* Accept(RegExpVisitor* visitor, void* data) override;
3149 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3150 RegExpNode* on_success) override;
3151 RegExpLookahead* AsLookahead() override;
3152 Interval CaptureRegisters() override;
3153 bool IsLookahead() override;
3154 bool IsAnchoredAtStart() override;
3155 int min_match() override { return 0; }
3156 int max_match() override { return 0; }
3157 RegExpTree* body() { return body_; }
3158 bool is_positive() { return is_positive_; }
3159 int capture_count() { return capture_count_; }
3160 int capture_from() { return capture_from_; }
3170 class RegExpBackReference final : public RegExpTree {
3172 explicit RegExpBackReference(RegExpCapture* capture)
3173 : capture_(capture) { }
3174 void* Accept(RegExpVisitor* visitor, void* data) override;
3175 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3176 RegExpNode* on_success) override;
3177 RegExpBackReference* AsBackReference() override;
3178 bool IsBackReference() override;
3179 int min_match() override { return 0; }
3180 int max_match() override { return capture_->max_match(); }
3181 int index() { return capture_->index(); }
3182 RegExpCapture* capture() { return capture_; }
3184 RegExpCapture* capture_;
3188 class RegExpEmpty final : public RegExpTree {
3191 void* Accept(RegExpVisitor* visitor, void* data) override;
3192 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3193 RegExpNode* on_success) override;
3194 RegExpEmpty* AsEmpty() override;
3195 bool IsEmpty() override;
3196 int min_match() override { return 0; }
3197 int max_match() override { return 0; }
3201 // ----------------------------------------------------------------------------
3203 // - leaf node visitors are abstract.
3205 class AstVisitor BASE_EMBEDDED {
3208 virtual ~AstVisitor() {}
3210 // Stack overflow check and dynamic dispatch.
3211 virtual void Visit(AstNode* node) = 0;
3213 // Iteration left-to-right.
3214 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3215 virtual void VisitStatements(ZoneList<Statement*>* statements);
3216 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3218 // Individual AST nodes.
3219 #define DEF_VISIT(type) \
3220 virtual void Visit##type(type* node) = 0;
3221 AST_NODE_LIST(DEF_VISIT)
3226 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3228 void Visit(AstNode* node) final { \
3229 if (!CheckStackOverflow()) node->Accept(this); \
3232 void SetStackOverflow() { stack_overflow_ = true; } \
3233 void ClearStackOverflow() { stack_overflow_ = false; } \
3234 bool HasStackOverflow() const { return stack_overflow_; } \
3236 bool CheckStackOverflow() { \
3237 if (stack_overflow_) return true; \
3238 StackLimitCheck check(isolate_); \
3239 if (!check.HasOverflowed()) return false; \
3240 stack_overflow_ = true; \
3245 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3246 isolate_ = isolate; \
3248 stack_overflow_ = false; \
3250 Zone* zone() { return zone_; } \
3251 Isolate* isolate() { return isolate_; } \
3253 Isolate* isolate_; \
3255 bool stack_overflow_
3258 // ----------------------------------------------------------------------------
3261 class AstNodeFactory final BASE_EMBEDDED {
3263 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3264 : zone_(ast_value_factory->zone()),
3265 ast_value_factory_(ast_value_factory) {}
3267 VariableDeclaration* NewVariableDeclaration(
3268 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3269 bool is_class_declaration = false, int declaration_group_start = -1) {
3271 VariableDeclaration(zone_, proxy, mode, scope, pos,
3272 is_class_declaration, declaration_group_start);
3275 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3277 FunctionLiteral* fun,
3280 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3283 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3284 const AstRawString* import_name,
3285 const AstRawString* module_specifier,
3286 Scope* scope, int pos) {
3287 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3288 module_specifier, scope, pos);
3291 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3294 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3297 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3298 bool ignore_completion_value, int pos) {
3300 Block(zone_, labels, capacity, ignore_completion_value, pos);
3303 #define STATEMENT_WITH_LABELS(NodeType) \
3304 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3305 return new (zone_) NodeType(zone_, labels, pos); \
3307 STATEMENT_WITH_LABELS(DoWhileStatement)
3308 STATEMENT_WITH_LABELS(WhileStatement)
3309 STATEMENT_WITH_LABELS(ForStatement)
3310 STATEMENT_WITH_LABELS(SwitchStatement)
3311 #undef STATEMENT_WITH_LABELS
3313 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3314 ZoneList<const AstRawString*>* labels,
3316 switch (visit_mode) {
3317 case ForEachStatement::ENUMERATE: {
3318 return new (zone_) ForInStatement(zone_, labels, pos);
3320 case ForEachStatement::ITERATE: {
3321 return new (zone_) ForOfStatement(zone_, labels, pos);
3328 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3329 return new (zone_) ExpressionStatement(zone_, expression, pos);
3332 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3333 return new (zone_) ContinueStatement(zone_, target, pos);
3336 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3337 return new (zone_) BreakStatement(zone_, target, pos);
3340 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3341 return new (zone_) ReturnStatement(zone_, expression, pos);
3344 WithStatement* NewWithStatement(Scope* scope,
3345 Expression* expression,
3346 Statement* statement,
3348 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3351 IfStatement* NewIfStatement(Expression* condition,
3352 Statement* then_statement,
3353 Statement* else_statement,
3356 IfStatement(zone_, condition, then_statement, else_statement, pos);
3359 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3361 Block* catch_block, int pos) {
3363 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3366 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3367 Block* finally_block, int pos) {
3369 TryFinallyStatement(zone_, try_block, finally_block, pos);
3372 DebuggerStatement* NewDebuggerStatement(int pos) {
3373 return new (zone_) DebuggerStatement(zone_, pos);
3376 EmptyStatement* NewEmptyStatement(int pos) {
3377 return new(zone_) EmptyStatement(zone_, pos);
3380 CaseClause* NewCaseClause(
3381 Expression* label, ZoneList<Statement*>* statements, int pos) {
3382 return new (zone_) CaseClause(zone_, label, statements, pos);
3385 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3387 Literal(zone_, ast_value_factory_->NewString(string), pos);
3390 // A JavaScript symbol (ECMA-262 edition 6).
3391 Literal* NewSymbolLiteral(const char* name, int pos) {
3392 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3395 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3397 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3400 Literal* NewSmiLiteral(int number, int pos) {
3401 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3404 Literal* NewBooleanLiteral(bool b, int pos) {
3405 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3408 Literal* NewNullLiteral(int pos) {
3409 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3412 Literal* NewUndefinedLiteral(int pos) {
3413 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3416 Literal* NewTheHoleLiteral(int pos) {
3417 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3420 ObjectLiteral* NewObjectLiteral(
3421 ZoneList<ObjectLiteral::Property*>* properties,
3423 int boilerplate_properties,
3427 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3428 boilerplate_properties, has_function,
3432 ObjectLiteral::Property* NewObjectLiteralProperty(
3433 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3434 bool is_static, bool is_computed_name) {
3436 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3439 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3442 bool is_computed_name) {
3443 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3444 is_static, is_computed_name);
3447 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3448 const AstRawString* flags,
3452 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3456 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3461 ArrayLiteral(zone_, values, -1, literal_index, is_strong, pos);
3464 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3465 int first_spread_index, int literal_index,
3466 bool is_strong, int pos) {
3467 return new (zone_) ArrayLiteral(zone_, values, first_spread_index,
3468 literal_index, is_strong, pos);
3471 VariableProxy* NewVariableProxy(Variable* var,
3472 int start_position = RelocInfo::kNoPosition,
3473 int end_position = RelocInfo::kNoPosition) {
3474 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3477 VariableProxy* NewVariableProxy(const AstRawString* name,
3478 Variable::Kind variable_kind,
3479 int start_position = RelocInfo::kNoPosition,
3480 int end_position = RelocInfo::kNoPosition) {
3481 DCHECK_NOT_NULL(name);
3483 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3486 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3487 return new (zone_) Property(zone_, obj, key, pos);
3490 Call* NewCall(Expression* expression,
3491 ZoneList<Expression*>* arguments,
3493 return new (zone_) Call(zone_, expression, arguments, pos);
3496 CallNew* NewCallNew(Expression* expression,
3497 ZoneList<Expression*>* arguments,
3499 return new (zone_) CallNew(zone_, expression, arguments, pos);
3502 CallRuntime* NewCallRuntime(const AstRawString* name,
3503 const Runtime::Function* function,
3504 ZoneList<Expression*>* arguments,
3506 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3509 UnaryOperation* NewUnaryOperation(Token::Value op,
3510 Expression* expression,
3512 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3515 BinaryOperation* NewBinaryOperation(Token::Value op,
3519 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3522 CountOperation* NewCountOperation(Token::Value op,
3526 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3529 CompareOperation* NewCompareOperation(Token::Value op,
3533 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3536 Spread* NewSpread(Expression* expression, int pos) {
3537 return new (zone_) Spread(zone_, expression, pos);
3540 Conditional* NewConditional(Expression* condition,
3541 Expression* then_expression,
3542 Expression* else_expression,
3544 return new (zone_) Conditional(zone_, condition, then_expression,
3545 else_expression, position);
3548 Assignment* NewAssignment(Token::Value op,
3552 DCHECK(Token::IsAssignmentOp(op));
3553 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3554 if (assign->is_compound()) {
3555 DCHECK(Token::IsAssignmentOp(op));
3556 assign->binary_operation_ =
3557 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3562 Yield* NewYield(Expression *generator_object,
3563 Expression* expression,
3564 Yield::Kind yield_kind,
3566 if (!expression) expression = NewUndefinedLiteral(pos);
3568 Yield(zone_, generator_object, expression, yield_kind, pos);
3571 Throw* NewThrow(Expression* exception, int pos) {
3572 return new (zone_) Throw(zone_, exception, pos);
3575 FunctionLiteral* NewFunctionLiteral(
3576 const AstRawString* name, AstValueFactory* ast_value_factory,
3577 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3578 int expected_property_count, int parameter_count,
3579 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3580 FunctionLiteral::FunctionType function_type,
3581 FunctionLiteral::IsFunctionFlag is_function,
3582 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3584 return new (zone_) FunctionLiteral(
3585 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3586 expected_property_count, parameter_count, function_type,
3587 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3591 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3592 VariableProxy* proxy, Expression* extends,
3593 FunctionLiteral* constructor,
3594 ZoneList<ObjectLiteral::Property*>* properties,
3595 int start_position, int end_position) {
3597 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3598 properties, start_position, end_position);
3601 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3602 v8::Extension* extension,
3604 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3607 ThisFunction* NewThisFunction(int pos) {
3608 return new (zone_) ThisFunction(zone_, pos);
3611 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3612 Expression* home_object,
3615 SuperPropertyReference(zone_, this_var, home_object, pos);
3618 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3619 VariableProxy* new_target_var,
3620 VariableProxy* this_function_var,
3622 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3623 this_function_var, pos);
3628 AstValueFactory* ast_value_factory_;
3632 } } // namespace v8::internal