1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/assembler.h"
11 #include "src/ast-value-factory.h"
12 #include "src/bailout-reason.h"
13 #include "src/base/flags.h"
14 #include "src/base/smart-pointers.h"
15 #include "src/factory.h"
16 #include "src/isolate.h"
17 #include "src/jsregexp.h"
18 #include "src/list-inl.h"
19 #include "src/modules.h"
20 #include "src/runtime/runtime.h"
21 #include "src/small-pointer-list.h"
22 #include "src/token.h"
23 #include "src/types.h"
24 #include "src/utils.h"
25 #include "src/variables.h"
30 // The abstract syntax tree is an intermediate, light-weight
31 // representation of the parsed JavaScript code suitable for
32 // compilation to native code.
34 // Nodes are allocated in a separate zone, which allows faster
35 // allocation and constant-time deallocation of the entire syntax
39 // ----------------------------------------------------------------------------
40 // Nodes of the abstract syntax tree. Only concrete classes are
43 #define DECLARATION_NODE_LIST(V) \
44 V(VariableDeclaration) \
45 V(FunctionDeclaration) \
46 V(ImportDeclaration) \
49 #define STATEMENT_NODE_LIST(V) \
51 V(ExpressionStatement) \
54 V(ContinueStatement) \
64 V(TryCatchStatement) \
65 V(TryFinallyStatement) \
68 #define EXPRESSION_NODE_LIST(V) \
71 V(NativeFunctionLiteral) \
91 V(SuperPropertyReference) \
92 V(SuperCallReference) \
95 #define AST_NODE_LIST(V) \
96 DECLARATION_NODE_LIST(V) \
97 STATEMENT_NODE_LIST(V) \
98 EXPRESSION_NODE_LIST(V)
100 // Forward declarations
101 class AstNodeFactory;
105 class BreakableStatement;
107 class IterationStatement;
108 class MaterializedLiteral;
110 class TypeFeedbackOracle;
112 class RegExpAlternative;
113 class RegExpAssertion;
115 class RegExpBackReference;
117 class RegExpCharacterClass;
118 class RegExpCompiler;
119 class RegExpDisjunction;
121 class RegExpLookahead;
122 class RegExpQuantifier;
125 #define DEF_FORWARD_DECLARATION(type) class type;
126 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
127 #undef DEF_FORWARD_DECLARATION
130 // Typedef only introduced to avoid unreadable code.
131 typedef ZoneList<Handle<String>> ZoneStringList;
132 typedef ZoneList<Handle<Object>> ZoneObjectList;
135 #define DECLARE_NODE_TYPE(type) \
136 void Accept(AstVisitor* v) override; \
137 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
138 friend class AstNodeFactory;
141 class FeedbackVectorRequirements {
143 FeedbackVectorRequirements(int slots, int ic_slots)
144 : slots_(slots), ic_slots_(ic_slots) {}
146 int slots() const { return slots_; }
147 int ic_slots() const { return ic_slots_; }
155 class VariableICSlotPair final {
157 VariableICSlotPair(Variable* variable, FeedbackVectorICSlot slot)
158 : variable_(variable), slot_(slot) {}
160 : variable_(NULL), slot_(FeedbackVectorICSlot::Invalid()) {}
162 Variable* variable() const { return variable_; }
163 FeedbackVectorICSlot slot() const { return slot_; }
167 FeedbackVectorICSlot slot_;
171 typedef List<VariableICSlotPair> ICSlotCache;
174 class AstProperties final BASE_EMBEDDED {
178 kDontSelfOptimize = 1 << 0,
179 kDontCrankshaft = 1 << 1
182 typedef base::Flags<Flag> Flags;
184 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
186 Flags& flags() { return flags_; }
187 Flags flags() const { return flags_; }
188 int node_count() { return node_count_; }
189 void add_node_count(int count) { node_count_ += count; }
191 int slots() const { return spec_.slots(); }
192 void increase_slots(int count) { spec_.increase_slots(count); }
194 int ic_slots() const { return spec_.ic_slots(); }
195 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
196 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
197 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
202 ZoneFeedbackVectorSpec spec_;
205 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
208 class AstNode: public ZoneObject {
210 #define DECLARE_TYPE_ENUM(type) k##type,
212 AST_NODE_LIST(DECLARE_TYPE_ENUM)
215 #undef DECLARE_TYPE_ENUM
217 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
219 explicit AstNode(int position): position_(position) {}
220 virtual ~AstNode() {}
222 virtual void Accept(AstVisitor* v) = 0;
223 virtual NodeType node_type() const = 0;
224 int position() const { return position_; }
226 // Type testing & conversion functions overridden by concrete subclasses.
227 #define DECLARE_NODE_FUNCTIONS(type) \
228 bool Is##type() const { return node_type() == AstNode::k##type; } \
230 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
232 const type* As##type() const { \
233 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
235 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
236 #undef DECLARE_NODE_FUNCTIONS
238 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
239 virtual IterationStatement* AsIterationStatement() { return NULL; }
240 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
242 // The interface for feedback slots, with default no-op implementations for
243 // node types which don't actually have this. Note that this is conceptually
244 // not really nice, but multiple inheritance would introduce yet another
245 // vtable entry per node, something we don't want for space reasons.
246 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
247 Isolate* isolate, const ICSlotCache* cache) {
248 return FeedbackVectorRequirements(0, 0);
250 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
251 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
252 ICSlotCache* cache) {
255 // Each ICSlot stores a kind of IC which the participating node should know.
256 virtual Code::Kind FeedbackICSlotKind(int index) {
258 return Code::NUMBER_OF_KINDS;
262 // Hidden to prevent accidental usage. It would have to load the
263 // current zone from the TLS.
264 void* operator new(size_t size);
266 friend class CaseClause; // Generates AST IDs.
272 class Statement : public AstNode {
274 explicit Statement(Zone* zone, int position) : AstNode(position) {}
276 bool IsEmpty() { return AsEmptyStatement() != NULL; }
277 virtual bool IsJump() const { return false; }
281 class SmallMapList final {
284 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
286 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
287 void Clear() { list_.Clear(); }
288 void Sort() { list_.Sort(); }
290 bool is_empty() const { return list_.is_empty(); }
291 int length() const { return list_.length(); }
293 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
294 if (!Map::TryUpdate(map).ToHandle(&map)) return;
295 for (int i = 0; i < length(); ++i) {
296 if (at(i).is_identical_to(map)) return;
301 void FilterForPossibleTransitions(Map* root_map) {
302 for (int i = list_.length() - 1; i >= 0; i--) {
303 if (at(i)->FindRootMap() != root_map) {
304 list_.RemoveElement(list_.at(i));
309 void Add(Handle<Map> handle, Zone* zone) {
310 list_.Add(handle.location(), zone);
313 Handle<Map> at(int i) const {
314 return Handle<Map>(list_.at(i));
317 Handle<Map> first() const { return at(0); }
318 Handle<Map> last() const { return at(length() - 1); }
321 // The list stores pointers to Map*, that is Map**, so it's GC safe.
322 SmallPointerList<Map*> list_;
324 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
328 class Expression : public AstNode {
331 // Not assigned a context yet, or else will not be visited during
334 // Evaluated for its side effects.
336 // Evaluated for its value (and side effects).
338 // Evaluated for control flow (and side effects).
342 virtual bool IsValidReferenceExpression() const { return false; }
344 // Helpers for ToBoolean conversion.
345 virtual bool ToBooleanIsTrue() const { return false; }
346 virtual bool ToBooleanIsFalse() const { return false; }
348 // Symbols that cannot be parsed as array indices are considered property
349 // names. We do not treat symbols that can be array indexes as property
350 // names because [] for string objects is handled only by keyed ICs.
351 virtual bool IsPropertyName() const { return false; }
353 // True iff the expression is a literal represented as a smi.
354 bool IsSmiLiteral() const;
356 // True iff the expression is a string literal.
357 bool IsStringLiteral() const;
359 // True iff the expression is the null literal.
360 bool IsNullLiteral() const;
362 // True if we can prove that the expression is the undefined literal.
363 bool IsUndefinedLiteral(Isolate* isolate) const;
365 // Expression type bounds
366 Bounds bounds() const { return bounds_; }
367 void set_bounds(Bounds bounds) { bounds_ = bounds; }
369 // Type feedback information for assignments and properties.
370 virtual bool IsMonomorphic() {
374 virtual SmallMapList* GetReceiverTypes() {
378 virtual KeyedAccessStoreMode GetStoreMode() const {
380 return STANDARD_STORE;
382 virtual IcCheckType GetKeyType() const {
387 // TODO(rossberg): this should move to its own AST node eventually.
388 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
389 uint16_t to_boolean_types() const {
390 return ToBooleanTypesField::decode(bit_field_);
393 void set_base_id(int id) { base_id_ = id; }
394 static int num_ids() { return parent_num_ids() + 2; }
395 BailoutId id() const { return BailoutId(local_id(0)); }
396 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
399 Expression(Zone* zone, int pos)
401 base_id_(BailoutId::None().ToInt()),
402 bounds_(Bounds::Unbounded(zone)),
404 static int parent_num_ids() { return 0; }
405 void set_to_boolean_types(uint16_t types) {
406 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
409 int base_id() const {
410 DCHECK(!BailoutId(base_id_).IsNone());
415 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
419 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
421 // Ends with 16-bit field; deriving classes in turn begin with
422 // 16-bit fields for optimum packing efficiency.
426 class BreakableStatement : public Statement {
429 TARGET_FOR_ANONYMOUS,
430 TARGET_FOR_NAMED_ONLY
433 // The labels associated with this statement. May be NULL;
434 // if it is != NULL, guaranteed to contain at least one entry.
435 ZoneList<const AstRawString*>* labels() const { return labels_; }
437 // Type testing & conversion.
438 BreakableStatement* AsBreakableStatement() final { return this; }
441 Label* break_target() { return &break_target_; }
444 bool is_target_for_anonymous() const {
445 return breakable_type_ == TARGET_FOR_ANONYMOUS;
448 void set_base_id(int id) { base_id_ = id; }
449 static int num_ids() { return parent_num_ids() + 2; }
450 BailoutId EntryId() const { return BailoutId(local_id(0)); }
451 BailoutId ExitId() const { return BailoutId(local_id(1)); }
454 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
455 BreakableType breakable_type, int position)
456 : Statement(zone, position),
458 breakable_type_(breakable_type),
459 base_id_(BailoutId::None().ToInt()) {
460 DCHECK(labels == NULL || labels->length() > 0);
462 static int parent_num_ids() { return 0; }
464 int base_id() const {
465 DCHECK(!BailoutId(base_id_).IsNone());
470 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
472 ZoneList<const AstRawString*>* labels_;
473 BreakableType breakable_type_;
479 class Block final : public BreakableStatement {
481 DECLARE_NODE_TYPE(Block)
483 void AddStatement(Statement* statement, Zone* zone) {
484 statements_.Add(statement, zone);
487 ZoneList<Statement*>* statements() { return &statements_; }
488 bool ignore_completion_value() const { return ignore_completion_value_; }
490 static int num_ids() { return parent_num_ids() + 1; }
491 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
493 bool IsJump() const override {
494 return !statements_.is_empty() && statements_.last()->IsJump()
495 && labels() == NULL; // Good enough as an approximation...
498 Scope* scope() const { return scope_; }
499 void set_scope(Scope* scope) { scope_ = scope; }
502 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
503 bool ignore_completion_value, int pos)
504 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
505 statements_(capacity, zone),
506 ignore_completion_value_(ignore_completion_value),
508 static int parent_num_ids() { return BreakableStatement::num_ids(); }
511 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
513 ZoneList<Statement*> statements_;
514 bool ignore_completion_value_;
519 class Declaration : public AstNode {
521 VariableProxy* proxy() const { return proxy_; }
522 VariableMode mode() const { return mode_; }
523 Scope* scope() const { return scope_; }
524 virtual InitializationFlag initialization() const = 0;
525 virtual bool IsInlineable() const;
528 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
530 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
531 DCHECK(IsDeclaredVariableMode(mode));
536 VariableProxy* proxy_;
538 // Nested scope from which the declaration originated.
543 class VariableDeclaration final : public Declaration {
545 DECLARE_NODE_TYPE(VariableDeclaration)
547 InitializationFlag initialization() const override {
548 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
551 bool is_class_declaration() const { return is_class_declaration_; }
553 // VariableDeclarations can be grouped into consecutive declaration
554 // groups. Each VariableDeclaration is associated with the start position of
555 // the group it belongs to. The positions are used for strong mode scope
556 // checks for classes and functions.
557 int declaration_group_start() const { return declaration_group_start_; }
560 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
561 Scope* scope, int pos, bool is_class_declaration = false,
562 int declaration_group_start = -1)
563 : Declaration(zone, proxy, mode, scope, pos),
564 is_class_declaration_(is_class_declaration),
565 declaration_group_start_(declaration_group_start) {}
567 bool is_class_declaration_;
568 int declaration_group_start_;
572 class FunctionDeclaration final : public Declaration {
574 DECLARE_NODE_TYPE(FunctionDeclaration)
576 FunctionLiteral* fun() const { return fun_; }
577 InitializationFlag initialization() const override {
578 return kCreatedInitialized;
580 bool IsInlineable() const override;
583 FunctionDeclaration(Zone* zone,
584 VariableProxy* proxy,
586 FunctionLiteral* fun,
589 : Declaration(zone, proxy, mode, scope, pos),
591 DCHECK(mode == VAR || mode == LET || mode == CONST);
596 FunctionLiteral* fun_;
600 class ImportDeclaration final : public Declaration {
602 DECLARE_NODE_TYPE(ImportDeclaration)
604 const AstRawString* import_name() const { return import_name_; }
605 const AstRawString* module_specifier() const { return module_specifier_; }
606 void set_module_specifier(const AstRawString* module_specifier) {
607 DCHECK(module_specifier_ == NULL);
608 module_specifier_ = module_specifier;
610 InitializationFlag initialization() const override {
611 return kNeedsInitialization;
615 ImportDeclaration(Zone* zone, VariableProxy* proxy,
616 const AstRawString* import_name,
617 const AstRawString* module_specifier, Scope* scope, int pos)
618 : Declaration(zone, proxy, IMPORT, scope, pos),
619 import_name_(import_name),
620 module_specifier_(module_specifier) {}
623 const AstRawString* import_name_;
624 const AstRawString* module_specifier_;
628 class ExportDeclaration final : public Declaration {
630 DECLARE_NODE_TYPE(ExportDeclaration)
632 InitializationFlag initialization() const override {
633 return kCreatedInitialized;
637 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
638 : Declaration(zone, proxy, LET, scope, pos) {}
642 class Module : public AstNode {
644 ModuleDescriptor* descriptor() const { return descriptor_; }
645 Block* body() const { return body_; }
648 Module(Zone* zone, int pos)
649 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
650 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
651 : AstNode(pos), descriptor_(descriptor), body_(body) {}
654 ModuleDescriptor* descriptor_;
659 class IterationStatement : public BreakableStatement {
661 // Type testing & conversion.
662 IterationStatement* AsIterationStatement() final { return this; }
664 Statement* body() const { return body_; }
666 static int num_ids() { return parent_num_ids() + 1; }
667 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
668 virtual BailoutId ContinueId() const = 0;
669 virtual BailoutId StackCheckId() const = 0;
672 Label* continue_target() { return &continue_target_; }
675 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
676 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
678 static int parent_num_ids() { return BreakableStatement::num_ids(); }
679 void Initialize(Statement* body) { body_ = body; }
682 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
685 Label continue_target_;
689 class DoWhileStatement final : public IterationStatement {
691 DECLARE_NODE_TYPE(DoWhileStatement)
693 void Initialize(Expression* cond, Statement* body) {
694 IterationStatement::Initialize(body);
698 Expression* cond() const { return cond_; }
700 static int num_ids() { return parent_num_ids() + 2; }
701 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
702 BailoutId StackCheckId() const override { return BackEdgeId(); }
703 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
706 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
707 : IterationStatement(zone, labels, pos), cond_(NULL) {}
708 static int parent_num_ids() { return IterationStatement::num_ids(); }
711 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
717 class WhileStatement final : public IterationStatement {
719 DECLARE_NODE_TYPE(WhileStatement)
721 void Initialize(Expression* cond, Statement* body) {
722 IterationStatement::Initialize(body);
726 Expression* cond() const { return cond_; }
728 static int num_ids() { return parent_num_ids() + 1; }
729 BailoutId ContinueId() const override { return EntryId(); }
730 BailoutId StackCheckId() const override { return BodyId(); }
731 BailoutId BodyId() const { return BailoutId(local_id(0)); }
734 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
735 : IterationStatement(zone, labels, pos), cond_(NULL) {}
736 static int parent_num_ids() { return IterationStatement::num_ids(); }
739 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
745 class ForStatement final : public IterationStatement {
747 DECLARE_NODE_TYPE(ForStatement)
749 void Initialize(Statement* init,
753 IterationStatement::Initialize(body);
759 Statement* init() const { return init_; }
760 Expression* cond() const { return cond_; }
761 Statement* next() const { return next_; }
763 static int num_ids() { return parent_num_ids() + 2; }
764 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
765 BailoutId StackCheckId() const override { return BodyId(); }
766 BailoutId BodyId() const { return BailoutId(local_id(1)); }
769 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
770 : IterationStatement(zone, labels, pos),
774 static int parent_num_ids() { return IterationStatement::num_ids(); }
777 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
785 class ForEachStatement : public IterationStatement {
788 ENUMERATE, // for (each in subject) body;
789 ITERATE // for (each of subject) body;
792 void Initialize(Expression* each, Expression* subject, Statement* body) {
793 IterationStatement::Initialize(body);
798 Expression* each() const { return each_; }
799 Expression* subject() const { return subject_; }
801 FeedbackVectorRequirements ComputeFeedbackRequirements(
802 Isolate* isolate, const ICSlotCache* cache) override;
803 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
804 ICSlotCache* cache) override {
807 Code::Kind FeedbackICSlotKind(int index) override;
808 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
811 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
812 : IterationStatement(zone, labels, pos),
815 each_slot_(FeedbackVectorICSlot::Invalid()) {}
819 Expression* subject_;
820 FeedbackVectorICSlot each_slot_;
824 class ForInStatement final : public ForEachStatement {
826 DECLARE_NODE_TYPE(ForInStatement)
828 Expression* enumerable() const {
832 // Type feedback information.
833 FeedbackVectorRequirements ComputeFeedbackRequirements(
834 Isolate* isolate, const ICSlotCache* cache) override {
835 FeedbackVectorRequirements base =
836 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
837 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
838 return FeedbackVectorRequirements(1, base.ic_slots());
840 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
841 for_in_feedback_slot_ = slot;
844 FeedbackVectorSlot ForInFeedbackSlot() {
845 DCHECK(!for_in_feedback_slot_.IsInvalid());
846 return for_in_feedback_slot_;
849 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
850 ForInType for_in_type() const { return for_in_type_; }
851 void set_for_in_type(ForInType type) { for_in_type_ = type; }
853 static int num_ids() { return parent_num_ids() + 6; }
854 BailoutId BodyId() const { return BailoutId(local_id(0)); }
855 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
856 BailoutId EnumId() const { return BailoutId(local_id(2)); }
857 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
858 BailoutId FilterId() const { return BailoutId(local_id(4)); }
859 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
860 BailoutId ContinueId() const override { return EntryId(); }
861 BailoutId StackCheckId() const override { return BodyId(); }
864 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
865 : ForEachStatement(zone, labels, pos),
866 for_in_type_(SLOW_FOR_IN),
867 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
868 static int parent_num_ids() { return ForEachStatement::num_ids(); }
871 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
873 ForInType for_in_type_;
874 FeedbackVectorSlot for_in_feedback_slot_;
878 class ForOfStatement final : public ForEachStatement {
880 DECLARE_NODE_TYPE(ForOfStatement)
882 void Initialize(Expression* each,
885 Expression* assign_iterator,
886 Expression* next_result,
887 Expression* result_done,
888 Expression* assign_each) {
889 ForEachStatement::Initialize(each, subject, body);
890 assign_iterator_ = assign_iterator;
891 next_result_ = next_result;
892 result_done_ = result_done;
893 assign_each_ = assign_each;
896 Expression* iterable() const {
900 // iterator = subject[Symbol.iterator]()
901 Expression* assign_iterator() const {
902 return assign_iterator_;
905 // result = iterator.next() // with type check
906 Expression* next_result() const {
911 Expression* result_done() const {
915 // each = result.value
916 Expression* assign_each() const {
920 BailoutId ContinueId() const override { return EntryId(); }
921 BailoutId StackCheckId() const override { return BackEdgeId(); }
923 static int num_ids() { return parent_num_ids() + 1; }
924 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
927 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
928 : ForEachStatement(zone, labels, pos),
929 assign_iterator_(NULL),
932 assign_each_(NULL) {}
933 static int parent_num_ids() { return ForEachStatement::num_ids(); }
936 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
938 Expression* assign_iterator_;
939 Expression* next_result_;
940 Expression* result_done_;
941 Expression* assign_each_;
945 class ExpressionStatement final : public Statement {
947 DECLARE_NODE_TYPE(ExpressionStatement)
949 void set_expression(Expression* e) { expression_ = e; }
950 Expression* expression() const { return expression_; }
951 bool IsJump() const override { return expression_->IsThrow(); }
954 ExpressionStatement(Zone* zone, Expression* expression, int pos)
955 : Statement(zone, pos), expression_(expression) { }
958 Expression* expression_;
962 class JumpStatement : public Statement {
964 bool IsJump() const final { return true; }
967 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
971 class ContinueStatement final : public JumpStatement {
973 DECLARE_NODE_TYPE(ContinueStatement)
975 IterationStatement* target() const { return target_; }
978 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
979 : JumpStatement(zone, pos), target_(target) { }
982 IterationStatement* target_;
986 class BreakStatement final : public JumpStatement {
988 DECLARE_NODE_TYPE(BreakStatement)
990 BreakableStatement* target() const { return target_; }
993 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
994 : JumpStatement(zone, pos), target_(target) { }
997 BreakableStatement* target_;
1001 class ReturnStatement final : public JumpStatement {
1003 DECLARE_NODE_TYPE(ReturnStatement)
1005 Expression* expression() const { return expression_; }
1008 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1009 : JumpStatement(zone, pos), expression_(expression) { }
1012 Expression* expression_;
1016 class WithStatement final : public Statement {
1018 DECLARE_NODE_TYPE(WithStatement)
1020 Scope* scope() { return scope_; }
1021 Expression* expression() const { return expression_; }
1022 Statement* statement() const { return statement_; }
1024 void set_base_id(int id) { base_id_ = id; }
1025 static int num_ids() { return parent_num_ids() + 1; }
1026 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1029 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1030 Statement* statement, int pos)
1031 : Statement(zone, pos),
1033 expression_(expression),
1034 statement_(statement),
1035 base_id_(BailoutId::None().ToInt()) {}
1036 static int parent_num_ids() { return 0; }
1038 int base_id() const {
1039 DCHECK(!BailoutId(base_id_).IsNone());
1044 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1047 Expression* expression_;
1048 Statement* statement_;
1053 class CaseClause final : public Expression {
1055 DECLARE_NODE_TYPE(CaseClause)
1057 bool is_default() const { return label_ == NULL; }
1058 Expression* label() const {
1059 CHECK(!is_default());
1062 Label* body_target() { return &body_target_; }
1063 ZoneList<Statement*>* statements() const { return statements_; }
1065 static int num_ids() { return parent_num_ids() + 2; }
1066 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1067 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1069 Type* compare_type() { return compare_type_; }
1070 void set_compare_type(Type* type) { compare_type_ = type; }
1073 static int parent_num_ids() { return Expression::num_ids(); }
1076 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1078 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1082 ZoneList<Statement*>* statements_;
1083 Type* compare_type_;
1087 class SwitchStatement final : public BreakableStatement {
1089 DECLARE_NODE_TYPE(SwitchStatement)
1091 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1096 Expression* tag() const { return tag_; }
1097 ZoneList<CaseClause*>* cases() const { return cases_; }
1100 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1101 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1107 ZoneList<CaseClause*>* cases_;
1111 // If-statements always have non-null references to their then- and
1112 // else-parts. When parsing if-statements with no explicit else-part,
1113 // the parser implicitly creates an empty statement. Use the
1114 // HasThenStatement() and HasElseStatement() functions to check if a
1115 // given if-statement has a then- or an else-part containing code.
1116 class IfStatement final : public Statement {
1118 DECLARE_NODE_TYPE(IfStatement)
1120 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1121 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1123 Expression* condition() const { return condition_; }
1124 Statement* then_statement() const { return then_statement_; }
1125 Statement* else_statement() const { return else_statement_; }
1127 bool IsJump() const override {
1128 return HasThenStatement() && then_statement()->IsJump()
1129 && HasElseStatement() && else_statement()->IsJump();
1132 void set_base_id(int id) { base_id_ = id; }
1133 static int num_ids() { return parent_num_ids() + 3; }
1134 BailoutId IfId() const { return BailoutId(local_id(0)); }
1135 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1136 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1139 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1140 Statement* else_statement, int pos)
1141 : Statement(zone, pos),
1142 condition_(condition),
1143 then_statement_(then_statement),
1144 else_statement_(else_statement),
1145 base_id_(BailoutId::None().ToInt()) {}
1146 static int parent_num_ids() { return 0; }
1148 int base_id() const {
1149 DCHECK(!BailoutId(base_id_).IsNone());
1154 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1156 Expression* condition_;
1157 Statement* then_statement_;
1158 Statement* else_statement_;
1163 class TryStatement : public Statement {
1165 Block* try_block() const { return try_block_; }
1167 void set_base_id(int id) { base_id_ = id; }
1168 static int num_ids() { return parent_num_ids() + 1; }
1169 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1172 TryStatement(Zone* zone, Block* try_block, int pos)
1173 : Statement(zone, pos),
1174 try_block_(try_block),
1175 base_id_(BailoutId::None().ToInt()) {}
1176 static int parent_num_ids() { return 0; }
1178 int base_id() const {
1179 DCHECK(!BailoutId(base_id_).IsNone());
1184 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1191 class TryCatchStatement final : public TryStatement {
1193 DECLARE_NODE_TYPE(TryCatchStatement)
1195 Scope* scope() { return scope_; }
1196 Variable* variable() { return variable_; }
1197 Block* catch_block() const { return catch_block_; }
1200 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1201 Variable* variable, Block* catch_block, int pos)
1202 : TryStatement(zone, try_block, pos),
1204 variable_(variable),
1205 catch_block_(catch_block) {}
1209 Variable* variable_;
1210 Block* catch_block_;
1214 class TryFinallyStatement final : public TryStatement {
1216 DECLARE_NODE_TYPE(TryFinallyStatement)
1218 Block* finally_block() const { return finally_block_; }
1221 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1223 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1226 Block* finally_block_;
1230 class DebuggerStatement final : public Statement {
1232 DECLARE_NODE_TYPE(DebuggerStatement)
1234 void set_base_id(int id) { base_id_ = id; }
1235 static int num_ids() { return parent_num_ids() + 1; }
1236 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1239 explicit DebuggerStatement(Zone* zone, int pos)
1240 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1241 static int parent_num_ids() { return 0; }
1243 int base_id() const {
1244 DCHECK(!BailoutId(base_id_).IsNone());
1249 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1255 class EmptyStatement final : public Statement {
1257 DECLARE_NODE_TYPE(EmptyStatement)
1260 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1264 class Literal final : public Expression {
1266 DECLARE_NODE_TYPE(Literal)
1268 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1270 Handle<String> AsPropertyName() {
1271 DCHECK(IsPropertyName());
1272 return Handle<String>::cast(value());
1275 const AstRawString* AsRawPropertyName() {
1276 DCHECK(IsPropertyName());
1277 return value_->AsString();
1280 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1281 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1283 Handle<Object> value() const { return value_->value(); }
1284 const AstValue* raw_value() const { return value_; }
1286 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1287 // only for string and number literals!
1289 static bool Match(void* literal1, void* literal2);
1291 static int num_ids() { return parent_num_ids() + 1; }
1292 TypeFeedbackId LiteralFeedbackId() const {
1293 return TypeFeedbackId(local_id(0));
1297 Literal(Zone* zone, const AstValue* value, int position)
1298 : Expression(zone, position), value_(value) {}
1299 static int parent_num_ids() { return Expression::num_ids(); }
1302 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1304 const AstValue* value_;
1308 class AstLiteralReindexer;
1310 // Base class for literals that needs space in the corresponding JSFunction.
1311 class MaterializedLiteral : public Expression {
1313 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1315 int literal_index() { return literal_index_; }
1318 // only callable after initialization.
1319 DCHECK(depth_ >= 1);
1323 bool is_strong() const { return is_strong_; }
1326 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1327 : Expression(zone, pos),
1328 literal_index_(literal_index),
1330 is_strong_(is_strong),
1333 // A materialized literal is simple if the values consist of only
1334 // constants and simple object and array literals.
1335 bool is_simple() const { return is_simple_; }
1336 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1337 friend class CompileTimeValue;
1339 void set_depth(int depth) {
1344 // Populate the constant properties/elements fixed array.
1345 void BuildConstants(Isolate* isolate);
1346 friend class ArrayLiteral;
1347 friend class ObjectLiteral;
1349 // If the expression is a literal, return the literal value;
1350 // if the expression is a materialized literal and is simple return a
1351 // compile time value as encoded by CompileTimeValue::GetValue().
1352 // Otherwise, return undefined literal as the placeholder
1353 // in the object literal boilerplate.
1354 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1362 friend class AstLiteralReindexer;
1366 // Property is used for passing information
1367 // about an object literal's properties from the parser
1368 // to the code generator.
1369 class ObjectLiteralProperty final : public ZoneObject {
1372 CONSTANT, // Property with constant value (compile time).
1373 COMPUTED, // Property with computed value (execution time).
1374 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1375 GETTER, SETTER, // Property is an accessor function.
1376 PROTOTYPE // Property is __proto__.
1379 Expression* key() { return key_; }
1380 Expression* value() { return value_; }
1381 Kind kind() { return kind_; }
1383 // Type feedback information.
1384 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1385 Handle<Map> GetReceiverType() { return receiver_type_; }
1387 bool IsCompileTimeValue();
1389 void set_emit_store(bool emit_store);
1392 bool is_static() const { return is_static_; }
1393 bool is_computed_name() const { return is_computed_name_; }
1395 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1398 friend class AstNodeFactory;
1400 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1401 bool is_static, bool is_computed_name);
1402 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1403 Expression* value, bool is_static,
1404 bool is_computed_name);
1412 bool is_computed_name_;
1413 Handle<Map> receiver_type_;
1417 // An object literal has a boilerplate object that is used
1418 // for minimizing the work when constructing it at runtime.
1419 class ObjectLiteral final : public MaterializedLiteral {
1421 typedef ObjectLiteralProperty Property;
1423 DECLARE_NODE_TYPE(ObjectLiteral)
1425 Handle<FixedArray> constant_properties() const {
1426 return constant_properties_;
1428 int properties_count() const { return constant_properties_->length() / 2; }
1429 ZoneList<Property*>* properties() const { return properties_; }
1430 bool fast_elements() const { return fast_elements_; }
1431 bool may_store_doubles() const { return may_store_doubles_; }
1432 bool has_function() const { return has_function_; }
1433 bool has_elements() const { return has_elements_; }
1435 // Decide if a property should be in the object boilerplate.
1436 static bool IsBoilerplateProperty(Property* property);
1438 // Populate the constant properties fixed array.
1439 void BuildConstantProperties(Isolate* isolate);
1441 // Mark all computed expressions that are bound to a key that
1442 // is shadowed by a later occurrence of the same key. For the
1443 // marked expressions, no store code is emitted.
1444 void CalculateEmitStore(Zone* zone);
1446 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1447 int ComputeFlags(bool disable_mementos = false) const {
1448 int flags = fast_elements() ? kFastElements : kNoFlags;
1449 flags |= has_function() ? kHasFunction : kNoFlags;
1450 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1451 flags |= kShallowProperties;
1453 if (disable_mementos) {
1454 flags |= kDisableMementos;
1465 kHasFunction = 1 << 1,
1466 kShallowProperties = 1 << 2,
1467 kDisableMementos = 1 << 3,
1471 struct Accessors: public ZoneObject {
1472 Accessors() : getter(NULL), setter(NULL) {}
1477 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1479 // Return an AST id for a property that is used in simulate instructions.
1480 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1482 // Unlike other AST nodes, this number of bailout IDs allocated for an
1483 // ObjectLiteral can vary, so num_ids() is not a static method.
1484 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1486 // Object literals need one feedback slot for each non-trivial value, as well
1487 // as some slots for home objects.
1488 FeedbackVectorRequirements ComputeFeedbackRequirements(
1489 Isolate* isolate, const ICSlotCache* cache) override;
1490 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1491 ICSlotCache* cache) override {
1494 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1495 FeedbackVectorICSlot GetNthSlot(int n) const {
1496 return FeedbackVectorICSlot(slot_.ToInt() + n);
1499 // If value needs a home object, returns a valid feedback vector ic slot
1500 // given by slot_index, and increments slot_index.
1501 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1502 int* slot_index) const;
1505 int slot_count() const { return slot_count_; }
1509 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1510 int boilerplate_properties, bool has_function, bool is_strong,
1512 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1513 properties_(properties),
1514 boilerplate_properties_(boilerplate_properties),
1515 fast_elements_(false),
1516 has_elements_(false),
1517 may_store_doubles_(false),
1518 has_function_(has_function),
1522 slot_(FeedbackVectorICSlot::Invalid()) {
1524 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1527 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1528 Handle<FixedArray> constant_properties_;
1529 ZoneList<Property*>* properties_;
1530 int boilerplate_properties_;
1531 bool fast_elements_;
1533 bool may_store_doubles_;
1536 // slot_count_ helps validate that the logic to allocate ic slots and the
1537 // logic to use them are in sync.
1540 FeedbackVectorICSlot slot_;
1544 // Node for capturing a regexp literal.
1545 class RegExpLiteral final : public MaterializedLiteral {
1547 DECLARE_NODE_TYPE(RegExpLiteral)
1549 Handle<String> pattern() const { return pattern_->string(); }
1550 Handle<String> flags() const { return flags_->string(); }
1553 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1554 const AstRawString* flags, int literal_index, bool is_strong,
1556 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1563 const AstRawString* pattern_;
1564 const AstRawString* flags_;
1568 // An array literal has a literals object that is used
1569 // for minimizing the work when constructing it at runtime.
1570 class ArrayLiteral final : public MaterializedLiteral {
1572 DECLARE_NODE_TYPE(ArrayLiteral)
1574 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1575 ElementsKind constant_elements_kind() const {
1576 DCHECK_EQ(2, constant_elements_->length());
1577 return static_cast<ElementsKind>(
1578 Smi::cast(constant_elements_->get(0))->value());
1581 ZoneList<Expression*>* values() const { return values_; }
1583 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1585 // Return an AST id for an element that is used in simulate instructions.
1586 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1588 // Unlike other AST nodes, this number of bailout IDs allocated for an
1589 // ArrayLiteral can vary, so num_ids() is not a static method.
1590 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1592 // Populate the constant elements fixed array.
1593 void BuildConstantElements(Isolate* isolate);
1595 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1596 int ComputeFlags(bool disable_mementos = false) const {
1597 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1598 if (disable_mementos) {
1599 flags |= kDisableMementos;
1609 kShallowElements = 1,
1610 kDisableMementos = 1 << 1,
1615 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values, int literal_index,
1616 bool is_strong, int pos)
1617 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1619 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1622 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1624 Handle<FixedArray> constant_elements_;
1625 ZoneList<Expression*>* values_;
1629 class VariableProxy final : public Expression {
1631 DECLARE_NODE_TYPE(VariableProxy)
1633 bool IsValidReferenceExpression() const override { return !is_this(); }
1635 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1637 Handle<String> name() const { return raw_name()->string(); }
1638 const AstRawString* raw_name() const {
1639 return is_resolved() ? var_->raw_name() : raw_name_;
1642 Variable* var() const {
1643 DCHECK(is_resolved());
1646 void set_var(Variable* v) {
1647 DCHECK(!is_resolved());
1652 bool is_this() const { return IsThisField::decode(bit_field_); }
1654 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1655 void set_is_assigned() {
1656 bit_field_ = IsAssignedField::update(bit_field_, true);
1659 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1660 void set_is_resolved() {
1661 bit_field_ = IsResolvedField::update(bit_field_, true);
1664 int end_position() const { return end_position_; }
1666 // Bind this proxy to the variable var.
1667 void BindTo(Variable* var);
1669 bool UsesVariableFeedbackSlot() const {
1670 return var()->IsUnallocated() || var()->IsLookupSlot();
1673 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1674 Isolate* isolate, const ICSlotCache* cache) override;
1676 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1677 ICSlotCache* cache) override;
1678 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1679 FeedbackVectorICSlot VariableFeedbackSlot() {
1680 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1681 return variable_feedback_slot_;
1684 static int num_ids() { return parent_num_ids() + 1; }
1685 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1688 VariableProxy(Zone* zone, Variable* var, int start_position,
1691 VariableProxy(Zone* zone, const AstRawString* name,
1692 Variable::Kind variable_kind, int start_position,
1694 static int parent_num_ids() { return Expression::num_ids(); }
1695 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1697 class IsThisField : public BitField8<bool, 0, 1> {};
1698 class IsAssignedField : public BitField8<bool, 1, 1> {};
1699 class IsResolvedField : public BitField8<bool, 2, 1> {};
1701 // Start with 16-bit (or smaller) field, which should get packed together
1702 // with Expression's trailing 16-bit field.
1704 FeedbackVectorICSlot variable_feedback_slot_;
1706 const AstRawString* raw_name_; // if !is_resolved_
1707 Variable* var_; // if is_resolved_
1709 // Position is stored in the AstNode superclass, but VariableProxy needs to
1710 // know its end position too (for error messages). It cannot be inferred from
1711 // the variable name length because it can contain escapes.
1716 // Left-hand side can only be a property, a global or a (parameter or local)
1722 NAMED_SUPER_PROPERTY,
1723 KEYED_SUPER_PROPERTY
1727 class Property final : public Expression {
1729 DECLARE_NODE_TYPE(Property)
1731 bool IsValidReferenceExpression() const override { return true; }
1733 Expression* obj() const { return obj_; }
1734 Expression* key() const { return key_; }
1736 static int num_ids() { return parent_num_ids() + 1; }
1737 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1739 bool IsStringAccess() const {
1740 return IsStringAccessField::decode(bit_field_);
1743 // Type feedback information.
1744 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1745 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1746 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1747 IcCheckType GetKeyType() const override {
1748 return KeyTypeField::decode(bit_field_);
1750 bool IsUninitialized() const {
1751 return !is_for_call() && HasNoTypeInformation();
1753 bool HasNoTypeInformation() const {
1754 return GetInlineCacheState() == UNINITIALIZED;
1756 InlineCacheState GetInlineCacheState() const {
1757 return InlineCacheStateField::decode(bit_field_);
1759 void set_is_string_access(bool b) {
1760 bit_field_ = IsStringAccessField::update(bit_field_, b);
1762 void set_key_type(IcCheckType key_type) {
1763 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1765 void set_inline_cache_state(InlineCacheState state) {
1766 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1768 void mark_for_call() {
1769 bit_field_ = IsForCallField::update(bit_field_, true);
1771 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1773 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1775 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1776 Isolate* isolate, const ICSlotCache* cache) override {
1777 return FeedbackVectorRequirements(0, 1);
1779 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1780 ICSlotCache* cache) override {
1781 property_feedback_slot_ = slot;
1783 Code::Kind FeedbackICSlotKind(int index) override {
1784 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1787 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1788 DCHECK(!property_feedback_slot_.IsInvalid());
1789 return property_feedback_slot_;
1792 static LhsKind GetAssignType(Property* property) {
1793 if (property == NULL) return VARIABLE;
1794 bool super_access = property->IsSuperAccess();
1795 return (property->key()->IsPropertyName())
1796 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1797 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1801 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1802 : Expression(zone, pos),
1803 bit_field_(IsForCallField::encode(false) |
1804 IsStringAccessField::encode(false) |
1805 InlineCacheStateField::encode(UNINITIALIZED)),
1806 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1809 static int parent_num_ids() { return Expression::num_ids(); }
1812 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1814 class IsForCallField : public BitField8<bool, 0, 1> {};
1815 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1816 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1817 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1819 FeedbackVectorICSlot property_feedback_slot_;
1822 SmallMapList receiver_types_;
1826 class Call final : public Expression {
1828 DECLARE_NODE_TYPE(Call)
1830 Expression* expression() const { return expression_; }
1831 ZoneList<Expression*>* arguments() const { return arguments_; }
1833 // Type feedback information.
1834 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1835 Isolate* isolate, const ICSlotCache* cache) override;
1836 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1837 ICSlotCache* cache) override {
1840 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1841 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1843 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1845 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1847 SmallMapList* GetReceiverTypes() override {
1848 if (expression()->IsProperty()) {
1849 return expression()->AsProperty()->GetReceiverTypes();
1854 bool IsMonomorphic() override {
1855 if (expression()->IsProperty()) {
1856 return expression()->AsProperty()->IsMonomorphic();
1858 return !target_.is_null();
1861 bool global_call() const {
1862 VariableProxy* proxy = expression_->AsVariableProxy();
1863 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1866 bool known_global_function() const {
1867 return global_call() && !target_.is_null();
1870 Handle<JSFunction> target() { return target_; }
1872 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1874 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1876 set_is_uninitialized(false);
1878 void set_target(Handle<JSFunction> target) { target_ = target; }
1879 void set_allocation_site(Handle<AllocationSite> site) {
1880 allocation_site_ = site;
1883 static int num_ids() { return parent_num_ids() + 3; }
1884 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1885 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1886 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1888 bool is_uninitialized() const {
1889 return IsUninitializedField::decode(bit_field_);
1891 void set_is_uninitialized(bool b) {
1892 bit_field_ = IsUninitializedField::update(bit_field_, b);
1904 // Helpers to determine how to handle the call.
1905 CallType GetCallType(Isolate* isolate) const;
1906 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1907 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1910 // Used to assert that the FullCodeGenerator records the return site.
1911 bool return_is_recorded_;
1915 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1917 : Expression(zone, pos),
1918 ic_slot_(FeedbackVectorICSlot::Invalid()),
1919 slot_(FeedbackVectorSlot::Invalid()),
1920 expression_(expression),
1921 arguments_(arguments),
1922 bit_field_(IsUninitializedField::encode(false)) {
1923 if (expression->IsProperty()) {
1924 expression->AsProperty()->mark_for_call();
1927 static int parent_num_ids() { return Expression::num_ids(); }
1930 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1932 FeedbackVectorICSlot ic_slot_;
1933 FeedbackVectorSlot slot_;
1934 Expression* expression_;
1935 ZoneList<Expression*>* arguments_;
1936 Handle<JSFunction> target_;
1937 Handle<AllocationSite> allocation_site_;
1938 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1943 class CallNew final : public Expression {
1945 DECLARE_NODE_TYPE(CallNew)
1947 Expression* expression() const { return expression_; }
1948 ZoneList<Expression*>* arguments() const { return arguments_; }
1950 // Type feedback information.
1951 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1952 Isolate* isolate, const ICSlotCache* cache) override {
1953 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1955 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1956 callnew_feedback_slot_ = slot;
1959 FeedbackVectorSlot CallNewFeedbackSlot() {
1960 DCHECK(!callnew_feedback_slot_.IsInvalid());
1961 return callnew_feedback_slot_;
1963 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1964 DCHECK(FLAG_pretenuring_call_new);
1965 return CallNewFeedbackSlot().next();
1968 bool IsMonomorphic() override { return is_monomorphic_; }
1969 Handle<JSFunction> target() const { return target_; }
1970 Handle<AllocationSite> allocation_site() const {
1971 return allocation_site_;
1974 static int num_ids() { return parent_num_ids() + 1; }
1975 static int feedback_slots() { return 1; }
1976 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1978 void set_allocation_site(Handle<AllocationSite> site) {
1979 allocation_site_ = site;
1981 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1982 void set_target(Handle<JSFunction> target) { target_ = target; }
1983 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1985 is_monomorphic_ = true;
1989 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1991 : Expression(zone, pos),
1992 expression_(expression),
1993 arguments_(arguments),
1994 is_monomorphic_(false),
1995 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
1997 static int parent_num_ids() { return Expression::num_ids(); }
2000 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2002 Expression* expression_;
2003 ZoneList<Expression*>* arguments_;
2004 bool is_monomorphic_;
2005 Handle<JSFunction> target_;
2006 Handle<AllocationSite> allocation_site_;
2007 FeedbackVectorSlot callnew_feedback_slot_;
2011 // The CallRuntime class does not represent any official JavaScript
2012 // language construct. Instead it is used to call a C or JS function
2013 // with a set of arguments. This is used from the builtins that are
2014 // implemented in JavaScript (see "v8natives.js").
2015 class CallRuntime final : public Expression {
2017 DECLARE_NODE_TYPE(CallRuntime)
2019 Handle<String> name() const { return raw_name_->string(); }
2020 const AstRawString* raw_name() const { return raw_name_; }
2021 const Runtime::Function* function() const { return function_; }
2022 ZoneList<Expression*>* arguments() const { return arguments_; }
2023 bool is_jsruntime() const { return function_ == NULL; }
2025 // Type feedback information.
2026 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2027 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2028 Isolate* isolate, const ICSlotCache* cache) override {
2029 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2031 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2032 ICSlotCache* cache) override {
2033 callruntime_feedback_slot_ = slot;
2035 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2037 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2038 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2039 !callruntime_feedback_slot_.IsInvalid());
2040 return callruntime_feedback_slot_;
2043 static int num_ids() { return parent_num_ids() + 1; }
2044 BailoutId CallId() { return BailoutId(local_id(0)); }
2047 CallRuntime(Zone* zone, const AstRawString* name,
2048 const Runtime::Function* function,
2049 ZoneList<Expression*>* arguments, int pos)
2050 : Expression(zone, pos),
2052 function_(function),
2053 arguments_(arguments),
2054 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2055 static int parent_num_ids() { return Expression::num_ids(); }
2058 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2060 const AstRawString* raw_name_;
2061 const Runtime::Function* function_;
2062 ZoneList<Expression*>* arguments_;
2063 FeedbackVectorICSlot callruntime_feedback_slot_;
2067 class UnaryOperation final : public Expression {
2069 DECLARE_NODE_TYPE(UnaryOperation)
2071 Token::Value op() const { return op_; }
2072 Expression* expression() const { return expression_; }
2074 // For unary not (Token::NOT), the AST ids where true and false will
2075 // actually be materialized, respectively.
2076 static int num_ids() { return parent_num_ids() + 2; }
2077 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2078 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2080 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2083 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2084 : Expression(zone, pos), op_(op), expression_(expression) {
2085 DCHECK(Token::IsUnaryOp(op));
2087 static int parent_num_ids() { return Expression::num_ids(); }
2090 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2093 Expression* expression_;
2097 class BinaryOperation final : public Expression {
2099 DECLARE_NODE_TYPE(BinaryOperation)
2101 Token::Value op() const { return static_cast<Token::Value>(op_); }
2102 Expression* left() const { return left_; }
2103 Expression* right() const { return right_; }
2104 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2105 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2106 allocation_site_ = allocation_site;
2109 // The short-circuit logical operations need an AST ID for their
2110 // right-hand subexpression.
2111 static int num_ids() { return parent_num_ids() + 2; }
2112 BailoutId RightId() const { return BailoutId(local_id(0)); }
2114 TypeFeedbackId BinaryOperationFeedbackId() const {
2115 return TypeFeedbackId(local_id(1));
2117 Maybe<int> fixed_right_arg() const {
2118 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2120 void set_fixed_right_arg(Maybe<int> arg) {
2121 has_fixed_right_arg_ = arg.IsJust();
2122 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2125 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2128 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2129 Expression* right, int pos)
2130 : Expression(zone, pos),
2131 op_(static_cast<byte>(op)),
2132 has_fixed_right_arg_(false),
2133 fixed_right_arg_value_(0),
2136 DCHECK(Token::IsBinaryOp(op));
2138 static int parent_num_ids() { return Expression::num_ids(); }
2141 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2143 const byte op_; // actually Token::Value
2144 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2145 // type for the RHS. Currenty it's actually a Maybe<int>
2146 bool has_fixed_right_arg_;
2147 int fixed_right_arg_value_;
2150 Handle<AllocationSite> allocation_site_;
2154 class CountOperation final : public Expression {
2156 DECLARE_NODE_TYPE(CountOperation)
2158 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2159 bool is_postfix() const { return !is_prefix(); }
2161 Token::Value op() const { return TokenField::decode(bit_field_); }
2162 Token::Value binary_op() {
2163 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2166 Expression* expression() const { return expression_; }
2168 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2169 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2170 IcCheckType GetKeyType() const override {
2171 return KeyTypeField::decode(bit_field_);
2173 KeyedAccessStoreMode GetStoreMode() const override {
2174 return StoreModeField::decode(bit_field_);
2176 Type* type() const { return type_; }
2177 void set_key_type(IcCheckType type) {
2178 bit_field_ = KeyTypeField::update(bit_field_, type);
2180 void set_store_mode(KeyedAccessStoreMode mode) {
2181 bit_field_ = StoreModeField::update(bit_field_, mode);
2183 void set_type(Type* type) { type_ = type; }
2185 static int num_ids() { return parent_num_ids() + 4; }
2186 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2187 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2188 TypeFeedbackId CountBinOpFeedbackId() const {
2189 return TypeFeedbackId(local_id(2));
2191 TypeFeedbackId CountStoreFeedbackId() const {
2192 return TypeFeedbackId(local_id(3));
2195 FeedbackVectorRequirements ComputeFeedbackRequirements(
2196 Isolate* isolate, const ICSlotCache* cache) override;
2197 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2198 ICSlotCache* cache) override {
2201 Code::Kind FeedbackICSlotKind(int index) override;
2202 FeedbackVectorICSlot CountSlot() const { return slot_; }
2205 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2207 : Expression(zone, pos),
2209 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2210 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2213 slot_(FeedbackVectorICSlot::Invalid()) {}
2214 static int parent_num_ids() { return Expression::num_ids(); }
2217 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2219 class IsPrefixField : public BitField16<bool, 0, 1> {};
2220 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2221 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2222 class TokenField : public BitField16<Token::Value, 6, 8> {};
2224 // Starts with 16-bit field, which should get packed together with
2225 // Expression's trailing 16-bit field.
2226 uint16_t bit_field_;
2228 Expression* expression_;
2229 SmallMapList receiver_types_;
2230 FeedbackVectorICSlot slot_;
2234 class CompareOperation final : public Expression {
2236 DECLARE_NODE_TYPE(CompareOperation)
2238 Token::Value op() const { return op_; }
2239 Expression* left() const { return left_; }
2240 Expression* right() const { return right_; }
2242 // Type feedback information.
2243 static int num_ids() { return parent_num_ids() + 1; }
2244 TypeFeedbackId CompareOperationFeedbackId() const {
2245 return TypeFeedbackId(local_id(0));
2247 Type* combined_type() const { return combined_type_; }
2248 void set_combined_type(Type* type) { combined_type_ = type; }
2250 // Match special cases.
2251 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2252 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2253 bool IsLiteralCompareNull(Expression** expr);
2256 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2257 Expression* right, int pos)
2258 : Expression(zone, pos),
2262 combined_type_(Type::None(zone)) {
2263 DCHECK(Token::IsCompareOp(op));
2265 static int parent_num_ids() { return Expression::num_ids(); }
2268 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2274 Type* combined_type_;
2278 class Spread final : public Expression {
2280 DECLARE_NODE_TYPE(Spread)
2282 Expression* expression() const { return expression_; }
2284 static int num_ids() { return parent_num_ids(); }
2287 Spread(Zone* zone, Expression* expression, int pos)
2288 : Expression(zone, pos), expression_(expression) {}
2289 static int parent_num_ids() { return Expression::num_ids(); }
2292 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2294 Expression* expression_;
2298 class Conditional final : public Expression {
2300 DECLARE_NODE_TYPE(Conditional)
2302 Expression* condition() const { return condition_; }
2303 Expression* then_expression() const { return then_expression_; }
2304 Expression* else_expression() const { return else_expression_; }
2306 static int num_ids() { return parent_num_ids() + 2; }
2307 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2308 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2311 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2312 Expression* else_expression, int position)
2313 : Expression(zone, position),
2314 condition_(condition),
2315 then_expression_(then_expression),
2316 else_expression_(else_expression) {}
2317 static int parent_num_ids() { return Expression::num_ids(); }
2320 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2322 Expression* condition_;
2323 Expression* then_expression_;
2324 Expression* else_expression_;
2328 class Assignment final : public Expression {
2330 DECLARE_NODE_TYPE(Assignment)
2332 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2334 Token::Value binary_op() const;
2336 Token::Value op() const { return TokenField::decode(bit_field_); }
2337 Expression* target() const { return target_; }
2338 Expression* value() const { return value_; }
2339 BinaryOperation* binary_operation() const { return binary_operation_; }
2341 // This check relies on the definition order of token in token.h.
2342 bool is_compound() const { return op() > Token::ASSIGN; }
2344 static int num_ids() { return parent_num_ids() + 2; }
2345 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2347 // Type feedback information.
2348 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2349 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2350 bool IsUninitialized() const {
2351 return IsUninitializedField::decode(bit_field_);
2353 bool HasNoTypeInformation() {
2354 return IsUninitializedField::decode(bit_field_);
2356 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2357 IcCheckType GetKeyType() const override {
2358 return KeyTypeField::decode(bit_field_);
2360 KeyedAccessStoreMode GetStoreMode() const override {
2361 return StoreModeField::decode(bit_field_);
2363 void set_is_uninitialized(bool b) {
2364 bit_field_ = IsUninitializedField::update(bit_field_, b);
2366 void set_key_type(IcCheckType key_type) {
2367 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2369 void set_store_mode(KeyedAccessStoreMode mode) {
2370 bit_field_ = StoreModeField::update(bit_field_, mode);
2373 FeedbackVectorRequirements ComputeFeedbackRequirements(
2374 Isolate* isolate, const ICSlotCache* cache) override;
2375 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2376 ICSlotCache* cache) override {
2379 Code::Kind FeedbackICSlotKind(int index) override;
2380 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2383 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2385 static int parent_num_ids() { return Expression::num_ids(); }
2388 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2390 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2391 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2392 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2393 class TokenField : public BitField16<Token::Value, 6, 8> {};
2395 // Starts with 16-bit field, which should get packed together with
2396 // Expression's trailing 16-bit field.
2397 uint16_t bit_field_;
2398 Expression* target_;
2400 BinaryOperation* binary_operation_;
2401 SmallMapList receiver_types_;
2402 FeedbackVectorICSlot slot_;
2406 class Yield final : public Expression {
2408 DECLARE_NODE_TYPE(Yield)
2411 kInitial, // The initial yield that returns the unboxed generator object.
2412 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2413 kDelegating, // A yield*.
2414 kFinal // A return: { value: EXPRESSION, done: true }
2417 Expression* generator_object() const { return generator_object_; }
2418 Expression* expression() const { return expression_; }
2419 Kind yield_kind() const { return yield_kind_; }
2421 // Type feedback information.
2422 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2423 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2424 Isolate* isolate, const ICSlotCache* cache) override {
2425 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2427 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2428 ICSlotCache* cache) override {
2429 yield_first_feedback_slot_ = slot;
2431 Code::Kind FeedbackICSlotKind(int index) override {
2432 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2435 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2436 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2437 return yield_first_feedback_slot_;
2440 FeedbackVectorICSlot DoneFeedbackSlot() {
2441 return KeyedLoadFeedbackSlot().next();
2444 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2447 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2448 Kind yield_kind, int pos)
2449 : Expression(zone, pos),
2450 generator_object_(generator_object),
2451 expression_(expression),
2452 yield_kind_(yield_kind),
2453 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2456 Expression* generator_object_;
2457 Expression* expression_;
2459 FeedbackVectorICSlot yield_first_feedback_slot_;
2463 class Throw final : public Expression {
2465 DECLARE_NODE_TYPE(Throw)
2467 Expression* exception() const { return exception_; }
2470 Throw(Zone* zone, Expression* exception, int pos)
2471 : Expression(zone, pos), exception_(exception) {}
2474 Expression* exception_;
2478 class FunctionLiteral final : public Expression {
2481 ANONYMOUS_EXPRESSION,
2486 enum ParameterFlag {
2487 kNoDuplicateParameters = 0,
2488 kHasDuplicateParameters = 1
2491 enum IsFunctionFlag {
2496 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2498 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2500 enum ArityRestriction {
2506 DECLARE_NODE_TYPE(FunctionLiteral)
2508 Handle<String> name() const { return raw_name_->string(); }
2509 const AstRawString* raw_name() const { return raw_name_; }
2510 Scope* scope() const { return scope_; }
2511 ZoneList<Statement*>* body() const { return body_; }
2512 void set_function_token_position(int pos) { function_token_position_ = pos; }
2513 int function_token_position() const { return function_token_position_; }
2514 int start_position() const;
2515 int end_position() const;
2516 int SourceSize() const { return end_position() - start_position(); }
2517 bool is_expression() const { return IsExpression::decode(bitfield_); }
2518 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2519 LanguageMode language_mode() const;
2521 static bool NeedsHomeObject(Expression* expr);
2523 int materialized_literal_count() { return materialized_literal_count_; }
2524 int expected_property_count() { return expected_property_count_; }
2525 int parameter_count() { return parameter_count_; }
2527 bool AllowsLazyCompilation();
2528 bool AllowsLazyCompilationWithoutContext();
2530 Handle<String> debug_name() const {
2531 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2532 return raw_name_->string();
2534 return inferred_name();
2537 Handle<String> inferred_name() const {
2538 if (!inferred_name_.is_null()) {
2539 DCHECK(raw_inferred_name_ == NULL);
2540 return inferred_name_;
2542 if (raw_inferred_name_ != NULL) {
2543 return raw_inferred_name_->string();
2546 return Handle<String>();
2549 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2550 void set_inferred_name(Handle<String> inferred_name) {
2551 DCHECK(!inferred_name.is_null());
2552 inferred_name_ = inferred_name;
2553 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2554 raw_inferred_name_ = NULL;
2557 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2558 DCHECK(raw_inferred_name != NULL);
2559 raw_inferred_name_ = raw_inferred_name;
2560 DCHECK(inferred_name_.is_null());
2561 inferred_name_ = Handle<String>();
2564 bool pretenure() { return Pretenure::decode(bitfield_); }
2565 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2567 bool has_duplicate_parameters() {
2568 return HasDuplicateParameters::decode(bitfield_);
2571 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2573 // This is used as a heuristic on when to eagerly compile a function
2574 // literal. We consider the following constructs as hints that the
2575 // function will be called immediately:
2576 // - (function() { ... })();
2577 // - var x = function() { ... }();
2578 bool should_eager_compile() const {
2579 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2581 void set_should_eager_compile() {
2582 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2585 // A hint that we expect this function to be called (exactly) once,
2586 // i.e. we suspect it's an initialization function.
2587 bool should_be_used_once_hint() const {
2588 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2590 void set_should_be_used_once_hint() {
2591 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2594 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2596 int ast_node_count() { return ast_properties_.node_count(); }
2597 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2598 void set_ast_properties(AstProperties* ast_properties) {
2599 ast_properties_ = *ast_properties;
2601 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2602 return ast_properties_.get_spec();
2604 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2605 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2606 void set_dont_optimize_reason(BailoutReason reason) {
2607 dont_optimize_reason_ = reason;
2611 FunctionLiteral(Zone* zone, const AstRawString* name,
2612 AstValueFactory* ast_value_factory, Scope* scope,
2613 ZoneList<Statement*>* body, int materialized_literal_count,
2614 int expected_property_count, int parameter_count,
2615 FunctionType function_type,
2616 ParameterFlag has_duplicate_parameters,
2617 IsFunctionFlag is_function,
2618 EagerCompileHint eager_compile_hint, FunctionKind kind,
2620 : Expression(zone, position),
2624 raw_inferred_name_(ast_value_factory->empty_string()),
2625 ast_properties_(zone),
2626 dont_optimize_reason_(kNoReason),
2627 materialized_literal_count_(materialized_literal_count),
2628 expected_property_count_(expected_property_count),
2629 parameter_count_(parameter_count),
2630 function_token_position_(RelocInfo::kNoPosition) {
2631 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2632 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2633 Pretenure::encode(false) |
2634 HasDuplicateParameters::encode(has_duplicate_parameters) |
2635 IsFunction::encode(is_function) |
2636 EagerCompileHintBit::encode(eager_compile_hint) |
2637 FunctionKindBits::encode(kind) |
2638 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2639 DCHECK(IsValidFunctionKind(kind));
2643 const AstRawString* raw_name_;
2644 Handle<String> name_;
2646 ZoneList<Statement*>* body_;
2647 const AstString* raw_inferred_name_;
2648 Handle<String> inferred_name_;
2649 AstProperties ast_properties_;
2650 BailoutReason dont_optimize_reason_;
2652 int materialized_literal_count_;
2653 int expected_property_count_;
2654 int parameter_count_;
2655 int function_token_position_;
2658 class IsExpression : public BitField<bool, 0, 1> {};
2659 class IsAnonymous : public BitField<bool, 1, 1> {};
2660 class Pretenure : public BitField<bool, 2, 1> {};
2661 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2662 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2663 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2664 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2665 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2670 class ClassLiteral final : public Expression {
2672 typedef ObjectLiteralProperty Property;
2674 DECLARE_NODE_TYPE(ClassLiteral)
2676 Handle<String> name() const { return raw_name_->string(); }
2677 const AstRawString* raw_name() const { return raw_name_; }
2678 Scope* scope() const { return scope_; }
2679 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2680 Expression* extends() const { return extends_; }
2681 FunctionLiteral* constructor() const { return constructor_; }
2682 ZoneList<Property*>* properties() const { return properties_; }
2683 int start_position() const { return position(); }
2684 int end_position() const { return end_position_; }
2686 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2687 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2688 BailoutId ExitId() { return BailoutId(local_id(2)); }
2689 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2691 // Return an AST id for a property that is used in simulate instructions.
2692 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2694 // Unlike other AST nodes, this number of bailout IDs allocated for an
2695 // ClassLiteral can vary, so num_ids() is not a static method.
2696 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2698 // Object literals need one feedback slot for each non-trivial value, as well
2699 // as some slots for home objects.
2700 FeedbackVectorRequirements ComputeFeedbackRequirements(
2701 Isolate* isolate, const ICSlotCache* cache) override;
2702 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2703 ICSlotCache* cache) override {
2706 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2707 FeedbackVectorICSlot GetNthSlot(int n) const {
2708 return FeedbackVectorICSlot(slot_.ToInt() + n);
2711 // If value needs a home object, returns a valid feedback vector ic slot
2712 // given by slot_index, and increments slot_index.
2713 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2714 int* slot_index) const;
2717 int slot_count() const { return slot_count_; }
2721 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2722 VariableProxy* class_variable_proxy, Expression* extends,
2723 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2724 int start_position, int end_position)
2725 : Expression(zone, start_position),
2728 class_variable_proxy_(class_variable_proxy),
2730 constructor_(constructor),
2731 properties_(properties),
2732 end_position_(end_position),
2736 slot_(FeedbackVectorICSlot::Invalid()) {
2739 static int parent_num_ids() { return Expression::num_ids(); }
2742 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2744 const AstRawString* raw_name_;
2746 VariableProxy* class_variable_proxy_;
2747 Expression* extends_;
2748 FunctionLiteral* constructor_;
2749 ZoneList<Property*>* properties_;
2752 // slot_count_ helps validate that the logic to allocate ic slots and the
2753 // logic to use them are in sync.
2756 FeedbackVectorICSlot slot_;
2760 class NativeFunctionLiteral final : public Expression {
2762 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2764 Handle<String> name() const { return name_->string(); }
2765 v8::Extension* extension() const { return extension_; }
2768 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2769 v8::Extension* extension, int pos)
2770 : Expression(zone, pos), name_(name), extension_(extension) {}
2773 const AstRawString* name_;
2774 v8::Extension* extension_;
2778 class ThisFunction final : public Expression {
2780 DECLARE_NODE_TYPE(ThisFunction)
2783 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2787 class SuperPropertyReference final : public Expression {
2789 DECLARE_NODE_TYPE(SuperPropertyReference)
2791 VariableProxy* this_var() const { return this_var_; }
2792 Expression* home_object() const { return home_object_; }
2795 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2796 Expression* home_object, int pos)
2797 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2798 DCHECK(this_var->is_this());
2799 DCHECK(home_object->IsProperty());
2803 VariableProxy* this_var_;
2804 Expression* home_object_;
2808 class SuperCallReference final : public Expression {
2810 DECLARE_NODE_TYPE(SuperCallReference)
2812 VariableProxy* this_var() const { return this_var_; }
2813 VariableProxy* new_target_var() const { return new_target_var_; }
2814 VariableProxy* this_function_var() const { return this_function_var_; }
2817 SuperCallReference(Zone* zone, VariableProxy* this_var,
2818 VariableProxy* new_target_var,
2819 VariableProxy* this_function_var, int pos)
2820 : Expression(zone, pos),
2821 this_var_(this_var),
2822 new_target_var_(new_target_var),
2823 this_function_var_(this_function_var) {
2824 DCHECK(this_var->is_this());
2825 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2826 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2830 VariableProxy* this_var_;
2831 VariableProxy* new_target_var_;
2832 VariableProxy* this_function_var_;
2836 #undef DECLARE_NODE_TYPE
2839 // ----------------------------------------------------------------------------
2840 // Regular expressions
2843 class RegExpVisitor BASE_EMBEDDED {
2845 virtual ~RegExpVisitor() { }
2846 #define MAKE_CASE(Name) \
2847 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2848 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2853 class RegExpTree : public ZoneObject {
2855 static const int kInfinity = kMaxInt;
2856 virtual ~RegExpTree() {}
2857 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2858 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2859 RegExpNode* on_success) = 0;
2860 virtual bool IsTextElement() { return false; }
2861 virtual bool IsAnchoredAtStart() { return false; }
2862 virtual bool IsAnchoredAtEnd() { return false; }
2863 virtual int min_match() = 0;
2864 virtual int max_match() = 0;
2865 // Returns the interval of registers used for captures within this
2867 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2868 virtual void AppendToText(RegExpText* text, Zone* zone);
2869 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2870 #define MAKE_ASTYPE(Name) \
2871 virtual RegExp##Name* As##Name(); \
2872 virtual bool Is##Name();
2873 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2878 class RegExpDisjunction final : public RegExpTree {
2880 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2881 void* Accept(RegExpVisitor* visitor, void* data) override;
2882 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2883 RegExpNode* on_success) override;
2884 RegExpDisjunction* AsDisjunction() override;
2885 Interval CaptureRegisters() override;
2886 bool IsDisjunction() override;
2887 bool IsAnchoredAtStart() override;
2888 bool IsAnchoredAtEnd() override;
2889 int min_match() override { return min_match_; }
2890 int max_match() override { return max_match_; }
2891 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2893 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2894 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2895 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2896 ZoneList<RegExpTree*>* alternatives_;
2902 class RegExpAlternative final : public RegExpTree {
2904 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2905 void* Accept(RegExpVisitor* visitor, void* data) override;
2906 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2907 RegExpNode* on_success) override;
2908 RegExpAlternative* AsAlternative() override;
2909 Interval CaptureRegisters() override;
2910 bool IsAlternative() override;
2911 bool IsAnchoredAtStart() override;
2912 bool IsAnchoredAtEnd() override;
2913 int min_match() override { return min_match_; }
2914 int max_match() override { return max_match_; }
2915 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2917 ZoneList<RegExpTree*>* nodes_;
2923 class RegExpAssertion final : public RegExpTree {
2925 enum AssertionType {
2933 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2934 void* Accept(RegExpVisitor* visitor, void* data) override;
2935 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2936 RegExpNode* on_success) override;
2937 RegExpAssertion* AsAssertion() override;
2938 bool IsAssertion() override;
2939 bool IsAnchoredAtStart() override;
2940 bool IsAnchoredAtEnd() override;
2941 int min_match() override { return 0; }
2942 int max_match() override { return 0; }
2943 AssertionType assertion_type() { return assertion_type_; }
2945 AssertionType assertion_type_;
2949 class CharacterSet final BASE_EMBEDDED {
2951 explicit CharacterSet(uc16 standard_set_type)
2953 standard_set_type_(standard_set_type) {}
2954 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2956 standard_set_type_(0) {}
2957 ZoneList<CharacterRange>* ranges(Zone* zone);
2958 uc16 standard_set_type() { return standard_set_type_; }
2959 void set_standard_set_type(uc16 special_set_type) {
2960 standard_set_type_ = special_set_type;
2962 bool is_standard() { return standard_set_type_ != 0; }
2963 void Canonicalize();
2965 ZoneList<CharacterRange>* ranges_;
2966 // If non-zero, the value represents a standard set (e.g., all whitespace
2967 // characters) without having to expand the ranges.
2968 uc16 standard_set_type_;
2972 class RegExpCharacterClass final : public RegExpTree {
2974 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2976 is_negated_(is_negated) { }
2977 explicit RegExpCharacterClass(uc16 type)
2979 is_negated_(false) { }
2980 void* Accept(RegExpVisitor* visitor, void* data) override;
2981 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2982 RegExpNode* on_success) override;
2983 RegExpCharacterClass* AsCharacterClass() override;
2984 bool IsCharacterClass() override;
2985 bool IsTextElement() override { return true; }
2986 int min_match() override { return 1; }
2987 int max_match() override { return 1; }
2988 void AppendToText(RegExpText* text, Zone* zone) override;
2989 CharacterSet character_set() { return set_; }
2990 // TODO(lrn): Remove need for complex version if is_standard that
2991 // recognizes a mangled standard set and just do { return set_.is_special(); }
2992 bool is_standard(Zone* zone);
2993 // Returns a value representing the standard character set if is_standard()
2995 // Currently used values are:
2996 // s : unicode whitespace
2997 // S : unicode non-whitespace
2998 // w : ASCII word character (digit, letter, underscore)
2999 // W : non-ASCII word character
3001 // D : non-ASCII digit
3002 // . : non-unicode non-newline
3003 // * : All characters
3004 uc16 standard_type() { return set_.standard_set_type(); }
3005 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3006 bool is_negated() { return is_negated_; }
3014 class RegExpAtom final : public RegExpTree {
3016 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3017 void* Accept(RegExpVisitor* visitor, void* data) override;
3018 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3019 RegExpNode* on_success) override;
3020 RegExpAtom* AsAtom() override;
3021 bool IsAtom() override;
3022 bool IsTextElement() override { return true; }
3023 int min_match() override { return data_.length(); }
3024 int max_match() override { return data_.length(); }
3025 void AppendToText(RegExpText* text, Zone* zone) override;
3026 Vector<const uc16> data() { return data_; }
3027 int length() { return data_.length(); }
3029 Vector<const uc16> data_;
3033 class RegExpText final : public RegExpTree {
3035 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3036 void* Accept(RegExpVisitor* visitor, void* data) override;
3037 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3038 RegExpNode* on_success) override;
3039 RegExpText* AsText() override;
3040 bool IsText() override;
3041 bool IsTextElement() override { return true; }
3042 int min_match() override { return length_; }
3043 int max_match() override { return length_; }
3044 void AppendToText(RegExpText* text, Zone* zone) override;
3045 void AddElement(TextElement elm, Zone* zone) {
3046 elements_.Add(elm, zone);
3047 length_ += elm.length();
3049 ZoneList<TextElement>* elements() { return &elements_; }
3051 ZoneList<TextElement> elements_;
3056 class RegExpQuantifier final : public RegExpTree {
3058 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3059 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3063 min_match_(min * body->min_match()),
3064 quantifier_type_(type) {
3065 if (max > 0 && body->max_match() > kInfinity / max) {
3066 max_match_ = kInfinity;
3068 max_match_ = max * body->max_match();
3071 void* Accept(RegExpVisitor* visitor, void* data) override;
3072 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3073 RegExpNode* on_success) override;
3074 static RegExpNode* ToNode(int min,
3078 RegExpCompiler* compiler,
3079 RegExpNode* on_success,
3080 bool not_at_start = false);
3081 RegExpQuantifier* AsQuantifier() override;
3082 Interval CaptureRegisters() override;
3083 bool IsQuantifier() override;
3084 int min_match() override { return min_match_; }
3085 int max_match() override { return max_match_; }
3086 int min() { return min_; }
3087 int max() { return max_; }
3088 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3089 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3090 bool is_greedy() { return quantifier_type_ == GREEDY; }
3091 RegExpTree* body() { return body_; }
3099 QuantifierType quantifier_type_;
3103 class RegExpCapture final : public RegExpTree {
3105 explicit RegExpCapture(RegExpTree* body, int index)
3106 : body_(body), index_(index) { }
3107 void* Accept(RegExpVisitor* visitor, void* data) override;
3108 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3109 RegExpNode* on_success) override;
3110 static RegExpNode* ToNode(RegExpTree* body,
3112 RegExpCompiler* compiler,
3113 RegExpNode* on_success);
3114 RegExpCapture* AsCapture() override;
3115 bool IsAnchoredAtStart() override;
3116 bool IsAnchoredAtEnd() override;
3117 Interval CaptureRegisters() override;
3118 bool IsCapture() override;
3119 int min_match() override { return body_->min_match(); }
3120 int max_match() override { return body_->max_match(); }
3121 RegExpTree* body() { return body_; }
3122 int index() { return index_; }
3123 static int StartRegister(int index) { return index * 2; }
3124 static int EndRegister(int index) { return index * 2 + 1; }
3132 class RegExpLookahead final : public RegExpTree {
3134 RegExpLookahead(RegExpTree* body,
3139 is_positive_(is_positive),
3140 capture_count_(capture_count),
3141 capture_from_(capture_from) { }
3143 void* Accept(RegExpVisitor* visitor, void* data) override;
3144 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3145 RegExpNode* on_success) override;
3146 RegExpLookahead* AsLookahead() override;
3147 Interval CaptureRegisters() override;
3148 bool IsLookahead() override;
3149 bool IsAnchoredAtStart() override;
3150 int min_match() override { return 0; }
3151 int max_match() override { return 0; }
3152 RegExpTree* body() { return body_; }
3153 bool is_positive() { return is_positive_; }
3154 int capture_count() { return capture_count_; }
3155 int capture_from() { return capture_from_; }
3165 class RegExpBackReference final : public RegExpTree {
3167 explicit RegExpBackReference(RegExpCapture* capture)
3168 : capture_(capture) { }
3169 void* Accept(RegExpVisitor* visitor, void* data) override;
3170 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3171 RegExpNode* on_success) override;
3172 RegExpBackReference* AsBackReference() override;
3173 bool IsBackReference() override;
3174 int min_match() override { return 0; }
3175 int max_match() override { return capture_->max_match(); }
3176 int index() { return capture_->index(); }
3177 RegExpCapture* capture() { return capture_; }
3179 RegExpCapture* capture_;
3183 class RegExpEmpty final : public RegExpTree {
3186 void* Accept(RegExpVisitor* visitor, void* data) override;
3187 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3188 RegExpNode* on_success) override;
3189 RegExpEmpty* AsEmpty() override;
3190 bool IsEmpty() override;
3191 int min_match() override { return 0; }
3192 int max_match() override { return 0; }
3196 // ----------------------------------------------------------------------------
3198 // - leaf node visitors are abstract.
3200 class AstVisitor BASE_EMBEDDED {
3203 virtual ~AstVisitor() {}
3205 // Stack overflow check and dynamic dispatch.
3206 virtual void Visit(AstNode* node) = 0;
3208 // Iteration left-to-right.
3209 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3210 virtual void VisitStatements(ZoneList<Statement*>* statements);
3211 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3213 // Individual AST nodes.
3214 #define DEF_VISIT(type) \
3215 virtual void Visit##type(type* node) = 0;
3216 AST_NODE_LIST(DEF_VISIT)
3221 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3223 void Visit(AstNode* node) final { \
3224 if (!CheckStackOverflow()) node->Accept(this); \
3227 void SetStackOverflow() { stack_overflow_ = true; } \
3228 void ClearStackOverflow() { stack_overflow_ = false; } \
3229 bool HasStackOverflow() const { return stack_overflow_; } \
3231 bool CheckStackOverflow() { \
3232 if (stack_overflow_) return true; \
3233 StackLimitCheck check(isolate_); \
3234 if (!check.HasOverflowed()) return false; \
3235 stack_overflow_ = true; \
3240 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3241 isolate_ = isolate; \
3243 stack_overflow_ = false; \
3245 Zone* zone() { return zone_; } \
3246 Isolate* isolate() { return isolate_; } \
3248 Isolate* isolate_; \
3250 bool stack_overflow_
3253 // ----------------------------------------------------------------------------
3256 class AstNodeFactory final BASE_EMBEDDED {
3258 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3259 : zone_(ast_value_factory->zone()),
3260 ast_value_factory_(ast_value_factory) {}
3262 VariableDeclaration* NewVariableDeclaration(
3263 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3264 bool is_class_declaration = false, int declaration_group_start = -1) {
3266 VariableDeclaration(zone_, proxy, mode, scope, pos,
3267 is_class_declaration, declaration_group_start);
3270 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3272 FunctionLiteral* fun,
3275 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3278 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3279 const AstRawString* import_name,
3280 const AstRawString* module_specifier,
3281 Scope* scope, int pos) {
3282 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3283 module_specifier, scope, pos);
3286 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3289 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3292 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3293 bool ignore_completion_value, int pos) {
3295 Block(zone_, labels, capacity, ignore_completion_value, pos);
3298 #define STATEMENT_WITH_LABELS(NodeType) \
3299 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3300 return new (zone_) NodeType(zone_, labels, pos); \
3302 STATEMENT_WITH_LABELS(DoWhileStatement)
3303 STATEMENT_WITH_LABELS(WhileStatement)
3304 STATEMENT_WITH_LABELS(ForStatement)
3305 STATEMENT_WITH_LABELS(SwitchStatement)
3306 #undef STATEMENT_WITH_LABELS
3308 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3309 ZoneList<const AstRawString*>* labels,
3311 switch (visit_mode) {
3312 case ForEachStatement::ENUMERATE: {
3313 return new (zone_) ForInStatement(zone_, labels, pos);
3315 case ForEachStatement::ITERATE: {
3316 return new (zone_) ForOfStatement(zone_, labels, pos);
3323 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3324 return new (zone_) ExpressionStatement(zone_, expression, pos);
3327 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3328 return new (zone_) ContinueStatement(zone_, target, pos);
3331 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3332 return new (zone_) BreakStatement(zone_, target, pos);
3335 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3336 return new (zone_) ReturnStatement(zone_, expression, pos);
3339 WithStatement* NewWithStatement(Scope* scope,
3340 Expression* expression,
3341 Statement* statement,
3343 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3346 IfStatement* NewIfStatement(Expression* condition,
3347 Statement* then_statement,
3348 Statement* else_statement,
3351 IfStatement(zone_, condition, then_statement, else_statement, pos);
3354 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3356 Block* catch_block, int pos) {
3358 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3361 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3362 Block* finally_block, int pos) {
3364 TryFinallyStatement(zone_, try_block, finally_block, pos);
3367 DebuggerStatement* NewDebuggerStatement(int pos) {
3368 return new (zone_) DebuggerStatement(zone_, pos);
3371 EmptyStatement* NewEmptyStatement(int pos) {
3372 return new(zone_) EmptyStatement(zone_, pos);
3375 CaseClause* NewCaseClause(
3376 Expression* label, ZoneList<Statement*>* statements, int pos) {
3377 return new (zone_) CaseClause(zone_, label, statements, pos);
3380 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3382 Literal(zone_, ast_value_factory_->NewString(string), pos);
3385 // A JavaScript symbol (ECMA-262 edition 6).
3386 Literal* NewSymbolLiteral(const char* name, int pos) {
3387 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3390 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3392 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3395 Literal* NewSmiLiteral(int number, int pos) {
3396 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3399 Literal* NewBooleanLiteral(bool b, int pos) {
3400 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3403 Literal* NewNullLiteral(int pos) {
3404 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3407 Literal* NewUndefinedLiteral(int pos) {
3408 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3411 Literal* NewTheHoleLiteral(int pos) {
3412 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3415 ObjectLiteral* NewObjectLiteral(
3416 ZoneList<ObjectLiteral::Property*>* properties,
3418 int boilerplate_properties,
3422 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3423 boilerplate_properties, has_function,
3427 ObjectLiteral::Property* NewObjectLiteralProperty(
3428 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3429 bool is_static, bool is_computed_name) {
3431 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3434 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3437 bool is_computed_name) {
3438 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3439 is_static, is_computed_name);
3442 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3443 const AstRawString* flags,
3447 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3451 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3455 return new (zone_) ArrayLiteral(zone_, values, literal_index, is_strong,
3459 VariableProxy* NewVariableProxy(Variable* var,
3460 int start_position = RelocInfo::kNoPosition,
3461 int end_position = RelocInfo::kNoPosition) {
3462 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3465 VariableProxy* NewVariableProxy(const AstRawString* name,
3466 Variable::Kind variable_kind,
3467 int start_position = RelocInfo::kNoPosition,
3468 int end_position = RelocInfo::kNoPosition) {
3469 DCHECK_NOT_NULL(name);
3471 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3474 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3475 return new (zone_) Property(zone_, obj, key, pos);
3478 Call* NewCall(Expression* expression,
3479 ZoneList<Expression*>* arguments,
3481 return new (zone_) Call(zone_, expression, arguments, pos);
3484 CallNew* NewCallNew(Expression* expression,
3485 ZoneList<Expression*>* arguments,
3487 return new (zone_) CallNew(zone_, expression, arguments, pos);
3490 CallRuntime* NewCallRuntime(const AstRawString* name,
3491 const Runtime::Function* function,
3492 ZoneList<Expression*>* arguments,
3494 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3497 UnaryOperation* NewUnaryOperation(Token::Value op,
3498 Expression* expression,
3500 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3503 BinaryOperation* NewBinaryOperation(Token::Value op,
3507 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3510 CountOperation* NewCountOperation(Token::Value op,
3514 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3517 CompareOperation* NewCompareOperation(Token::Value op,
3521 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3524 Spread* NewSpread(Expression* expression, int pos) {
3525 return new (zone_) Spread(zone_, expression, pos);
3528 Conditional* NewConditional(Expression* condition,
3529 Expression* then_expression,
3530 Expression* else_expression,
3532 return new (zone_) Conditional(zone_, condition, then_expression,
3533 else_expression, position);
3536 Assignment* NewAssignment(Token::Value op,
3540 DCHECK(Token::IsAssignmentOp(op));
3541 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3542 if (assign->is_compound()) {
3543 DCHECK(Token::IsAssignmentOp(op));
3544 assign->binary_operation_ =
3545 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3550 Yield* NewYield(Expression *generator_object,
3551 Expression* expression,
3552 Yield::Kind yield_kind,
3554 if (!expression) expression = NewUndefinedLiteral(pos);
3556 Yield(zone_, generator_object, expression, yield_kind, pos);
3559 Throw* NewThrow(Expression* exception, int pos) {
3560 return new (zone_) Throw(zone_, exception, pos);
3563 FunctionLiteral* NewFunctionLiteral(
3564 const AstRawString* name, AstValueFactory* ast_value_factory,
3565 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3566 int expected_property_count, int parameter_count,
3567 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3568 FunctionLiteral::FunctionType function_type,
3569 FunctionLiteral::IsFunctionFlag is_function,
3570 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3572 return new (zone_) FunctionLiteral(
3573 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3574 expected_property_count, parameter_count, function_type,
3575 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3579 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3580 VariableProxy* proxy, Expression* extends,
3581 FunctionLiteral* constructor,
3582 ZoneList<ObjectLiteral::Property*>* properties,
3583 int start_position, int end_position) {
3585 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3586 properties, start_position, end_position);
3589 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3590 v8::Extension* extension,
3592 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3595 ThisFunction* NewThisFunction(int pos) {
3596 return new (zone_) ThisFunction(zone_, pos);
3599 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3600 Expression* home_object,
3603 SuperPropertyReference(zone_, this_var, home_object, pos);
3606 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3607 VariableProxy* new_target_var,
3608 VariableProxy* this_function_var,
3610 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3611 this_function_var, pos);
3616 AstValueFactory* ast_value_factory_;
3620 } } // namespace v8::internal