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/factory.h"
14 #include "src/isolate.h"
15 #include "src/jsregexp.h"
16 #include "src/list-inl.h"
17 #include "src/modules.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/smart-pointers.h"
21 #include "src/token.h"
22 #include "src/types.h"
23 #include "src/utils.h"
24 #include "src/variables.h"
29 // The abstract syntax tree is an intermediate, light-weight
30 // representation of the parsed JavaScript code suitable for
31 // compilation to native code.
33 // Nodes are allocated in a separate zone, which allows faster
34 // allocation and constant-time deallocation of the entire syntax
38 // ----------------------------------------------------------------------------
39 // Nodes of the abstract syntax tree. Only concrete classes are
42 #define DECLARATION_NODE_LIST(V) \
43 V(VariableDeclaration) \
44 V(FunctionDeclaration) \
45 V(ImportDeclaration) \
48 #define STATEMENT_NODE_LIST(V) \
50 V(ExpressionStatement) \
53 V(ContinueStatement) \
63 V(TryCatchStatement) \
64 V(TryFinallyStatement) \
67 #define EXPRESSION_NODE_LIST(V) \
70 V(NativeFunctionLiteral) \
90 V(SuperPropertyReference) \
91 V(SuperCallReference) \
94 #define AST_NODE_LIST(V) \
95 DECLARATION_NODE_LIST(V) \
96 STATEMENT_NODE_LIST(V) \
97 EXPRESSION_NODE_LIST(V)
99 // Forward declarations
100 class AstNodeFactory;
104 class BreakableStatement;
106 class IterationStatement;
107 class MaterializedLiteral;
109 class TypeFeedbackOracle;
111 class RegExpAlternative;
112 class RegExpAssertion;
114 class RegExpBackReference;
116 class RegExpCharacterClass;
117 class RegExpCompiler;
118 class RegExpDisjunction;
120 class RegExpLookahead;
121 class RegExpQuantifier;
124 #define DEF_FORWARD_DECLARATION(type) class type;
125 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
126 #undef DEF_FORWARD_DECLARATION
129 // Typedef only introduced to avoid unreadable code.
130 typedef ZoneList<Handle<String>> ZoneStringList;
131 typedef ZoneList<Handle<Object>> ZoneObjectList;
134 #define DECLARE_NODE_TYPE(type) \
135 void Accept(AstVisitor* v) override; \
136 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
137 friend class AstNodeFactory;
140 enum AstPropertiesFlag { kDontSelfOptimize, kDontCrankshaft };
143 class FeedbackVectorRequirements {
145 FeedbackVectorRequirements(int slots, int ic_slots)
146 : slots_(slots), ic_slots_(ic_slots) {}
148 int slots() const { return slots_; }
149 int ic_slots() const { return ic_slots_; }
157 class VariableICSlotPair final {
159 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
160 : variable_(variable), slot_(slot) {}
162 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
164 Variable* variable() const { return variable_; }
165 FeedbackVectorICSlot slot() const { return slot_; }
169 FeedbackVectorICSlot slot_;
173 typedef List<VariableICSlotPair> ICSlotCache;
176 class AstProperties final BASE_EMBEDDED {
178 class Flags : public EnumSet<AstPropertiesFlag, int> {};
180 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
182 Flags* flags() { return &flags_; }
183 int node_count() { return node_count_; }
184 void add_node_count(int count) { node_count_ += count; }
186 int slots() const { return spec_.slots(); }
187 void increase_slots(int count) { spec_.increase_slots(count); }
189 int ic_slots() const { return spec_.ic_slots(); }
190 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
191 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
192 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
197 ZoneFeedbackVectorSpec spec_;
201 class AstNode: public ZoneObject {
203 #define DECLARE_TYPE_ENUM(type) k##type,
205 AST_NODE_LIST(DECLARE_TYPE_ENUM)
208 #undef DECLARE_TYPE_ENUM
210 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
212 explicit AstNode(int position): position_(position) {}
213 virtual ~AstNode() {}
215 virtual void Accept(AstVisitor* v) = 0;
216 virtual NodeType node_type() const = 0;
217 int position() const { return position_; }
219 // Type testing & conversion functions overridden by concrete subclasses.
220 #define DECLARE_NODE_FUNCTIONS(type) \
221 bool Is##type() const { return node_type() == AstNode::k##type; } \
223 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
225 const type* As##type() const { \
226 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
228 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
229 #undef DECLARE_NODE_FUNCTIONS
231 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
232 virtual IterationStatement* AsIterationStatement() { return NULL; }
233 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
235 // The interface for feedback slots, with default no-op implementations for
236 // node types which don't actually have this. Note that this is conceptually
237 // not really nice, but multiple inheritance would introduce yet another
238 // vtable entry per node, something we don't want for space reasons.
239 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
240 Isolate* isolate, const ICSlotCache* cache) {
241 return FeedbackVectorRequirements(0, 0);
243 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
244 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
245 ICSlotCache* cache) {
248 // Each ICSlot stores a kind of IC which the participating node should know.
249 virtual Code::Kind FeedbackICSlotKind(int index) {
251 return Code::NUMBER_OF_KINDS;
255 // Hidden to prevent accidental usage. It would have to load the
256 // current zone from the TLS.
257 void* operator new(size_t size);
259 friend class CaseClause; // Generates AST IDs.
265 class Statement : public AstNode {
267 explicit Statement(Zone* zone, int position) : AstNode(position) {}
269 bool IsEmpty() { return AsEmptyStatement() != NULL; }
270 virtual bool IsJump() const { return false; }
274 class SmallMapList final {
277 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
279 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
280 void Clear() { list_.Clear(); }
281 void Sort() { list_.Sort(); }
283 bool is_empty() const { return list_.is_empty(); }
284 int length() const { return list_.length(); }
286 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
287 if (!Map::TryUpdate(map).ToHandle(&map)) return;
288 for (int i = 0; i < length(); ++i) {
289 if (at(i).is_identical_to(map)) return;
294 void FilterForPossibleTransitions(Map* root_map) {
295 for (int i = list_.length() - 1; i >= 0; i--) {
296 if (at(i)->FindRootMap() != root_map) {
297 list_.RemoveElement(list_.at(i));
302 void Add(Handle<Map> handle, Zone* zone) {
303 list_.Add(handle.location(), zone);
306 Handle<Map> at(int i) const {
307 return Handle<Map>(list_.at(i));
310 Handle<Map> first() const { return at(0); }
311 Handle<Map> last() const { return at(length() - 1); }
314 // The list stores pointers to Map*, that is Map**, so it's GC safe.
315 SmallPointerList<Map*> list_;
317 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
321 class Expression : public AstNode {
324 // Not assigned a context yet, or else will not be visited during
327 // Evaluated for its side effects.
329 // Evaluated for its value (and side effects).
331 // Evaluated for control flow (and side effects).
335 virtual bool IsValidReferenceExpression() const { return false; }
337 // Helpers for ToBoolean conversion.
338 virtual bool ToBooleanIsTrue() const { return false; }
339 virtual bool ToBooleanIsFalse() const { return false; }
341 // Symbols that cannot be parsed as array indices are considered property
342 // names. We do not treat symbols that can be array indexes as property
343 // names because [] for string objects is handled only by keyed ICs.
344 virtual bool IsPropertyName() const { return false; }
346 // True iff the expression is a literal represented as a smi.
347 bool IsSmiLiteral() const;
349 // True iff the expression is a string literal.
350 bool IsStringLiteral() const;
352 // True iff the expression is the null literal.
353 bool IsNullLiteral() const;
355 // True if we can prove that the expression is the undefined literal.
356 bool IsUndefinedLiteral(Isolate* isolate) const;
358 // Expression type bounds
359 Bounds bounds() const { return bounds_; }
360 void set_bounds(Bounds bounds) { bounds_ = bounds; }
362 // Type feedback information for assignments and properties.
363 virtual bool IsMonomorphic() {
367 virtual SmallMapList* GetReceiverTypes() {
371 virtual KeyedAccessStoreMode GetStoreMode() const {
373 return STANDARD_STORE;
375 virtual IcCheckType GetKeyType() const {
380 // TODO(rossberg): this should move to its own AST node eventually.
381 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
382 uint16_t to_boolean_types() const {
383 return ToBooleanTypesField::decode(bit_field_);
386 void set_base_id(int id) { base_id_ = id; }
387 static int num_ids() { return parent_num_ids() + 2; }
388 BailoutId id() const { return BailoutId(local_id(0)); }
389 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
392 Expression(Zone* zone, int pos)
394 base_id_(BailoutId::None().ToInt()),
395 bounds_(Bounds::Unbounded(zone)),
397 static int parent_num_ids() { return 0; }
398 void set_to_boolean_types(uint16_t types) {
399 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
402 int base_id() const {
403 DCHECK(!BailoutId(base_id_).IsNone());
408 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
412 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
414 // Ends with 16-bit field; deriving classes in turn begin with
415 // 16-bit fields for optimum packing efficiency.
419 class BreakableStatement : public Statement {
422 TARGET_FOR_ANONYMOUS,
423 TARGET_FOR_NAMED_ONLY
426 // The labels associated with this statement. May be NULL;
427 // if it is != NULL, guaranteed to contain at least one entry.
428 ZoneList<const AstRawString*>* labels() const { return labels_; }
430 // Type testing & conversion.
431 BreakableStatement* AsBreakableStatement() final { return this; }
434 Label* break_target() { return &break_target_; }
437 bool is_target_for_anonymous() const {
438 return breakable_type_ == TARGET_FOR_ANONYMOUS;
441 void set_base_id(int id) { base_id_ = id; }
442 static int num_ids() { return parent_num_ids() + 2; }
443 BailoutId EntryId() const { return BailoutId(local_id(0)); }
444 BailoutId ExitId() const { return BailoutId(local_id(1)); }
447 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
448 BreakableType breakable_type, int position)
449 : Statement(zone, position),
451 breakable_type_(breakable_type),
452 base_id_(BailoutId::None().ToInt()) {
453 DCHECK(labels == NULL || labels->length() > 0);
455 static int parent_num_ids() { return 0; }
457 int base_id() const {
458 DCHECK(!BailoutId(base_id_).IsNone());
463 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
465 ZoneList<const AstRawString*>* labels_;
466 BreakableType breakable_type_;
472 class Block final : public BreakableStatement {
474 DECLARE_NODE_TYPE(Block)
476 void AddStatement(Statement* statement, Zone* zone) {
477 statements_.Add(statement, zone);
480 ZoneList<Statement*>* statements() { return &statements_; }
481 bool ignore_completion_value() const { return ignore_completion_value_; }
483 static int num_ids() { return parent_num_ids() + 1; }
484 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
486 bool IsJump() const override {
487 return !statements_.is_empty() && statements_.last()->IsJump()
488 && labels() == NULL; // Good enough as an approximation...
491 Scope* scope() const { return scope_; }
492 void set_scope(Scope* scope) { scope_ = scope; }
495 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
496 bool ignore_completion_value, int pos)
497 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
498 statements_(capacity, zone),
499 ignore_completion_value_(ignore_completion_value),
501 static int parent_num_ids() { return BreakableStatement::num_ids(); }
504 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
506 ZoneList<Statement*> statements_;
507 bool ignore_completion_value_;
512 class Declaration : public AstNode {
514 VariableProxy* proxy() const { return proxy_; }
515 VariableMode mode() const { return mode_; }
516 Scope* scope() const { return scope_; }
517 virtual InitializationFlag initialization() const = 0;
518 virtual bool IsInlineable() const;
521 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
523 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
524 DCHECK(IsDeclaredVariableMode(mode));
529 VariableProxy* proxy_;
531 // Nested scope from which the declaration originated.
536 class VariableDeclaration final : public Declaration {
538 DECLARE_NODE_TYPE(VariableDeclaration)
540 InitializationFlag initialization() const override {
541 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
544 bool is_class_declaration() const { return is_class_declaration_; }
546 // VariableDeclarations can be grouped into consecutive declaration
547 // groups. Each VariableDeclaration is associated with the start position of
548 // the group it belongs to. The positions are used for strong mode scope
549 // checks for classes and functions.
550 int declaration_group_start() const { return declaration_group_start_; }
553 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
554 Scope* scope, int pos, bool is_class_declaration = false,
555 int declaration_group_start = -1)
556 : Declaration(zone, proxy, mode, scope, pos),
557 is_class_declaration_(is_class_declaration),
558 declaration_group_start_(declaration_group_start) {}
560 bool is_class_declaration_;
561 int declaration_group_start_;
565 class FunctionDeclaration final : public Declaration {
567 DECLARE_NODE_TYPE(FunctionDeclaration)
569 FunctionLiteral* fun() const { return fun_; }
570 InitializationFlag initialization() const override {
571 return kCreatedInitialized;
573 bool IsInlineable() const override;
576 FunctionDeclaration(Zone* zone,
577 VariableProxy* proxy,
579 FunctionLiteral* fun,
582 : Declaration(zone, proxy, mode, scope, pos),
584 DCHECK(mode == VAR || mode == LET || mode == CONST);
589 FunctionLiteral* fun_;
593 class ImportDeclaration final : public Declaration {
595 DECLARE_NODE_TYPE(ImportDeclaration)
597 const AstRawString* import_name() const { return import_name_; }
598 const AstRawString* module_specifier() const { return module_specifier_; }
599 void set_module_specifier(const AstRawString* module_specifier) {
600 DCHECK(module_specifier_ == NULL);
601 module_specifier_ = module_specifier;
603 InitializationFlag initialization() const override {
604 return kNeedsInitialization;
608 ImportDeclaration(Zone* zone, VariableProxy* proxy,
609 const AstRawString* import_name,
610 const AstRawString* module_specifier, Scope* scope, int pos)
611 : Declaration(zone, proxy, IMPORT, scope, pos),
612 import_name_(import_name),
613 module_specifier_(module_specifier) {}
616 const AstRawString* import_name_;
617 const AstRawString* module_specifier_;
621 class ExportDeclaration final : public Declaration {
623 DECLARE_NODE_TYPE(ExportDeclaration)
625 InitializationFlag initialization() const override {
626 return kCreatedInitialized;
630 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
631 : Declaration(zone, proxy, LET, scope, pos) {}
635 class Module : public AstNode {
637 ModuleDescriptor* descriptor() const { return descriptor_; }
638 Block* body() const { return body_; }
641 Module(Zone* zone, int pos)
642 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
643 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
644 : AstNode(pos), descriptor_(descriptor), body_(body) {}
647 ModuleDescriptor* descriptor_;
652 class IterationStatement : public BreakableStatement {
654 // Type testing & conversion.
655 IterationStatement* AsIterationStatement() final { return this; }
657 Statement* body() const { return body_; }
659 static int num_ids() { return parent_num_ids() + 1; }
660 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
661 virtual BailoutId ContinueId() const = 0;
662 virtual BailoutId StackCheckId() const = 0;
665 Label* continue_target() { return &continue_target_; }
668 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
669 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
671 static int parent_num_ids() { return BreakableStatement::num_ids(); }
672 void Initialize(Statement* body) { body_ = body; }
675 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
678 Label continue_target_;
682 class DoWhileStatement final : public IterationStatement {
684 DECLARE_NODE_TYPE(DoWhileStatement)
686 void Initialize(Expression* cond, Statement* body) {
687 IterationStatement::Initialize(body);
691 Expression* cond() const { return cond_; }
693 static int num_ids() { return parent_num_ids() + 2; }
694 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
695 BailoutId StackCheckId() const override { return BackEdgeId(); }
696 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
699 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
700 : IterationStatement(zone, labels, pos), cond_(NULL) {}
701 static int parent_num_ids() { return IterationStatement::num_ids(); }
704 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
710 class WhileStatement final : public IterationStatement {
712 DECLARE_NODE_TYPE(WhileStatement)
714 void Initialize(Expression* cond, Statement* body) {
715 IterationStatement::Initialize(body);
719 Expression* cond() const { return cond_; }
721 static int num_ids() { return parent_num_ids() + 1; }
722 BailoutId ContinueId() const override { return EntryId(); }
723 BailoutId StackCheckId() const override { return BodyId(); }
724 BailoutId BodyId() const { return BailoutId(local_id(0)); }
727 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
728 : IterationStatement(zone, labels, pos), cond_(NULL) {}
729 static int parent_num_ids() { return IterationStatement::num_ids(); }
732 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
738 class ForStatement final : public IterationStatement {
740 DECLARE_NODE_TYPE(ForStatement)
742 void Initialize(Statement* init,
746 IterationStatement::Initialize(body);
752 Statement* init() const { return init_; }
753 Expression* cond() const { return cond_; }
754 Statement* next() const { return next_; }
756 static int num_ids() { return parent_num_ids() + 2; }
757 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
758 BailoutId StackCheckId() const override { return BodyId(); }
759 BailoutId BodyId() const { return BailoutId(local_id(1)); }
762 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
763 : IterationStatement(zone, labels, pos),
767 static int parent_num_ids() { return IterationStatement::num_ids(); }
770 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
778 class ForEachStatement : public IterationStatement {
781 ENUMERATE, // for (each in subject) body;
782 ITERATE // for (each of subject) body;
785 void Initialize(Expression* each, Expression* subject, Statement* body) {
786 IterationStatement::Initialize(body);
791 Expression* each() const { return each_; }
792 Expression* subject() const { return subject_; }
794 FeedbackVectorRequirements ComputeFeedbackRequirements(
795 Isolate* isolate, const ICSlotCache* cache) override;
796 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
797 ICSlotCache* cache) override {
800 Code::Kind FeedbackICSlotKind(int index) override;
801 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
804 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
805 : IterationStatement(zone, labels, pos),
808 each_slot_(FeedbackVectorICSlot::Invalid()) {}
812 Expression* subject_;
813 FeedbackVectorICSlot each_slot_;
817 class ForInStatement final : public ForEachStatement {
819 DECLARE_NODE_TYPE(ForInStatement)
821 Expression* enumerable() const {
825 // Type feedback information.
826 FeedbackVectorRequirements ComputeFeedbackRequirements(
827 Isolate* isolate, const ICSlotCache* cache) override {
828 FeedbackVectorRequirements base =
829 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
830 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
831 return FeedbackVectorRequirements(1, base.ic_slots());
833 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
834 for_in_feedback_slot_ = slot;
837 FeedbackVectorSlot ForInFeedbackSlot() {
838 DCHECK(!for_in_feedback_slot_.IsInvalid());
839 return for_in_feedback_slot_;
842 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
843 ForInType for_in_type() const { return for_in_type_; }
844 void set_for_in_type(ForInType type) { for_in_type_ = type; }
846 static int num_ids() { return parent_num_ids() + 6; }
847 BailoutId BodyId() const { return BailoutId(local_id(0)); }
848 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
849 BailoutId EnumId() const { return BailoutId(local_id(2)); }
850 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
851 BailoutId FilterId() const { return BailoutId(local_id(4)); }
852 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
853 BailoutId ContinueId() const override { return EntryId(); }
854 BailoutId StackCheckId() const override { return BodyId(); }
857 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
858 : ForEachStatement(zone, labels, pos),
859 for_in_type_(SLOW_FOR_IN),
860 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
861 static int parent_num_ids() { return ForEachStatement::num_ids(); }
864 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
866 ForInType for_in_type_;
867 FeedbackVectorSlot for_in_feedback_slot_;
871 class ForOfStatement final : public ForEachStatement {
873 DECLARE_NODE_TYPE(ForOfStatement)
875 void Initialize(Expression* each,
878 Expression* assign_iterator,
879 Expression* next_result,
880 Expression* result_done,
881 Expression* assign_each) {
882 ForEachStatement::Initialize(each, subject, body);
883 assign_iterator_ = assign_iterator;
884 next_result_ = next_result;
885 result_done_ = result_done;
886 assign_each_ = assign_each;
889 Expression* iterable() const {
893 // iterator = subject[Symbol.iterator]()
894 Expression* assign_iterator() const {
895 return assign_iterator_;
898 // result = iterator.next() // with type check
899 Expression* next_result() const {
904 Expression* result_done() const {
908 // each = result.value
909 Expression* assign_each() const {
913 BailoutId ContinueId() const override { return EntryId(); }
914 BailoutId StackCheckId() const override { return BackEdgeId(); }
916 static int num_ids() { return parent_num_ids() + 1; }
917 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
920 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
921 : ForEachStatement(zone, labels, pos),
922 assign_iterator_(NULL),
925 assign_each_(NULL) {}
926 static int parent_num_ids() { return ForEachStatement::num_ids(); }
929 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
931 Expression* assign_iterator_;
932 Expression* next_result_;
933 Expression* result_done_;
934 Expression* assign_each_;
938 class ExpressionStatement final : public Statement {
940 DECLARE_NODE_TYPE(ExpressionStatement)
942 void set_expression(Expression* e) { expression_ = e; }
943 Expression* expression() const { return expression_; }
944 bool IsJump() const override { return expression_->IsThrow(); }
947 ExpressionStatement(Zone* zone, Expression* expression, int pos)
948 : Statement(zone, pos), expression_(expression) { }
951 Expression* expression_;
955 class JumpStatement : public Statement {
957 bool IsJump() const final { return true; }
960 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
964 class ContinueStatement final : public JumpStatement {
966 DECLARE_NODE_TYPE(ContinueStatement)
968 IterationStatement* target() const { return target_; }
971 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
972 : JumpStatement(zone, pos), target_(target) { }
975 IterationStatement* target_;
979 class BreakStatement final : public JumpStatement {
981 DECLARE_NODE_TYPE(BreakStatement)
983 BreakableStatement* target() const { return target_; }
986 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
987 : JumpStatement(zone, pos), target_(target) { }
990 BreakableStatement* target_;
994 class ReturnStatement final : public JumpStatement {
996 DECLARE_NODE_TYPE(ReturnStatement)
998 Expression* expression() const { return expression_; }
1001 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1002 : JumpStatement(zone, pos), expression_(expression) { }
1005 Expression* expression_;
1009 class WithStatement final : public Statement {
1011 DECLARE_NODE_TYPE(WithStatement)
1013 Scope* scope() { return scope_; }
1014 Expression* expression() const { return expression_; }
1015 Statement* statement() const { return statement_; }
1017 void set_base_id(int id) { base_id_ = id; }
1018 static int num_ids() { return parent_num_ids() + 1; }
1019 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1022 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1023 Statement* statement, int pos)
1024 : Statement(zone, pos),
1026 expression_(expression),
1027 statement_(statement),
1028 base_id_(BailoutId::None().ToInt()) {}
1029 static int parent_num_ids() { return 0; }
1031 int base_id() const {
1032 DCHECK(!BailoutId(base_id_).IsNone());
1037 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1040 Expression* expression_;
1041 Statement* statement_;
1046 class CaseClause final : public Expression {
1048 DECLARE_NODE_TYPE(CaseClause)
1050 bool is_default() const { return label_ == NULL; }
1051 Expression* label() const {
1052 CHECK(!is_default());
1055 Label* body_target() { return &body_target_; }
1056 ZoneList<Statement*>* statements() const { return statements_; }
1058 static int num_ids() { return parent_num_ids() + 2; }
1059 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1060 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1062 Type* compare_type() { return compare_type_; }
1063 void set_compare_type(Type* type) { compare_type_ = type; }
1066 static int parent_num_ids() { return Expression::num_ids(); }
1069 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1071 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1075 ZoneList<Statement*>* statements_;
1076 Type* compare_type_;
1080 class SwitchStatement final : public BreakableStatement {
1082 DECLARE_NODE_TYPE(SwitchStatement)
1084 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1089 Expression* tag() const { return tag_; }
1090 ZoneList<CaseClause*>* cases() const { return cases_; }
1093 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1094 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1100 ZoneList<CaseClause*>* cases_;
1104 // If-statements always have non-null references to their then- and
1105 // else-parts. When parsing if-statements with no explicit else-part,
1106 // the parser implicitly creates an empty statement. Use the
1107 // HasThenStatement() and HasElseStatement() functions to check if a
1108 // given if-statement has a then- or an else-part containing code.
1109 class IfStatement final : public Statement {
1111 DECLARE_NODE_TYPE(IfStatement)
1113 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1114 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1116 Expression* condition() const { return condition_; }
1117 Statement* then_statement() const { return then_statement_; }
1118 Statement* else_statement() const { return else_statement_; }
1120 bool IsJump() const override {
1121 return HasThenStatement() && then_statement()->IsJump()
1122 && HasElseStatement() && else_statement()->IsJump();
1125 void set_base_id(int id) { base_id_ = id; }
1126 static int num_ids() { return parent_num_ids() + 3; }
1127 BailoutId IfId() const { return BailoutId(local_id(0)); }
1128 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1129 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1132 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1133 Statement* else_statement, int pos)
1134 : Statement(zone, pos),
1135 condition_(condition),
1136 then_statement_(then_statement),
1137 else_statement_(else_statement),
1138 base_id_(BailoutId::None().ToInt()) {}
1139 static int parent_num_ids() { return 0; }
1141 int base_id() const {
1142 DCHECK(!BailoutId(base_id_).IsNone());
1147 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1149 Expression* condition_;
1150 Statement* then_statement_;
1151 Statement* else_statement_;
1156 class TryStatement : public Statement {
1158 Block* try_block() const { return try_block_; }
1160 void set_base_id(int id) { base_id_ = id; }
1161 static int num_ids() { return parent_num_ids() + 1; }
1162 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1165 TryStatement(Zone* zone, Block* try_block, int pos)
1166 : Statement(zone, pos),
1167 try_block_(try_block),
1168 base_id_(BailoutId::None().ToInt()) {}
1169 static int parent_num_ids() { return 0; }
1171 int base_id() const {
1172 DCHECK(!BailoutId(base_id_).IsNone());
1177 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1184 class TryCatchStatement final : public TryStatement {
1186 DECLARE_NODE_TYPE(TryCatchStatement)
1188 Scope* scope() { return scope_; }
1189 Variable* variable() { return variable_; }
1190 Block* catch_block() const { return catch_block_; }
1193 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1194 Variable* variable, Block* catch_block, int pos)
1195 : TryStatement(zone, try_block, pos),
1197 variable_(variable),
1198 catch_block_(catch_block) {}
1202 Variable* variable_;
1203 Block* catch_block_;
1207 class TryFinallyStatement final : public TryStatement {
1209 DECLARE_NODE_TYPE(TryFinallyStatement)
1211 Block* finally_block() const { return finally_block_; }
1214 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1216 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1219 Block* finally_block_;
1223 class DebuggerStatement final : public Statement {
1225 DECLARE_NODE_TYPE(DebuggerStatement)
1227 void set_base_id(int id) { base_id_ = id; }
1228 static int num_ids() { return parent_num_ids() + 1; }
1229 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1232 explicit DebuggerStatement(Zone* zone, int pos)
1233 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1234 static int parent_num_ids() { return 0; }
1236 int base_id() const {
1237 DCHECK(!BailoutId(base_id_).IsNone());
1242 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1248 class EmptyStatement final : public Statement {
1250 DECLARE_NODE_TYPE(EmptyStatement)
1253 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1257 class Literal final : public Expression {
1259 DECLARE_NODE_TYPE(Literal)
1261 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1263 Handle<String> AsPropertyName() {
1264 DCHECK(IsPropertyName());
1265 return Handle<String>::cast(value());
1268 const AstRawString* AsRawPropertyName() {
1269 DCHECK(IsPropertyName());
1270 return value_->AsString();
1273 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1274 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1276 Handle<Object> value() const { return value_->value(); }
1277 const AstValue* raw_value() const { return value_; }
1279 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1280 // only for string and number literals!
1282 static bool Match(void* literal1, void* literal2);
1284 static int num_ids() { return parent_num_ids() + 1; }
1285 TypeFeedbackId LiteralFeedbackId() const {
1286 return TypeFeedbackId(local_id(0));
1290 Literal(Zone* zone, const AstValue* value, int position)
1291 : Expression(zone, position), value_(value) {}
1292 static int parent_num_ids() { return Expression::num_ids(); }
1295 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1297 const AstValue* value_;
1301 // Base class for literals that needs space in the corresponding JSFunction.
1302 class MaterializedLiteral : public Expression {
1304 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1306 int literal_index() { return literal_index_; }
1309 // only callable after initialization.
1310 DCHECK(depth_ >= 1);
1314 bool is_strong() const { return is_strong_; }
1317 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1318 : Expression(zone, pos),
1319 literal_index_(literal_index),
1321 is_strong_(is_strong),
1324 // A materialized literal is simple if the values consist of only
1325 // constants and simple object and array literals.
1326 bool is_simple() const { return is_simple_; }
1327 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1328 friend class CompileTimeValue;
1330 void set_depth(int depth) {
1335 // Populate the constant properties/elements fixed array.
1336 void BuildConstants(Isolate* isolate);
1337 friend class ArrayLiteral;
1338 friend class ObjectLiteral;
1340 // If the expression is a literal, return the literal value;
1341 // if the expression is a materialized literal and is simple return a
1342 // compile time value as encoded by CompileTimeValue::GetValue().
1343 // Otherwise, return undefined literal as the placeholder
1344 // in the object literal boilerplate.
1345 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1355 // Property is used for passing information
1356 // about an object literal's properties from the parser
1357 // to the code generator.
1358 class ObjectLiteralProperty final : public ZoneObject {
1361 CONSTANT, // Property with constant value (compile time).
1362 COMPUTED, // Property with computed value (execution time).
1363 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1364 GETTER, SETTER, // Property is an accessor function.
1365 PROTOTYPE // Property is __proto__.
1368 Expression* key() { return key_; }
1369 Expression* value() { return value_; }
1370 Kind kind() { return kind_; }
1372 // Type feedback information.
1373 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1374 Handle<Map> GetReceiverType() { return receiver_type_; }
1376 bool IsCompileTimeValue();
1378 void set_emit_store(bool emit_store);
1381 bool is_static() const { return is_static_; }
1382 bool is_computed_name() const { return is_computed_name_; }
1384 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1387 friend class AstNodeFactory;
1389 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1390 bool is_static, bool is_computed_name);
1391 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1392 Expression* value, bool is_static,
1393 bool is_computed_name);
1401 bool is_computed_name_;
1402 Handle<Map> receiver_type_;
1406 // An object literal has a boilerplate object that is used
1407 // for minimizing the work when constructing it at runtime.
1408 class ObjectLiteral final : public MaterializedLiteral {
1410 typedef ObjectLiteralProperty Property;
1412 DECLARE_NODE_TYPE(ObjectLiteral)
1414 Handle<FixedArray> constant_properties() const {
1415 return constant_properties_;
1417 int properties_count() const { return constant_properties_->length() / 2; }
1418 ZoneList<Property*>* properties() const { return properties_; }
1419 bool fast_elements() const { return fast_elements_; }
1420 bool may_store_doubles() const { return may_store_doubles_; }
1421 bool has_function() const { return has_function_; }
1422 bool has_elements() const { return has_elements_; }
1424 // Decide if a property should be in the object boilerplate.
1425 static bool IsBoilerplateProperty(Property* property);
1427 // Populate the constant properties fixed array.
1428 void BuildConstantProperties(Isolate* isolate);
1430 // Mark all computed expressions that are bound to a key that
1431 // is shadowed by a later occurrence of the same key. For the
1432 // marked expressions, no store code is emitted.
1433 void CalculateEmitStore(Zone* zone);
1435 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1436 int ComputeFlags(bool disable_mementos = false) const {
1437 int flags = fast_elements() ? kFastElements : kNoFlags;
1438 flags |= has_function() ? kHasFunction : kNoFlags;
1439 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1440 flags |= kShallowProperties;
1442 if (disable_mementos) {
1443 flags |= kDisableMementos;
1454 kHasFunction = 1 << 1,
1455 kShallowProperties = 1 << 2,
1456 kDisableMementos = 1 << 3,
1460 struct Accessors: public ZoneObject {
1461 Accessors() : getter(NULL), setter(NULL) {}
1466 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1468 // Return an AST id for a property that is used in simulate instructions.
1469 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1471 // Unlike other AST nodes, this number of bailout IDs allocated for an
1472 // ObjectLiteral can vary, so num_ids() is not a static method.
1473 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1475 // Object literals need one feedback slot for each non-trivial value, as well
1476 // as some slots for home objects.
1477 FeedbackVectorRequirements ComputeFeedbackRequirements(
1478 Isolate* isolate, const ICSlotCache* cache) override;
1479 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1480 ICSlotCache* cache) override {
1483 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1484 FeedbackVectorICSlot GetNthSlot(int n) const {
1485 return FeedbackVectorICSlot(slot_.ToInt() + n);
1488 // If value needs a home object, returns a valid feedback vector ic slot
1489 // given by slot_index, and increments slot_index.
1490 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1491 int* slot_index) const;
1494 int slot_count() const { return slot_count_; }
1498 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1499 int boilerplate_properties, bool has_function, bool is_strong,
1501 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1502 properties_(properties),
1503 boilerplate_properties_(boilerplate_properties),
1504 fast_elements_(false),
1505 has_elements_(false),
1506 may_store_doubles_(false),
1507 has_function_(has_function),
1511 slot_(FeedbackVectorICSlot::Invalid()) {
1513 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1516 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1517 Handle<FixedArray> constant_properties_;
1518 ZoneList<Property*>* properties_;
1519 int boilerplate_properties_;
1520 bool fast_elements_;
1522 bool may_store_doubles_;
1525 // slot_count_ helps validate that the logic to allocate ic slots and the
1526 // logic to use them are in sync.
1529 FeedbackVectorICSlot slot_;
1533 // Node for capturing a regexp literal.
1534 class RegExpLiteral final : public MaterializedLiteral {
1536 DECLARE_NODE_TYPE(RegExpLiteral)
1538 Handle<String> pattern() const { return pattern_->string(); }
1539 Handle<String> flags() const { return flags_->string(); }
1542 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1543 const AstRawString* flags, int literal_index, bool is_strong,
1545 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1552 const AstRawString* pattern_;
1553 const AstRawString* flags_;
1557 // An array literal has a literals object that is used
1558 // for minimizing the work when constructing it at runtime.
1559 class ArrayLiteral final : public MaterializedLiteral {
1561 DECLARE_NODE_TYPE(ArrayLiteral)
1563 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1564 ElementsKind constant_elements_kind() const {
1565 DCHECK_EQ(2, constant_elements_->length());
1566 return static_cast<ElementsKind>(
1567 Smi::cast(constant_elements_->get(0))->value());
1570 ZoneList<Expression*>* values() const { return values_; }
1572 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1574 // Return an AST id for an element that is used in simulate instructions.
1575 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1577 // Unlike other AST nodes, this number of bailout IDs allocated for an
1578 // ArrayLiteral can vary, so num_ids() is not a static method.
1579 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1581 // Populate the constant elements fixed array.
1582 void BuildConstantElements(Isolate* isolate);
1584 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1585 int ComputeFlags(bool disable_mementos = false) const {
1586 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1587 if (disable_mementos) {
1588 flags |= kDisableMementos;
1598 kShallowElements = 1,
1599 kDisableMementos = 1 << 1,
1604 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1605 bool is_strong, int pos)
1606 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1608 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1611 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1613 Handle<FixedArray> constant_elements_;
1614 ZoneList<Expression*>* values_;
1618 class VariableProxy final : public Expression {
1620 DECLARE_NODE_TYPE(VariableProxy)
1622 bool IsValidReferenceExpression() const override { return !is_this(); }
1624 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1626 Handle<String> name() const { return raw_name()->string(); }
1627 const AstRawString* raw_name() const {
1628 return is_resolved() ? var_->raw_name() : raw_name_;
1631 Variable* var() const {
1632 DCHECK(is_resolved());
1635 void set_var(Variable* v) {
1636 DCHECK(!is_resolved());
1641 bool is_this() const { return IsThisField::decode(bit_field_); }
1643 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1644 void set_is_assigned() {
1645 bit_field_ = IsAssignedField::update(bit_field_, true);
1648 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1649 void set_is_resolved() {
1650 bit_field_ = IsResolvedField::update(bit_field_, true);
1653 int end_position() const { return end_position_; }
1655 // Bind this proxy to the variable var.
1656 void BindTo(Variable* var);
1658 bool UsesVariableFeedbackSlot() const {
1659 return var()->IsUnallocated() || var()->IsLookupSlot();
1662 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1663 Isolate* isolate, const ICSlotCache* cache) override;
1665 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1666 ICSlotCache* cache) override;
1667 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1668 FeedbackVectorICSlot VariableFeedbackSlot() {
1669 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1670 return variable_feedback_slot_;
1673 static int num_ids() { return parent_num_ids() + 1; }
1674 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1677 VariableProxy(Zone* zone, Variable* var, int start_position,
1680 VariableProxy(Zone* zone, const AstRawString* name,
1681 Variable::Kind variable_kind, int start_position,
1683 static int parent_num_ids() { return Expression::num_ids(); }
1684 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1686 class IsThisField : public BitField8<bool, 0, 1> {};
1687 class IsAssignedField : public BitField8<bool, 1, 1> {};
1688 class IsResolvedField : public BitField8<bool, 2, 1> {};
1690 // Start with 16-bit (or smaller) field, which should get packed together
1691 // with Expression's trailing 16-bit field.
1693 FeedbackVectorICSlot variable_feedback_slot_;
1695 const AstRawString* raw_name_; // if !is_resolved_
1696 Variable* var_; // if is_resolved_
1698 // Position is stored in the AstNode superclass, but VariableProxy needs to
1699 // know its end position too (for error messages). It cannot be inferred from
1700 // the variable name length because it can contain escapes.
1705 // Left-hand side can only be a property, a global or a (parameter or local)
1711 NAMED_SUPER_PROPERTY,
1712 KEYED_SUPER_PROPERTY
1716 class Property final : public Expression {
1718 DECLARE_NODE_TYPE(Property)
1720 bool IsValidReferenceExpression() const override { return true; }
1722 Expression* obj() const { return obj_; }
1723 Expression* key() const { return key_; }
1725 static int num_ids() { return parent_num_ids() + 1; }
1726 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1728 bool IsStringAccess() const {
1729 return IsStringAccessField::decode(bit_field_);
1732 // Type feedback information.
1733 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1734 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1735 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1736 IcCheckType GetKeyType() const override {
1737 return KeyTypeField::decode(bit_field_);
1739 bool IsUninitialized() const {
1740 return !is_for_call() && HasNoTypeInformation();
1742 bool HasNoTypeInformation() const {
1743 return GetInlineCacheState() == UNINITIALIZED;
1745 InlineCacheState GetInlineCacheState() const {
1746 return InlineCacheStateField::decode(bit_field_);
1748 void set_is_string_access(bool b) {
1749 bit_field_ = IsStringAccessField::update(bit_field_, b);
1751 void set_key_type(IcCheckType key_type) {
1752 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1754 void set_inline_cache_state(InlineCacheState state) {
1755 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1757 void mark_for_call() {
1758 bit_field_ = IsForCallField::update(bit_field_, true);
1760 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1762 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1764 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1765 Isolate* isolate, const ICSlotCache* cache) override {
1766 return FeedbackVectorRequirements(0, 1);
1768 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1769 ICSlotCache* cache) override {
1770 property_feedback_slot_ = slot;
1772 Code::Kind FeedbackICSlotKind(int index) override {
1773 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1776 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1777 DCHECK(!property_feedback_slot_.IsInvalid());
1778 return property_feedback_slot_;
1781 static LhsKind GetAssignType(Property* property) {
1782 if (property == NULL) return VARIABLE;
1783 bool super_access = property->IsSuperAccess();
1784 return (property->key()->IsPropertyName())
1785 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1786 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1790 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1791 : Expression(zone, pos),
1792 bit_field_(IsForCallField::encode(false) |
1793 IsStringAccessField::encode(false) |
1794 InlineCacheStateField::encode(UNINITIALIZED)),
1795 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1798 static int parent_num_ids() { return Expression::num_ids(); }
1801 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1803 class IsForCallField : public BitField8<bool, 0, 1> {};
1804 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1805 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1806 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1808 FeedbackVectorICSlot property_feedback_slot_;
1811 SmallMapList receiver_types_;
1815 class Call final : public Expression {
1817 DECLARE_NODE_TYPE(Call)
1819 Expression* expression() const { return expression_; }
1820 ZoneList<Expression*>* arguments() const { return arguments_; }
1822 // Type feedback information.
1823 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1824 Isolate* isolate, const ICSlotCache* cache) override;
1825 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1826 ICSlotCache* cache) override {
1829 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1830 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1832 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1834 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1836 SmallMapList* GetReceiverTypes() override {
1837 if (expression()->IsProperty()) {
1838 return expression()->AsProperty()->GetReceiverTypes();
1843 bool IsMonomorphic() override {
1844 if (expression()->IsProperty()) {
1845 return expression()->AsProperty()->IsMonomorphic();
1847 return !target_.is_null();
1850 bool global_call() const {
1851 VariableProxy* proxy = expression_->AsVariableProxy();
1852 return proxy != NULL && proxy->var()->IsUnallocated();
1855 bool known_global_function() const {
1856 return global_call() && !target_.is_null();
1859 Handle<JSFunction> target() { return target_; }
1861 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1863 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1865 set_is_uninitialized(false);
1867 void set_target(Handle<JSFunction> target) { target_ = target; }
1868 void set_allocation_site(Handle<AllocationSite> site) {
1869 allocation_site_ = site;
1872 static int num_ids() { return parent_num_ids() + 3; }
1873 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1874 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1875 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1877 bool is_uninitialized() const {
1878 return IsUninitializedField::decode(bit_field_);
1880 void set_is_uninitialized(bool b) {
1881 bit_field_ = IsUninitializedField::update(bit_field_, b);
1893 // Helpers to determine how to handle the call.
1894 CallType GetCallType(Isolate* isolate) const;
1895 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1896 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1899 // Used to assert that the FullCodeGenerator records the return site.
1900 bool return_is_recorded_;
1904 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1906 : Expression(zone, pos),
1907 ic_slot_(FeedbackVectorICSlot::Invalid()),
1908 slot_(FeedbackVectorSlot::Invalid()),
1909 expression_(expression),
1910 arguments_(arguments),
1911 bit_field_(IsUninitializedField::encode(false)) {
1912 if (expression->IsProperty()) {
1913 expression->AsProperty()->mark_for_call();
1916 static int parent_num_ids() { return Expression::num_ids(); }
1919 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1921 FeedbackVectorICSlot ic_slot_;
1922 FeedbackVectorSlot slot_;
1923 Expression* expression_;
1924 ZoneList<Expression*>* arguments_;
1925 Handle<JSFunction> target_;
1926 Handle<AllocationSite> allocation_site_;
1927 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1932 class CallNew final : public Expression {
1934 DECLARE_NODE_TYPE(CallNew)
1936 Expression* expression() const { return expression_; }
1937 ZoneList<Expression*>* arguments() const { return arguments_; }
1939 // Type feedback information.
1940 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1941 Isolate* isolate, const ICSlotCache* cache) override {
1942 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1944 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1945 callnew_feedback_slot_ = slot;
1948 FeedbackVectorSlot CallNewFeedbackSlot() {
1949 DCHECK(!callnew_feedback_slot_.IsInvalid());
1950 return callnew_feedback_slot_;
1952 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1953 DCHECK(FLAG_pretenuring_call_new);
1954 return CallNewFeedbackSlot().next();
1957 bool IsMonomorphic() override { return is_monomorphic_; }
1958 Handle<JSFunction> target() const { return target_; }
1959 Handle<AllocationSite> allocation_site() const {
1960 return allocation_site_;
1963 static int num_ids() { return parent_num_ids() + 1; }
1964 static int feedback_slots() { return 1; }
1965 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1967 void set_allocation_site(Handle<AllocationSite> site) {
1968 allocation_site_ = site;
1970 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1971 void set_target(Handle<JSFunction> target) { target_ = target; }
1972 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1974 is_monomorphic_ = true;
1978 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1980 : Expression(zone, pos),
1981 expression_(expression),
1982 arguments_(arguments),
1983 is_monomorphic_(false),
1984 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1986 static int parent_num_ids() { return Expression::num_ids(); }
1989 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1991 Expression* expression_;
1992 ZoneList<Expression*>* arguments_;
1993 bool is_monomorphic_;
1994 Handle<JSFunction> target_;
1995 Handle<AllocationSite> allocation_site_;
1996 FeedbackVectorSlot callnew_feedback_slot_;
2000 // The CallRuntime class does not represent any official JavaScript
2001 // language construct. Instead it is used to call a C or JS function
2002 // with a set of arguments. This is used from the builtins that are
2003 // implemented in JavaScript (see "v8natives.js").
2004 class CallRuntime final : public Expression {
2006 DECLARE_NODE_TYPE(CallRuntime)
2008 Handle<String> name() const { return raw_name_->string(); }
2009 const AstRawString* raw_name() const { return raw_name_; }
2010 const Runtime::Function* function() const { return function_; }
2011 ZoneList<Expression*>* arguments() const { return arguments_; }
2012 bool is_jsruntime() const { return function_ == NULL; }
2014 // Type feedback information.
2015 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2016 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2017 Isolate* isolate, const ICSlotCache* cache) override {
2018 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2020 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2021 ICSlotCache* cache) override {
2022 callruntime_feedback_slot_ = slot;
2024 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2026 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2027 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2028 !callruntime_feedback_slot_.IsInvalid());
2029 return callruntime_feedback_slot_;
2032 static int num_ids() { return parent_num_ids() + 1; }
2033 BailoutId CallId() { return BailoutId(local_id(0)); }
2036 CallRuntime(Zone* zone, const AstRawString* name,
2037 const Runtime::Function* function,
2038 ZoneList<Expression*>* arguments, int pos)
2039 : Expression(zone, pos),
2041 function_(function),
2042 arguments_(arguments),
2043 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2044 static int parent_num_ids() { return Expression::num_ids(); }
2047 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2049 const AstRawString* raw_name_;
2050 const Runtime::Function* function_;
2051 ZoneList<Expression*>* arguments_;
2052 FeedbackVectorICSlot callruntime_feedback_slot_;
2056 class UnaryOperation final : public Expression {
2058 DECLARE_NODE_TYPE(UnaryOperation)
2060 Token::Value op() const { return op_; }
2061 Expression* expression() const { return expression_; }
2063 // For unary not (Token::NOT), the AST ids where true and false will
2064 // actually be materialized, respectively.
2065 static int num_ids() { return parent_num_ids() + 2; }
2066 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2067 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2069 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2072 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2073 : Expression(zone, pos), op_(op), expression_(expression) {
2074 DCHECK(Token::IsUnaryOp(op));
2076 static int parent_num_ids() { return Expression::num_ids(); }
2079 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2082 Expression* expression_;
2086 class BinaryOperation final : public Expression {
2088 DECLARE_NODE_TYPE(BinaryOperation)
2090 Token::Value op() const { return static_cast<Token::Value>(op_); }
2091 Expression* left() const { return left_; }
2092 Expression* right() const { return right_; }
2093 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2094 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2095 allocation_site_ = allocation_site;
2098 // The short-circuit logical operations need an AST ID for their
2099 // right-hand subexpression.
2100 static int num_ids() { return parent_num_ids() + 2; }
2101 BailoutId RightId() const { return BailoutId(local_id(0)); }
2103 TypeFeedbackId BinaryOperationFeedbackId() const {
2104 return TypeFeedbackId(local_id(1));
2106 Maybe<int> fixed_right_arg() const {
2107 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2109 void set_fixed_right_arg(Maybe<int> arg) {
2110 has_fixed_right_arg_ = arg.IsJust();
2111 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2114 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2117 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2118 Expression* right, int pos)
2119 : Expression(zone, pos),
2120 op_(static_cast<byte>(op)),
2121 has_fixed_right_arg_(false),
2122 fixed_right_arg_value_(0),
2125 DCHECK(Token::IsBinaryOp(op));
2127 static int parent_num_ids() { return Expression::num_ids(); }
2130 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2132 const byte op_; // actually Token::Value
2133 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2134 // type for the RHS. Currenty it's actually a Maybe<int>
2135 bool has_fixed_right_arg_;
2136 int fixed_right_arg_value_;
2139 Handle<AllocationSite> allocation_site_;
2143 class CountOperation final : public Expression {
2145 DECLARE_NODE_TYPE(CountOperation)
2147 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2148 bool is_postfix() const { return !is_prefix(); }
2150 Token::Value op() const { return TokenField::decode(bit_field_); }
2151 Token::Value binary_op() {
2152 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2155 Expression* expression() const { return expression_; }
2157 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2158 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2159 IcCheckType GetKeyType() const override {
2160 return KeyTypeField::decode(bit_field_);
2162 KeyedAccessStoreMode GetStoreMode() const override {
2163 return StoreModeField::decode(bit_field_);
2165 Type* type() const { return type_; }
2166 void set_key_type(IcCheckType type) {
2167 bit_field_ = KeyTypeField::update(bit_field_, type);
2169 void set_store_mode(KeyedAccessStoreMode mode) {
2170 bit_field_ = StoreModeField::update(bit_field_, mode);
2172 void set_type(Type* type) { type_ = type; }
2174 static int num_ids() { return parent_num_ids() + 4; }
2175 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2176 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2177 TypeFeedbackId CountBinOpFeedbackId() const {
2178 return TypeFeedbackId(local_id(2));
2180 TypeFeedbackId CountStoreFeedbackId() const {
2181 return TypeFeedbackId(local_id(3));
2184 FeedbackVectorRequirements ComputeFeedbackRequirements(
2185 Isolate* isolate, const ICSlotCache* cache) override;
2186 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2187 ICSlotCache* cache) override {
2190 Code::Kind FeedbackICSlotKind(int index) override;
2191 FeedbackVectorICSlot CountSlot() const { return slot_; }
2194 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2196 : Expression(zone, pos),
2198 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2199 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2202 slot_(FeedbackVectorICSlot::Invalid()) {}
2203 static int parent_num_ids() { return Expression::num_ids(); }
2206 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2208 class IsPrefixField : public BitField16<bool, 0, 1> {};
2209 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2210 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2211 class TokenField : public BitField16<Token::Value, 6, 8> {};
2213 // Starts with 16-bit field, which should get packed together with
2214 // Expression's trailing 16-bit field.
2215 uint16_t bit_field_;
2217 Expression* expression_;
2218 SmallMapList receiver_types_;
2219 FeedbackVectorICSlot slot_;
2223 class CompareOperation final : public Expression {
2225 DECLARE_NODE_TYPE(CompareOperation)
2227 Token::Value op() const { return op_; }
2228 Expression* left() const { return left_; }
2229 Expression* right() const { return right_; }
2231 // Type feedback information.
2232 static int num_ids() { return parent_num_ids() + 1; }
2233 TypeFeedbackId CompareOperationFeedbackId() const {
2234 return TypeFeedbackId(local_id(0));
2236 Type* combined_type() const { return combined_type_; }
2237 void set_combined_type(Type* type) { combined_type_ = type; }
2239 // Match special cases.
2240 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2241 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2242 bool IsLiteralCompareNull(Expression** expr);
2245 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2246 Expression* right, int pos)
2247 : Expression(zone, pos),
2251 combined_type_(Type::None(zone)) {
2252 DCHECK(Token::IsCompareOp(op));
2254 static int parent_num_ids() { return Expression::num_ids(); }
2257 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2263 Type* combined_type_;
2267 class Spread final : public Expression {
2269 DECLARE_NODE_TYPE(Spread)
2271 Expression* expression() const { return expression_; }
2273 static int num_ids() { return parent_num_ids(); }
2276 Spread(Zone* zone, Expression* expression, int pos)
2277 : Expression(zone, pos), expression_(expression) {}
2278 static int parent_num_ids() { return Expression::num_ids(); }
2281 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2283 Expression* expression_;
2287 class Conditional final : public Expression {
2289 DECLARE_NODE_TYPE(Conditional)
2291 Expression* condition() const { return condition_; }
2292 Expression* then_expression() const { return then_expression_; }
2293 Expression* else_expression() const { return else_expression_; }
2295 static int num_ids() { return parent_num_ids() + 2; }
2296 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2297 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2300 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2301 Expression* else_expression, int position)
2302 : Expression(zone, position),
2303 condition_(condition),
2304 then_expression_(then_expression),
2305 else_expression_(else_expression) {}
2306 static int parent_num_ids() { return Expression::num_ids(); }
2309 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2311 Expression* condition_;
2312 Expression* then_expression_;
2313 Expression* else_expression_;
2317 class Assignment final : public Expression {
2319 DECLARE_NODE_TYPE(Assignment)
2321 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2323 Token::Value binary_op() const;
2325 Token::Value op() const { return TokenField::decode(bit_field_); }
2326 Expression* target() const { return target_; }
2327 Expression* value() const { return value_; }
2328 BinaryOperation* binary_operation() const { return binary_operation_; }
2330 // This check relies on the definition order of token in token.h.
2331 bool is_compound() const { return op() > Token::ASSIGN; }
2333 static int num_ids() { return parent_num_ids() + 2; }
2334 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2336 // Type feedback information.
2337 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2338 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2339 bool IsUninitialized() const {
2340 return IsUninitializedField::decode(bit_field_);
2342 bool HasNoTypeInformation() {
2343 return IsUninitializedField::decode(bit_field_);
2345 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2346 IcCheckType GetKeyType() const override {
2347 return KeyTypeField::decode(bit_field_);
2349 KeyedAccessStoreMode GetStoreMode() const override {
2350 return StoreModeField::decode(bit_field_);
2352 void set_is_uninitialized(bool b) {
2353 bit_field_ = IsUninitializedField::update(bit_field_, b);
2355 void set_key_type(IcCheckType key_type) {
2356 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2358 void set_store_mode(KeyedAccessStoreMode mode) {
2359 bit_field_ = StoreModeField::update(bit_field_, mode);
2362 FeedbackVectorRequirements ComputeFeedbackRequirements(
2363 Isolate* isolate, const ICSlotCache* cache) override;
2364 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2365 ICSlotCache* cache) override {
2368 Code::Kind FeedbackICSlotKind(int index) override;
2369 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2372 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2374 static int parent_num_ids() { return Expression::num_ids(); }
2377 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2379 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2380 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2381 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2382 class TokenField : public BitField16<Token::Value, 6, 8> {};
2384 // Starts with 16-bit field, which should get packed together with
2385 // Expression's trailing 16-bit field.
2386 uint16_t bit_field_;
2387 Expression* target_;
2389 BinaryOperation* binary_operation_;
2390 SmallMapList receiver_types_;
2391 FeedbackVectorICSlot slot_;
2395 class Yield final : public Expression {
2397 DECLARE_NODE_TYPE(Yield)
2400 kInitial, // The initial yield that returns the unboxed generator object.
2401 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2402 kDelegating, // A yield*.
2403 kFinal // A return: { value: EXPRESSION, done: true }
2406 Expression* generator_object() const { return generator_object_; }
2407 Expression* expression() const { return expression_; }
2408 Kind yield_kind() const { return yield_kind_; }
2410 // Type feedback information.
2411 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2412 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2413 Isolate* isolate, const ICSlotCache* cache) override {
2414 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2416 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2417 ICSlotCache* cache) override {
2418 yield_first_feedback_slot_ = slot;
2420 Code::Kind FeedbackICSlotKind(int index) override {
2421 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2424 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2425 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2426 return yield_first_feedback_slot_;
2429 FeedbackVectorICSlot DoneFeedbackSlot() {
2430 return KeyedLoadFeedbackSlot().next();
2433 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2436 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2437 Kind yield_kind, int pos)
2438 : Expression(zone, pos),
2439 generator_object_(generator_object),
2440 expression_(expression),
2441 yield_kind_(yield_kind),
2442 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2445 Expression* generator_object_;
2446 Expression* expression_;
2448 FeedbackVectorICSlot yield_first_feedback_slot_;
2452 class Throw final : public Expression {
2454 DECLARE_NODE_TYPE(Throw)
2456 Expression* exception() const { return exception_; }
2459 Throw(Zone* zone, Expression* exception, int pos)
2460 : Expression(zone, pos), exception_(exception) {}
2463 Expression* exception_;
2467 class FunctionLiteral final : public Expression {
2470 ANONYMOUS_EXPRESSION,
2475 enum ParameterFlag {
2476 kNoDuplicateParameters = 0,
2477 kHasDuplicateParameters = 1
2480 enum IsFunctionFlag {
2485 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2487 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2489 enum ArityRestriction {
2495 DECLARE_NODE_TYPE(FunctionLiteral)
2497 Handle<String> name() const { return raw_name_->string(); }
2498 const AstRawString* raw_name() const { return raw_name_; }
2499 Scope* scope() const { return scope_; }
2500 ZoneList<Statement*>* body() const { return body_; }
2501 void set_function_token_position(int pos) { function_token_position_ = pos; }
2502 int function_token_position() const { return function_token_position_; }
2503 int start_position() const;
2504 int end_position() const;
2505 int SourceSize() const { return end_position() - start_position(); }
2506 bool is_expression() const { return IsExpression::decode(bitfield_); }
2507 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2508 LanguageMode language_mode() const;
2510 static bool NeedsHomeObject(Expression* expr);
2512 int materialized_literal_count() { return materialized_literal_count_; }
2513 int expected_property_count() { return expected_property_count_; }
2514 int parameter_count() { return parameter_count_; }
2516 bool AllowsLazyCompilation();
2517 bool AllowsLazyCompilationWithoutContext();
2519 Handle<String> debug_name() const {
2520 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2521 return raw_name_->string();
2523 return inferred_name();
2526 Handle<String> inferred_name() const {
2527 if (!inferred_name_.is_null()) {
2528 DCHECK(raw_inferred_name_ == NULL);
2529 return inferred_name_;
2531 if (raw_inferred_name_ != NULL) {
2532 return raw_inferred_name_->string();
2535 return Handle<String>();
2538 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2539 void set_inferred_name(Handle<String> inferred_name) {
2540 DCHECK(!inferred_name.is_null());
2541 inferred_name_ = inferred_name;
2542 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2543 raw_inferred_name_ = NULL;
2546 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2547 DCHECK(raw_inferred_name != NULL);
2548 raw_inferred_name_ = raw_inferred_name;
2549 DCHECK(inferred_name_.is_null());
2550 inferred_name_ = Handle<String>();
2553 bool pretenure() { return Pretenure::decode(bitfield_); }
2554 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2556 bool has_duplicate_parameters() {
2557 return HasDuplicateParameters::decode(bitfield_);
2560 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2562 // This is used as a heuristic on when to eagerly compile a function
2563 // literal. We consider the following constructs as hints that the
2564 // function will be called immediately:
2565 // - (function() { ... })();
2566 // - var x = function() { ... }();
2567 bool should_eager_compile() const {
2568 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2570 void set_should_eager_compile() {
2571 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2574 // A hint that we expect this function to be called (exactly) once,
2575 // i.e. we suspect it's an initialization function.
2576 bool should_be_used_once_hint() const {
2577 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2579 void set_should_be_used_once_hint() {
2580 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2583 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2585 int ast_node_count() { return ast_properties_.node_count(); }
2586 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2587 void set_ast_properties(AstProperties* ast_properties) {
2588 ast_properties_ = *ast_properties;
2590 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2591 return ast_properties_.get_spec();
2593 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2594 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2595 void set_dont_optimize_reason(BailoutReason reason) {
2596 dont_optimize_reason_ = reason;
2600 FunctionLiteral(Zone* zone, const AstRawString* name,
2601 AstValueFactory* ast_value_factory, Scope* scope,
2602 ZoneList<Statement*>* body, int materialized_literal_count,
2603 int expected_property_count, int parameter_count,
2604 FunctionType function_type,
2605 ParameterFlag has_duplicate_parameters,
2606 IsFunctionFlag is_function,
2607 EagerCompileHint eager_compile_hint, FunctionKind kind,
2609 : Expression(zone, position),
2613 raw_inferred_name_(ast_value_factory->empty_string()),
2614 ast_properties_(zone),
2615 dont_optimize_reason_(kNoReason),
2616 materialized_literal_count_(materialized_literal_count),
2617 expected_property_count_(expected_property_count),
2618 parameter_count_(parameter_count),
2619 function_token_position_(RelocInfo::kNoPosition) {
2620 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2621 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2622 Pretenure::encode(false) |
2623 HasDuplicateParameters::encode(has_duplicate_parameters) |
2624 IsFunction::encode(is_function) |
2625 EagerCompileHintBit::encode(eager_compile_hint) |
2626 FunctionKindBits::encode(kind) |
2627 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2628 DCHECK(IsValidFunctionKind(kind));
2632 const AstRawString* raw_name_;
2633 Handle<String> name_;
2635 ZoneList<Statement*>* body_;
2636 const AstString* raw_inferred_name_;
2637 Handle<String> inferred_name_;
2638 AstProperties ast_properties_;
2639 BailoutReason dont_optimize_reason_;
2641 int materialized_literal_count_;
2642 int expected_property_count_;
2643 int parameter_count_;
2644 int function_token_position_;
2647 class IsExpression : public BitField<bool, 0, 1> {};
2648 class IsAnonymous : public BitField<bool, 1, 1> {};
2649 class Pretenure : public BitField<bool, 2, 1> {};
2650 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2651 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2652 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2653 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2654 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2659 class ClassLiteral final : public Expression {
2661 typedef ObjectLiteralProperty Property;
2663 DECLARE_NODE_TYPE(ClassLiteral)
2665 Handle<String> name() const { return raw_name_->string(); }
2666 const AstRawString* raw_name() const { return raw_name_; }
2667 Scope* scope() const { return scope_; }
2668 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2669 Expression* extends() const { return extends_; }
2670 FunctionLiteral* constructor() const { return constructor_; }
2671 ZoneList<Property*>* properties() const { return properties_; }
2672 int start_position() const { return position(); }
2673 int end_position() const { return end_position_; }
2675 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2676 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2677 BailoutId ExitId() { return BailoutId(local_id(2)); }
2678 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2680 // Return an AST id for a property that is used in simulate instructions.
2681 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2683 // Unlike other AST nodes, this number of bailout IDs allocated for an
2684 // ClassLiteral can vary, so num_ids() is not a static method.
2685 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2687 // Object literals need one feedback slot for each non-trivial value, as well
2688 // as some slots for home objects.
2689 FeedbackVectorRequirements ComputeFeedbackRequirements(
2690 Isolate* isolate, const ICSlotCache* cache) override;
2691 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2692 ICSlotCache* cache) override {
2695 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2696 FeedbackVectorICSlot GetNthSlot(int n) const {
2697 return FeedbackVectorICSlot(slot_.ToInt() + n);
2700 // If value needs a home object, returns a valid feedback vector ic slot
2701 // given by slot_index, and increments slot_index.
2702 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2703 int* slot_index) const;
2706 int slot_count() const { return slot_count_; }
2710 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2711 VariableProxy* class_variable_proxy, Expression* extends,
2712 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2713 int start_position, int end_position)
2714 : Expression(zone, start_position),
2717 class_variable_proxy_(class_variable_proxy),
2719 constructor_(constructor),
2720 properties_(properties),
2721 end_position_(end_position),
2725 slot_(FeedbackVectorICSlot::Invalid()) {
2728 static int parent_num_ids() { return Expression::num_ids(); }
2731 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2733 const AstRawString* raw_name_;
2735 VariableProxy* class_variable_proxy_;
2736 Expression* extends_;
2737 FunctionLiteral* constructor_;
2738 ZoneList<Property*>* properties_;
2741 // slot_count_ helps validate that the logic to allocate ic slots and the
2742 // logic to use them are in sync.
2745 FeedbackVectorICSlot slot_;
2749 class NativeFunctionLiteral final : public Expression {
2751 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2753 Handle<String> name() const { return name_->string(); }
2754 v8::Extension* extension() const { return extension_; }
2757 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2758 v8::Extension* extension, int pos)
2759 : Expression(zone, pos), name_(name), extension_(extension) {}
2762 const AstRawString* name_;
2763 v8::Extension* extension_;
2767 class ThisFunction final : public Expression {
2769 DECLARE_NODE_TYPE(ThisFunction)
2772 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2776 class SuperPropertyReference final : public Expression {
2778 DECLARE_NODE_TYPE(SuperPropertyReference)
2780 VariableProxy* this_var() const { return this_var_; }
2781 Expression* home_object() const { return home_object_; }
2784 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2785 Expression* home_object, int pos)
2786 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2787 DCHECK(this_var->is_this());
2788 DCHECK(home_object->IsProperty());
2792 VariableProxy* this_var_;
2793 Expression* home_object_;
2797 class SuperCallReference final : public Expression {
2799 DECLARE_NODE_TYPE(SuperCallReference)
2801 VariableProxy* this_var() const { return this_var_; }
2802 VariableProxy* new_target_var() const { return new_target_var_; }
2803 VariableProxy* this_function_var() const { return this_function_var_; }
2806 SuperCallReference(Zone* zone, VariableProxy* this_var,
2807 VariableProxy* new_target_var,
2808 VariableProxy* this_function_var, int pos)
2809 : Expression(zone, pos),
2810 this_var_(this_var),
2811 new_target_var_(new_target_var),
2812 this_function_var_(this_function_var) {
2813 DCHECK(this_var->is_this());
2814 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2815 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2819 VariableProxy* this_var_;
2820 VariableProxy* new_target_var_;
2821 VariableProxy* this_function_var_;
2825 #undef DECLARE_NODE_TYPE
2828 // ----------------------------------------------------------------------------
2829 // Regular expressions
2832 class RegExpVisitor BASE_EMBEDDED {
2834 virtual ~RegExpVisitor() { }
2835 #define MAKE_CASE(Name) \
2836 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2837 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2842 class RegExpTree : public ZoneObject {
2844 static const int kInfinity = kMaxInt;
2845 virtual ~RegExpTree() {}
2846 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2847 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2848 RegExpNode* on_success) = 0;
2849 virtual bool IsTextElement() { return false; }
2850 virtual bool IsAnchoredAtStart() { return false; }
2851 virtual bool IsAnchoredAtEnd() { return false; }
2852 virtual int min_match() = 0;
2853 virtual int max_match() = 0;
2854 // Returns the interval of registers used for captures within this
2856 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2857 virtual void AppendToText(RegExpText* text, Zone* zone);
2858 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2859 #define MAKE_ASTYPE(Name) \
2860 virtual RegExp##Name* As##Name(); \
2861 virtual bool Is##Name();
2862 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2867 class RegExpDisjunction final : public RegExpTree {
2869 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2870 void* Accept(RegExpVisitor* visitor, void* data) override;
2871 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2872 RegExpNode* on_success) override;
2873 RegExpDisjunction* AsDisjunction() override;
2874 Interval CaptureRegisters() override;
2875 bool IsDisjunction() override;
2876 bool IsAnchoredAtStart() override;
2877 bool IsAnchoredAtEnd() override;
2878 int min_match() override { return min_match_; }
2879 int max_match() override { return max_match_; }
2880 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2882 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2883 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2884 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2885 ZoneList<RegExpTree*>* alternatives_;
2891 class RegExpAlternative final : public RegExpTree {
2893 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2894 void* Accept(RegExpVisitor* visitor, void* data) override;
2895 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2896 RegExpNode* on_success) override;
2897 RegExpAlternative* AsAlternative() override;
2898 Interval CaptureRegisters() override;
2899 bool IsAlternative() override;
2900 bool IsAnchoredAtStart() override;
2901 bool IsAnchoredAtEnd() override;
2902 int min_match() override { return min_match_; }
2903 int max_match() override { return max_match_; }
2904 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2906 ZoneList<RegExpTree*>* nodes_;
2912 class RegExpAssertion final : public RegExpTree {
2914 enum AssertionType {
2922 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2923 void* Accept(RegExpVisitor* visitor, void* data) override;
2924 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2925 RegExpNode* on_success) override;
2926 RegExpAssertion* AsAssertion() override;
2927 bool IsAssertion() override;
2928 bool IsAnchoredAtStart() override;
2929 bool IsAnchoredAtEnd() override;
2930 int min_match() override { return 0; }
2931 int max_match() override { return 0; }
2932 AssertionType assertion_type() { return assertion_type_; }
2934 AssertionType assertion_type_;
2938 class CharacterSet final BASE_EMBEDDED {
2940 explicit CharacterSet(uc16 standard_set_type)
2942 standard_set_type_(standard_set_type) {}
2943 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2945 standard_set_type_(0) {}
2946 ZoneList<CharacterRange>* ranges(Zone* zone);
2947 uc16 standard_set_type() { return standard_set_type_; }
2948 void set_standard_set_type(uc16 special_set_type) {
2949 standard_set_type_ = special_set_type;
2951 bool is_standard() { return standard_set_type_ != 0; }
2952 void Canonicalize();
2954 ZoneList<CharacterRange>* ranges_;
2955 // If non-zero, the value represents a standard set (e.g., all whitespace
2956 // characters) without having to expand the ranges.
2957 uc16 standard_set_type_;
2961 class RegExpCharacterClass final : public RegExpTree {
2963 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2965 is_negated_(is_negated) { }
2966 explicit RegExpCharacterClass(uc16 type)
2968 is_negated_(false) { }
2969 void* Accept(RegExpVisitor* visitor, void* data) override;
2970 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2971 RegExpNode* on_success) override;
2972 RegExpCharacterClass* AsCharacterClass() override;
2973 bool IsCharacterClass() override;
2974 bool IsTextElement() override { return true; }
2975 int min_match() override { return 1; }
2976 int max_match() override { return 1; }
2977 void AppendToText(RegExpText* text, Zone* zone) override;
2978 CharacterSet character_set() { return set_; }
2979 // TODO(lrn): Remove need for complex version if is_standard that
2980 // recognizes a mangled standard set and just do { return set_.is_special(); }
2981 bool is_standard(Zone* zone);
2982 // Returns a value representing the standard character set if is_standard()
2984 // Currently used values are:
2985 // s : unicode whitespace
2986 // S : unicode non-whitespace
2987 // w : ASCII word character (digit, letter, underscore)
2988 // W : non-ASCII word character
2990 // D : non-ASCII digit
2991 // . : non-unicode non-newline
2992 // * : All characters
2993 uc16 standard_type() { return set_.standard_set_type(); }
2994 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2995 bool is_negated() { return is_negated_; }
3003 class RegExpAtom final : public RegExpTree {
3005 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3006 void* Accept(RegExpVisitor* visitor, void* data) override;
3007 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3008 RegExpNode* on_success) override;
3009 RegExpAtom* AsAtom() override;
3010 bool IsAtom() override;
3011 bool IsTextElement() override { return true; }
3012 int min_match() override { return data_.length(); }
3013 int max_match() override { return data_.length(); }
3014 void AppendToText(RegExpText* text, Zone* zone) override;
3015 Vector<const uc16> data() { return data_; }
3016 int length() { return data_.length(); }
3018 Vector<const uc16> data_;
3022 class RegExpText final : public RegExpTree {
3024 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3025 void* Accept(RegExpVisitor* visitor, void* data) override;
3026 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3027 RegExpNode* on_success) override;
3028 RegExpText* AsText() override;
3029 bool IsText() override;
3030 bool IsTextElement() override { return true; }
3031 int min_match() override { return length_; }
3032 int max_match() override { return length_; }
3033 void AppendToText(RegExpText* text, Zone* zone) override;
3034 void AddElement(TextElement elm, Zone* zone) {
3035 elements_.Add(elm, zone);
3036 length_ += elm.length();
3038 ZoneList<TextElement>* elements() { return &elements_; }
3040 ZoneList<TextElement> elements_;
3045 class RegExpQuantifier final : public RegExpTree {
3047 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3048 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3052 min_match_(min * body->min_match()),
3053 quantifier_type_(type) {
3054 if (max > 0 && body->max_match() > kInfinity / max) {
3055 max_match_ = kInfinity;
3057 max_match_ = max * body->max_match();
3060 void* Accept(RegExpVisitor* visitor, void* data) override;
3061 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3062 RegExpNode* on_success) override;
3063 static RegExpNode* ToNode(int min,
3067 RegExpCompiler* compiler,
3068 RegExpNode* on_success,
3069 bool not_at_start = false);
3070 RegExpQuantifier* AsQuantifier() override;
3071 Interval CaptureRegisters() override;
3072 bool IsQuantifier() override;
3073 int min_match() override { return min_match_; }
3074 int max_match() override { return max_match_; }
3075 int min() { return min_; }
3076 int max() { return max_; }
3077 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3078 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3079 bool is_greedy() { return quantifier_type_ == GREEDY; }
3080 RegExpTree* body() { return body_; }
3088 QuantifierType quantifier_type_;
3092 class RegExpCapture final : public RegExpTree {
3094 explicit RegExpCapture(RegExpTree* body, int index)
3095 : body_(body), index_(index) { }
3096 void* Accept(RegExpVisitor* visitor, void* data) override;
3097 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3098 RegExpNode* on_success) override;
3099 static RegExpNode* ToNode(RegExpTree* body,
3101 RegExpCompiler* compiler,
3102 RegExpNode* on_success);
3103 RegExpCapture* AsCapture() override;
3104 bool IsAnchoredAtStart() override;
3105 bool IsAnchoredAtEnd() override;
3106 Interval CaptureRegisters() override;
3107 bool IsCapture() override;
3108 int min_match() override { return body_->min_match(); }
3109 int max_match() override { return body_->max_match(); }
3110 RegExpTree* body() { return body_; }
3111 int index() { return index_; }
3112 static int StartRegister(int index) { return index * 2; }
3113 static int EndRegister(int index) { return index * 2 + 1; }
3121 class RegExpLookahead final : public RegExpTree {
3123 RegExpLookahead(RegExpTree* body,
3128 is_positive_(is_positive),
3129 capture_count_(capture_count),
3130 capture_from_(capture_from) { }
3132 void* Accept(RegExpVisitor* visitor, void* data) override;
3133 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3134 RegExpNode* on_success) override;
3135 RegExpLookahead* AsLookahead() override;
3136 Interval CaptureRegisters() override;
3137 bool IsLookahead() override;
3138 bool IsAnchoredAtStart() override;
3139 int min_match() override { return 0; }
3140 int max_match() override { return 0; }
3141 RegExpTree* body() { return body_; }
3142 bool is_positive() { return is_positive_; }
3143 int capture_count() { return capture_count_; }
3144 int capture_from() { return capture_from_; }
3154 class RegExpBackReference final : public RegExpTree {
3156 explicit RegExpBackReference(RegExpCapture* capture)
3157 : capture_(capture) { }
3158 void* Accept(RegExpVisitor* visitor, void* data) override;
3159 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3160 RegExpNode* on_success) override;
3161 RegExpBackReference* AsBackReference() override;
3162 bool IsBackReference() override;
3163 int min_match() override { return 0; }
3164 int max_match() override { return capture_->max_match(); }
3165 int index() { return capture_->index(); }
3166 RegExpCapture* capture() { return capture_; }
3168 RegExpCapture* capture_;
3172 class RegExpEmpty final : public RegExpTree {
3175 void* Accept(RegExpVisitor* visitor, void* data) override;
3176 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3177 RegExpNode* on_success) override;
3178 RegExpEmpty* AsEmpty() override;
3179 bool IsEmpty() override;
3180 int min_match() override { return 0; }
3181 int max_match() override { return 0; }
3185 // ----------------------------------------------------------------------------
3187 // - leaf node visitors are abstract.
3189 class AstVisitor BASE_EMBEDDED {
3192 virtual ~AstVisitor() {}
3194 // Stack overflow check and dynamic dispatch.
3195 virtual void Visit(AstNode* node) = 0;
3197 // Iteration left-to-right.
3198 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3199 virtual void VisitStatements(ZoneList<Statement*>* statements);
3200 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3202 // Individual AST nodes.
3203 #define DEF_VISIT(type) \
3204 virtual void Visit##type(type* node) = 0;
3205 AST_NODE_LIST(DEF_VISIT)
3210 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3212 void Visit(AstNode* node) final { \
3213 if (!CheckStackOverflow()) node->Accept(this); \
3216 void SetStackOverflow() { stack_overflow_ = true; } \
3217 void ClearStackOverflow() { stack_overflow_ = false; } \
3218 bool HasStackOverflow() const { return stack_overflow_; } \
3220 bool CheckStackOverflow() { \
3221 if (stack_overflow_) return true; \
3222 StackLimitCheck check(isolate_); \
3223 if (!check.HasOverflowed()) return false; \
3224 stack_overflow_ = true; \
3229 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3230 isolate_ = isolate; \
3232 stack_overflow_ = false; \
3234 Zone* zone() { return zone_; } \
3235 Isolate* isolate() { return isolate_; } \
3237 Isolate* isolate_; \
3239 bool stack_overflow_
3242 // ----------------------------------------------------------------------------
3245 class AstNodeFactory final BASE_EMBEDDED {
3247 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3248 : zone_(ast_value_factory->zone()),
3249 ast_value_factory_(ast_value_factory) {}
3251 VariableDeclaration* NewVariableDeclaration(
3252 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3253 bool is_class_declaration = false, int declaration_group_start = -1) {
3255 VariableDeclaration(zone_, proxy, mode, scope, pos,
3256 is_class_declaration, declaration_group_start);
3259 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3261 FunctionLiteral* fun,
3264 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3267 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3268 const AstRawString* import_name,
3269 const AstRawString* module_specifier,
3270 Scope* scope, int pos) {
3271 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3272 module_specifier, scope, pos);
3275 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3278 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3281 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3282 bool ignore_completion_value, int pos) {
3284 Block(zone_, labels, capacity, ignore_completion_value, pos);
3287 #define STATEMENT_WITH_LABELS(NodeType) \
3288 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3289 return new (zone_) NodeType(zone_, labels, pos); \
3291 STATEMENT_WITH_LABELS(DoWhileStatement)
3292 STATEMENT_WITH_LABELS(WhileStatement)
3293 STATEMENT_WITH_LABELS(ForStatement)
3294 STATEMENT_WITH_LABELS(SwitchStatement)
3295 #undef STATEMENT_WITH_LABELS
3297 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3298 ZoneList<const AstRawString*>* labels,
3300 switch (visit_mode) {
3301 case ForEachStatement::ENUMERATE: {
3302 return new (zone_) ForInStatement(zone_, labels, pos);
3304 case ForEachStatement::ITERATE: {
3305 return new (zone_) ForOfStatement(zone_, labels, pos);
3312 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3313 return new (zone_) ExpressionStatement(zone_, expression, pos);
3316 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3317 return new (zone_) ContinueStatement(zone_, target, pos);
3320 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3321 return new (zone_) BreakStatement(zone_, target, pos);
3324 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3325 return new (zone_) ReturnStatement(zone_, expression, pos);
3328 WithStatement* NewWithStatement(Scope* scope,
3329 Expression* expression,
3330 Statement* statement,
3332 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3335 IfStatement* NewIfStatement(Expression* condition,
3336 Statement* then_statement,
3337 Statement* else_statement,
3340 IfStatement(zone_, condition, then_statement, else_statement, pos);
3343 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3345 Block* catch_block, int pos) {
3347 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3350 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3351 Block* finally_block, int pos) {
3353 TryFinallyStatement(zone_, try_block, finally_block, pos);
3356 DebuggerStatement* NewDebuggerStatement(int pos) {
3357 return new (zone_) DebuggerStatement(zone_, pos);
3360 EmptyStatement* NewEmptyStatement(int pos) {
3361 return new(zone_) EmptyStatement(zone_, pos);
3364 CaseClause* NewCaseClause(
3365 Expression* label, ZoneList<Statement*>* statements, int pos) {
3366 return new (zone_) CaseClause(zone_, label, statements, pos);
3369 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3371 Literal(zone_, ast_value_factory_->NewString(string), pos);
3374 // A JavaScript symbol (ECMA-262 edition 6).
3375 Literal* NewSymbolLiteral(const char* name, int pos) {
3376 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3379 Literal* NewNumberLiteral(double number, int pos) {
3381 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3384 Literal* NewSmiLiteral(int number, int pos) {
3385 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3388 Literal* NewBooleanLiteral(bool b, int pos) {
3389 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3392 Literal* NewNullLiteral(int pos) {
3393 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3396 Literal* NewUndefinedLiteral(int pos) {
3397 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3400 Literal* NewTheHoleLiteral(int pos) {
3401 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3404 ObjectLiteral* NewObjectLiteral(
3405 ZoneList<ObjectLiteral::Property*>* properties,
3407 int boilerplate_properties,
3411 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3412 boilerplate_properties, has_function,
3416 ObjectLiteral::Property* NewObjectLiteralProperty(
3417 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3418 bool is_static, bool is_computed_name) {
3420 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3423 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3426 bool is_computed_name) {
3427 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3428 is_static, is_computed_name);
3431 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3432 const AstRawString* flags,
3436 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3440 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3444 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3448 VariableProxy* NewVariableProxy(Variable* var,
3449 int start_position = RelocInfo::kNoPosition,
3450 int end_position = RelocInfo::kNoPosition) {
3451 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3454 VariableProxy* NewVariableProxy(const AstRawString* name,
3455 Variable::Kind variable_kind,
3456 int start_position = RelocInfo::kNoPosition,
3457 int end_position = RelocInfo::kNoPosition) {
3458 DCHECK_NOT_NULL(name);
3460 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3463 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3464 return new (zone_) Property(zone_, obj, key, pos);
3467 Call* NewCall(Expression* expression,
3468 ZoneList<Expression*>* arguments,
3470 return new (zone_) Call(zone_, expression, arguments, pos);
3473 CallNew* NewCallNew(Expression* expression,
3474 ZoneList<Expression*>* arguments,
3476 return new (zone_) CallNew(zone_, expression, arguments, pos);
3479 CallRuntime* NewCallRuntime(const AstRawString* name,
3480 const Runtime::Function* function,
3481 ZoneList<Expression*>* arguments,
3483 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3486 UnaryOperation* NewUnaryOperation(Token::Value op,
3487 Expression* expression,
3489 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3492 BinaryOperation* NewBinaryOperation(Token::Value op,
3496 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3499 CountOperation* NewCountOperation(Token::Value op,
3503 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3506 CompareOperation* NewCompareOperation(Token::Value op,
3510 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3513 Spread* NewSpread(Expression* expression, int pos) {
3514 return new (zone_) Spread(zone_, expression, pos);
3517 Conditional* NewConditional(Expression* condition,
3518 Expression* then_expression,
3519 Expression* else_expression,
3521 return new (zone_) Conditional(zone_, condition, then_expression,
3522 else_expression, position);
3525 Assignment* NewAssignment(Token::Value op,
3529 DCHECK(Token::IsAssignmentOp(op));
3530 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3531 if (assign->is_compound()) {
3532 DCHECK(Token::IsAssignmentOp(op));
3533 assign->binary_operation_ =
3534 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3539 Yield* NewYield(Expression *generator_object,
3540 Expression* expression,
3541 Yield::Kind yield_kind,
3543 if (!expression) expression = NewUndefinedLiteral(pos);
3545 Yield(zone_, generator_object, expression, yield_kind, pos);
3548 Throw* NewThrow(Expression* exception, int pos) {
3549 return new (zone_) Throw(zone_, exception, pos);
3552 FunctionLiteral* NewFunctionLiteral(
3553 const AstRawString* name, AstValueFactory* ast_value_factory,
3554 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3555 int expected_property_count, int parameter_count,
3556 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3557 FunctionLiteral::FunctionType function_type,
3558 FunctionLiteral::IsFunctionFlag is_function,
3559 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3561 return new (zone_) FunctionLiteral(
3562 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3563 expected_property_count, parameter_count, function_type,
3564 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3568 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3569 VariableProxy* proxy, Expression* extends,
3570 FunctionLiteral* constructor,
3571 ZoneList<ObjectLiteral::Property*>* properties,
3572 int start_position, int end_position) {
3574 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3575 properties, start_position, end_position);
3578 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3579 v8::Extension* extension,
3581 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3584 ThisFunction* NewThisFunction(int pos) {
3585 return new (zone_) ThisFunction(zone_, pos);
3588 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3589 Expression* home_object,
3592 SuperPropertyReference(zone_, this_var, home_object, pos);
3595 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3596 VariableProxy* new_target_var,
3597 VariableProxy* this_function_var,
3599 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3600 this_function_var, pos);
3605 AstValueFactory* ast_value_factory_;
3609 } } // namespace v8::internal