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() + 2; }
1873 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1874 BailoutId EvalOrLookupId() const { return BailoutId(local_id(1)); }
1876 bool is_uninitialized() const {
1877 return IsUninitializedField::decode(bit_field_);
1879 void set_is_uninitialized(bool b) {
1880 bit_field_ = IsUninitializedField::update(bit_field_, b);
1892 // Helpers to determine how to handle the call.
1893 CallType GetCallType(Isolate* isolate) const;
1894 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1895 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1898 // Used to assert that the FullCodeGenerator records the return site.
1899 bool return_is_recorded_;
1903 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1905 : Expression(zone, pos),
1906 ic_slot_(FeedbackVectorICSlot::Invalid()),
1907 slot_(FeedbackVectorSlot::Invalid()),
1908 expression_(expression),
1909 arguments_(arguments),
1910 bit_field_(IsUninitializedField::encode(false)) {
1911 if (expression->IsProperty()) {
1912 expression->AsProperty()->mark_for_call();
1915 static int parent_num_ids() { return Expression::num_ids(); }
1918 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1920 FeedbackVectorICSlot ic_slot_;
1921 FeedbackVectorSlot slot_;
1922 Expression* expression_;
1923 ZoneList<Expression*>* arguments_;
1924 Handle<JSFunction> target_;
1925 Handle<AllocationSite> allocation_site_;
1926 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1931 class CallNew final : public Expression {
1933 DECLARE_NODE_TYPE(CallNew)
1935 Expression* expression() const { return expression_; }
1936 ZoneList<Expression*>* arguments() const { return arguments_; }
1938 // Type feedback information.
1939 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1940 Isolate* isolate, const ICSlotCache* cache) override {
1941 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1943 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1944 callnew_feedback_slot_ = slot;
1947 FeedbackVectorSlot CallNewFeedbackSlot() {
1948 DCHECK(!callnew_feedback_slot_.IsInvalid());
1949 return callnew_feedback_slot_;
1951 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1952 DCHECK(FLAG_pretenuring_call_new);
1953 return CallNewFeedbackSlot().next();
1956 bool IsMonomorphic() override { return is_monomorphic_; }
1957 Handle<JSFunction> target() const { return target_; }
1958 Handle<AllocationSite> allocation_site() const {
1959 return allocation_site_;
1962 static int num_ids() { return parent_num_ids() + 1; }
1963 static int feedback_slots() { return 1; }
1964 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1966 void set_allocation_site(Handle<AllocationSite> site) {
1967 allocation_site_ = site;
1969 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1970 void set_target(Handle<JSFunction> target) { target_ = target; }
1971 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1973 is_monomorphic_ = true;
1977 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1979 : Expression(zone, pos),
1980 expression_(expression),
1981 arguments_(arguments),
1982 is_monomorphic_(false),
1983 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1985 static int parent_num_ids() { return Expression::num_ids(); }
1988 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1990 Expression* expression_;
1991 ZoneList<Expression*>* arguments_;
1992 bool is_monomorphic_;
1993 Handle<JSFunction> target_;
1994 Handle<AllocationSite> allocation_site_;
1995 FeedbackVectorSlot callnew_feedback_slot_;
1999 // The CallRuntime class does not represent any official JavaScript
2000 // language construct. Instead it is used to call a C or JS function
2001 // with a set of arguments. This is used from the builtins that are
2002 // implemented in JavaScript (see "v8natives.js").
2003 class CallRuntime final : public Expression {
2005 DECLARE_NODE_TYPE(CallRuntime)
2007 Handle<String> name() const { return raw_name_->string(); }
2008 const AstRawString* raw_name() const { return raw_name_; }
2009 const Runtime::Function* function() const { return function_; }
2010 ZoneList<Expression*>* arguments() const { return arguments_; }
2011 bool is_jsruntime() const { return function_ == NULL; }
2013 // Type feedback information.
2014 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2015 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2016 Isolate* isolate, const ICSlotCache* cache) override {
2017 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2019 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2020 ICSlotCache* cache) override {
2021 callruntime_feedback_slot_ = slot;
2023 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2025 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2026 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2027 !callruntime_feedback_slot_.IsInvalid());
2028 return callruntime_feedback_slot_;
2031 static int num_ids() { return parent_num_ids(); }
2034 CallRuntime(Zone* zone, const AstRawString* name,
2035 const Runtime::Function* function,
2036 ZoneList<Expression*>* arguments, int pos)
2037 : Expression(zone, pos),
2039 function_(function),
2040 arguments_(arguments),
2041 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2042 static int parent_num_ids() { return Expression::num_ids(); }
2045 const AstRawString* raw_name_;
2046 const Runtime::Function* function_;
2047 ZoneList<Expression*>* arguments_;
2048 FeedbackVectorICSlot callruntime_feedback_slot_;
2052 class UnaryOperation final : public Expression {
2054 DECLARE_NODE_TYPE(UnaryOperation)
2056 Token::Value op() const { return op_; }
2057 Expression* expression() const { return expression_; }
2059 // For unary not (Token::NOT), the AST ids where true and false will
2060 // actually be materialized, respectively.
2061 static int num_ids() { return parent_num_ids() + 2; }
2062 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2063 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2065 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2068 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2069 : Expression(zone, pos), op_(op), expression_(expression) {
2070 DCHECK(Token::IsUnaryOp(op));
2072 static int parent_num_ids() { return Expression::num_ids(); }
2075 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2078 Expression* expression_;
2082 class BinaryOperation final : public Expression {
2084 DECLARE_NODE_TYPE(BinaryOperation)
2086 Token::Value op() const { return static_cast<Token::Value>(op_); }
2087 Expression* left() const { return left_; }
2088 Expression* right() const { return right_; }
2089 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2090 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2091 allocation_site_ = allocation_site;
2094 // The short-circuit logical operations need an AST ID for their
2095 // right-hand subexpression.
2096 static int num_ids() { return parent_num_ids() + 2; }
2097 BailoutId RightId() const { return BailoutId(local_id(0)); }
2099 TypeFeedbackId BinaryOperationFeedbackId() const {
2100 return TypeFeedbackId(local_id(1));
2102 Maybe<int> fixed_right_arg() const {
2103 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2105 void set_fixed_right_arg(Maybe<int> arg) {
2106 has_fixed_right_arg_ = arg.IsJust();
2107 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2110 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2113 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2114 Expression* right, int pos)
2115 : Expression(zone, pos),
2116 op_(static_cast<byte>(op)),
2117 has_fixed_right_arg_(false),
2118 fixed_right_arg_value_(0),
2121 DCHECK(Token::IsBinaryOp(op));
2123 static int parent_num_ids() { return Expression::num_ids(); }
2126 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2128 const byte op_; // actually Token::Value
2129 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2130 // type for the RHS. Currenty it's actually a Maybe<int>
2131 bool has_fixed_right_arg_;
2132 int fixed_right_arg_value_;
2135 Handle<AllocationSite> allocation_site_;
2139 class CountOperation final : public Expression {
2141 DECLARE_NODE_TYPE(CountOperation)
2143 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2144 bool is_postfix() const { return !is_prefix(); }
2146 Token::Value op() const { return TokenField::decode(bit_field_); }
2147 Token::Value binary_op() {
2148 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2151 Expression* expression() const { return expression_; }
2153 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2154 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2155 IcCheckType GetKeyType() const override {
2156 return KeyTypeField::decode(bit_field_);
2158 KeyedAccessStoreMode GetStoreMode() const override {
2159 return StoreModeField::decode(bit_field_);
2161 Type* type() const { return type_; }
2162 void set_key_type(IcCheckType type) {
2163 bit_field_ = KeyTypeField::update(bit_field_, type);
2165 void set_store_mode(KeyedAccessStoreMode mode) {
2166 bit_field_ = StoreModeField::update(bit_field_, mode);
2168 void set_type(Type* type) { type_ = type; }
2170 static int num_ids() { return parent_num_ids() + 4; }
2171 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2172 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2173 TypeFeedbackId CountBinOpFeedbackId() const {
2174 return TypeFeedbackId(local_id(2));
2176 TypeFeedbackId CountStoreFeedbackId() const {
2177 return TypeFeedbackId(local_id(3));
2180 FeedbackVectorRequirements ComputeFeedbackRequirements(
2181 Isolate* isolate, const ICSlotCache* cache) override;
2182 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2183 ICSlotCache* cache) override {
2186 Code::Kind FeedbackICSlotKind(int index) override;
2187 FeedbackVectorICSlot CountSlot() const { return slot_; }
2190 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2192 : Expression(zone, pos),
2194 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2195 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2198 slot_(FeedbackVectorICSlot::Invalid()) {}
2199 static int parent_num_ids() { return Expression::num_ids(); }
2202 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2204 class IsPrefixField : public BitField16<bool, 0, 1> {};
2205 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2206 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2207 class TokenField : public BitField16<Token::Value, 6, 8> {};
2209 // Starts with 16-bit field, which should get packed together with
2210 // Expression's trailing 16-bit field.
2211 uint16_t bit_field_;
2213 Expression* expression_;
2214 SmallMapList receiver_types_;
2215 FeedbackVectorICSlot slot_;
2219 class CompareOperation final : public Expression {
2221 DECLARE_NODE_TYPE(CompareOperation)
2223 Token::Value op() const { return op_; }
2224 Expression* left() const { return left_; }
2225 Expression* right() const { return right_; }
2227 // Type feedback information.
2228 static int num_ids() { return parent_num_ids() + 1; }
2229 TypeFeedbackId CompareOperationFeedbackId() const {
2230 return TypeFeedbackId(local_id(0));
2232 Type* combined_type() const { return combined_type_; }
2233 void set_combined_type(Type* type) { combined_type_ = type; }
2235 // Match special cases.
2236 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2237 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2238 bool IsLiteralCompareNull(Expression** expr);
2241 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2242 Expression* right, int pos)
2243 : Expression(zone, pos),
2247 combined_type_(Type::None(zone)) {
2248 DCHECK(Token::IsCompareOp(op));
2250 static int parent_num_ids() { return Expression::num_ids(); }
2253 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2259 Type* combined_type_;
2263 class Spread final : public Expression {
2265 DECLARE_NODE_TYPE(Spread)
2267 Expression* expression() const { return expression_; }
2269 static int num_ids() { return parent_num_ids(); }
2272 Spread(Zone* zone, Expression* expression, int pos)
2273 : Expression(zone, pos), expression_(expression) {}
2274 static int parent_num_ids() { return Expression::num_ids(); }
2277 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2279 Expression* expression_;
2283 class Conditional final : public Expression {
2285 DECLARE_NODE_TYPE(Conditional)
2287 Expression* condition() const { return condition_; }
2288 Expression* then_expression() const { return then_expression_; }
2289 Expression* else_expression() const { return else_expression_; }
2291 static int num_ids() { return parent_num_ids() + 2; }
2292 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2293 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2296 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2297 Expression* else_expression, int position)
2298 : Expression(zone, position),
2299 condition_(condition),
2300 then_expression_(then_expression),
2301 else_expression_(else_expression) {}
2302 static int parent_num_ids() { return Expression::num_ids(); }
2305 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2307 Expression* condition_;
2308 Expression* then_expression_;
2309 Expression* else_expression_;
2313 class Assignment final : public Expression {
2315 DECLARE_NODE_TYPE(Assignment)
2317 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2319 Token::Value binary_op() const;
2321 Token::Value op() const { return TokenField::decode(bit_field_); }
2322 Expression* target() const { return target_; }
2323 Expression* value() const { return value_; }
2324 BinaryOperation* binary_operation() const { return binary_operation_; }
2326 // This check relies on the definition order of token in token.h.
2327 bool is_compound() const { return op() > Token::ASSIGN; }
2329 static int num_ids() { return parent_num_ids() + 2; }
2330 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2332 // Type feedback information.
2333 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2334 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2335 bool IsUninitialized() const {
2336 return IsUninitializedField::decode(bit_field_);
2338 bool HasNoTypeInformation() {
2339 return IsUninitializedField::decode(bit_field_);
2341 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2342 IcCheckType GetKeyType() const override {
2343 return KeyTypeField::decode(bit_field_);
2345 KeyedAccessStoreMode GetStoreMode() const override {
2346 return StoreModeField::decode(bit_field_);
2348 void set_is_uninitialized(bool b) {
2349 bit_field_ = IsUninitializedField::update(bit_field_, b);
2351 void set_key_type(IcCheckType key_type) {
2352 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2354 void set_store_mode(KeyedAccessStoreMode mode) {
2355 bit_field_ = StoreModeField::update(bit_field_, mode);
2358 FeedbackVectorRequirements ComputeFeedbackRequirements(
2359 Isolate* isolate, const ICSlotCache* cache) override;
2360 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2361 ICSlotCache* cache) override {
2364 Code::Kind FeedbackICSlotKind(int index) override;
2365 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2368 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2370 static int parent_num_ids() { return Expression::num_ids(); }
2373 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2375 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2376 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2377 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2378 class TokenField : public BitField16<Token::Value, 6, 8> {};
2380 // Starts with 16-bit field, which should get packed together with
2381 // Expression's trailing 16-bit field.
2382 uint16_t bit_field_;
2383 Expression* target_;
2385 BinaryOperation* binary_operation_;
2386 SmallMapList receiver_types_;
2387 FeedbackVectorICSlot slot_;
2391 class Yield final : public Expression {
2393 DECLARE_NODE_TYPE(Yield)
2396 kInitial, // The initial yield that returns the unboxed generator object.
2397 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2398 kDelegating, // A yield*.
2399 kFinal // A return: { value: EXPRESSION, done: true }
2402 Expression* generator_object() const { return generator_object_; }
2403 Expression* expression() const { return expression_; }
2404 Kind yield_kind() const { return yield_kind_; }
2406 // Type feedback information.
2407 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2408 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2409 Isolate* isolate, const ICSlotCache* cache) override {
2410 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2412 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2413 ICSlotCache* cache) override {
2414 yield_first_feedback_slot_ = slot;
2416 Code::Kind FeedbackICSlotKind(int index) override {
2417 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2420 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2421 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2422 return yield_first_feedback_slot_;
2425 FeedbackVectorICSlot DoneFeedbackSlot() {
2426 return KeyedLoadFeedbackSlot().next();
2429 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2432 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2433 Kind yield_kind, int pos)
2434 : Expression(zone, pos),
2435 generator_object_(generator_object),
2436 expression_(expression),
2437 yield_kind_(yield_kind),
2438 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2441 Expression* generator_object_;
2442 Expression* expression_;
2444 FeedbackVectorICSlot yield_first_feedback_slot_;
2448 class Throw final : public Expression {
2450 DECLARE_NODE_TYPE(Throw)
2452 Expression* exception() const { return exception_; }
2455 Throw(Zone* zone, Expression* exception, int pos)
2456 : Expression(zone, pos), exception_(exception) {}
2459 Expression* exception_;
2463 class FunctionLiteral final : public Expression {
2466 ANONYMOUS_EXPRESSION,
2471 enum ParameterFlag {
2472 kNoDuplicateParameters = 0,
2473 kHasDuplicateParameters = 1
2476 enum IsFunctionFlag {
2481 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2483 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2485 enum ArityRestriction {
2491 DECLARE_NODE_TYPE(FunctionLiteral)
2493 Handle<String> name() const { return raw_name_->string(); }
2494 const AstRawString* raw_name() const { return raw_name_; }
2495 Scope* scope() const { return scope_; }
2496 ZoneList<Statement*>* body() const { return body_; }
2497 void set_function_token_position(int pos) { function_token_position_ = pos; }
2498 int function_token_position() const { return function_token_position_; }
2499 int start_position() const;
2500 int end_position() const;
2501 int SourceSize() const { return end_position() - start_position(); }
2502 bool is_expression() const { return IsExpression::decode(bitfield_); }
2503 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2504 LanguageMode language_mode() const;
2506 static bool NeedsHomeObject(Expression* expr);
2508 int materialized_literal_count() { return materialized_literal_count_; }
2509 int expected_property_count() { return expected_property_count_; }
2510 int parameter_count() { return parameter_count_; }
2512 bool AllowsLazyCompilation();
2513 bool AllowsLazyCompilationWithoutContext();
2515 void InitializeSharedInfo(Handle<Code> code);
2517 Handle<String> debug_name() const {
2518 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2519 return raw_name_->string();
2521 return inferred_name();
2524 Handle<String> inferred_name() const {
2525 if (!inferred_name_.is_null()) {
2526 DCHECK(raw_inferred_name_ == NULL);
2527 return inferred_name_;
2529 if (raw_inferred_name_ != NULL) {
2530 return raw_inferred_name_->string();
2533 return Handle<String>();
2536 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2537 void set_inferred_name(Handle<String> inferred_name) {
2538 DCHECK(!inferred_name.is_null());
2539 inferred_name_ = inferred_name;
2540 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2541 raw_inferred_name_ = NULL;
2544 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2545 DCHECK(raw_inferred_name != NULL);
2546 raw_inferred_name_ = raw_inferred_name;
2547 DCHECK(inferred_name_.is_null());
2548 inferred_name_ = Handle<String>();
2551 // shared_info may be null if it's not cached in full code.
2552 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2554 bool pretenure() { return Pretenure::decode(bitfield_); }
2555 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2557 bool has_duplicate_parameters() {
2558 return HasDuplicateParameters::decode(bitfield_);
2561 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2563 // This is used as a heuristic on when to eagerly compile a function
2564 // literal. We consider the following constructs as hints that the
2565 // function will be called immediately:
2566 // - (function() { ... })();
2567 // - var x = function() { ... }();
2568 bool should_eager_compile() const {
2569 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2571 void set_should_eager_compile() {
2572 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2575 // A hint that we expect this function to be called (exactly) once,
2576 // i.e. we suspect it's an initialization function.
2577 bool should_be_used_once_hint() const {
2578 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2580 void set_should_be_used_once_hint() {
2581 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2584 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2586 int ast_node_count() { return ast_properties_.node_count(); }
2587 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2588 void set_ast_properties(AstProperties* ast_properties) {
2589 ast_properties_ = *ast_properties;
2591 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2592 return ast_properties_.get_spec();
2594 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2595 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2596 void set_dont_optimize_reason(BailoutReason reason) {
2597 dont_optimize_reason_ = reason;
2601 FunctionLiteral(Zone* zone, const AstRawString* name,
2602 AstValueFactory* ast_value_factory, Scope* scope,
2603 ZoneList<Statement*>* body, int materialized_literal_count,
2604 int expected_property_count, int parameter_count,
2605 FunctionType function_type,
2606 ParameterFlag has_duplicate_parameters,
2607 IsFunctionFlag is_function,
2608 EagerCompileHint eager_compile_hint, FunctionKind kind,
2610 : Expression(zone, position),
2614 raw_inferred_name_(ast_value_factory->empty_string()),
2615 ast_properties_(zone),
2616 dont_optimize_reason_(kNoReason),
2617 materialized_literal_count_(materialized_literal_count),
2618 expected_property_count_(expected_property_count),
2619 parameter_count_(parameter_count),
2620 function_token_position_(RelocInfo::kNoPosition) {
2621 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2622 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2623 Pretenure::encode(false) |
2624 HasDuplicateParameters::encode(has_duplicate_parameters) |
2625 IsFunction::encode(is_function) |
2626 EagerCompileHintBit::encode(eager_compile_hint) |
2627 FunctionKindBits::encode(kind) |
2628 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2629 DCHECK(IsValidFunctionKind(kind));
2633 const AstRawString* raw_name_;
2634 Handle<String> name_;
2635 Handle<SharedFunctionInfo> shared_info_;
2637 ZoneList<Statement*>* body_;
2638 const AstString* raw_inferred_name_;
2639 Handle<String> inferred_name_;
2640 AstProperties ast_properties_;
2641 BailoutReason dont_optimize_reason_;
2643 int materialized_literal_count_;
2644 int expected_property_count_;
2645 int parameter_count_;
2646 int function_token_position_;
2649 class IsExpression : public BitField<bool, 0, 1> {};
2650 class IsAnonymous : public BitField<bool, 1, 1> {};
2651 class Pretenure : public BitField<bool, 2, 1> {};
2652 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2653 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2654 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2655 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2656 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2661 class ClassLiteral final : public Expression {
2663 typedef ObjectLiteralProperty Property;
2665 DECLARE_NODE_TYPE(ClassLiteral)
2667 Handle<String> name() const { return raw_name_->string(); }
2668 const AstRawString* raw_name() const { return raw_name_; }
2669 Scope* scope() const { return scope_; }
2670 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2671 Expression* extends() const { return extends_; }
2672 FunctionLiteral* constructor() const { return constructor_; }
2673 ZoneList<Property*>* properties() const { return properties_; }
2674 int start_position() const { return position(); }
2675 int end_position() const { return end_position_; }
2677 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2678 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2679 BailoutId ExitId() { return BailoutId(local_id(2)); }
2680 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2682 // Return an AST id for a property that is used in simulate instructions.
2683 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2685 // Unlike other AST nodes, this number of bailout IDs allocated for an
2686 // ClassLiteral can vary, so num_ids() is not a static method.
2687 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2689 // Object literals need one feedback slot for each non-trivial value, as well
2690 // as some slots for home objects.
2691 FeedbackVectorRequirements ComputeFeedbackRequirements(
2692 Isolate* isolate, const ICSlotCache* cache) override;
2693 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2694 ICSlotCache* cache) override {
2697 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2698 FeedbackVectorICSlot GetNthSlot(int n) const {
2699 return FeedbackVectorICSlot(slot_.ToInt() + n);
2702 // If value needs a home object, returns a valid feedback vector ic slot
2703 // given by slot_index, and increments slot_index.
2704 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2705 int* slot_index) const;
2708 int slot_count() const { return slot_count_; }
2712 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2713 VariableProxy* class_variable_proxy, Expression* extends,
2714 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2715 int start_position, int end_position)
2716 : Expression(zone, start_position),
2719 class_variable_proxy_(class_variable_proxy),
2721 constructor_(constructor),
2722 properties_(properties),
2723 end_position_(end_position),
2727 slot_(FeedbackVectorICSlot::Invalid()) {
2730 static int parent_num_ids() { return Expression::num_ids(); }
2733 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2735 const AstRawString* raw_name_;
2737 VariableProxy* class_variable_proxy_;
2738 Expression* extends_;
2739 FunctionLiteral* constructor_;
2740 ZoneList<Property*>* properties_;
2743 // slot_count_ helps validate that the logic to allocate ic slots and the
2744 // logic to use them are in sync.
2747 FeedbackVectorICSlot slot_;
2751 class NativeFunctionLiteral final : public Expression {
2753 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2755 Handle<String> name() const { return name_->string(); }
2756 v8::Extension* extension() const { return extension_; }
2759 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2760 v8::Extension* extension, int pos)
2761 : Expression(zone, pos), name_(name), extension_(extension) {}
2764 const AstRawString* name_;
2765 v8::Extension* extension_;
2769 class ThisFunction final : public Expression {
2771 DECLARE_NODE_TYPE(ThisFunction)
2774 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2778 class SuperPropertyReference final : public Expression {
2780 DECLARE_NODE_TYPE(SuperPropertyReference)
2782 VariableProxy* this_var() const { return this_var_; }
2783 Expression* home_object() const { return home_object_; }
2786 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2787 Expression* home_object, int pos)
2788 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2789 DCHECK(this_var->is_this());
2790 DCHECK(home_object->IsProperty());
2794 VariableProxy* this_var_;
2795 Expression* home_object_;
2799 class SuperCallReference final : public Expression {
2801 DECLARE_NODE_TYPE(SuperCallReference)
2803 VariableProxy* this_var() const { return this_var_; }
2804 VariableProxy* new_target_var() const { return new_target_var_; }
2805 VariableProxy* this_function_var() const { return this_function_var_; }
2808 SuperCallReference(Zone* zone, VariableProxy* this_var,
2809 VariableProxy* new_target_var,
2810 VariableProxy* this_function_var, int pos)
2811 : Expression(zone, pos),
2812 this_var_(this_var),
2813 new_target_var_(new_target_var),
2814 this_function_var_(this_function_var) {
2815 DCHECK(this_var->is_this());
2816 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2817 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2821 VariableProxy* this_var_;
2822 VariableProxy* new_target_var_;
2823 VariableProxy* this_function_var_;
2827 #undef DECLARE_NODE_TYPE
2830 // ----------------------------------------------------------------------------
2831 // Regular expressions
2834 class RegExpVisitor BASE_EMBEDDED {
2836 virtual ~RegExpVisitor() { }
2837 #define MAKE_CASE(Name) \
2838 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2839 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2844 class RegExpTree : public ZoneObject {
2846 static const int kInfinity = kMaxInt;
2847 virtual ~RegExpTree() {}
2848 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2849 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2850 RegExpNode* on_success) = 0;
2851 virtual bool IsTextElement() { return false; }
2852 virtual bool IsAnchoredAtStart() { return false; }
2853 virtual bool IsAnchoredAtEnd() { return false; }
2854 virtual int min_match() = 0;
2855 virtual int max_match() = 0;
2856 // Returns the interval of registers used for captures within this
2858 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2859 virtual void AppendToText(RegExpText* text, Zone* zone);
2860 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2861 #define MAKE_ASTYPE(Name) \
2862 virtual RegExp##Name* As##Name(); \
2863 virtual bool Is##Name();
2864 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2869 class RegExpDisjunction final : public RegExpTree {
2871 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2872 void* Accept(RegExpVisitor* visitor, void* data) override;
2873 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2874 RegExpNode* on_success) override;
2875 RegExpDisjunction* AsDisjunction() override;
2876 Interval CaptureRegisters() override;
2877 bool IsDisjunction() override;
2878 bool IsAnchoredAtStart() override;
2879 bool IsAnchoredAtEnd() override;
2880 int min_match() override { return min_match_; }
2881 int max_match() override { return max_match_; }
2882 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2884 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2885 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2886 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2887 ZoneList<RegExpTree*>* alternatives_;
2893 class RegExpAlternative final : public RegExpTree {
2895 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2896 void* Accept(RegExpVisitor* visitor, void* data) override;
2897 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2898 RegExpNode* on_success) override;
2899 RegExpAlternative* AsAlternative() override;
2900 Interval CaptureRegisters() override;
2901 bool IsAlternative() override;
2902 bool IsAnchoredAtStart() override;
2903 bool IsAnchoredAtEnd() override;
2904 int min_match() override { return min_match_; }
2905 int max_match() override { return max_match_; }
2906 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2908 ZoneList<RegExpTree*>* nodes_;
2914 class RegExpAssertion final : public RegExpTree {
2916 enum AssertionType {
2924 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2925 void* Accept(RegExpVisitor* visitor, void* data) override;
2926 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2927 RegExpNode* on_success) override;
2928 RegExpAssertion* AsAssertion() override;
2929 bool IsAssertion() override;
2930 bool IsAnchoredAtStart() override;
2931 bool IsAnchoredAtEnd() override;
2932 int min_match() override { return 0; }
2933 int max_match() override { return 0; }
2934 AssertionType assertion_type() { return assertion_type_; }
2936 AssertionType assertion_type_;
2940 class CharacterSet final BASE_EMBEDDED {
2942 explicit CharacterSet(uc16 standard_set_type)
2944 standard_set_type_(standard_set_type) {}
2945 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2947 standard_set_type_(0) {}
2948 ZoneList<CharacterRange>* ranges(Zone* zone);
2949 uc16 standard_set_type() { return standard_set_type_; }
2950 void set_standard_set_type(uc16 special_set_type) {
2951 standard_set_type_ = special_set_type;
2953 bool is_standard() { return standard_set_type_ != 0; }
2954 void Canonicalize();
2956 ZoneList<CharacterRange>* ranges_;
2957 // If non-zero, the value represents a standard set (e.g., all whitespace
2958 // characters) without having to expand the ranges.
2959 uc16 standard_set_type_;
2963 class RegExpCharacterClass final : public RegExpTree {
2965 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2967 is_negated_(is_negated) { }
2968 explicit RegExpCharacterClass(uc16 type)
2970 is_negated_(false) { }
2971 void* Accept(RegExpVisitor* visitor, void* data) override;
2972 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2973 RegExpNode* on_success) override;
2974 RegExpCharacterClass* AsCharacterClass() override;
2975 bool IsCharacterClass() override;
2976 bool IsTextElement() override { return true; }
2977 int min_match() override { return 1; }
2978 int max_match() override { return 1; }
2979 void AppendToText(RegExpText* text, Zone* zone) override;
2980 CharacterSet character_set() { return set_; }
2981 // TODO(lrn): Remove need for complex version if is_standard that
2982 // recognizes a mangled standard set and just do { return set_.is_special(); }
2983 bool is_standard(Zone* zone);
2984 // Returns a value representing the standard character set if is_standard()
2986 // Currently used values are:
2987 // s : unicode whitespace
2988 // S : unicode non-whitespace
2989 // w : ASCII word character (digit, letter, underscore)
2990 // W : non-ASCII word character
2992 // D : non-ASCII digit
2993 // . : non-unicode non-newline
2994 // * : All characters
2995 uc16 standard_type() { return set_.standard_set_type(); }
2996 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2997 bool is_negated() { return is_negated_; }
3005 class RegExpAtom final : public RegExpTree {
3007 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3008 void* Accept(RegExpVisitor* visitor, void* data) override;
3009 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3010 RegExpNode* on_success) override;
3011 RegExpAtom* AsAtom() override;
3012 bool IsAtom() override;
3013 bool IsTextElement() override { return true; }
3014 int min_match() override { return data_.length(); }
3015 int max_match() override { return data_.length(); }
3016 void AppendToText(RegExpText* text, Zone* zone) override;
3017 Vector<const uc16> data() { return data_; }
3018 int length() { return data_.length(); }
3020 Vector<const uc16> data_;
3024 class RegExpText final : public RegExpTree {
3026 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3027 void* Accept(RegExpVisitor* visitor, void* data) override;
3028 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3029 RegExpNode* on_success) override;
3030 RegExpText* AsText() override;
3031 bool IsText() override;
3032 bool IsTextElement() override { return true; }
3033 int min_match() override { return length_; }
3034 int max_match() override { return length_; }
3035 void AppendToText(RegExpText* text, Zone* zone) override;
3036 void AddElement(TextElement elm, Zone* zone) {
3037 elements_.Add(elm, zone);
3038 length_ += elm.length();
3040 ZoneList<TextElement>* elements() { return &elements_; }
3042 ZoneList<TextElement> elements_;
3047 class RegExpQuantifier final : public RegExpTree {
3049 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3050 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3054 min_match_(min * body->min_match()),
3055 quantifier_type_(type) {
3056 if (max > 0 && body->max_match() > kInfinity / max) {
3057 max_match_ = kInfinity;
3059 max_match_ = max * body->max_match();
3062 void* Accept(RegExpVisitor* visitor, void* data) override;
3063 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3064 RegExpNode* on_success) override;
3065 static RegExpNode* ToNode(int min,
3069 RegExpCompiler* compiler,
3070 RegExpNode* on_success,
3071 bool not_at_start = false);
3072 RegExpQuantifier* AsQuantifier() override;
3073 Interval CaptureRegisters() override;
3074 bool IsQuantifier() override;
3075 int min_match() override { return min_match_; }
3076 int max_match() override { return max_match_; }
3077 int min() { return min_; }
3078 int max() { return max_; }
3079 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3080 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3081 bool is_greedy() { return quantifier_type_ == GREEDY; }
3082 RegExpTree* body() { return body_; }
3090 QuantifierType quantifier_type_;
3094 class RegExpCapture final : public RegExpTree {
3096 explicit RegExpCapture(RegExpTree* body, int index)
3097 : body_(body), index_(index) { }
3098 void* Accept(RegExpVisitor* visitor, void* data) override;
3099 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3100 RegExpNode* on_success) override;
3101 static RegExpNode* ToNode(RegExpTree* body,
3103 RegExpCompiler* compiler,
3104 RegExpNode* on_success);
3105 RegExpCapture* AsCapture() override;
3106 bool IsAnchoredAtStart() override;
3107 bool IsAnchoredAtEnd() override;
3108 Interval CaptureRegisters() override;
3109 bool IsCapture() override;
3110 int min_match() override { return body_->min_match(); }
3111 int max_match() override { return body_->max_match(); }
3112 RegExpTree* body() { return body_; }
3113 int index() { return index_; }
3114 static int StartRegister(int index) { return index * 2; }
3115 static int EndRegister(int index) { return index * 2 + 1; }
3123 class RegExpLookahead final : public RegExpTree {
3125 RegExpLookahead(RegExpTree* body,
3130 is_positive_(is_positive),
3131 capture_count_(capture_count),
3132 capture_from_(capture_from) { }
3134 void* Accept(RegExpVisitor* visitor, void* data) override;
3135 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3136 RegExpNode* on_success) override;
3137 RegExpLookahead* AsLookahead() override;
3138 Interval CaptureRegisters() override;
3139 bool IsLookahead() override;
3140 bool IsAnchoredAtStart() override;
3141 int min_match() override { return 0; }
3142 int max_match() override { return 0; }
3143 RegExpTree* body() { return body_; }
3144 bool is_positive() { return is_positive_; }
3145 int capture_count() { return capture_count_; }
3146 int capture_from() { return capture_from_; }
3156 class RegExpBackReference final : public RegExpTree {
3158 explicit RegExpBackReference(RegExpCapture* capture)
3159 : capture_(capture) { }
3160 void* Accept(RegExpVisitor* visitor, void* data) override;
3161 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3162 RegExpNode* on_success) override;
3163 RegExpBackReference* AsBackReference() override;
3164 bool IsBackReference() override;
3165 int min_match() override { return 0; }
3166 int max_match() override { return capture_->max_match(); }
3167 int index() { return capture_->index(); }
3168 RegExpCapture* capture() { return capture_; }
3170 RegExpCapture* capture_;
3174 class RegExpEmpty final : public RegExpTree {
3177 void* Accept(RegExpVisitor* visitor, void* data) override;
3178 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3179 RegExpNode* on_success) override;
3180 RegExpEmpty* AsEmpty() override;
3181 bool IsEmpty() override;
3182 int min_match() override { return 0; }
3183 int max_match() override { return 0; }
3187 // ----------------------------------------------------------------------------
3189 // - leaf node visitors are abstract.
3191 class AstVisitor BASE_EMBEDDED {
3194 virtual ~AstVisitor() {}
3196 // Stack overflow check and dynamic dispatch.
3197 virtual void Visit(AstNode* node) = 0;
3199 // Iteration left-to-right.
3200 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3201 virtual void VisitStatements(ZoneList<Statement*>* statements);
3202 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3204 // Individual AST nodes.
3205 #define DEF_VISIT(type) \
3206 virtual void Visit##type(type* node) = 0;
3207 AST_NODE_LIST(DEF_VISIT)
3212 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3214 void Visit(AstNode* node) final { \
3215 if (!CheckStackOverflow()) node->Accept(this); \
3218 void SetStackOverflow() { stack_overflow_ = true; } \
3219 void ClearStackOverflow() { stack_overflow_ = false; } \
3220 bool HasStackOverflow() const { return stack_overflow_; } \
3222 bool CheckStackOverflow() { \
3223 if (stack_overflow_) return true; \
3224 StackLimitCheck check(isolate_); \
3225 if (!check.HasOverflowed()) return false; \
3226 stack_overflow_ = true; \
3231 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3232 isolate_ = isolate; \
3234 stack_overflow_ = false; \
3236 Zone* zone() { return zone_; } \
3237 Isolate* isolate() { return isolate_; } \
3239 Isolate* isolate_; \
3241 bool stack_overflow_
3244 // ----------------------------------------------------------------------------
3247 class AstNodeFactory final BASE_EMBEDDED {
3249 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3250 : zone_(ast_value_factory->zone()),
3251 ast_value_factory_(ast_value_factory) {}
3253 VariableDeclaration* NewVariableDeclaration(
3254 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3255 bool is_class_declaration = false, int declaration_group_start = -1) {
3257 VariableDeclaration(zone_, proxy, mode, scope, pos,
3258 is_class_declaration, declaration_group_start);
3261 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3263 FunctionLiteral* fun,
3266 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3269 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3270 const AstRawString* import_name,
3271 const AstRawString* module_specifier,
3272 Scope* scope, int pos) {
3273 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3274 module_specifier, scope, pos);
3277 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3280 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3283 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3284 bool ignore_completion_value, int pos) {
3286 Block(zone_, labels, capacity, ignore_completion_value, pos);
3289 #define STATEMENT_WITH_LABELS(NodeType) \
3290 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3291 return new (zone_) NodeType(zone_, labels, pos); \
3293 STATEMENT_WITH_LABELS(DoWhileStatement)
3294 STATEMENT_WITH_LABELS(WhileStatement)
3295 STATEMENT_WITH_LABELS(ForStatement)
3296 STATEMENT_WITH_LABELS(SwitchStatement)
3297 #undef STATEMENT_WITH_LABELS
3299 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3300 ZoneList<const AstRawString*>* labels,
3302 switch (visit_mode) {
3303 case ForEachStatement::ENUMERATE: {
3304 return new (zone_) ForInStatement(zone_, labels, pos);
3306 case ForEachStatement::ITERATE: {
3307 return new (zone_) ForOfStatement(zone_, labels, pos);
3314 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3315 return new (zone_) ExpressionStatement(zone_, expression, pos);
3318 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3319 return new (zone_) ContinueStatement(zone_, target, pos);
3322 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3323 return new (zone_) BreakStatement(zone_, target, pos);
3326 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3327 return new (zone_) ReturnStatement(zone_, expression, pos);
3330 WithStatement* NewWithStatement(Scope* scope,
3331 Expression* expression,
3332 Statement* statement,
3334 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3337 IfStatement* NewIfStatement(Expression* condition,
3338 Statement* then_statement,
3339 Statement* else_statement,
3342 IfStatement(zone_, condition, then_statement, else_statement, pos);
3345 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3347 Block* catch_block, int pos) {
3349 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3352 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3353 Block* finally_block, int pos) {
3355 TryFinallyStatement(zone_, try_block, finally_block, pos);
3358 DebuggerStatement* NewDebuggerStatement(int pos) {
3359 return new (zone_) DebuggerStatement(zone_, pos);
3362 EmptyStatement* NewEmptyStatement(int pos) {
3363 return new(zone_) EmptyStatement(zone_, pos);
3366 CaseClause* NewCaseClause(
3367 Expression* label, ZoneList<Statement*>* statements, int pos) {
3368 return new (zone_) CaseClause(zone_, label, statements, pos);
3371 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3373 Literal(zone_, ast_value_factory_->NewString(string), pos);
3376 // A JavaScript symbol (ECMA-262 edition 6).
3377 Literal* NewSymbolLiteral(const char* name, int pos) {
3378 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3381 Literal* NewNumberLiteral(double number, int pos) {
3383 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3386 Literal* NewSmiLiteral(int number, int pos) {
3387 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3390 Literal* NewBooleanLiteral(bool b, int pos) {
3391 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3394 Literal* NewNullLiteral(int pos) {
3395 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3398 Literal* NewUndefinedLiteral(int pos) {
3399 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3402 Literal* NewTheHoleLiteral(int pos) {
3403 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3406 ObjectLiteral* NewObjectLiteral(
3407 ZoneList<ObjectLiteral::Property*>* properties,
3409 int boilerplate_properties,
3413 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3414 boilerplate_properties, has_function,
3418 ObjectLiteral::Property* NewObjectLiteralProperty(
3419 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3420 bool is_static, bool is_computed_name) {
3422 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3425 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3428 bool is_computed_name) {
3429 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3430 is_static, is_computed_name);
3433 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3434 const AstRawString* flags,
3438 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3442 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3446 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3450 VariableProxy* NewVariableProxy(Variable* var,
3451 int start_position = RelocInfo::kNoPosition,
3452 int end_position = RelocInfo::kNoPosition) {
3453 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3456 VariableProxy* NewVariableProxy(const AstRawString* name,
3457 Variable::Kind variable_kind,
3458 int start_position = RelocInfo::kNoPosition,
3459 int end_position = RelocInfo::kNoPosition) {
3460 DCHECK_NOT_NULL(name);
3462 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3465 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3466 return new (zone_) Property(zone_, obj, key, pos);
3469 Call* NewCall(Expression* expression,
3470 ZoneList<Expression*>* arguments,
3472 return new (zone_) Call(zone_, expression, arguments, pos);
3475 CallNew* NewCallNew(Expression* expression,
3476 ZoneList<Expression*>* arguments,
3478 return new (zone_) CallNew(zone_, expression, arguments, pos);
3481 CallRuntime* NewCallRuntime(const AstRawString* name,
3482 const Runtime::Function* function,
3483 ZoneList<Expression*>* arguments,
3485 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3488 UnaryOperation* NewUnaryOperation(Token::Value op,
3489 Expression* expression,
3491 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3494 BinaryOperation* NewBinaryOperation(Token::Value op,
3498 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3501 CountOperation* NewCountOperation(Token::Value op,
3505 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3508 CompareOperation* NewCompareOperation(Token::Value op,
3512 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3515 Spread* NewSpread(Expression* expression, int pos) {
3516 return new (zone_) Spread(zone_, expression, pos);
3519 Conditional* NewConditional(Expression* condition,
3520 Expression* then_expression,
3521 Expression* else_expression,
3523 return new (zone_) Conditional(zone_, condition, then_expression,
3524 else_expression, position);
3527 Assignment* NewAssignment(Token::Value op,
3531 DCHECK(Token::IsAssignmentOp(op));
3532 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3533 if (assign->is_compound()) {
3534 DCHECK(Token::IsAssignmentOp(op));
3535 assign->binary_operation_ =
3536 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3541 Yield* NewYield(Expression *generator_object,
3542 Expression* expression,
3543 Yield::Kind yield_kind,
3545 if (!expression) expression = NewUndefinedLiteral(pos);
3547 Yield(zone_, generator_object, expression, yield_kind, pos);
3550 Throw* NewThrow(Expression* exception, int pos) {
3551 return new (zone_) Throw(zone_, exception, pos);
3554 FunctionLiteral* NewFunctionLiteral(
3555 const AstRawString* name, AstValueFactory* ast_value_factory,
3556 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3557 int expected_property_count, int parameter_count,
3558 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3559 FunctionLiteral::FunctionType function_type,
3560 FunctionLiteral::IsFunctionFlag is_function,
3561 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3563 return new (zone_) FunctionLiteral(
3564 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3565 expected_property_count, parameter_count, function_type,
3566 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3570 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3571 VariableProxy* proxy, Expression* extends,
3572 FunctionLiteral* constructor,
3573 ZoneList<ObjectLiteral::Property*>* properties,
3574 int start_position, int end_position) {
3576 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3577 properties, start_position, end_position);
3580 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3581 v8::Extension* extension,
3583 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3586 ThisFunction* NewThisFunction(int pos) {
3587 return new (zone_) ThisFunction(zone_, pos);
3590 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3591 Expression* home_object,
3594 SuperPropertyReference(zone_, this_var, home_object, pos);
3597 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3598 VariableProxy* new_target_var,
3599 VariableProxy* this_function_var,
3601 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3602 this_function_var, pos);
3607 AstValueFactory* ast_value_factory_;
3611 } } // namespace v8::internal