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 byte 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(byte 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<byte, 0, 8> {};
414 // Ends with 16-bit field; deriving classes in turn begin with
415 // 16-bit fields for optimum packing efficiency.
419 class BreakableStatement : public Statement {
422 TARGET_FOR_ANONYMOUS,
423 TARGET_FOR_NAMED_ONLY
426 // The labels associated with this statement. May be NULL;
427 // if it is != NULL, guaranteed to contain at least one entry.
428 ZoneList<const AstRawString*>* labels() const { return labels_; }
430 // Type testing & conversion.
431 BreakableStatement* AsBreakableStatement() final { return this; }
434 Label* break_target() { return &break_target_; }
437 bool is_target_for_anonymous() const {
438 return breakable_type_ == TARGET_FOR_ANONYMOUS;
441 void set_base_id(int id) { base_id_ = id; }
442 static int num_ids() { return parent_num_ids() + 2; }
443 BailoutId EntryId() const { return BailoutId(local_id(0)); }
444 BailoutId ExitId() const { return BailoutId(local_id(1)); }
447 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
448 BreakableType breakable_type, int position)
449 : Statement(zone, position),
451 breakable_type_(breakable_type),
452 base_id_(BailoutId::None().ToInt()) {
453 DCHECK(labels == NULL || labels->length() > 0);
455 static int parent_num_ids() { return 0; }
457 int base_id() const {
458 DCHECK(!BailoutId(base_id_).IsNone());
463 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
465 ZoneList<const AstRawString*>* labels_;
466 BreakableType breakable_type_;
472 class Block final : public BreakableStatement {
474 DECLARE_NODE_TYPE(Block)
476 void AddStatement(Statement* statement, Zone* zone) {
477 statements_.Add(statement, zone);
480 ZoneList<Statement*>* statements() { return &statements_; }
481 bool ignore_completion_value() const { return ignore_completion_value_; }
483 static int num_ids() { return parent_num_ids() + 1; }
484 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
486 bool IsJump() const override {
487 return !statements_.is_empty() && statements_.last()->IsJump()
488 && labels() == NULL; // Good enough as an approximation...
491 Scope* scope() const { return scope_; }
492 void set_scope(Scope* scope) { scope_ = scope; }
495 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
496 bool ignore_completion_value, int pos)
497 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
498 statements_(capacity, zone),
499 ignore_completion_value_(ignore_completion_value),
501 static int parent_num_ids() { return BreakableStatement::num_ids(); }
504 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
506 ZoneList<Statement*> statements_;
507 bool ignore_completion_value_;
512 class Declaration : public AstNode {
514 VariableProxy* proxy() const { return proxy_; }
515 VariableMode mode() const { return mode_; }
516 Scope* scope() const { return scope_; }
517 virtual InitializationFlag initialization() const = 0;
518 virtual bool IsInlineable() const;
521 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
523 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
524 DCHECK(IsDeclaredVariableMode(mode));
529 VariableProxy* proxy_;
531 // Nested scope from which the declaration originated.
536 class VariableDeclaration final : public Declaration {
538 DECLARE_NODE_TYPE(VariableDeclaration)
540 InitializationFlag initialization() const override {
541 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
544 bool is_class_declaration() const { return is_class_declaration_; }
546 // VariableDeclarations can be grouped into consecutive declaration
547 // groups. Each VariableDeclaration is associated with the start position of
548 // the group it belongs to. The positions are used for strong mode scope
549 // checks for classes and functions.
550 int declaration_group_start() const { return declaration_group_start_; }
553 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
554 Scope* scope, int pos, bool is_class_declaration = false,
555 int declaration_group_start = -1)
556 : Declaration(zone, proxy, mode, scope, pos),
557 is_class_declaration_(is_class_declaration),
558 declaration_group_start_(declaration_group_start) {}
560 bool is_class_declaration_;
561 int declaration_group_start_;
565 class FunctionDeclaration final : public Declaration {
567 DECLARE_NODE_TYPE(FunctionDeclaration)
569 FunctionLiteral* fun() const { return fun_; }
570 InitializationFlag initialization() const override {
571 return kCreatedInitialized;
573 bool IsInlineable() const override;
576 FunctionDeclaration(Zone* zone,
577 VariableProxy* proxy,
579 FunctionLiteral* fun,
582 : Declaration(zone, proxy, mode, scope, pos),
584 DCHECK(mode == VAR || mode == LET || mode == CONST);
589 FunctionLiteral* fun_;
593 class ImportDeclaration final : public Declaration {
595 DECLARE_NODE_TYPE(ImportDeclaration)
597 const AstRawString* import_name() const { return import_name_; }
598 const AstRawString* module_specifier() const { return module_specifier_; }
599 void set_module_specifier(const AstRawString* module_specifier) {
600 DCHECK(module_specifier_ == NULL);
601 module_specifier_ = module_specifier;
603 InitializationFlag initialization() const override {
604 return kNeedsInitialization;
608 ImportDeclaration(Zone* zone, VariableProxy* proxy,
609 const AstRawString* import_name,
610 const AstRawString* module_specifier, Scope* scope, int pos)
611 : Declaration(zone, proxy, IMPORT, scope, pos),
612 import_name_(import_name),
613 module_specifier_(module_specifier) {}
616 const AstRawString* import_name_;
617 const AstRawString* module_specifier_;
621 class ExportDeclaration final : public Declaration {
623 DECLARE_NODE_TYPE(ExportDeclaration)
625 InitializationFlag initialization() const override {
626 return kCreatedInitialized;
630 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
631 : Declaration(zone, proxy, LET, scope, pos) {}
635 class Module : public AstNode {
637 ModuleDescriptor* descriptor() const { return descriptor_; }
638 Block* body() const { return body_; }
641 Module(Zone* zone, int pos)
642 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
643 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
644 : AstNode(pos), descriptor_(descriptor), body_(body) {}
647 ModuleDescriptor* descriptor_;
652 class IterationStatement : public BreakableStatement {
654 // Type testing & conversion.
655 IterationStatement* AsIterationStatement() final { return this; }
657 Statement* body() const { return body_; }
659 static int num_ids() { return parent_num_ids() + 1; }
660 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
661 virtual BailoutId ContinueId() const = 0;
662 virtual BailoutId StackCheckId() const = 0;
665 Label* continue_target() { return &continue_target_; }
668 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
669 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
671 static int parent_num_ids() { return BreakableStatement::num_ids(); }
672 void Initialize(Statement* body) { body_ = body; }
675 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
678 Label continue_target_;
682 class DoWhileStatement final : public IterationStatement {
684 DECLARE_NODE_TYPE(DoWhileStatement)
686 void Initialize(Expression* cond, Statement* body) {
687 IterationStatement::Initialize(body);
691 Expression* cond() const { return cond_; }
693 static int num_ids() { return parent_num_ids() + 2; }
694 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
695 BailoutId StackCheckId() const override { return BackEdgeId(); }
696 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
699 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
700 : IterationStatement(zone, labels, pos), cond_(NULL) {}
701 static int parent_num_ids() { return IterationStatement::num_ids(); }
704 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
710 class WhileStatement final : public IterationStatement {
712 DECLARE_NODE_TYPE(WhileStatement)
714 void Initialize(Expression* cond, Statement* body) {
715 IterationStatement::Initialize(body);
719 Expression* cond() const { return cond_; }
721 static int num_ids() { return parent_num_ids() + 1; }
722 BailoutId ContinueId() const override { return EntryId(); }
723 BailoutId StackCheckId() const override { return BodyId(); }
724 BailoutId BodyId() const { return BailoutId(local_id(0)); }
727 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
728 : IterationStatement(zone, labels, pos), cond_(NULL) {}
729 static int parent_num_ids() { return IterationStatement::num_ids(); }
732 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
738 class ForStatement final : public IterationStatement {
740 DECLARE_NODE_TYPE(ForStatement)
742 void Initialize(Statement* init,
746 IterationStatement::Initialize(body);
752 Statement* init() const { return init_; }
753 Expression* cond() const { return cond_; }
754 Statement* next() const { return next_; }
756 static int num_ids() { return parent_num_ids() + 2; }
757 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
758 BailoutId StackCheckId() const override { return BodyId(); }
759 BailoutId BodyId() const { return BailoutId(local_id(1)); }
762 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
763 : IterationStatement(zone, labels, pos),
767 static int parent_num_ids() { return IterationStatement::num_ids(); }
770 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
778 class ForEachStatement : public IterationStatement {
781 ENUMERATE, // for (each in subject) body;
782 ITERATE // for (each of subject) body;
785 void Initialize(Expression* each, Expression* subject, Statement* body) {
786 IterationStatement::Initialize(body);
791 Expression* each() const { return each_; }
792 Expression* subject() const { return subject_; }
794 FeedbackVectorRequirements ComputeFeedbackRequirements(
795 Isolate* isolate, const ICSlotCache* cache) override;
796 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
797 ICSlotCache* cache) override {
800 Code::Kind FeedbackICSlotKind(int index) override;
801 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
804 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
805 : IterationStatement(zone, labels, pos),
808 each_slot_(FeedbackVectorICSlot::Invalid()) {}
812 Expression* subject_;
813 FeedbackVectorICSlot each_slot_;
817 class ForInStatement final : public ForEachStatement {
819 DECLARE_NODE_TYPE(ForInStatement)
821 Expression* enumerable() const {
825 // Type feedback information.
826 FeedbackVectorRequirements ComputeFeedbackRequirements(
827 Isolate* isolate, const ICSlotCache* cache) override {
828 FeedbackVectorRequirements base =
829 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
830 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
831 return FeedbackVectorRequirements(1, base.ic_slots());
833 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
834 for_in_feedback_slot_ = slot;
837 FeedbackVectorSlot ForInFeedbackSlot() {
838 DCHECK(!for_in_feedback_slot_.IsInvalid());
839 return for_in_feedback_slot_;
842 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
843 ForInType for_in_type() const { return for_in_type_; }
844 void set_for_in_type(ForInType type) { for_in_type_ = type; }
846 static int num_ids() { return parent_num_ids() + 6; }
847 BailoutId BodyId() const { return BailoutId(local_id(0)); }
848 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
849 BailoutId EnumId() const { return BailoutId(local_id(2)); }
850 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
851 BailoutId FilterId() const { return BailoutId(local_id(4)); }
852 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
853 BailoutId ContinueId() const override { return EntryId(); }
854 BailoutId StackCheckId() const override { return BodyId(); }
857 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
858 : ForEachStatement(zone, labels, pos),
859 for_in_type_(SLOW_FOR_IN),
860 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
861 static int parent_num_ids() { return ForEachStatement::num_ids(); }
864 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
866 ForInType for_in_type_;
867 FeedbackVectorSlot for_in_feedback_slot_;
871 class ForOfStatement final : public ForEachStatement {
873 DECLARE_NODE_TYPE(ForOfStatement)
875 void Initialize(Expression* each,
878 Expression* assign_iterator,
879 Expression* next_result,
880 Expression* result_done,
881 Expression* assign_each) {
882 ForEachStatement::Initialize(each, subject, body);
883 assign_iterator_ = assign_iterator;
884 next_result_ = next_result;
885 result_done_ = result_done;
886 assign_each_ = assign_each;
889 Expression* iterable() const {
893 // iterator = subject[Symbol.iterator]()
894 Expression* assign_iterator() const {
895 return assign_iterator_;
898 // result = iterator.next() // with type check
899 Expression* next_result() const {
904 Expression* result_done() const {
908 // each = result.value
909 Expression* assign_each() const {
913 BailoutId ContinueId() const override { return EntryId(); }
914 BailoutId StackCheckId() const override { return BackEdgeId(); }
916 static int num_ids() { return parent_num_ids() + 1; }
917 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
920 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
921 : ForEachStatement(zone, labels, pos),
922 assign_iterator_(NULL),
925 assign_each_(NULL) {}
926 static int parent_num_ids() { return ForEachStatement::num_ids(); }
929 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
931 Expression* assign_iterator_;
932 Expression* next_result_;
933 Expression* result_done_;
934 Expression* assign_each_;
938 class ExpressionStatement final : public Statement {
940 DECLARE_NODE_TYPE(ExpressionStatement)
942 void set_expression(Expression* e) { expression_ = e; }
943 Expression* expression() const { return expression_; }
944 bool IsJump() const override { return expression_->IsThrow(); }
947 ExpressionStatement(Zone* zone, Expression* expression, int pos)
948 : Statement(zone, pos), expression_(expression) { }
951 Expression* expression_;
955 class JumpStatement : public Statement {
957 bool IsJump() const final { return true; }
960 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
964 class ContinueStatement final : public JumpStatement {
966 DECLARE_NODE_TYPE(ContinueStatement)
968 IterationStatement* target() const { return target_; }
971 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
972 : JumpStatement(zone, pos), target_(target) { }
975 IterationStatement* target_;
979 class BreakStatement final : public JumpStatement {
981 DECLARE_NODE_TYPE(BreakStatement)
983 BreakableStatement* target() const { return target_; }
986 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
987 : JumpStatement(zone, pos), target_(target) { }
990 BreakableStatement* target_;
994 class ReturnStatement final : public JumpStatement {
996 DECLARE_NODE_TYPE(ReturnStatement)
998 Expression* expression() const { return expression_; }
1001 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1002 : JumpStatement(zone, pos), expression_(expression) { }
1005 Expression* expression_;
1009 class WithStatement final : public Statement {
1011 DECLARE_NODE_TYPE(WithStatement)
1013 Scope* scope() { return scope_; }
1014 Expression* expression() const { return expression_; }
1015 Statement* statement() const { return statement_; }
1017 void set_base_id(int id) { base_id_ = id; }
1018 static int num_ids() { return parent_num_ids() + 1; }
1019 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1022 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1023 Statement* statement, int pos)
1024 : Statement(zone, pos),
1026 expression_(expression),
1027 statement_(statement),
1028 base_id_(BailoutId::None().ToInt()) {}
1029 static int parent_num_ids() { return 0; }
1031 int base_id() const {
1032 DCHECK(!BailoutId(base_id_).IsNone());
1037 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1040 Expression* expression_;
1041 Statement* statement_;
1046 class CaseClause final : public Expression {
1048 DECLARE_NODE_TYPE(CaseClause)
1050 bool is_default() const { return label_ == NULL; }
1051 Expression* label() const {
1052 CHECK(!is_default());
1055 Label* body_target() { return &body_target_; }
1056 ZoneList<Statement*>* statements() const { return statements_; }
1058 static int num_ids() { return parent_num_ids() + 2; }
1059 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1060 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1062 Type* compare_type() { return compare_type_; }
1063 void set_compare_type(Type* type) { compare_type_ = type; }
1066 static int parent_num_ids() { return Expression::num_ids(); }
1069 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1071 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1075 ZoneList<Statement*>* statements_;
1076 Type* compare_type_;
1080 class SwitchStatement final : public BreakableStatement {
1082 DECLARE_NODE_TYPE(SwitchStatement)
1084 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1089 Expression* tag() const { return tag_; }
1090 ZoneList<CaseClause*>* cases() const { return cases_; }
1093 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1094 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1100 ZoneList<CaseClause*>* cases_;
1104 // If-statements always have non-null references to their then- and
1105 // else-parts. When parsing if-statements with no explicit else-part,
1106 // the parser implicitly creates an empty statement. Use the
1107 // HasThenStatement() and HasElseStatement() functions to check if a
1108 // given if-statement has a then- or an else-part containing code.
1109 class IfStatement final : public Statement {
1111 DECLARE_NODE_TYPE(IfStatement)
1113 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1114 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1116 Expression* condition() const { return condition_; }
1117 Statement* then_statement() const { return then_statement_; }
1118 Statement* else_statement() const { return else_statement_; }
1120 bool IsJump() const override {
1121 return HasThenStatement() && then_statement()->IsJump()
1122 && HasElseStatement() && else_statement()->IsJump();
1125 void set_base_id(int id) { base_id_ = id; }
1126 static int num_ids() { return parent_num_ids() + 3; }
1127 BailoutId IfId() const { return BailoutId(local_id(0)); }
1128 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1129 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1132 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1133 Statement* else_statement, int pos)
1134 : Statement(zone, pos),
1135 condition_(condition),
1136 then_statement_(then_statement),
1137 else_statement_(else_statement),
1138 base_id_(BailoutId::None().ToInt()) {}
1139 static int parent_num_ids() { return 0; }
1141 int base_id() const {
1142 DCHECK(!BailoutId(base_id_).IsNone());
1147 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1149 Expression* condition_;
1150 Statement* then_statement_;
1151 Statement* else_statement_;
1156 class TryStatement : public Statement {
1158 Block* try_block() const { return try_block_; }
1160 void set_base_id(int id) { base_id_ = id; }
1161 static int num_ids() { return parent_num_ids() + 1; }
1162 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1165 TryStatement(Zone* zone, Block* try_block, int pos)
1166 : Statement(zone, pos),
1167 try_block_(try_block),
1168 base_id_(BailoutId::None().ToInt()) {}
1169 static int parent_num_ids() { return 0; }
1171 int base_id() const {
1172 DCHECK(!BailoutId(base_id_).IsNone());
1177 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1184 class TryCatchStatement final : public TryStatement {
1186 DECLARE_NODE_TYPE(TryCatchStatement)
1188 Scope* scope() { return scope_; }
1189 Variable* variable() { return variable_; }
1190 Block* catch_block() const { return catch_block_; }
1193 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1194 Variable* variable, Block* catch_block, int pos)
1195 : TryStatement(zone, try_block, pos),
1197 variable_(variable),
1198 catch_block_(catch_block) {}
1202 Variable* variable_;
1203 Block* catch_block_;
1207 class TryFinallyStatement final : public TryStatement {
1209 DECLARE_NODE_TYPE(TryFinallyStatement)
1211 Block* finally_block() const { return finally_block_; }
1214 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1216 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1219 Block* finally_block_;
1223 class DebuggerStatement final : public Statement {
1225 DECLARE_NODE_TYPE(DebuggerStatement)
1227 void set_base_id(int id) { base_id_ = id; }
1228 static int num_ids() { return parent_num_ids() + 1; }
1229 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1232 explicit DebuggerStatement(Zone* zone, int pos)
1233 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1234 static int parent_num_ids() { return 0; }
1236 int base_id() const {
1237 DCHECK(!BailoutId(base_id_).IsNone());
1242 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1248 class EmptyStatement final : public Statement {
1250 DECLARE_NODE_TYPE(EmptyStatement)
1253 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1257 class Literal final : public Expression {
1259 DECLARE_NODE_TYPE(Literal)
1261 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1263 Handle<String> AsPropertyName() {
1264 DCHECK(IsPropertyName());
1265 return Handle<String>::cast(value());
1268 const AstRawString* AsRawPropertyName() {
1269 DCHECK(IsPropertyName());
1270 return value_->AsString();
1273 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1274 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1276 Handle<Object> value() const { return value_->value(); }
1277 const AstValue* raw_value() const { return value_; }
1279 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1280 // only for string and number literals!
1282 static bool Match(void* literal1, void* literal2);
1284 static int num_ids() { return parent_num_ids() + 1; }
1285 TypeFeedbackId LiteralFeedbackId() const {
1286 return TypeFeedbackId(local_id(0));
1290 Literal(Zone* zone, const AstValue* value, int position)
1291 : Expression(zone, position), value_(value) {}
1292 static int parent_num_ids() { return Expression::num_ids(); }
1295 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1297 const AstValue* value_;
1301 // Base class for literals that needs space in the corresponding JSFunction.
1302 class MaterializedLiteral : public Expression {
1304 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1306 int literal_index() { return literal_index_; }
1309 // only callable after initialization.
1310 DCHECK(depth_ >= 1);
1314 bool is_strong() const { return is_strong_; }
1317 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1318 : Expression(zone, pos),
1319 literal_index_(literal_index),
1321 is_strong_(is_strong),
1324 // A materialized literal is simple if the values consist of only
1325 // constants and simple object and array literals.
1326 bool is_simple() const { return is_simple_; }
1327 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1328 friend class CompileTimeValue;
1330 void set_depth(int depth) {
1335 // Populate the constant properties/elements fixed array.
1336 void BuildConstants(Isolate* isolate);
1337 friend class ArrayLiteral;
1338 friend class ObjectLiteral;
1340 // If the expression is a literal, return the literal value;
1341 // if the expression is a materialized literal and is simple return a
1342 // compile time value as encoded by CompileTimeValue::GetValue().
1343 // Otherwise, return undefined literal as the placeholder
1344 // in the object literal boilerplate.
1345 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1355 // Property is used for passing information
1356 // about an object literal's properties from the parser
1357 // to the code generator.
1358 class ObjectLiteralProperty final : public ZoneObject {
1361 CONSTANT, // Property with constant value (compile time).
1362 COMPUTED, // Property with computed value (execution time).
1363 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1364 GETTER, SETTER, // Property is an accessor function.
1365 PROTOTYPE // Property is __proto__.
1368 Expression* key() { return key_; }
1369 Expression* value() { return value_; }
1370 Kind kind() { return kind_; }
1372 // Type feedback information.
1373 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1374 Handle<Map> GetReceiverType() { return receiver_type_; }
1376 bool IsCompileTimeValue();
1378 void set_emit_store(bool emit_store);
1381 bool is_static() const { return is_static_; }
1382 bool is_computed_name() const { return is_computed_name_; }
1384 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1387 friend class AstNodeFactory;
1389 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1390 bool is_static, bool is_computed_name);
1391 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1392 Expression* value, bool is_static,
1393 bool is_computed_name);
1401 bool is_computed_name_;
1402 Handle<Map> receiver_type_;
1406 // An object literal has a boilerplate object that is used
1407 // for minimizing the work when constructing it at runtime.
1408 class ObjectLiteral final : public MaterializedLiteral {
1410 typedef ObjectLiteralProperty Property;
1412 DECLARE_NODE_TYPE(ObjectLiteral)
1414 Handle<FixedArray> constant_properties() const {
1415 return constant_properties_;
1417 int properties_count() const { return constant_properties_->length() / 2; }
1418 ZoneList<Property*>* properties() const { return properties_; }
1419 bool fast_elements() const { return fast_elements_; }
1420 bool may_store_doubles() const { return may_store_doubles_; }
1421 bool has_function() const { return has_function_; }
1422 bool has_elements() const { return has_elements_; }
1424 // Decide if a property should be in the object boilerplate.
1425 static bool IsBoilerplateProperty(Property* property);
1427 // Populate the constant properties fixed array.
1428 void BuildConstantProperties(Isolate* isolate);
1430 // Mark all computed expressions that are bound to a key that
1431 // is shadowed by a later occurrence of the same key. For the
1432 // marked expressions, no store code is emitted.
1433 void CalculateEmitStore(Zone* zone);
1435 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1436 int ComputeFlags(bool disable_mementos = false) const {
1437 int flags = fast_elements() ? kFastElements : kNoFlags;
1438 flags |= has_function() ? kHasFunction : kNoFlags;
1439 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1440 flags |= kShallowProperties;
1442 if (disable_mementos) {
1443 flags |= kDisableMementos;
1454 kHasFunction = 1 << 1,
1455 kShallowProperties = 1 << 2,
1456 kDisableMementos = 1 << 3,
1460 struct Accessors: public ZoneObject {
1461 Accessors() : getter(NULL), setter(NULL) {}
1466 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1468 // Return an AST id for a property that is used in simulate instructions.
1469 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1471 // Unlike other AST nodes, this number of bailout IDs allocated for an
1472 // ObjectLiteral can vary, so num_ids() is not a static method.
1473 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1475 // Object literals need one feedback slot for each non-trivial value, as well
1476 // as some slots for home objects.
1477 FeedbackVectorRequirements ComputeFeedbackRequirements(
1478 Isolate* isolate, const ICSlotCache* cache) override;
1479 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1480 ICSlotCache* cache) override {
1483 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1484 FeedbackVectorICSlot GetNthSlot(int n) const {
1485 return FeedbackVectorICSlot(slot_.ToInt() + n);
1488 // If value needs a home object, returns a valid feedback vector ic slot
1489 // given by slot_index, and increments slot_index.
1490 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1491 int* slot_index) const;
1494 int slot_count() const { return slot_count_; }
1498 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1499 int boilerplate_properties, bool has_function, bool is_strong,
1501 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1502 properties_(properties),
1503 boilerplate_properties_(boilerplate_properties),
1504 fast_elements_(false),
1505 has_elements_(false),
1506 may_store_doubles_(false),
1507 has_function_(has_function),
1511 slot_(FeedbackVectorICSlot::Invalid()) {
1513 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1516 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1517 Handle<FixedArray> constant_properties_;
1518 ZoneList<Property*>* properties_;
1519 int boilerplate_properties_;
1520 bool fast_elements_;
1522 bool may_store_doubles_;
1525 // slot_count_ helps validate that the logic to allocate ic slots and the
1526 // logic to use them are in sync.
1529 FeedbackVectorICSlot slot_;
1533 // Node for capturing a regexp literal.
1534 class RegExpLiteral final : public MaterializedLiteral {
1536 DECLARE_NODE_TYPE(RegExpLiteral)
1538 Handle<String> pattern() const { return pattern_->string(); }
1539 Handle<String> flags() const { return flags_->string(); }
1542 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1543 const AstRawString* flags, int literal_index, bool is_strong,
1545 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1552 const AstRawString* pattern_;
1553 const AstRawString* flags_;
1557 // An array literal has a literals object that is used
1558 // for minimizing the work when constructing it at runtime.
1559 class ArrayLiteral final : public MaterializedLiteral {
1561 DECLARE_NODE_TYPE(ArrayLiteral)
1563 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1564 ElementsKind constant_elements_kind() const {
1565 DCHECK_EQ(2, constant_elements_->length());
1566 return static_cast<ElementsKind>(
1567 Smi::cast(constant_elements_->get(0))->value());
1570 ZoneList<Expression*>* values() const { return values_; }
1572 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1574 // Return an AST id for an element that is used in simulate instructions.
1575 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1577 // Unlike other AST nodes, this number of bailout IDs allocated for an
1578 // ArrayLiteral can vary, so num_ids() is not a static method.
1579 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1581 // Populate the constant elements fixed array.
1582 void BuildConstantElements(Isolate* isolate);
1584 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1585 int ComputeFlags(bool disable_mementos = false) const {
1586 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1587 if (disable_mementos) {
1588 flags |= kDisableMementos;
1598 kShallowElements = 1,
1599 kDisableMementos = 1 << 1,
1604 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1605 bool is_strong, int pos)
1606 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1608 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1611 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1613 Handle<FixedArray> constant_elements_;
1614 ZoneList<Expression*>* values_;
1618 class VariableProxy final : public Expression {
1620 DECLARE_NODE_TYPE(VariableProxy)
1622 bool IsValidReferenceExpression() const override { return !is_this(); }
1624 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1626 Handle<String> name() const { return raw_name()->string(); }
1627 const AstRawString* raw_name() const {
1628 return is_resolved() ? var_->raw_name() : raw_name_;
1631 Variable* var() const {
1632 DCHECK(is_resolved());
1635 void set_var(Variable* v) {
1636 DCHECK(!is_resolved());
1641 bool is_this() const { return IsThisField::decode(bit_field_); }
1643 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1644 void set_is_assigned() {
1645 bit_field_ = IsAssignedField::update(bit_field_, true);
1648 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1649 void set_is_resolved() {
1650 bit_field_ = IsResolvedField::update(bit_field_, true);
1653 int end_position() const { return end_position_; }
1655 // Bind this proxy to the variable var.
1656 void BindTo(Variable* var);
1658 bool UsesVariableFeedbackSlot() const {
1659 return var()->IsUnallocated() || var()->IsLookupSlot();
1662 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1663 Isolate* isolate, const ICSlotCache* cache) override;
1665 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1666 ICSlotCache* cache) override;
1667 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1668 FeedbackVectorICSlot VariableFeedbackSlot() {
1669 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1670 return variable_feedback_slot_;
1673 static int num_ids() { return parent_num_ids() + 1; }
1674 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1677 VariableProxy(Zone* zone, Variable* var, int start_position,
1680 VariableProxy(Zone* zone, const AstRawString* name,
1681 Variable::Kind variable_kind, int start_position,
1683 static int parent_num_ids() { return Expression::num_ids(); }
1684 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1686 class IsThisField : public BitField8<bool, 0, 1> {};
1687 class IsAssignedField : public BitField8<bool, 1, 1> {};
1688 class IsResolvedField : public BitField8<bool, 2, 1> {};
1690 // Start with 16-bit (or smaller) field, which should get packed together
1691 // with Expression's trailing 16-bit field.
1693 FeedbackVectorICSlot variable_feedback_slot_;
1695 const AstRawString* raw_name_; // if !is_resolved_
1696 Variable* var_; // if is_resolved_
1698 // Position is stored in the AstNode superclass, but VariableProxy needs to
1699 // know its end position too (for error messages). It cannot be inferred from
1700 // the variable name length because it can contain escapes.
1705 // Left-hand side can only be a property, a global or a (parameter or local)
1711 NAMED_SUPER_PROPERTY,
1712 KEYED_SUPER_PROPERTY
1716 class Property final : public Expression {
1718 DECLARE_NODE_TYPE(Property)
1720 bool IsValidReferenceExpression() const override { return true; }
1722 Expression* obj() const { return obj_; }
1723 Expression* key() const { return key_; }
1725 static int num_ids() { return parent_num_ids() + 1; }
1726 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1728 bool IsStringAccess() const {
1729 return IsStringAccessField::decode(bit_field_);
1732 // Type feedback information.
1733 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1734 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1735 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1736 IcCheckType GetKeyType() const override {
1737 return KeyTypeField::decode(bit_field_);
1739 bool IsUninitialized() const {
1740 return !is_for_call() && HasNoTypeInformation();
1742 bool HasNoTypeInformation() const {
1743 return GetInlineCacheState() == UNINITIALIZED;
1745 InlineCacheState GetInlineCacheState() const {
1746 return InlineCacheStateField::decode(bit_field_);
1748 void set_is_string_access(bool b) {
1749 bit_field_ = IsStringAccessField::update(bit_field_, b);
1751 void set_key_type(IcCheckType key_type) {
1752 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1754 void set_inline_cache_state(InlineCacheState state) {
1755 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1757 void mark_for_call() {
1758 bit_field_ = IsForCallField::update(bit_field_, true);
1760 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1762 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1764 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1765 Isolate* isolate, const ICSlotCache* cache) override {
1766 return FeedbackVectorRequirements(0, 1);
1768 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1769 ICSlotCache* cache) override {
1770 property_feedback_slot_ = slot;
1772 Code::Kind FeedbackICSlotKind(int index) override {
1773 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1776 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1777 DCHECK(!property_feedback_slot_.IsInvalid());
1778 return property_feedback_slot_;
1781 static LhsKind GetAssignType(Property* property) {
1782 if (property == NULL) return VARIABLE;
1783 bool super_access = property->IsSuperAccess();
1784 return (property->key()->IsPropertyName())
1785 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1786 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1790 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1791 : Expression(zone, pos),
1792 bit_field_(IsForCallField::encode(false) |
1793 IsStringAccessField::encode(false) |
1794 InlineCacheStateField::encode(UNINITIALIZED)),
1795 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1798 static int parent_num_ids() { return Expression::num_ids(); }
1801 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1803 class IsForCallField : public BitField8<bool, 0, 1> {};
1804 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1805 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1806 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1808 FeedbackVectorICSlot property_feedback_slot_;
1811 SmallMapList receiver_types_;
1815 class Call final : public Expression {
1817 DECLARE_NODE_TYPE(Call)
1819 Expression* expression() const { return expression_; }
1820 ZoneList<Expression*>* arguments() const { return arguments_; }
1822 // Type feedback information.
1823 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1824 Isolate* isolate, const ICSlotCache* cache) override;
1825 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1826 ICSlotCache* cache) override {
1829 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1830 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1832 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1834 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1836 SmallMapList* GetReceiverTypes() override {
1837 if (expression()->IsProperty()) {
1838 return expression()->AsProperty()->GetReceiverTypes();
1843 bool IsMonomorphic() override {
1844 if (expression()->IsProperty()) {
1845 return expression()->AsProperty()->IsMonomorphic();
1847 return !target_.is_null();
1850 bool global_call() const {
1851 VariableProxy* proxy = expression_->AsVariableProxy();
1852 return proxy != NULL && proxy->var()->IsUnallocated();
1855 bool known_global_function() const {
1856 return global_call() && !target_.is_null();
1859 Handle<JSFunction> target() { return target_; }
1861 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1863 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1865 set_is_uninitialized(false);
1867 void set_target(Handle<JSFunction> target) { target_ = target; }
1868 void set_allocation_site(Handle<AllocationSite> site) {
1869 allocation_site_ = site;
1872 static int num_ids() { return parent_num_ids() + 3; }
1873 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1874 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1875 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1877 bool is_uninitialized() const {
1878 return IsUninitializedField::decode(bit_field_);
1880 void set_is_uninitialized(bool b) {
1881 bit_field_ = IsUninitializedField::update(bit_field_, b);
1893 // Helpers to determine how to handle the call.
1894 CallType GetCallType(Isolate* isolate) const;
1895 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1896 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1899 // Used to assert that the FullCodeGenerator records the return site.
1900 bool return_is_recorded_;
1904 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1906 : Expression(zone, pos),
1907 ic_slot_(FeedbackVectorICSlot::Invalid()),
1908 slot_(FeedbackVectorSlot::Invalid()),
1909 expression_(expression),
1910 arguments_(arguments),
1911 bit_field_(IsUninitializedField::encode(false)) {
1912 if (expression->IsProperty()) {
1913 expression->AsProperty()->mark_for_call();
1916 static int parent_num_ids() { return Expression::num_ids(); }
1919 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1921 FeedbackVectorICSlot ic_slot_;
1922 FeedbackVectorSlot slot_;
1923 Expression* expression_;
1924 ZoneList<Expression*>* arguments_;
1925 Handle<JSFunction> target_;
1926 Handle<AllocationSite> allocation_site_;
1927 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1932 class CallNew final : public Expression {
1934 DECLARE_NODE_TYPE(CallNew)
1936 Expression* expression() const { return expression_; }
1937 ZoneList<Expression*>* arguments() const { return arguments_; }
1939 // Type feedback information.
1940 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1941 Isolate* isolate, const ICSlotCache* cache) override {
1942 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1944 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1945 callnew_feedback_slot_ = slot;
1948 FeedbackVectorSlot CallNewFeedbackSlot() {
1949 DCHECK(!callnew_feedback_slot_.IsInvalid());
1950 return callnew_feedback_slot_;
1952 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1953 DCHECK(FLAG_pretenuring_call_new);
1954 return CallNewFeedbackSlot().next();
1957 bool IsMonomorphic() override { return is_monomorphic_; }
1958 Handle<JSFunction> target() const { return target_; }
1959 Handle<AllocationSite> allocation_site() const {
1960 return allocation_site_;
1963 static int num_ids() { return parent_num_ids() + 1; }
1964 static int feedback_slots() { return 1; }
1965 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1967 void set_allocation_site(Handle<AllocationSite> site) {
1968 allocation_site_ = site;
1970 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1971 void set_target(Handle<JSFunction> target) { target_ = target; }
1972 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1974 is_monomorphic_ = true;
1978 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1980 : Expression(zone, pos),
1981 expression_(expression),
1982 arguments_(arguments),
1983 is_monomorphic_(false),
1984 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1986 static int parent_num_ids() { return Expression::num_ids(); }
1989 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1991 Expression* expression_;
1992 ZoneList<Expression*>* arguments_;
1993 bool is_monomorphic_;
1994 Handle<JSFunction> target_;
1995 Handle<AllocationSite> allocation_site_;
1996 FeedbackVectorSlot callnew_feedback_slot_;
2000 // The CallRuntime class does not represent any official JavaScript
2001 // language construct. Instead it is used to call a C or JS function
2002 // with a set of arguments. This is used from the builtins that are
2003 // implemented in JavaScript (see "v8natives.js").
2004 class CallRuntime final : public Expression {
2006 DECLARE_NODE_TYPE(CallRuntime)
2008 Handle<String> name() const { return raw_name_->string(); }
2009 const AstRawString* raw_name() const { return raw_name_; }
2010 const Runtime::Function* function() const { return function_; }
2011 ZoneList<Expression*>* arguments() const { return arguments_; }
2012 bool is_jsruntime() const { return function_ == NULL; }
2014 // Type feedback information.
2015 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2016 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2017 Isolate* isolate, const ICSlotCache* cache) override {
2018 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2020 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2021 ICSlotCache* cache) override {
2022 callruntime_feedback_slot_ = slot;
2024 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2026 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2027 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2028 !callruntime_feedback_slot_.IsInvalid());
2029 return callruntime_feedback_slot_;
2032 static int num_ids() { return parent_num_ids(); }
2035 CallRuntime(Zone* zone, const AstRawString* name,
2036 const Runtime::Function* function,
2037 ZoneList<Expression*>* arguments, int pos)
2038 : Expression(zone, pos),
2040 function_(function),
2041 arguments_(arguments),
2042 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2043 static int parent_num_ids() { return Expression::num_ids(); }
2046 const AstRawString* raw_name_;
2047 const Runtime::Function* function_;
2048 ZoneList<Expression*>* arguments_;
2049 FeedbackVectorICSlot callruntime_feedback_slot_;
2053 class UnaryOperation final : public Expression {
2055 DECLARE_NODE_TYPE(UnaryOperation)
2057 Token::Value op() const { return op_; }
2058 Expression* expression() const { return expression_; }
2060 // For unary not (Token::NOT), the AST ids where true and false will
2061 // actually be materialized, respectively.
2062 static int num_ids() { return parent_num_ids() + 2; }
2063 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2064 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2066 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2069 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2070 : Expression(zone, pos), op_(op), expression_(expression) {
2071 DCHECK(Token::IsUnaryOp(op));
2073 static int parent_num_ids() { return Expression::num_ids(); }
2076 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2079 Expression* expression_;
2083 class BinaryOperation final : public Expression {
2085 DECLARE_NODE_TYPE(BinaryOperation)
2087 Token::Value op() const { return static_cast<Token::Value>(op_); }
2088 Expression* left() const { return left_; }
2089 Expression* right() const { return right_; }
2090 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2091 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2092 allocation_site_ = allocation_site;
2095 // The short-circuit logical operations need an AST ID for their
2096 // right-hand subexpression.
2097 static int num_ids() { return parent_num_ids() + 2; }
2098 BailoutId RightId() const { return BailoutId(local_id(0)); }
2100 TypeFeedbackId BinaryOperationFeedbackId() const {
2101 return TypeFeedbackId(local_id(1));
2103 Maybe<int> fixed_right_arg() const {
2104 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2106 void set_fixed_right_arg(Maybe<int> arg) {
2107 has_fixed_right_arg_ = arg.IsJust();
2108 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2111 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2114 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2115 Expression* right, int pos)
2116 : Expression(zone, pos),
2117 op_(static_cast<byte>(op)),
2118 has_fixed_right_arg_(false),
2119 fixed_right_arg_value_(0),
2122 DCHECK(Token::IsBinaryOp(op));
2124 static int parent_num_ids() { return Expression::num_ids(); }
2127 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2129 const byte op_; // actually Token::Value
2130 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2131 // type for the RHS. Currenty it's actually a Maybe<int>
2132 bool has_fixed_right_arg_;
2133 int fixed_right_arg_value_;
2136 Handle<AllocationSite> allocation_site_;
2140 class CountOperation final : public Expression {
2142 DECLARE_NODE_TYPE(CountOperation)
2144 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2145 bool is_postfix() const { return !is_prefix(); }
2147 Token::Value op() const { return TokenField::decode(bit_field_); }
2148 Token::Value binary_op() {
2149 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2152 Expression* expression() const { return expression_; }
2154 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2155 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2156 IcCheckType GetKeyType() const override {
2157 return KeyTypeField::decode(bit_field_);
2159 KeyedAccessStoreMode GetStoreMode() const override {
2160 return StoreModeField::decode(bit_field_);
2162 Type* type() const { return type_; }
2163 void set_key_type(IcCheckType type) {
2164 bit_field_ = KeyTypeField::update(bit_field_, type);
2166 void set_store_mode(KeyedAccessStoreMode mode) {
2167 bit_field_ = StoreModeField::update(bit_field_, mode);
2169 void set_type(Type* type) { type_ = type; }
2171 static int num_ids() { return parent_num_ids() + 4; }
2172 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2173 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2174 TypeFeedbackId CountBinOpFeedbackId() const {
2175 return TypeFeedbackId(local_id(2));
2177 TypeFeedbackId CountStoreFeedbackId() const {
2178 return TypeFeedbackId(local_id(3));
2181 FeedbackVectorRequirements ComputeFeedbackRequirements(
2182 Isolate* isolate, const ICSlotCache* cache) override;
2183 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2184 ICSlotCache* cache) override {
2187 Code::Kind FeedbackICSlotKind(int index) override;
2188 FeedbackVectorICSlot CountSlot() const { return slot_; }
2191 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2193 : Expression(zone, pos),
2195 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2196 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2199 slot_(FeedbackVectorICSlot::Invalid()) {}
2200 static int parent_num_ids() { return Expression::num_ids(); }
2203 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2205 class IsPrefixField : public BitField16<bool, 0, 1> {};
2206 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2207 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2208 class TokenField : public BitField16<Token::Value, 6, 8> {};
2210 // Starts with 16-bit field, which should get packed together with
2211 // Expression's trailing 16-bit field.
2212 uint16_t bit_field_;
2214 Expression* expression_;
2215 SmallMapList receiver_types_;
2216 FeedbackVectorICSlot slot_;
2220 class CompareOperation final : public Expression {
2222 DECLARE_NODE_TYPE(CompareOperation)
2224 Token::Value op() const { return op_; }
2225 Expression* left() const { return left_; }
2226 Expression* right() const { return right_; }
2228 // Type feedback information.
2229 static int num_ids() { return parent_num_ids() + 1; }
2230 TypeFeedbackId CompareOperationFeedbackId() const {
2231 return TypeFeedbackId(local_id(0));
2233 Type* combined_type() const { return combined_type_; }
2234 void set_combined_type(Type* type) { combined_type_ = type; }
2236 // Match special cases.
2237 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2238 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2239 bool IsLiteralCompareNull(Expression** expr);
2242 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2243 Expression* right, int pos)
2244 : Expression(zone, pos),
2248 combined_type_(Type::None(zone)) {
2249 DCHECK(Token::IsCompareOp(op));
2251 static int parent_num_ids() { return Expression::num_ids(); }
2254 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2260 Type* combined_type_;
2264 class Spread final : public Expression {
2266 DECLARE_NODE_TYPE(Spread)
2268 Expression* expression() const { return expression_; }
2270 static int num_ids() { return parent_num_ids(); }
2273 Spread(Zone* zone, Expression* expression, int pos)
2274 : Expression(zone, pos), expression_(expression) {}
2275 static int parent_num_ids() { return Expression::num_ids(); }
2278 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2280 Expression* expression_;
2284 class Conditional final : public Expression {
2286 DECLARE_NODE_TYPE(Conditional)
2288 Expression* condition() const { return condition_; }
2289 Expression* then_expression() const { return then_expression_; }
2290 Expression* else_expression() const { return else_expression_; }
2292 static int num_ids() { return parent_num_ids() + 2; }
2293 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2294 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2297 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2298 Expression* else_expression, int position)
2299 : Expression(zone, position),
2300 condition_(condition),
2301 then_expression_(then_expression),
2302 else_expression_(else_expression) {}
2303 static int parent_num_ids() { return Expression::num_ids(); }
2306 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2308 Expression* condition_;
2309 Expression* then_expression_;
2310 Expression* else_expression_;
2314 class Assignment final : public Expression {
2316 DECLARE_NODE_TYPE(Assignment)
2318 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2320 Token::Value binary_op() const;
2322 Token::Value op() const { return TokenField::decode(bit_field_); }
2323 Expression* target() const { return target_; }
2324 Expression* value() const { return value_; }
2325 BinaryOperation* binary_operation() const { return binary_operation_; }
2327 // This check relies on the definition order of token in token.h.
2328 bool is_compound() const { return op() > Token::ASSIGN; }
2330 static int num_ids() { return parent_num_ids() + 2; }
2331 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2333 // Type feedback information.
2334 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2335 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2336 bool IsUninitialized() const {
2337 return IsUninitializedField::decode(bit_field_);
2339 bool HasNoTypeInformation() {
2340 return IsUninitializedField::decode(bit_field_);
2342 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2343 IcCheckType GetKeyType() const override {
2344 return KeyTypeField::decode(bit_field_);
2346 KeyedAccessStoreMode GetStoreMode() const override {
2347 return StoreModeField::decode(bit_field_);
2349 void set_is_uninitialized(bool b) {
2350 bit_field_ = IsUninitializedField::update(bit_field_, b);
2352 void set_key_type(IcCheckType key_type) {
2353 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2355 void set_store_mode(KeyedAccessStoreMode mode) {
2356 bit_field_ = StoreModeField::update(bit_field_, mode);
2359 FeedbackVectorRequirements ComputeFeedbackRequirements(
2360 Isolate* isolate, const ICSlotCache* cache) override;
2361 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2362 ICSlotCache* cache) override {
2365 Code::Kind FeedbackICSlotKind(int index) override;
2366 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2369 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2371 static int parent_num_ids() { return Expression::num_ids(); }
2374 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2376 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2377 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2378 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2379 class TokenField : public BitField16<Token::Value, 6, 8> {};
2381 // Starts with 16-bit field, which should get packed together with
2382 // Expression's trailing 16-bit field.
2383 uint16_t bit_field_;
2384 Expression* target_;
2386 BinaryOperation* binary_operation_;
2387 SmallMapList receiver_types_;
2388 FeedbackVectorICSlot slot_;
2392 class Yield final : public Expression {
2394 DECLARE_NODE_TYPE(Yield)
2397 kInitial, // The initial yield that returns the unboxed generator object.
2398 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2399 kDelegating, // A yield*.
2400 kFinal // A return: { value: EXPRESSION, done: true }
2403 Expression* generator_object() const { return generator_object_; }
2404 Expression* expression() const { return expression_; }
2405 Kind yield_kind() const { return yield_kind_; }
2407 // Type feedback information.
2408 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2409 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2410 Isolate* isolate, const ICSlotCache* cache) override {
2411 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2413 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2414 ICSlotCache* cache) override {
2415 yield_first_feedback_slot_ = slot;
2417 Code::Kind FeedbackICSlotKind(int index) override {
2418 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2421 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2422 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2423 return yield_first_feedback_slot_;
2426 FeedbackVectorICSlot DoneFeedbackSlot() {
2427 return KeyedLoadFeedbackSlot().next();
2430 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2433 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2434 Kind yield_kind, int pos)
2435 : Expression(zone, pos),
2436 generator_object_(generator_object),
2437 expression_(expression),
2438 yield_kind_(yield_kind),
2439 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2442 Expression* generator_object_;
2443 Expression* expression_;
2445 FeedbackVectorICSlot yield_first_feedback_slot_;
2449 class Throw final : public Expression {
2451 DECLARE_NODE_TYPE(Throw)
2453 Expression* exception() const { return exception_; }
2456 Throw(Zone* zone, Expression* exception, int pos)
2457 : Expression(zone, pos), exception_(exception) {}
2460 Expression* exception_;
2464 class FunctionLiteral final : public Expression {
2467 ANONYMOUS_EXPRESSION,
2472 enum ParameterFlag {
2473 kNoDuplicateParameters = 0,
2474 kHasDuplicateParameters = 1
2477 enum IsFunctionFlag {
2482 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2484 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2486 enum ArityRestriction {
2492 DECLARE_NODE_TYPE(FunctionLiteral)
2494 Handle<String> name() const { return raw_name_->string(); }
2495 const AstRawString* raw_name() const { return raw_name_; }
2496 Scope* scope() const { return scope_; }
2497 ZoneList<Statement*>* body() const { return body_; }
2498 void set_function_token_position(int pos) { function_token_position_ = pos; }
2499 int function_token_position() const { return function_token_position_; }
2500 int start_position() const;
2501 int end_position() const;
2502 int SourceSize() const { return end_position() - start_position(); }
2503 bool is_expression() const { return IsExpression::decode(bitfield_); }
2504 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2505 LanguageMode language_mode() const;
2507 static bool NeedsHomeObject(Expression* expr);
2509 int materialized_literal_count() { return materialized_literal_count_; }
2510 int expected_property_count() { return expected_property_count_; }
2511 int parameter_count() { return parameter_count_; }
2513 bool AllowsLazyCompilation();
2514 bool AllowsLazyCompilationWithoutContext();
2516 void InitializeSharedInfo(Handle<Code> code);
2518 Handle<String> debug_name() const {
2519 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2520 return raw_name_->string();
2522 return inferred_name();
2525 Handle<String> inferred_name() const {
2526 if (!inferred_name_.is_null()) {
2527 DCHECK(raw_inferred_name_ == NULL);
2528 return inferred_name_;
2530 if (raw_inferred_name_ != NULL) {
2531 return raw_inferred_name_->string();
2534 return Handle<String>();
2537 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2538 void set_inferred_name(Handle<String> inferred_name) {
2539 DCHECK(!inferred_name.is_null());
2540 inferred_name_ = inferred_name;
2541 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2542 raw_inferred_name_ = NULL;
2545 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2546 DCHECK(raw_inferred_name != NULL);
2547 raw_inferred_name_ = raw_inferred_name;
2548 DCHECK(inferred_name_.is_null());
2549 inferred_name_ = Handle<String>();
2552 // shared_info may be null if it's not cached in full code.
2553 Handle<SharedFunctionInfo> shared_info() { return shared_info_; }
2555 bool pretenure() { return Pretenure::decode(bitfield_); }
2556 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2558 bool has_duplicate_parameters() {
2559 return HasDuplicateParameters::decode(bitfield_);
2562 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2564 // This is used as a heuristic on when to eagerly compile a function
2565 // literal. We consider the following constructs as hints that the
2566 // function will be called immediately:
2567 // - (function() { ... })();
2568 // - var x = function() { ... }();
2569 bool should_eager_compile() const {
2570 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2572 void set_should_eager_compile() {
2573 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2576 // A hint that we expect this function to be called (exactly) once,
2577 // i.e. we suspect it's an initialization function.
2578 bool should_be_used_once_hint() const {
2579 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2581 void set_should_be_used_once_hint() {
2582 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2585 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2587 int ast_node_count() { return ast_properties_.node_count(); }
2588 AstProperties::Flags* flags() { return ast_properties_.flags(); }
2589 void set_ast_properties(AstProperties* ast_properties) {
2590 ast_properties_ = *ast_properties;
2592 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2593 return ast_properties_.get_spec();
2595 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2596 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2597 void set_dont_optimize_reason(BailoutReason reason) {
2598 dont_optimize_reason_ = reason;
2602 FunctionLiteral(Zone* zone, const AstRawString* name,
2603 AstValueFactory* ast_value_factory, Scope* scope,
2604 ZoneList<Statement*>* body, int materialized_literal_count,
2605 int expected_property_count, int parameter_count,
2606 FunctionType function_type,
2607 ParameterFlag has_duplicate_parameters,
2608 IsFunctionFlag is_function,
2609 EagerCompileHint eager_compile_hint, FunctionKind kind,
2611 : Expression(zone, position),
2615 raw_inferred_name_(ast_value_factory->empty_string()),
2616 ast_properties_(zone),
2617 dont_optimize_reason_(kNoReason),
2618 materialized_literal_count_(materialized_literal_count),
2619 expected_property_count_(expected_property_count),
2620 parameter_count_(parameter_count),
2621 function_token_position_(RelocInfo::kNoPosition) {
2622 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2623 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2624 Pretenure::encode(false) |
2625 HasDuplicateParameters::encode(has_duplicate_parameters) |
2626 IsFunction::encode(is_function) |
2627 EagerCompileHintBit::encode(eager_compile_hint) |
2628 FunctionKindBits::encode(kind) |
2629 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2630 DCHECK(IsValidFunctionKind(kind));
2634 const AstRawString* raw_name_;
2635 Handle<String> name_;
2636 Handle<SharedFunctionInfo> shared_info_;
2638 ZoneList<Statement*>* body_;
2639 const AstString* raw_inferred_name_;
2640 Handle<String> inferred_name_;
2641 AstProperties ast_properties_;
2642 BailoutReason dont_optimize_reason_;
2644 int materialized_literal_count_;
2645 int expected_property_count_;
2646 int parameter_count_;
2647 int function_token_position_;
2650 class IsExpression : public BitField<bool, 0, 1> {};
2651 class IsAnonymous : public BitField<bool, 1, 1> {};
2652 class Pretenure : public BitField<bool, 2, 1> {};
2653 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2654 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2655 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2656 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2657 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2662 class ClassLiteral final : public Expression {
2664 typedef ObjectLiteralProperty Property;
2666 DECLARE_NODE_TYPE(ClassLiteral)
2668 Handle<String> name() const { return raw_name_->string(); }
2669 const AstRawString* raw_name() const { return raw_name_; }
2670 Scope* scope() const { return scope_; }
2671 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2672 Expression* extends() const { return extends_; }
2673 FunctionLiteral* constructor() const { return constructor_; }
2674 ZoneList<Property*>* properties() const { return properties_; }
2675 int start_position() const { return position(); }
2676 int end_position() const { return end_position_; }
2678 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2679 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2680 BailoutId ExitId() { return BailoutId(local_id(2)); }
2681 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2683 // Return an AST id for a property that is used in simulate instructions.
2684 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2686 // Unlike other AST nodes, this number of bailout IDs allocated for an
2687 // ClassLiteral can vary, so num_ids() is not a static method.
2688 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2690 // Object literals need one feedback slot for each non-trivial value, as well
2691 // as some slots for home objects.
2692 FeedbackVectorRequirements ComputeFeedbackRequirements(
2693 Isolate* isolate, const ICSlotCache* cache) override;
2694 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2695 ICSlotCache* cache) override {
2698 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2699 FeedbackVectorICSlot GetNthSlot(int n) const {
2700 return FeedbackVectorICSlot(slot_.ToInt() + n);
2703 // If value needs a home object, returns a valid feedback vector ic slot
2704 // given by slot_index, and increments slot_index.
2705 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2706 int* slot_index) const;
2709 int slot_count() const { return slot_count_; }
2713 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2714 VariableProxy* class_variable_proxy, Expression* extends,
2715 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2716 int start_position, int end_position)
2717 : Expression(zone, start_position),
2720 class_variable_proxy_(class_variable_proxy),
2722 constructor_(constructor),
2723 properties_(properties),
2724 end_position_(end_position),
2728 slot_(FeedbackVectorICSlot::Invalid()) {
2731 static int parent_num_ids() { return Expression::num_ids(); }
2734 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2736 const AstRawString* raw_name_;
2738 VariableProxy* class_variable_proxy_;
2739 Expression* extends_;
2740 FunctionLiteral* constructor_;
2741 ZoneList<Property*>* properties_;
2744 // slot_count_ helps validate that the logic to allocate ic slots and the
2745 // logic to use them are in sync.
2748 FeedbackVectorICSlot slot_;
2752 class NativeFunctionLiteral final : public Expression {
2754 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2756 Handle<String> name() const { return name_->string(); }
2757 v8::Extension* extension() const { return extension_; }
2760 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2761 v8::Extension* extension, int pos)
2762 : Expression(zone, pos), name_(name), extension_(extension) {}
2765 const AstRawString* name_;
2766 v8::Extension* extension_;
2770 class ThisFunction final : public Expression {
2772 DECLARE_NODE_TYPE(ThisFunction)
2775 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2779 class SuperPropertyReference final : public Expression {
2781 DECLARE_NODE_TYPE(SuperPropertyReference)
2783 VariableProxy* this_var() const { return this_var_; }
2784 Expression* home_object() const { return home_object_; }
2787 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2788 Expression* home_object, int pos)
2789 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2790 DCHECK(this_var->is_this());
2791 DCHECK(home_object->IsProperty());
2795 VariableProxy* this_var_;
2796 Expression* home_object_;
2800 class SuperCallReference final : public Expression {
2802 DECLARE_NODE_TYPE(SuperCallReference)
2804 VariableProxy* this_var() const { return this_var_; }
2805 VariableProxy* new_target_var() const { return new_target_var_; }
2806 VariableProxy* this_function_var() const { return this_function_var_; }
2809 SuperCallReference(Zone* zone, VariableProxy* this_var,
2810 VariableProxy* new_target_var,
2811 VariableProxy* this_function_var, int pos)
2812 : Expression(zone, pos),
2813 this_var_(this_var),
2814 new_target_var_(new_target_var),
2815 this_function_var_(this_function_var) {
2816 DCHECK(this_var->is_this());
2817 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2818 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2822 VariableProxy* this_var_;
2823 VariableProxy* new_target_var_;
2824 VariableProxy* this_function_var_;
2828 #undef DECLARE_NODE_TYPE
2831 // ----------------------------------------------------------------------------
2832 // Regular expressions
2835 class RegExpVisitor BASE_EMBEDDED {
2837 virtual ~RegExpVisitor() { }
2838 #define MAKE_CASE(Name) \
2839 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2840 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2845 class RegExpTree : public ZoneObject {
2847 static const int kInfinity = kMaxInt;
2848 virtual ~RegExpTree() {}
2849 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2850 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2851 RegExpNode* on_success) = 0;
2852 virtual bool IsTextElement() { return false; }
2853 virtual bool IsAnchoredAtStart() { return false; }
2854 virtual bool IsAnchoredAtEnd() { return false; }
2855 virtual int min_match() = 0;
2856 virtual int max_match() = 0;
2857 // Returns the interval of registers used for captures within this
2859 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2860 virtual void AppendToText(RegExpText* text, Zone* zone);
2861 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2862 #define MAKE_ASTYPE(Name) \
2863 virtual RegExp##Name* As##Name(); \
2864 virtual bool Is##Name();
2865 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2870 class RegExpDisjunction final : public RegExpTree {
2872 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2873 void* Accept(RegExpVisitor* visitor, void* data) override;
2874 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2875 RegExpNode* on_success) override;
2876 RegExpDisjunction* AsDisjunction() override;
2877 Interval CaptureRegisters() override;
2878 bool IsDisjunction() override;
2879 bool IsAnchoredAtStart() override;
2880 bool IsAnchoredAtEnd() override;
2881 int min_match() override { return min_match_; }
2882 int max_match() override { return max_match_; }
2883 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2885 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2886 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2887 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2888 ZoneList<RegExpTree*>* alternatives_;
2894 class RegExpAlternative final : public RegExpTree {
2896 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2897 void* Accept(RegExpVisitor* visitor, void* data) override;
2898 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2899 RegExpNode* on_success) override;
2900 RegExpAlternative* AsAlternative() override;
2901 Interval CaptureRegisters() override;
2902 bool IsAlternative() override;
2903 bool IsAnchoredAtStart() override;
2904 bool IsAnchoredAtEnd() override;
2905 int min_match() override { return min_match_; }
2906 int max_match() override { return max_match_; }
2907 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2909 ZoneList<RegExpTree*>* nodes_;
2915 class RegExpAssertion final : public RegExpTree {
2917 enum AssertionType {
2925 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2926 void* Accept(RegExpVisitor* visitor, void* data) override;
2927 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2928 RegExpNode* on_success) override;
2929 RegExpAssertion* AsAssertion() override;
2930 bool IsAssertion() override;
2931 bool IsAnchoredAtStart() override;
2932 bool IsAnchoredAtEnd() override;
2933 int min_match() override { return 0; }
2934 int max_match() override { return 0; }
2935 AssertionType assertion_type() { return assertion_type_; }
2937 AssertionType assertion_type_;
2941 class CharacterSet final BASE_EMBEDDED {
2943 explicit CharacterSet(uc16 standard_set_type)
2945 standard_set_type_(standard_set_type) {}
2946 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2948 standard_set_type_(0) {}
2949 ZoneList<CharacterRange>* ranges(Zone* zone);
2950 uc16 standard_set_type() { return standard_set_type_; }
2951 void set_standard_set_type(uc16 special_set_type) {
2952 standard_set_type_ = special_set_type;
2954 bool is_standard() { return standard_set_type_ != 0; }
2955 void Canonicalize();
2957 ZoneList<CharacterRange>* ranges_;
2958 // If non-zero, the value represents a standard set (e.g., all whitespace
2959 // characters) without having to expand the ranges.
2960 uc16 standard_set_type_;
2964 class RegExpCharacterClass final : public RegExpTree {
2966 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2968 is_negated_(is_negated) { }
2969 explicit RegExpCharacterClass(uc16 type)
2971 is_negated_(false) { }
2972 void* Accept(RegExpVisitor* visitor, void* data) override;
2973 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2974 RegExpNode* on_success) override;
2975 RegExpCharacterClass* AsCharacterClass() override;
2976 bool IsCharacterClass() override;
2977 bool IsTextElement() override { return true; }
2978 int min_match() override { return 1; }
2979 int max_match() override { return 1; }
2980 void AppendToText(RegExpText* text, Zone* zone) override;
2981 CharacterSet character_set() { return set_; }
2982 // TODO(lrn): Remove need for complex version if is_standard that
2983 // recognizes a mangled standard set and just do { return set_.is_special(); }
2984 bool is_standard(Zone* zone);
2985 // Returns a value representing the standard character set if is_standard()
2987 // Currently used values are:
2988 // s : unicode whitespace
2989 // S : unicode non-whitespace
2990 // w : ASCII word character (digit, letter, underscore)
2991 // W : non-ASCII word character
2993 // D : non-ASCII digit
2994 // . : non-unicode non-newline
2995 // * : All characters
2996 uc16 standard_type() { return set_.standard_set_type(); }
2997 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
2998 bool is_negated() { return is_negated_; }
3006 class RegExpAtom final : public RegExpTree {
3008 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3009 void* Accept(RegExpVisitor* visitor, void* data) override;
3010 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3011 RegExpNode* on_success) override;
3012 RegExpAtom* AsAtom() override;
3013 bool IsAtom() override;
3014 bool IsTextElement() override { return true; }
3015 int min_match() override { return data_.length(); }
3016 int max_match() override { return data_.length(); }
3017 void AppendToText(RegExpText* text, Zone* zone) override;
3018 Vector<const uc16> data() { return data_; }
3019 int length() { return data_.length(); }
3021 Vector<const uc16> data_;
3025 class RegExpText final : public RegExpTree {
3027 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3028 void* Accept(RegExpVisitor* visitor, void* data) override;
3029 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3030 RegExpNode* on_success) override;
3031 RegExpText* AsText() override;
3032 bool IsText() override;
3033 bool IsTextElement() override { return true; }
3034 int min_match() override { return length_; }
3035 int max_match() override { return length_; }
3036 void AppendToText(RegExpText* text, Zone* zone) override;
3037 void AddElement(TextElement elm, Zone* zone) {
3038 elements_.Add(elm, zone);
3039 length_ += elm.length();
3041 ZoneList<TextElement>* elements() { return &elements_; }
3043 ZoneList<TextElement> elements_;
3048 class RegExpQuantifier final : public RegExpTree {
3050 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3051 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3055 min_match_(min * body->min_match()),
3056 quantifier_type_(type) {
3057 if (max > 0 && body->max_match() > kInfinity / max) {
3058 max_match_ = kInfinity;
3060 max_match_ = max * body->max_match();
3063 void* Accept(RegExpVisitor* visitor, void* data) override;
3064 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3065 RegExpNode* on_success) override;
3066 static RegExpNode* ToNode(int min,
3070 RegExpCompiler* compiler,
3071 RegExpNode* on_success,
3072 bool not_at_start = false);
3073 RegExpQuantifier* AsQuantifier() override;
3074 Interval CaptureRegisters() override;
3075 bool IsQuantifier() override;
3076 int min_match() override { return min_match_; }
3077 int max_match() override { return max_match_; }
3078 int min() { return min_; }
3079 int max() { return max_; }
3080 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3081 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3082 bool is_greedy() { return quantifier_type_ == GREEDY; }
3083 RegExpTree* body() { return body_; }
3091 QuantifierType quantifier_type_;
3095 class RegExpCapture final : public RegExpTree {
3097 explicit RegExpCapture(RegExpTree* body, int index)
3098 : body_(body), index_(index) { }
3099 void* Accept(RegExpVisitor* visitor, void* data) override;
3100 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3101 RegExpNode* on_success) override;
3102 static RegExpNode* ToNode(RegExpTree* body,
3104 RegExpCompiler* compiler,
3105 RegExpNode* on_success);
3106 RegExpCapture* AsCapture() override;
3107 bool IsAnchoredAtStart() override;
3108 bool IsAnchoredAtEnd() override;
3109 Interval CaptureRegisters() override;
3110 bool IsCapture() override;
3111 int min_match() override { return body_->min_match(); }
3112 int max_match() override { return body_->max_match(); }
3113 RegExpTree* body() { return body_; }
3114 int index() { return index_; }
3115 static int StartRegister(int index) { return index * 2; }
3116 static int EndRegister(int index) { return index * 2 + 1; }
3124 class RegExpLookahead final : public RegExpTree {
3126 RegExpLookahead(RegExpTree* body,
3131 is_positive_(is_positive),
3132 capture_count_(capture_count),
3133 capture_from_(capture_from) { }
3135 void* Accept(RegExpVisitor* visitor, void* data) override;
3136 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3137 RegExpNode* on_success) override;
3138 RegExpLookahead* AsLookahead() override;
3139 Interval CaptureRegisters() override;
3140 bool IsLookahead() override;
3141 bool IsAnchoredAtStart() override;
3142 int min_match() override { return 0; }
3143 int max_match() override { return 0; }
3144 RegExpTree* body() { return body_; }
3145 bool is_positive() { return is_positive_; }
3146 int capture_count() { return capture_count_; }
3147 int capture_from() { return capture_from_; }
3157 class RegExpBackReference final : public RegExpTree {
3159 explicit RegExpBackReference(RegExpCapture* capture)
3160 : capture_(capture) { }
3161 void* Accept(RegExpVisitor* visitor, void* data) override;
3162 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3163 RegExpNode* on_success) override;
3164 RegExpBackReference* AsBackReference() override;
3165 bool IsBackReference() override;
3166 int min_match() override { return 0; }
3167 int max_match() override { return capture_->max_match(); }
3168 int index() { return capture_->index(); }
3169 RegExpCapture* capture() { return capture_; }
3171 RegExpCapture* capture_;
3175 class RegExpEmpty final : public RegExpTree {
3178 void* Accept(RegExpVisitor* visitor, void* data) override;
3179 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3180 RegExpNode* on_success) override;
3181 RegExpEmpty* AsEmpty() override;
3182 bool IsEmpty() override;
3183 int min_match() override { return 0; }
3184 int max_match() override { return 0; }
3188 // ----------------------------------------------------------------------------
3190 // - leaf node visitors are abstract.
3192 class AstVisitor BASE_EMBEDDED {
3195 virtual ~AstVisitor() {}
3197 // Stack overflow check and dynamic dispatch.
3198 virtual void Visit(AstNode* node) = 0;
3200 // Iteration left-to-right.
3201 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3202 virtual void VisitStatements(ZoneList<Statement*>* statements);
3203 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3205 // Individual AST nodes.
3206 #define DEF_VISIT(type) \
3207 virtual void Visit##type(type* node) = 0;
3208 AST_NODE_LIST(DEF_VISIT)
3213 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3215 void Visit(AstNode* node) final { \
3216 if (!CheckStackOverflow()) node->Accept(this); \
3219 void SetStackOverflow() { stack_overflow_ = true; } \
3220 void ClearStackOverflow() { stack_overflow_ = false; } \
3221 bool HasStackOverflow() const { return stack_overflow_; } \
3223 bool CheckStackOverflow() { \
3224 if (stack_overflow_) return true; \
3225 StackLimitCheck check(isolate_); \
3226 if (!check.HasOverflowed()) return false; \
3227 stack_overflow_ = true; \
3232 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3233 isolate_ = isolate; \
3235 stack_overflow_ = false; \
3237 Zone* zone() { return zone_; } \
3238 Isolate* isolate() { return isolate_; } \
3240 Isolate* isolate_; \
3242 bool stack_overflow_
3245 // ----------------------------------------------------------------------------
3248 class AstNodeFactory final BASE_EMBEDDED {
3250 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3251 : zone_(ast_value_factory->zone()),
3252 ast_value_factory_(ast_value_factory) {}
3254 VariableDeclaration* NewVariableDeclaration(
3255 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3256 bool is_class_declaration = false, int declaration_group_start = -1) {
3258 VariableDeclaration(zone_, proxy, mode, scope, pos,
3259 is_class_declaration, declaration_group_start);
3262 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3264 FunctionLiteral* fun,
3267 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3270 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3271 const AstRawString* import_name,
3272 const AstRawString* module_specifier,
3273 Scope* scope, int pos) {
3274 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3275 module_specifier, scope, pos);
3278 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3281 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3284 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3285 bool ignore_completion_value, int pos) {
3287 Block(zone_, labels, capacity, ignore_completion_value, pos);
3290 #define STATEMENT_WITH_LABELS(NodeType) \
3291 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3292 return new (zone_) NodeType(zone_, labels, pos); \
3294 STATEMENT_WITH_LABELS(DoWhileStatement)
3295 STATEMENT_WITH_LABELS(WhileStatement)
3296 STATEMENT_WITH_LABELS(ForStatement)
3297 STATEMENT_WITH_LABELS(SwitchStatement)
3298 #undef STATEMENT_WITH_LABELS
3300 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3301 ZoneList<const AstRawString*>* labels,
3303 switch (visit_mode) {
3304 case ForEachStatement::ENUMERATE: {
3305 return new (zone_) ForInStatement(zone_, labels, pos);
3307 case ForEachStatement::ITERATE: {
3308 return new (zone_) ForOfStatement(zone_, labels, pos);
3315 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3316 return new (zone_) ExpressionStatement(zone_, expression, pos);
3319 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3320 return new (zone_) ContinueStatement(zone_, target, pos);
3323 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3324 return new (zone_) BreakStatement(zone_, target, pos);
3327 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3328 return new (zone_) ReturnStatement(zone_, expression, pos);
3331 WithStatement* NewWithStatement(Scope* scope,
3332 Expression* expression,
3333 Statement* statement,
3335 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3338 IfStatement* NewIfStatement(Expression* condition,
3339 Statement* then_statement,
3340 Statement* else_statement,
3343 IfStatement(zone_, condition, then_statement, else_statement, pos);
3346 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3348 Block* catch_block, int pos) {
3350 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3353 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3354 Block* finally_block, int pos) {
3356 TryFinallyStatement(zone_, try_block, finally_block, pos);
3359 DebuggerStatement* NewDebuggerStatement(int pos) {
3360 return new (zone_) DebuggerStatement(zone_, pos);
3363 EmptyStatement* NewEmptyStatement(int pos) {
3364 return new(zone_) EmptyStatement(zone_, pos);
3367 CaseClause* NewCaseClause(
3368 Expression* label, ZoneList<Statement*>* statements, int pos) {
3369 return new (zone_) CaseClause(zone_, label, statements, pos);
3372 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3374 Literal(zone_, ast_value_factory_->NewString(string), pos);
3377 // A JavaScript symbol (ECMA-262 edition 6).
3378 Literal* NewSymbolLiteral(const char* name, int pos) {
3379 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3382 Literal* NewNumberLiteral(double number, int pos) {
3384 Literal(zone_, ast_value_factory_->NewNumber(number), pos);
3387 Literal* NewSmiLiteral(int number, int pos) {
3388 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3391 Literal* NewBooleanLiteral(bool b, int pos) {
3392 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3395 Literal* NewNullLiteral(int pos) {
3396 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3399 Literal* NewUndefinedLiteral(int pos) {
3400 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3403 Literal* NewTheHoleLiteral(int pos) {
3404 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3407 ObjectLiteral* NewObjectLiteral(
3408 ZoneList<ObjectLiteral::Property*>* properties,
3410 int boilerplate_properties,
3414 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3415 boilerplate_properties, has_function,
3419 ObjectLiteral::Property* NewObjectLiteralProperty(
3420 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3421 bool is_static, bool is_computed_name) {
3423 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3426 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3429 bool is_computed_name) {
3430 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3431 is_static, is_computed_name);
3434 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3435 const AstRawString* flags,
3439 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3443 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3447 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3451 VariableProxy* NewVariableProxy(Variable* var,
3452 int start_position = RelocInfo::kNoPosition,
3453 int end_position = RelocInfo::kNoPosition) {
3454 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3457 VariableProxy* NewVariableProxy(const AstRawString* name,
3458 Variable::Kind variable_kind,
3459 int start_position = RelocInfo::kNoPosition,
3460 int end_position = RelocInfo::kNoPosition) {
3461 DCHECK_NOT_NULL(name);
3463 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3466 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3467 return new (zone_) Property(zone_, obj, key, pos);
3470 Call* NewCall(Expression* expression,
3471 ZoneList<Expression*>* arguments,
3473 return new (zone_) Call(zone_, expression, arguments, pos);
3476 CallNew* NewCallNew(Expression* expression,
3477 ZoneList<Expression*>* arguments,
3479 return new (zone_) CallNew(zone_, expression, arguments, pos);
3482 CallRuntime* NewCallRuntime(const AstRawString* name,
3483 const Runtime::Function* function,
3484 ZoneList<Expression*>* arguments,
3486 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3489 UnaryOperation* NewUnaryOperation(Token::Value op,
3490 Expression* expression,
3492 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3495 BinaryOperation* NewBinaryOperation(Token::Value op,
3499 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3502 CountOperation* NewCountOperation(Token::Value op,
3506 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3509 CompareOperation* NewCompareOperation(Token::Value op,
3513 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3516 Spread* NewSpread(Expression* expression, int pos) {
3517 return new (zone_) Spread(zone_, expression, pos);
3520 Conditional* NewConditional(Expression* condition,
3521 Expression* then_expression,
3522 Expression* else_expression,
3524 return new (zone_) Conditional(zone_, condition, then_expression,
3525 else_expression, position);
3528 Assignment* NewAssignment(Token::Value op,
3532 DCHECK(Token::IsAssignmentOp(op));
3533 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3534 if (assign->is_compound()) {
3535 DCHECK(Token::IsAssignmentOp(op));
3536 assign->binary_operation_ =
3537 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3542 Yield* NewYield(Expression *generator_object,
3543 Expression* expression,
3544 Yield::Kind yield_kind,
3546 if (!expression) expression = NewUndefinedLiteral(pos);
3548 Yield(zone_, generator_object, expression, yield_kind, pos);
3551 Throw* NewThrow(Expression* exception, int pos) {
3552 return new (zone_) Throw(zone_, exception, pos);
3555 FunctionLiteral* NewFunctionLiteral(
3556 const AstRawString* name, AstValueFactory* ast_value_factory,
3557 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3558 int expected_property_count, int parameter_count,
3559 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3560 FunctionLiteral::FunctionType function_type,
3561 FunctionLiteral::IsFunctionFlag is_function,
3562 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3564 return new (zone_) FunctionLiteral(
3565 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3566 expected_property_count, parameter_count, function_type,
3567 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3571 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3572 VariableProxy* proxy, Expression* extends,
3573 FunctionLiteral* constructor,
3574 ZoneList<ObjectLiteral::Property*>* properties,
3575 int start_position, int end_position) {
3577 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3578 properties, start_position, end_position);
3581 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3582 v8::Extension* extension,
3584 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3587 ThisFunction* NewThisFunction(int pos) {
3588 return new (zone_) ThisFunction(zone_, pos);
3591 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3592 Expression* home_object,
3595 SuperPropertyReference(zone_, this_var, home_object, pos);
3598 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3599 VariableProxy* new_target_var,
3600 VariableProxy* this_function_var,
3602 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3603 this_function_var, pos);
3608 AstValueFactory* ast_value_factory_;
3612 } } // namespace v8::internal