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(); }
2035 CallRuntime(Zone* zone, const AstRawString* name,
2036 const Runtime::Function* function,
2037 ZoneList<Expression*>* arguments, int pos)
2038 : Expression(zone, pos),
2040 function_(function),
2041 arguments_(arguments),
2042 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2043 static int parent_num_ids() { return Expression::num_ids(); }
2046 const AstRawString* raw_name_;
2047 const Runtime::Function* function_;
2048 ZoneList<Expression*>* arguments_;
2049 FeedbackVectorICSlot callruntime_feedback_slot_;
2053 class UnaryOperation final : public Expression {
2055 DECLARE_NODE_TYPE(UnaryOperation)
2057 Token::Value op() const { return op_; }
2058 Expression* expression() const { return expression_; }
2060 // For unary not (Token::NOT), the AST ids where true and false will
2061 // actually be materialized, respectively.
2062 static int num_ids() { return parent_num_ids() + 2; }
2063 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2064 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2066 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2069 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2070 : Expression(zone, pos), op_(op), expression_(expression) {
2071 DCHECK(Token::IsUnaryOp(op));
2073 static int parent_num_ids() { return Expression::num_ids(); }
2076 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2079 Expression* expression_;
2083 class BinaryOperation final : public Expression {
2085 DECLARE_NODE_TYPE(BinaryOperation)
2087 Token::Value op() const { return static_cast<Token::Value>(op_); }
2088 Expression* left() const { return left_; }
2089 Expression* right() const { return right_; }
2090 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2091 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2092 allocation_site_ = allocation_site;
2095 // The short-circuit logical operations need an AST ID for their
2096 // right-hand subexpression.
2097 static int num_ids() { return parent_num_ids() + 2; }
2098 BailoutId RightId() const { return BailoutId(local_id(0)); }
2100 TypeFeedbackId BinaryOperationFeedbackId() const {
2101 return TypeFeedbackId(local_id(1));
2103 Maybe<int> fixed_right_arg() const {
2104 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2106 void set_fixed_right_arg(Maybe<int> arg) {
2107 has_fixed_right_arg_ = arg.IsJust();
2108 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2111 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2114 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2115 Expression* right, int pos)
2116 : Expression(zone, pos),
2117 op_(static_cast<byte>(op)),
2118 has_fixed_right_arg_(false),
2119 fixed_right_arg_value_(0),
2122 DCHECK(Token::IsBinaryOp(op));
2124 static int parent_num_ids() { return Expression::num_ids(); }
2127 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2129 const byte op_; // actually Token::Value
2130 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2131 // type for the RHS. Currenty it's actually a Maybe<int>
2132 bool has_fixed_right_arg_;
2133 int fixed_right_arg_value_;
2136 Handle<AllocationSite> allocation_site_;
2140 class CountOperation final : public Expression {
2142 DECLARE_NODE_TYPE(CountOperation)
2144 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2145 bool is_postfix() const { return !is_prefix(); }
2147 Token::Value op() const { return TokenField::decode(bit_field_); }
2148 Token::Value binary_op() {
2149 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2152 Expression* expression() const { return expression_; }
2154 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2155 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2156 IcCheckType GetKeyType() const override {
2157 return KeyTypeField::decode(bit_field_);
2159 KeyedAccessStoreMode GetStoreMode() const override {
2160 return StoreModeField::decode(bit_field_);
2162 Type* type() const { return type_; }
2163 void set_key_type(IcCheckType type) {
2164 bit_field_ = KeyTypeField::update(bit_field_, type);
2166 void set_store_mode(KeyedAccessStoreMode mode) {
2167 bit_field_ = StoreModeField::update(bit_field_, mode);
2169 void set_type(Type* type) { type_ = type; }
2171 static int num_ids() { return parent_num_ids() + 4; }
2172 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2173 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2174 TypeFeedbackId CountBinOpFeedbackId() const {
2175 return TypeFeedbackId(local_id(2));
2177 TypeFeedbackId CountStoreFeedbackId() const {
2178 return TypeFeedbackId(local_id(3));
2181 FeedbackVectorRequirements ComputeFeedbackRequirements(
2182 Isolate* isolate, const ICSlotCache* cache) override;
2183 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2184 ICSlotCache* cache) override {
2187 Code::Kind FeedbackICSlotKind(int index) override;
2188 FeedbackVectorICSlot CountSlot() const { return slot_; }
2191 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2193 : Expression(zone, pos),
2195 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2196 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2199 slot_(FeedbackVectorICSlot::Invalid()) {}
2200 static int parent_num_ids() { return Expression::num_ids(); }
2203 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2205 class IsPrefixField : public BitField16<bool, 0, 1> {};
2206 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2207 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2208 class TokenField : public BitField16<Token::Value, 6, 8> {};
2210 // Starts with 16-bit field, which should get packed together with
2211 // Expression's trailing 16-bit field.
2212 uint16_t bit_field_;
2214 Expression* expression_;
2215 SmallMapList receiver_types_;
2216 FeedbackVectorICSlot slot_;
2220 class CompareOperation final : public Expression {
2222 DECLARE_NODE_TYPE(CompareOperation)
2224 Token::Value op() const { return op_; }
2225 Expression* left() const { return left_; }
2226 Expression* right() const { return right_; }
2228 // Type feedback information.
2229 static int num_ids() { return parent_num_ids() + 1; }
2230 TypeFeedbackId CompareOperationFeedbackId() const {
2231 return TypeFeedbackId(local_id(0));
2233 Type* combined_type() const { return combined_type_; }
2234 void set_combined_type(Type* type) { combined_type_ = type; }
2236 // Match special cases.
2237 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2238 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2239 bool IsLiteralCompareNull(Expression** expr);
2242 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2243 Expression* right, int pos)
2244 : Expression(zone, pos),
2248 combined_type_(Type::None(zone)) {
2249 DCHECK(Token::IsCompareOp(op));
2251 static int parent_num_ids() { return Expression::num_ids(); }
2254 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2260 Type* combined_type_;
2264 class Spread final : public Expression {
2266 DECLARE_NODE_TYPE(Spread)
2268 Expression* expression() const { return expression_; }
2270 static int num_ids() { return parent_num_ids(); }
2273 Spread(Zone* zone, Expression* expression, int pos)
2274 : Expression(zone, pos), expression_(expression) {}
2275 static int parent_num_ids() { return Expression::num_ids(); }
2278 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2280 Expression* expression_;
2284 class Conditional final : public Expression {
2286 DECLARE_NODE_TYPE(Conditional)
2288 Expression* condition() const { return condition_; }
2289 Expression* then_expression() const { return then_expression_; }
2290 Expression* else_expression() const { return else_expression_; }
2292 static int num_ids() { return parent_num_ids() + 2; }
2293 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2294 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2297 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2298 Expression* else_expression, int position)
2299 : Expression(zone, position),
2300 condition_(condition),
2301 then_expression_(then_expression),
2302 else_expression_(else_expression) {}
2303 static int parent_num_ids() { return Expression::num_ids(); }
2306 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2308 Expression* condition_;
2309 Expression* then_expression_;
2310 Expression* else_expression_;
2314 class Assignment final : public Expression {
2316 DECLARE_NODE_TYPE(Assignment)
2318 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2320 Token::Value binary_op() const;
2322 Token::Value op() const { return TokenField::decode(bit_field_); }
2323 Expression* target() const { return target_; }
2324 Expression* value() const { return value_; }
2325 BinaryOperation* binary_operation() const { return binary_operation_; }
2327 // This check relies on the definition order of token in token.h.
2328 bool is_compound() const { return op() > Token::ASSIGN; }
2330 static int num_ids() { return parent_num_ids() + 2; }
2331 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2333 // Type feedback information.
2334 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2335 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2336 bool IsUninitialized() const {
2337 return IsUninitializedField::decode(bit_field_);
2339 bool HasNoTypeInformation() {
2340 return IsUninitializedField::decode(bit_field_);
2342 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2343 IcCheckType GetKeyType() const override {
2344 return KeyTypeField::decode(bit_field_);
2346 KeyedAccessStoreMode GetStoreMode() const override {
2347 return StoreModeField::decode(bit_field_);
2349 void set_is_uninitialized(bool b) {
2350 bit_field_ = IsUninitializedField::update(bit_field_, b);
2352 void set_key_type(IcCheckType key_type) {
2353 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2355 void set_store_mode(KeyedAccessStoreMode mode) {
2356 bit_field_ = StoreModeField::update(bit_field_, mode);
2359 FeedbackVectorRequirements ComputeFeedbackRequirements(
2360 Isolate* isolate, const ICSlotCache* cache) override;
2361 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2362 ICSlotCache* cache) override {
2365 Code::Kind FeedbackICSlotKind(int index) override;
2366 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2369 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2371 static int parent_num_ids() { return Expression::num_ids(); }
2374 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2376 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2377 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2378 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2379 class TokenField : public BitField16<Token::Value, 6, 8> {};
2381 // Starts with 16-bit field, which should get packed together with
2382 // Expression's trailing 16-bit field.
2383 uint16_t bit_field_;
2384 Expression* target_;
2386 BinaryOperation* binary_operation_;
2387 SmallMapList receiver_types_;
2388 FeedbackVectorICSlot slot_;
2392 class Yield final : public Expression {
2394 DECLARE_NODE_TYPE(Yield)
2397 kInitial, // The initial yield that returns the unboxed generator object.
2398 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2399 kDelegating, // A yield*.
2400 kFinal // A return: { value: EXPRESSION, done: true }
2403 Expression* generator_object() const { return generator_object_; }
2404 Expression* expression() const { return expression_; }
2405 Kind yield_kind() const { return yield_kind_; }
2407 // Type feedback information.
2408 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2409 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2410 Isolate* isolate, const ICSlotCache* cache) override {
2411 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2413 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2414 ICSlotCache* cache) override {
2415 yield_first_feedback_slot_ = slot;
2417 Code::Kind FeedbackICSlotKind(int index) override {
2418 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2421 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2422 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2423 return yield_first_feedback_slot_;
2426 FeedbackVectorICSlot DoneFeedbackSlot() {
2427 return KeyedLoadFeedbackSlot().next();
2430 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2433 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2434 Kind yield_kind, int pos)
2435 : Expression(zone, pos),
2436 generator_object_(generator_object),
2437 expression_(expression),
2438 yield_kind_(yield_kind),
2439 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2442 Expression* generator_object_;
2443 Expression* expression_;
2445 FeedbackVectorICSlot yield_first_feedback_slot_;
2449 class Throw final : public Expression {
2451 DECLARE_NODE_TYPE(Throw)
2453 Expression* exception() const { return exception_; }
2456 Throw(Zone* zone, Expression* exception, int pos)
2457 : Expression(zone, pos), exception_(exception) {}
2460 Expression* exception_;
2464 class FunctionLiteral final : public Expression {
2467 ANONYMOUS_EXPRESSION,
2472 enum ParameterFlag {
2473 kNoDuplicateParameters = 0,
2474 kHasDuplicateParameters = 1
2477 enum IsFunctionFlag {
2482 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2484 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2486 enum ArityRestriction {
2492 DECLARE_NODE_TYPE(FunctionLiteral)
2494 Handle<String> name() const { return raw_name_->string(); }
2495 const AstRawString* raw_name() const { return raw_name_; }
2496 Scope* scope() const { return scope_; }
2497 ZoneList<Statement*>* body() const { return body_; }
2498 void set_function_token_position(int pos) { function_token_position_ = pos; }
2499 int function_token_position() const { return function_token_position_; }
2500 int start_position() const;
2501 int end_position() const;
2502 int SourceSize() const { return end_position() - start_position(); }
2503 bool is_expression() const { return IsExpression::decode(bitfield_); }
2504 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2505 LanguageMode language_mode() const;
2507 static bool NeedsHomeObject(Expression* expr);
2509 int materialized_literal_count() { return materialized_literal_count_; }
2510 int expected_property_count() { return expected_property_count_; }
2511 int parameter_count() { return parameter_count_; }
2513 bool AllowsLazyCompilation();
2514 bool AllowsLazyCompilationWithoutContext();
2516 Handle<String> debug_name() const {
2517 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2518 return raw_name_->string();
2520 return inferred_name();
2523 Handle<String> inferred_name() const {
2524 if (!inferred_name_.is_null()) {
2525 DCHECK(raw_inferred_name_ == NULL);
2526 return inferred_name_;
2528 if (raw_inferred_name_ != NULL) {
2529 return raw_inferred_name_->string();
2532 return Handle<String>();
2535 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2536 void set_inferred_name(Handle<String> inferred_name) {
2537 DCHECK(!inferred_name.is_null());
2538 inferred_name_ = inferred_name;
2539 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2540 raw_inferred_name_ = NULL;
2543 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2544 DCHECK(raw_inferred_name != NULL);
2545 raw_inferred_name_ = raw_inferred_name;
2546 DCHECK(inferred_name_.is_null());
2547 inferred_name_ = Handle<String>();
2550 bool pretenure() { return Pretenure::decode(bitfield_); }
2551 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2553 bool has_duplicate_parameters() {
2554 return HasDuplicateParameters::decode(bitfield_);
2557 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2559 // This is used as a heuristic on when to eagerly compile a function
2560 // literal. We consider the following constructs as hints that the
2561 // function will be called immediately:
2562 // - (function() { ... })();
2563 // - var x = function() { ... }();
2564 bool should_eager_compile() const {
2565 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2567 void set_should_eager_compile() {
2568 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2571 // A hint that we expect this function to be called (exactly) once,
2572 // i.e. we suspect it's an initialization function.
2573 bool should_be_used_once_hint() const {
2574 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2576 void set_should_be_used_once_hint() {
2577 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2580 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2582 int ast_node_count() { return ast_properties_.node_count(); }
2583 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2584 void set_ast_properties(AstProperties* ast_properties) {
2585 ast_properties_ = *ast_properties;
2587 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2588 return ast_properties_.get_spec();
2590 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2591 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2592 void set_dont_optimize_reason(BailoutReason reason) {
2593 dont_optimize_reason_ = reason;
2597 FunctionLiteral(Zone* zone, const AstRawString* name,
2598 AstValueFactory* ast_value_factory, Scope* scope,
2599 ZoneList<Statement*>* body, int materialized_literal_count,
2600 int expected_property_count, int parameter_count,
2601 FunctionType function_type,
2602 ParameterFlag has_duplicate_parameters,
2603 IsFunctionFlag is_function,
2604 EagerCompileHint eager_compile_hint, FunctionKind kind,
2606 : Expression(zone, position),
2610 raw_inferred_name_(ast_value_factory->empty_string()),
2611 ast_properties_(zone),
2612 dont_optimize_reason_(kNoReason),
2613 materialized_literal_count_(materialized_literal_count),
2614 expected_property_count_(expected_property_count),
2615 parameter_count_(parameter_count),
2616 function_token_position_(RelocInfo::kNoPosition) {
2617 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2618 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2619 Pretenure::encode(false) |
2620 HasDuplicateParameters::encode(has_duplicate_parameters) |
2621 IsFunction::encode(is_function) |
2622 EagerCompileHintBit::encode(eager_compile_hint) |
2623 FunctionKindBits::encode(kind) |
2624 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2625 DCHECK(IsValidFunctionKind(kind));
2629 const AstRawString* raw_name_;
2630 Handle<String> name_;
2632 ZoneList<Statement*>* body_;
2633 const AstString* raw_inferred_name_;
2634 Handle<String> inferred_name_;
2635 AstProperties ast_properties_;
2636 BailoutReason dont_optimize_reason_;
2638 int materialized_literal_count_;
2639 int expected_property_count_;
2640 int parameter_count_;
2641 int function_token_position_;
2644 class IsExpression : public BitField<bool, 0, 1> {};
2645 class IsAnonymous : public BitField<bool, 1, 1> {};
2646 class Pretenure : public BitField<bool, 2, 1> {};
2647 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2648 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2649 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2650 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2651 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2656 class ClassLiteral final : public Expression {
2658 typedef ObjectLiteralProperty Property;
2660 DECLARE_NODE_TYPE(ClassLiteral)
2662 Handle<String> name() const { return raw_name_->string(); }
2663 const AstRawString* raw_name() const { return raw_name_; }
2664 Scope* scope() const { return scope_; }
2665 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2666 Expression* extends() const { return extends_; }
2667 FunctionLiteral* constructor() const { return constructor_; }
2668 ZoneList<Property*>* properties() const { return properties_; }
2669 int start_position() const { return position(); }
2670 int end_position() const { return end_position_; }
2672 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2673 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2674 BailoutId ExitId() { return BailoutId(local_id(2)); }
2675 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2677 // Return an AST id for a property that is used in simulate instructions.
2678 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2680 // Unlike other AST nodes, this number of bailout IDs allocated for an
2681 // ClassLiteral can vary, so num_ids() is not a static method.
2682 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2684 // Object literals need one feedback slot for each non-trivial value, as well
2685 // as some slots for home objects.
2686 FeedbackVectorRequirements ComputeFeedbackRequirements(
2687 Isolate* isolate, const ICSlotCache* cache) override;
2688 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2689 ICSlotCache* cache) override {
2692 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2693 FeedbackVectorICSlot GetNthSlot(int n) const {
2694 return FeedbackVectorICSlot(slot_.ToInt() + n);
2697 // If value needs a home object, returns a valid feedback vector ic slot
2698 // given by slot_index, and increments slot_index.
2699 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2700 int* slot_index) const;
2703 int slot_count() const { return slot_count_; }
2707 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2708 VariableProxy* class_variable_proxy, Expression* extends,
2709 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2710 int start_position, int end_position)
2711 : Expression(zone, start_position),
2714 class_variable_proxy_(class_variable_proxy),
2716 constructor_(constructor),
2717 properties_(properties),
2718 end_position_(end_position),
2722 slot_(FeedbackVectorICSlot::Invalid()) {
2725 static int parent_num_ids() { return Expression::num_ids(); }
2728 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2730 const AstRawString* raw_name_;
2732 VariableProxy* class_variable_proxy_;
2733 Expression* extends_;
2734 FunctionLiteral* constructor_;
2735 ZoneList<Property*>* properties_;
2738 // slot_count_ helps validate that the logic to allocate ic slots and the
2739 // logic to use them are in sync.
2742 FeedbackVectorICSlot slot_;
2746 class NativeFunctionLiteral final : public Expression {
2748 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2750 Handle<String> name() const { return name_->string(); }
2751 v8::Extension* extension() const { return extension_; }
2754 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2755 v8::Extension* extension, int pos)
2756 : Expression(zone, pos), name_(name), extension_(extension) {}
2759 const AstRawString* name_;
2760 v8::Extension* extension_;
2764 class ThisFunction final : public Expression {
2766 DECLARE_NODE_TYPE(ThisFunction)
2769 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2773 class SuperPropertyReference final : public Expression {
2775 DECLARE_NODE_TYPE(SuperPropertyReference)
2777 VariableProxy* this_var() const { return this_var_; }
2778 Expression* home_object() const { return home_object_; }
2781 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2782 Expression* home_object, int pos)
2783 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2784 DCHECK(this_var->is_this());
2785 DCHECK(home_object->IsProperty());
2789 VariableProxy* this_var_;
2790 Expression* home_object_;
2794 class SuperCallReference final : public Expression {
2796 DECLARE_NODE_TYPE(SuperCallReference)
2798 VariableProxy* this_var() const { return this_var_; }
2799 VariableProxy* new_target_var() const { return new_target_var_; }
2800 VariableProxy* this_function_var() const { return this_function_var_; }
2803 SuperCallReference(Zone* zone, VariableProxy* this_var,
2804 VariableProxy* new_target_var,
2805 VariableProxy* this_function_var, int pos)
2806 : Expression(zone, pos),
2807 this_var_(this_var),
2808 new_target_var_(new_target_var),
2809 this_function_var_(this_function_var) {
2810 DCHECK(this_var->is_this());
2811 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2812 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2816 VariableProxy* this_var_;
2817 VariableProxy* new_target_var_;
2818 VariableProxy* this_function_var_;
2822 #undef DECLARE_NODE_TYPE
2825 // ----------------------------------------------------------------------------
2826 // Regular expressions
2829 class RegExpVisitor BASE_EMBEDDED {
2831 virtual ~RegExpVisitor() { }
2832 #define MAKE_CASE(Name) \
2833 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2834 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2839 class RegExpTree : public ZoneObject {
2841 static const int kInfinity = kMaxInt;
2842 virtual ~RegExpTree() {}
2843 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2844 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2845 RegExpNode* on_success) = 0;
2846 virtual bool IsTextElement() { return false; }
2847 virtual bool IsAnchoredAtStart() { return false; }
2848 virtual bool IsAnchoredAtEnd() { return false; }
2849 virtual int min_match() = 0;
2850 virtual int max_match() = 0;
2851 // Returns the interval of registers used for captures within this
2853 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2854 virtual void AppendToText(RegExpText* text, Zone* zone);
2855 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2856 #define MAKE_ASTYPE(Name) \
2857 virtual RegExp##Name* As##Name(); \
2858 virtual bool Is##Name();
2859 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2864 class RegExpDisjunction final : public RegExpTree {
2866 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2867 void* Accept(RegExpVisitor* visitor, void* data) override;
2868 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2869 RegExpNode* on_success) override;
2870 RegExpDisjunction* AsDisjunction() override;
2871 Interval CaptureRegisters() override;
2872 bool IsDisjunction() override;
2873 bool IsAnchoredAtStart() override;
2874 bool IsAnchoredAtEnd() override;
2875 int min_match() override { return min_match_; }
2876 int max_match() override { return max_match_; }
2877 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2879 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2880 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2881 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2882 ZoneList<RegExpTree*>* alternatives_;
2888 class RegExpAlternative final : public RegExpTree {
2890 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2891 void* Accept(RegExpVisitor* visitor, void* data) override;
2892 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2893 RegExpNode* on_success) override;
2894 RegExpAlternative* AsAlternative() override;
2895 Interval CaptureRegisters() override;
2896 bool IsAlternative() override;
2897 bool IsAnchoredAtStart() override;
2898 bool IsAnchoredAtEnd() override;
2899 int min_match() override { return min_match_; }
2900 int max_match() override { return max_match_; }
2901 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2903 ZoneList<RegExpTree*>* nodes_;
2909 class RegExpAssertion final : public RegExpTree {
2911 enum AssertionType {
2919 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2920 void* Accept(RegExpVisitor* visitor, void* data) override;
2921 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2922 RegExpNode* on_success) override;
2923 RegExpAssertion* AsAssertion() override;
2924 bool IsAssertion() override;
2925 bool IsAnchoredAtStart() override;
2926 bool IsAnchoredAtEnd() override;
2927 int min_match() override { return 0; }
2928 int max_match() override { return 0; }
2929 AssertionType assertion_type() { return assertion_type_; }
2931 AssertionType assertion_type_;
2935 class CharacterSet final BASE_EMBEDDED {
2937 explicit CharacterSet(uc16 standard_set_type)
2939 standard_set_type_(standard_set_type) {}
2940 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2942 standard_set_type_(0) {}
2943 ZoneList<CharacterRange>* ranges(Zone* zone);
2944 uc16 standard_set_type() { return standard_set_type_; }
2945 void set_standard_set_type(uc16 special_set_type) {
2946 standard_set_type_ = special_set_type;
2948 bool is_standard() { return standard_set_type_ != 0; }
2949 void Canonicalize();
2951 ZoneList<CharacterRange>* ranges_;
2952 // If non-zero, the value represents a standard set (e.g., all whitespace
2953 // characters) without having to expand the ranges.
2954 uc16 standard_set_type_;
2958 class RegExpCharacterClass final : public RegExpTree {
2960 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2962 is_negated_(is_negated) { }
2963 explicit RegExpCharacterClass(uc16 type)
2965 is_negated_(false) { }
2966 void* Accept(RegExpVisitor* visitor, void* data) override;
2967 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2968 RegExpNode* on_success) override;
2969 RegExpCharacterClass* AsCharacterClass() override;
2970 bool IsCharacterClass() override;
2971 bool IsTextElement() override { return true; }
2972 int min_match() override { return 1; }
2973 int max_match() override { return 1; }
2974 void AppendToText(RegExpText* text, Zone* zone) override;
2975 CharacterSet character_set() { return set_; }
2976 // TODO(lrn): Remove need for complex version if is_standard that
2977 // recognizes a mangled standard set and just do { return set_.is_special(); }
2978 bool is_standard(Zone* zone);
2979 // Returns a value representing the standard character set if is_standard()
2981 // Currently used values are:
2982 // s : unicode whitespace
2983 // S : unicode non-whitespace
2984 // w : ASCII word character (digit, letter, underscore)
2985 // W : non-ASCII word character
2987 // D : non-ASCII digit
2988 // . : non-unicode non-newline
2989 // * : All characters
2990 uc16 standard_type() { return set_.standard_set_type(); }
2991 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2992 bool is_negated() { return is_negated_; }
3000 class RegExpAtom final : public RegExpTree {
3002 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3003 void* Accept(RegExpVisitor* visitor, void* data) override;
3004 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3005 RegExpNode* on_success) override;
3006 RegExpAtom* AsAtom() override;
3007 bool IsAtom() override;
3008 bool IsTextElement() override { return true; }
3009 int min_match() override { return data_.length(); }
3010 int max_match() override { return data_.length(); }
3011 void AppendToText(RegExpText* text, Zone* zone) override;
3012 Vector<const uc16> data() { return data_; }
3013 int length() { return data_.length(); }
3015 Vector<const uc16> data_;
3019 class RegExpText final : public RegExpTree {
3021 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3022 void* Accept(RegExpVisitor* visitor, void* data) override;
3023 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3024 RegExpNode* on_success) override;
3025 RegExpText* AsText() override;
3026 bool IsText() override;
3027 bool IsTextElement() override { return true; }
3028 int min_match() override { return length_; }
3029 int max_match() override { return length_; }
3030 void AppendToText(RegExpText* text, Zone* zone) override;
3031 void AddElement(TextElement elm, Zone* zone) {
3032 elements_.Add(elm, zone);
3033 length_ += elm.length();
3035 ZoneList<TextElement>* elements() { return &elements_; }
3037 ZoneList<TextElement> elements_;
3042 class RegExpQuantifier final : public RegExpTree {
3044 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3045 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3049 min_match_(min * body->min_match()),
3050 quantifier_type_(type) {
3051 if (max > 0 && body->max_match() > kInfinity / max) {
3052 max_match_ = kInfinity;
3054 max_match_ = max * body->max_match();
3057 void* Accept(RegExpVisitor* visitor, void* data) override;
3058 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3059 RegExpNode* on_success) override;
3060 static RegExpNode* ToNode(int min,
3064 RegExpCompiler* compiler,
3065 RegExpNode* on_success,
3066 bool not_at_start = false);
3067 RegExpQuantifier* AsQuantifier() override;
3068 Interval CaptureRegisters() override;
3069 bool IsQuantifier() override;
3070 int min_match() override { return min_match_; }
3071 int max_match() override { return max_match_; }
3072 int min() { return min_; }
3073 int max() { return max_; }
3074 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3075 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3076 bool is_greedy() { return quantifier_type_ == GREEDY; }
3077 RegExpTree* body() { return body_; }
3085 QuantifierType quantifier_type_;
3089 class RegExpCapture final : public RegExpTree {
3091 explicit RegExpCapture(RegExpTree* body, int index)
3092 : body_(body), index_(index) { }
3093 void* Accept(RegExpVisitor* visitor, void* data) override;
3094 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3095 RegExpNode* on_success) override;
3096 static RegExpNode* ToNode(RegExpTree* body,
3098 RegExpCompiler* compiler,
3099 RegExpNode* on_success);
3100 RegExpCapture* AsCapture() override;
3101 bool IsAnchoredAtStart() override;
3102 bool IsAnchoredAtEnd() override;
3103 Interval CaptureRegisters() override;
3104 bool IsCapture() override;
3105 int min_match() override { return body_->min_match(); }
3106 int max_match() override { return body_->max_match(); }
3107 RegExpTree* body() { return body_; }
3108 int index() { return index_; }
3109 static int StartRegister(int index) { return index * 2; }
3110 static int EndRegister(int index) { return index * 2 + 1; }
3118 class RegExpLookahead final : public RegExpTree {
3120 RegExpLookahead(RegExpTree* body,
3125 is_positive_(is_positive),
3126 capture_count_(capture_count),
3127 capture_from_(capture_from) { }
3129 void* Accept(RegExpVisitor* visitor, void* data) override;
3130 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3131 RegExpNode* on_success) override;
3132 RegExpLookahead* AsLookahead() override;
3133 Interval CaptureRegisters() override;
3134 bool IsLookahead() override;
3135 bool IsAnchoredAtStart() override;
3136 int min_match() override { return 0; }
3137 int max_match() override { return 0; }
3138 RegExpTree* body() { return body_; }
3139 bool is_positive() { return is_positive_; }
3140 int capture_count() { return capture_count_; }
3141 int capture_from() { return capture_from_; }
3151 class RegExpBackReference final : public RegExpTree {
3153 explicit RegExpBackReference(RegExpCapture* capture)
3154 : capture_(capture) { }
3155 void* Accept(RegExpVisitor* visitor, void* data) override;
3156 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3157 RegExpNode* on_success) override;
3158 RegExpBackReference* AsBackReference() override;
3159 bool IsBackReference() override;
3160 int min_match() override { return 0; }
3161 int max_match() override { return capture_->max_match(); }
3162 int index() { return capture_->index(); }
3163 RegExpCapture* capture() { return capture_; }
3165 RegExpCapture* capture_;
3169 class RegExpEmpty final : public RegExpTree {
3172 void* Accept(RegExpVisitor* visitor, void* data) override;
3173 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3174 RegExpNode* on_success) override;
3175 RegExpEmpty* AsEmpty() override;
3176 bool IsEmpty() override;
3177 int min_match() override { return 0; }
3178 int max_match() override { return 0; }
3182 // ----------------------------------------------------------------------------
3184 // - leaf node visitors are abstract.
3186 class AstVisitor BASE_EMBEDDED {
3189 virtual ~AstVisitor() {}
3191 // Stack overflow check and dynamic dispatch.
3192 virtual void Visit(AstNode* node) = 0;
3194 // Iteration left-to-right.
3195 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3196 virtual void VisitStatements(ZoneList<Statement*>* statements);
3197 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3199 // Individual AST nodes.
3200 #define DEF_VISIT(type) \
3201 virtual void Visit##type(type* node) = 0;
3202 AST_NODE_LIST(DEF_VISIT)
3207 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3209 void Visit(AstNode* node) final { \
3210 if (!CheckStackOverflow()) node->Accept(this); \
3213 void SetStackOverflow() { stack_overflow_ = true; } \
3214 void ClearStackOverflow() { stack_overflow_ = false; } \
3215 bool HasStackOverflow() const { return stack_overflow_; } \
3217 bool CheckStackOverflow() { \
3218 if (stack_overflow_) return true; \
3219 StackLimitCheck check(isolate_); \
3220 if (!check.HasOverflowed()) return false; \
3221 stack_overflow_ = true; \
3226 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3227 isolate_ = isolate; \
3229 stack_overflow_ = false; \
3231 Zone* zone() { return zone_; } \
3232 Isolate* isolate() { return isolate_; } \
3234 Isolate* isolate_; \
3236 bool stack_overflow_
3239 // ----------------------------------------------------------------------------
3242 class AstNodeFactory final BASE_EMBEDDED {
3244 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3245 : zone_(ast_value_factory->zone()),
3246 ast_value_factory_(ast_value_factory) {}
3248 VariableDeclaration* NewVariableDeclaration(
3249 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3250 bool is_class_declaration = false, int declaration_group_start = -1) {
3252 VariableDeclaration(zone_, proxy, mode, scope, pos,
3253 is_class_declaration, declaration_group_start);
3256 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3258 FunctionLiteral* fun,
3261 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3264 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3265 const AstRawString* import_name,
3266 const AstRawString* module_specifier,
3267 Scope* scope, int pos) {
3268 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3269 module_specifier, scope, pos);
3272 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3275 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3278 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3279 bool ignore_completion_value, int pos) {
3281 Block(zone_, labels, capacity, ignore_completion_value, pos);
3284 #define STATEMENT_WITH_LABELS(NodeType) \
3285 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3286 return new (zone_) NodeType(zone_, labels, pos); \
3288 STATEMENT_WITH_LABELS(DoWhileStatement)
3289 STATEMENT_WITH_LABELS(WhileStatement)
3290 STATEMENT_WITH_LABELS(ForStatement)
3291 STATEMENT_WITH_LABELS(SwitchStatement)
3292 #undef STATEMENT_WITH_LABELS
3294 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3295 ZoneList<const AstRawString*>* labels,
3297 switch (visit_mode) {
3298 case ForEachStatement::ENUMERATE: {
3299 return new (zone_) ForInStatement(zone_, labels, pos);
3301 case ForEachStatement::ITERATE: {
3302 return new (zone_) ForOfStatement(zone_, labels, pos);
3309 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3310 return new (zone_) ExpressionStatement(zone_, expression, pos);
3313 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3314 return new (zone_) ContinueStatement(zone_, target, pos);
3317 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3318 return new (zone_) BreakStatement(zone_, target, pos);
3321 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3322 return new (zone_) ReturnStatement(zone_, expression, pos);
3325 WithStatement* NewWithStatement(Scope* scope,
3326 Expression* expression,
3327 Statement* statement,
3329 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3332 IfStatement* NewIfStatement(Expression* condition,
3333 Statement* then_statement,
3334 Statement* else_statement,
3337 IfStatement(zone_, condition, then_statement, else_statement, pos);
3340 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3342 Block* catch_block, int pos) {
3344 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3347 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3348 Block* finally_block, int pos) {
3350 TryFinallyStatement(zone_, try_block, finally_block, pos);
3353 DebuggerStatement* NewDebuggerStatement(int pos) {
3354 return new (zone_) DebuggerStatement(zone_, pos);
3357 EmptyStatement* NewEmptyStatement(int pos) {
3358 return new(zone_) EmptyStatement(zone_, pos);
3361 CaseClause* NewCaseClause(
3362 Expression* label, ZoneList<Statement*>* statements, int pos) {
3363 return new (zone_) CaseClause(zone_, label, statements, pos);
3366 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3368 Literal(zone_, ast_value_factory_->NewString(string), pos);
3371 // A JavaScript symbol (ECMA-262 edition 6).
3372 Literal* NewSymbolLiteral(const char* name, int pos) {
3373 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3376 Literal* NewNumberLiteral(double number, int pos) {
3378 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3381 Literal* NewSmiLiteral(int number, int pos) {
3382 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3385 Literal* NewBooleanLiteral(bool b, int pos) {
3386 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3389 Literal* NewNullLiteral(int pos) {
3390 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3393 Literal* NewUndefinedLiteral(int pos) {
3394 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3397 Literal* NewTheHoleLiteral(int pos) {
3398 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3401 ObjectLiteral* NewObjectLiteral(
3402 ZoneList<ObjectLiteral::Property*>* properties,
3404 int boilerplate_properties,
3408 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3409 boilerplate_properties, has_function,
3413 ObjectLiteral::Property* NewObjectLiteralProperty(
3414 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3415 bool is_static, bool is_computed_name) {
3417 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3420 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3423 bool is_computed_name) {
3424 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3425 is_static, is_computed_name);
3428 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3429 const AstRawString* flags,
3433 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3437 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3441 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3445 VariableProxy* NewVariableProxy(Variable* var,
3446 int start_position = RelocInfo::kNoPosition,
3447 int end_position = RelocInfo::kNoPosition) {
3448 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3451 VariableProxy* NewVariableProxy(const AstRawString* name,
3452 Variable::Kind variable_kind,
3453 int start_position = RelocInfo::kNoPosition,
3454 int end_position = RelocInfo::kNoPosition) {
3455 DCHECK_NOT_NULL(name);
3457 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3460 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3461 return new (zone_) Property(zone_, obj, key, pos);
3464 Call* NewCall(Expression* expression,
3465 ZoneList<Expression*>* arguments,
3467 return new (zone_) Call(zone_, expression, arguments, pos);
3470 CallNew* NewCallNew(Expression* expression,
3471 ZoneList<Expression*>* arguments,
3473 return new (zone_) CallNew(zone_, expression, arguments, pos);
3476 CallRuntime* NewCallRuntime(const AstRawString* name,
3477 const Runtime::Function* function,
3478 ZoneList<Expression*>* arguments,
3480 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3483 UnaryOperation* NewUnaryOperation(Token::Value op,
3484 Expression* expression,
3486 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3489 BinaryOperation* NewBinaryOperation(Token::Value op,
3493 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3496 CountOperation* NewCountOperation(Token::Value op,
3500 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3503 CompareOperation* NewCompareOperation(Token::Value op,
3507 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3510 Spread* NewSpread(Expression* expression, int pos) {
3511 return new (zone_) Spread(zone_, expression, pos);
3514 Conditional* NewConditional(Expression* condition,
3515 Expression* then_expression,
3516 Expression* else_expression,
3518 return new (zone_) Conditional(zone_, condition, then_expression,
3519 else_expression, position);
3522 Assignment* NewAssignment(Token::Value op,
3526 DCHECK(Token::IsAssignmentOp(op));
3527 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3528 if (assign->is_compound()) {
3529 DCHECK(Token::IsAssignmentOp(op));
3530 assign->binary_operation_ =
3531 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3536 Yield* NewYield(Expression *generator_object,
3537 Expression* expression,
3538 Yield::Kind yield_kind,
3540 if (!expression) expression = NewUndefinedLiteral(pos);
3542 Yield(zone_, generator_object, expression, yield_kind, pos);
3545 Throw* NewThrow(Expression* exception, int pos) {
3546 return new (zone_) Throw(zone_, exception, pos);
3549 FunctionLiteral* NewFunctionLiteral(
3550 const AstRawString* name, AstValueFactory* ast_value_factory,
3551 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3552 int expected_property_count, int parameter_count,
3553 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3554 FunctionLiteral::FunctionType function_type,
3555 FunctionLiteral::IsFunctionFlag is_function,
3556 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3558 return new (zone_) FunctionLiteral(
3559 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3560 expected_property_count, parameter_count, function_type,
3561 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3565 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3566 VariableProxy* proxy, Expression* extends,
3567 FunctionLiteral* constructor,
3568 ZoneList<ObjectLiteral::Property*>* properties,
3569 int start_position, int end_position) {
3571 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3572 properties, start_position, end_position);
3575 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3576 v8::Extension* extension,
3578 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3581 ThisFunction* NewThisFunction(int pos) {
3582 return new (zone_) ThisFunction(zone_, pos);
3585 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3586 Expression* home_object,
3589 SuperPropertyReference(zone_, this_var, home_object, pos);
3592 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3593 VariableProxy* new_target_var,
3594 VariableProxy* this_function_var,
3596 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3597 this_function_var, pos);
3602 AstValueFactory* ast_value_factory_;
3606 } } // namespace v8::internal