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,
1616 int first_spread_index, int literal_index, bool is_strong,
1618 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1620 first_spread_index_(first_spread_index) {}
1621 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1624 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1626 Handle<FixedArray> constant_elements_;
1627 ZoneList<Expression*>* values_;
1628 int first_spread_index_;
1632 class VariableProxy final : public Expression {
1634 DECLARE_NODE_TYPE(VariableProxy)
1636 bool IsValidReferenceExpression() const override { return !is_this(); }
1638 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1640 Handle<String> name() const { return raw_name()->string(); }
1641 const AstRawString* raw_name() const {
1642 return is_resolved() ? var_->raw_name() : raw_name_;
1645 Variable* var() const {
1646 DCHECK(is_resolved());
1649 void set_var(Variable* v) {
1650 DCHECK(!is_resolved());
1655 bool is_this() const { return IsThisField::decode(bit_field_); }
1657 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1658 void set_is_assigned() {
1659 bit_field_ = IsAssignedField::update(bit_field_, true);
1662 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1663 void set_is_resolved() {
1664 bit_field_ = IsResolvedField::update(bit_field_, true);
1667 int end_position() const { return end_position_; }
1669 // Bind this proxy to the variable var.
1670 void BindTo(Variable* var);
1672 bool UsesVariableFeedbackSlot() const {
1673 return var()->IsUnallocated() || var()->IsLookupSlot();
1676 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1677 Isolate* isolate, const ICSlotCache* cache) override;
1679 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1680 ICSlotCache* cache) override;
1681 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1682 FeedbackVectorICSlot VariableFeedbackSlot() {
1683 DCHECK(!UsesVariableFeedbackSlot() || !variable_feedback_slot_.IsInvalid());
1684 return variable_feedback_slot_;
1687 static int num_ids() { return parent_num_ids() + 1; }
1688 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1691 VariableProxy(Zone* zone, Variable* var, int start_position,
1694 VariableProxy(Zone* zone, const AstRawString* name,
1695 Variable::Kind variable_kind, int start_position,
1697 static int parent_num_ids() { return Expression::num_ids(); }
1698 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1700 class IsThisField : public BitField8<bool, 0, 1> {};
1701 class IsAssignedField : public BitField8<bool, 1, 1> {};
1702 class IsResolvedField : public BitField8<bool, 2, 1> {};
1704 // Start with 16-bit (or smaller) field, which should get packed together
1705 // with Expression's trailing 16-bit field.
1707 FeedbackVectorICSlot variable_feedback_slot_;
1709 const AstRawString* raw_name_; // if !is_resolved_
1710 Variable* var_; // if is_resolved_
1712 // Position is stored in the AstNode superclass, but VariableProxy needs to
1713 // know its end position too (for error messages). It cannot be inferred from
1714 // the variable name length because it can contain escapes.
1719 // Left-hand side can only be a property, a global or a (parameter or local)
1725 NAMED_SUPER_PROPERTY,
1726 KEYED_SUPER_PROPERTY
1730 class Property final : public Expression {
1732 DECLARE_NODE_TYPE(Property)
1734 bool IsValidReferenceExpression() const override { return true; }
1736 Expression* obj() const { return obj_; }
1737 Expression* key() const { return key_; }
1739 static int num_ids() { return parent_num_ids() + 1; }
1740 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1742 bool IsStringAccess() const {
1743 return IsStringAccessField::decode(bit_field_);
1746 // Type feedback information.
1747 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1748 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1749 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1750 IcCheckType GetKeyType() const override {
1751 return KeyTypeField::decode(bit_field_);
1753 bool IsUninitialized() const {
1754 return !is_for_call() && HasNoTypeInformation();
1756 bool HasNoTypeInformation() const {
1757 return GetInlineCacheState() == UNINITIALIZED;
1759 InlineCacheState GetInlineCacheState() const {
1760 return InlineCacheStateField::decode(bit_field_);
1762 void set_is_string_access(bool b) {
1763 bit_field_ = IsStringAccessField::update(bit_field_, b);
1765 void set_key_type(IcCheckType key_type) {
1766 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1768 void set_inline_cache_state(InlineCacheState state) {
1769 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1771 void mark_for_call() {
1772 bit_field_ = IsForCallField::update(bit_field_, true);
1774 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1776 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1778 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1779 Isolate* isolate, const ICSlotCache* cache) override {
1780 return FeedbackVectorRequirements(0, 1);
1782 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1783 ICSlotCache* cache) override {
1784 property_feedback_slot_ = slot;
1786 Code::Kind FeedbackICSlotKind(int index) override {
1787 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1790 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1791 DCHECK(!property_feedback_slot_.IsInvalid());
1792 return property_feedback_slot_;
1795 static LhsKind GetAssignType(Property* property) {
1796 if (property == NULL) return VARIABLE;
1797 bool super_access = property->IsSuperAccess();
1798 return (property->key()->IsPropertyName())
1799 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1800 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1804 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1805 : Expression(zone, pos),
1806 bit_field_(IsForCallField::encode(false) |
1807 IsStringAccessField::encode(false) |
1808 InlineCacheStateField::encode(UNINITIALIZED)),
1809 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1812 static int parent_num_ids() { return Expression::num_ids(); }
1815 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1817 class IsForCallField : public BitField8<bool, 0, 1> {};
1818 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1819 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1820 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1822 FeedbackVectorICSlot property_feedback_slot_;
1825 SmallMapList receiver_types_;
1829 class Call final : public Expression {
1831 DECLARE_NODE_TYPE(Call)
1833 Expression* expression() const { return expression_; }
1834 ZoneList<Expression*>* arguments() const { return arguments_; }
1836 // Type feedback information.
1837 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1838 Isolate* isolate, const ICSlotCache* cache) override;
1839 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1840 ICSlotCache* cache) override {
1843 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1844 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1846 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1848 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1850 SmallMapList* GetReceiverTypes() override {
1851 if (expression()->IsProperty()) {
1852 return expression()->AsProperty()->GetReceiverTypes();
1857 bool IsMonomorphic() override {
1858 if (expression()->IsProperty()) {
1859 return expression()->AsProperty()->IsMonomorphic();
1861 return !target_.is_null();
1864 bool global_call() const {
1865 VariableProxy* proxy = expression_->AsVariableProxy();
1866 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1869 bool known_global_function() const {
1870 return global_call() && !target_.is_null();
1873 Handle<JSFunction> target() { return target_; }
1875 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1877 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1879 set_is_uninitialized(false);
1881 void set_target(Handle<JSFunction> target) { target_ = target; }
1882 void set_allocation_site(Handle<AllocationSite> site) {
1883 allocation_site_ = site;
1886 static int num_ids() { return parent_num_ids() + 3; }
1887 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1888 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1889 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1891 bool is_uninitialized() const {
1892 return IsUninitializedField::decode(bit_field_);
1894 void set_is_uninitialized(bool b) {
1895 bit_field_ = IsUninitializedField::update(bit_field_, b);
1907 // Helpers to determine how to handle the call.
1908 CallType GetCallType(Isolate* isolate) const;
1909 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1910 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1913 // Used to assert that the FullCodeGenerator records the return site.
1914 bool return_is_recorded_;
1918 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1920 : Expression(zone, pos),
1921 ic_slot_(FeedbackVectorICSlot::Invalid()),
1922 slot_(FeedbackVectorSlot::Invalid()),
1923 expression_(expression),
1924 arguments_(arguments),
1925 bit_field_(IsUninitializedField::encode(false)) {
1926 if (expression->IsProperty()) {
1927 expression->AsProperty()->mark_for_call();
1930 static int parent_num_ids() { return Expression::num_ids(); }
1933 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1935 FeedbackVectorICSlot ic_slot_;
1936 FeedbackVectorSlot slot_;
1937 Expression* expression_;
1938 ZoneList<Expression*>* arguments_;
1939 Handle<JSFunction> target_;
1940 Handle<AllocationSite> allocation_site_;
1941 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1946 class CallNew final : public Expression {
1948 DECLARE_NODE_TYPE(CallNew)
1950 Expression* expression() const { return expression_; }
1951 ZoneList<Expression*>* arguments() const { return arguments_; }
1953 // Type feedback information.
1954 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1955 Isolate* isolate, const ICSlotCache* cache) override {
1956 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1958 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1959 callnew_feedback_slot_ = slot;
1962 FeedbackVectorSlot CallNewFeedbackSlot() {
1963 DCHECK(!callnew_feedback_slot_.IsInvalid());
1964 return callnew_feedback_slot_;
1966 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1967 DCHECK(FLAG_pretenuring_call_new);
1968 return CallNewFeedbackSlot().next();
1971 bool IsMonomorphic() override { return is_monomorphic_; }
1972 Handle<JSFunction> target() const { return target_; }
1973 Handle<AllocationSite> allocation_site() const {
1974 return allocation_site_;
1977 static int num_ids() { return parent_num_ids() + 1; }
1978 static int feedback_slots() { return 1; }
1979 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1981 void set_allocation_site(Handle<AllocationSite> site) {
1982 allocation_site_ = site;
1984 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1985 void set_target(Handle<JSFunction> target) { target_ = target; }
1986 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1988 is_monomorphic_ = true;
1992 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1994 : Expression(zone, pos),
1995 expression_(expression),
1996 arguments_(arguments),
1997 is_monomorphic_(false),
1998 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2000 static int parent_num_ids() { return Expression::num_ids(); }
2003 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2005 Expression* expression_;
2006 ZoneList<Expression*>* arguments_;
2007 bool is_monomorphic_;
2008 Handle<JSFunction> target_;
2009 Handle<AllocationSite> allocation_site_;
2010 FeedbackVectorSlot callnew_feedback_slot_;
2014 // The CallRuntime class does not represent any official JavaScript
2015 // language construct. Instead it is used to call a C or JS function
2016 // with a set of arguments. This is used from the builtins that are
2017 // implemented in JavaScript (see "v8natives.js").
2018 class CallRuntime final : public Expression {
2020 DECLARE_NODE_TYPE(CallRuntime)
2022 Handle<String> name() const { return raw_name_->string(); }
2023 const AstRawString* raw_name() const { return raw_name_; }
2024 const Runtime::Function* function() const { return function_; }
2025 ZoneList<Expression*>* arguments() const { return arguments_; }
2026 bool is_jsruntime() const { return function_ == NULL; }
2028 // Type feedback information.
2029 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2030 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2031 Isolate* isolate, const ICSlotCache* cache) override {
2032 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2034 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2035 ICSlotCache* cache) override {
2036 callruntime_feedback_slot_ = slot;
2038 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2040 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2041 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2042 !callruntime_feedback_slot_.IsInvalid());
2043 return callruntime_feedback_slot_;
2046 static int num_ids() { return parent_num_ids() + 1; }
2047 BailoutId CallId() { return BailoutId(local_id(0)); }
2050 CallRuntime(Zone* zone, const AstRawString* name,
2051 const Runtime::Function* function,
2052 ZoneList<Expression*>* arguments, int pos)
2053 : Expression(zone, pos),
2055 function_(function),
2056 arguments_(arguments),
2057 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2058 static int parent_num_ids() { return Expression::num_ids(); }
2061 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2063 const AstRawString* raw_name_;
2064 const Runtime::Function* function_;
2065 ZoneList<Expression*>* arguments_;
2066 FeedbackVectorICSlot callruntime_feedback_slot_;
2070 class UnaryOperation final : public Expression {
2072 DECLARE_NODE_TYPE(UnaryOperation)
2074 Token::Value op() const { return op_; }
2075 Expression* expression() const { return expression_; }
2077 // For unary not (Token::NOT), the AST ids where true and false will
2078 // actually be materialized, respectively.
2079 static int num_ids() { return parent_num_ids() + 2; }
2080 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2081 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2083 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2086 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2087 : Expression(zone, pos), op_(op), expression_(expression) {
2088 DCHECK(Token::IsUnaryOp(op));
2090 static int parent_num_ids() { return Expression::num_ids(); }
2093 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2096 Expression* expression_;
2100 class BinaryOperation final : public Expression {
2102 DECLARE_NODE_TYPE(BinaryOperation)
2104 Token::Value op() const { return static_cast<Token::Value>(op_); }
2105 Expression* left() const { return left_; }
2106 Expression* right() const { return right_; }
2107 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2108 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2109 allocation_site_ = allocation_site;
2112 // The short-circuit logical operations need an AST ID for their
2113 // right-hand subexpression.
2114 static int num_ids() { return parent_num_ids() + 2; }
2115 BailoutId RightId() const { return BailoutId(local_id(0)); }
2117 TypeFeedbackId BinaryOperationFeedbackId() const {
2118 return TypeFeedbackId(local_id(1));
2120 Maybe<int> fixed_right_arg() const {
2121 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2123 void set_fixed_right_arg(Maybe<int> arg) {
2124 has_fixed_right_arg_ = arg.IsJust();
2125 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2128 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2131 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2132 Expression* right, int pos)
2133 : Expression(zone, pos),
2134 op_(static_cast<byte>(op)),
2135 has_fixed_right_arg_(false),
2136 fixed_right_arg_value_(0),
2139 DCHECK(Token::IsBinaryOp(op));
2141 static int parent_num_ids() { return Expression::num_ids(); }
2144 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2146 const byte op_; // actually Token::Value
2147 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2148 // type for the RHS. Currenty it's actually a Maybe<int>
2149 bool has_fixed_right_arg_;
2150 int fixed_right_arg_value_;
2153 Handle<AllocationSite> allocation_site_;
2157 class CountOperation final : public Expression {
2159 DECLARE_NODE_TYPE(CountOperation)
2161 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2162 bool is_postfix() const { return !is_prefix(); }
2164 Token::Value op() const { return TokenField::decode(bit_field_); }
2165 Token::Value binary_op() {
2166 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2169 Expression* expression() const { return expression_; }
2171 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2172 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2173 IcCheckType GetKeyType() const override {
2174 return KeyTypeField::decode(bit_field_);
2176 KeyedAccessStoreMode GetStoreMode() const override {
2177 return StoreModeField::decode(bit_field_);
2179 Type* type() const { return type_; }
2180 void set_key_type(IcCheckType type) {
2181 bit_field_ = KeyTypeField::update(bit_field_, type);
2183 void set_store_mode(KeyedAccessStoreMode mode) {
2184 bit_field_ = StoreModeField::update(bit_field_, mode);
2186 void set_type(Type* type) { type_ = type; }
2188 static int num_ids() { return parent_num_ids() + 4; }
2189 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2190 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2191 TypeFeedbackId CountBinOpFeedbackId() const {
2192 return TypeFeedbackId(local_id(2));
2194 TypeFeedbackId CountStoreFeedbackId() const {
2195 return TypeFeedbackId(local_id(3));
2198 FeedbackVectorRequirements ComputeFeedbackRequirements(
2199 Isolate* isolate, const ICSlotCache* cache) override;
2200 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2201 ICSlotCache* cache) override {
2204 Code::Kind FeedbackICSlotKind(int index) override;
2205 FeedbackVectorICSlot CountSlot() const { return slot_; }
2208 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2210 : Expression(zone, pos),
2212 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2213 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2216 slot_(FeedbackVectorICSlot::Invalid()) {}
2217 static int parent_num_ids() { return Expression::num_ids(); }
2220 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2222 class IsPrefixField : public BitField16<bool, 0, 1> {};
2223 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2224 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2225 class TokenField : public BitField16<Token::Value, 6, 8> {};
2227 // Starts with 16-bit field, which should get packed together with
2228 // Expression's trailing 16-bit field.
2229 uint16_t bit_field_;
2231 Expression* expression_;
2232 SmallMapList receiver_types_;
2233 FeedbackVectorICSlot slot_;
2237 class CompareOperation final : public Expression {
2239 DECLARE_NODE_TYPE(CompareOperation)
2241 Token::Value op() const { return op_; }
2242 Expression* left() const { return left_; }
2243 Expression* right() const { return right_; }
2245 // Type feedback information.
2246 static int num_ids() { return parent_num_ids() + 1; }
2247 TypeFeedbackId CompareOperationFeedbackId() const {
2248 return TypeFeedbackId(local_id(0));
2250 Type* combined_type() const { return combined_type_; }
2251 void set_combined_type(Type* type) { combined_type_ = type; }
2253 // Match special cases.
2254 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2255 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2256 bool IsLiteralCompareNull(Expression** expr);
2259 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2260 Expression* right, int pos)
2261 : Expression(zone, pos),
2265 combined_type_(Type::None(zone)) {
2266 DCHECK(Token::IsCompareOp(op));
2268 static int parent_num_ids() { return Expression::num_ids(); }
2271 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2277 Type* combined_type_;
2281 class Spread final : public Expression {
2283 DECLARE_NODE_TYPE(Spread)
2285 Expression* expression() const { return expression_; }
2287 static int num_ids() { return parent_num_ids(); }
2290 Spread(Zone* zone, Expression* expression, int pos)
2291 : Expression(zone, pos), expression_(expression) {}
2292 static int parent_num_ids() { return Expression::num_ids(); }
2295 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2297 Expression* expression_;
2301 class Conditional final : public Expression {
2303 DECLARE_NODE_TYPE(Conditional)
2305 Expression* condition() const { return condition_; }
2306 Expression* then_expression() const { return then_expression_; }
2307 Expression* else_expression() const { return else_expression_; }
2309 static int num_ids() { return parent_num_ids() + 2; }
2310 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2311 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2314 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2315 Expression* else_expression, int position)
2316 : Expression(zone, position),
2317 condition_(condition),
2318 then_expression_(then_expression),
2319 else_expression_(else_expression) {}
2320 static int parent_num_ids() { return Expression::num_ids(); }
2323 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2325 Expression* condition_;
2326 Expression* then_expression_;
2327 Expression* else_expression_;
2331 class Assignment final : public Expression {
2333 DECLARE_NODE_TYPE(Assignment)
2335 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2337 Token::Value binary_op() const;
2339 Token::Value op() const { return TokenField::decode(bit_field_); }
2340 Expression* target() const { return target_; }
2341 Expression* value() const { return value_; }
2342 BinaryOperation* binary_operation() const { return binary_operation_; }
2344 // This check relies on the definition order of token in token.h.
2345 bool is_compound() const { return op() > Token::ASSIGN; }
2347 static int num_ids() { return parent_num_ids() + 2; }
2348 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2350 // Type feedback information.
2351 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2352 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2353 bool IsUninitialized() const {
2354 return IsUninitializedField::decode(bit_field_);
2356 bool HasNoTypeInformation() {
2357 return IsUninitializedField::decode(bit_field_);
2359 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2360 IcCheckType GetKeyType() const override {
2361 return KeyTypeField::decode(bit_field_);
2363 KeyedAccessStoreMode GetStoreMode() const override {
2364 return StoreModeField::decode(bit_field_);
2366 void set_is_uninitialized(bool b) {
2367 bit_field_ = IsUninitializedField::update(bit_field_, b);
2369 void set_key_type(IcCheckType key_type) {
2370 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2372 void set_store_mode(KeyedAccessStoreMode mode) {
2373 bit_field_ = StoreModeField::update(bit_field_, mode);
2376 FeedbackVectorRequirements ComputeFeedbackRequirements(
2377 Isolate* isolate, const ICSlotCache* cache) override;
2378 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2379 ICSlotCache* cache) override {
2382 Code::Kind FeedbackICSlotKind(int index) override;
2383 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2386 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2388 static int parent_num_ids() { return Expression::num_ids(); }
2391 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2393 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2394 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2395 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2396 class TokenField : public BitField16<Token::Value, 6, 8> {};
2398 // Starts with 16-bit field, which should get packed together with
2399 // Expression's trailing 16-bit field.
2400 uint16_t bit_field_;
2401 Expression* target_;
2403 BinaryOperation* binary_operation_;
2404 SmallMapList receiver_types_;
2405 FeedbackVectorICSlot slot_;
2409 class Yield final : public Expression {
2411 DECLARE_NODE_TYPE(Yield)
2414 kInitial, // The initial yield that returns the unboxed generator object.
2415 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2416 kDelegating, // A yield*.
2417 kFinal // A return: { value: EXPRESSION, done: true }
2420 Expression* generator_object() const { return generator_object_; }
2421 Expression* expression() const { return expression_; }
2422 Kind yield_kind() const { return yield_kind_; }
2424 // Type feedback information.
2425 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2426 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2427 Isolate* isolate, const ICSlotCache* cache) override {
2428 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2430 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2431 ICSlotCache* cache) override {
2432 yield_first_feedback_slot_ = slot;
2434 Code::Kind FeedbackICSlotKind(int index) override {
2435 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2438 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2439 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2440 return yield_first_feedback_slot_;
2443 FeedbackVectorICSlot DoneFeedbackSlot() {
2444 return KeyedLoadFeedbackSlot().next();
2447 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2450 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2451 Kind yield_kind, int pos)
2452 : Expression(zone, pos),
2453 generator_object_(generator_object),
2454 expression_(expression),
2455 yield_kind_(yield_kind),
2456 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2459 Expression* generator_object_;
2460 Expression* expression_;
2462 FeedbackVectorICSlot yield_first_feedback_slot_;
2466 class Throw final : public Expression {
2468 DECLARE_NODE_TYPE(Throw)
2470 Expression* exception() const { return exception_; }
2473 Throw(Zone* zone, Expression* exception, int pos)
2474 : Expression(zone, pos), exception_(exception) {}
2477 Expression* exception_;
2481 class FunctionLiteral final : public Expression {
2484 ANONYMOUS_EXPRESSION,
2489 enum ParameterFlag {
2490 kNoDuplicateParameters = 0,
2491 kHasDuplicateParameters = 1
2494 enum IsFunctionFlag {
2499 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2501 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2503 enum ArityRestriction {
2509 DECLARE_NODE_TYPE(FunctionLiteral)
2511 Handle<String> name() const { return raw_name_->string(); }
2512 const AstRawString* raw_name() const { return raw_name_; }
2513 Scope* scope() const { return scope_; }
2514 ZoneList<Statement*>* body() const { return body_; }
2515 void set_function_token_position(int pos) { function_token_position_ = pos; }
2516 int function_token_position() const { return function_token_position_; }
2517 int start_position() const;
2518 int end_position() const;
2519 int SourceSize() const { return end_position() - start_position(); }
2520 bool is_expression() const { return IsExpression::decode(bitfield_); }
2521 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2522 LanguageMode language_mode() const;
2524 static bool NeedsHomeObject(Expression* expr);
2526 int materialized_literal_count() { return materialized_literal_count_; }
2527 int expected_property_count() { return expected_property_count_; }
2528 int parameter_count() { return parameter_count_; }
2530 bool AllowsLazyCompilation();
2531 bool AllowsLazyCompilationWithoutContext();
2533 Handle<String> debug_name() const {
2534 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2535 return raw_name_->string();
2537 return inferred_name();
2540 Handle<String> inferred_name() const {
2541 if (!inferred_name_.is_null()) {
2542 DCHECK(raw_inferred_name_ == NULL);
2543 return inferred_name_;
2545 if (raw_inferred_name_ != NULL) {
2546 return raw_inferred_name_->string();
2549 return Handle<String>();
2552 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2553 void set_inferred_name(Handle<String> inferred_name) {
2554 DCHECK(!inferred_name.is_null());
2555 inferred_name_ = inferred_name;
2556 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2557 raw_inferred_name_ = NULL;
2560 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2561 DCHECK(raw_inferred_name != NULL);
2562 raw_inferred_name_ = raw_inferred_name;
2563 DCHECK(inferred_name_.is_null());
2564 inferred_name_ = Handle<String>();
2567 bool pretenure() { return Pretenure::decode(bitfield_); }
2568 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2570 bool has_duplicate_parameters() {
2571 return HasDuplicateParameters::decode(bitfield_);
2574 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2576 // This is used as a heuristic on when to eagerly compile a function
2577 // literal. We consider the following constructs as hints that the
2578 // function will be called immediately:
2579 // - (function() { ... })();
2580 // - var x = function() { ... }();
2581 bool should_eager_compile() const {
2582 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2584 void set_should_eager_compile() {
2585 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2588 // A hint that we expect this function to be called (exactly) once,
2589 // i.e. we suspect it's an initialization function.
2590 bool should_be_used_once_hint() const {
2591 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2593 void set_should_be_used_once_hint() {
2594 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2597 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2599 int ast_node_count() { return ast_properties_.node_count(); }
2600 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2601 void set_ast_properties(AstProperties* ast_properties) {
2602 ast_properties_ = *ast_properties;
2604 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2605 return ast_properties_.get_spec();
2607 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2608 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2609 void set_dont_optimize_reason(BailoutReason reason) {
2610 dont_optimize_reason_ = reason;
2614 FunctionLiteral(Zone* zone, const AstRawString* name,
2615 AstValueFactory* ast_value_factory, Scope* scope,
2616 ZoneList<Statement*>* body, int materialized_literal_count,
2617 int expected_property_count, int parameter_count,
2618 FunctionType function_type,
2619 ParameterFlag has_duplicate_parameters,
2620 IsFunctionFlag is_function,
2621 EagerCompileHint eager_compile_hint, FunctionKind kind,
2623 : Expression(zone, position),
2627 raw_inferred_name_(ast_value_factory->empty_string()),
2628 ast_properties_(zone),
2629 dont_optimize_reason_(kNoReason),
2630 materialized_literal_count_(materialized_literal_count),
2631 expected_property_count_(expected_property_count),
2632 parameter_count_(parameter_count),
2633 function_token_position_(RelocInfo::kNoPosition) {
2634 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2635 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2636 Pretenure::encode(false) |
2637 HasDuplicateParameters::encode(has_duplicate_parameters) |
2638 IsFunction::encode(is_function) |
2639 EagerCompileHintBit::encode(eager_compile_hint) |
2640 FunctionKindBits::encode(kind) |
2641 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2642 DCHECK(IsValidFunctionKind(kind));
2646 const AstRawString* raw_name_;
2647 Handle<String> name_;
2649 ZoneList<Statement*>* body_;
2650 const AstString* raw_inferred_name_;
2651 Handle<String> inferred_name_;
2652 AstProperties ast_properties_;
2653 BailoutReason dont_optimize_reason_;
2655 int materialized_literal_count_;
2656 int expected_property_count_;
2657 int parameter_count_;
2658 int function_token_position_;
2661 class IsExpression : public BitField<bool, 0, 1> {};
2662 class IsAnonymous : public BitField<bool, 1, 1> {};
2663 class Pretenure : public BitField<bool, 2, 1> {};
2664 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2665 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2666 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2667 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2668 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2673 class ClassLiteral final : public Expression {
2675 typedef ObjectLiteralProperty Property;
2677 DECLARE_NODE_TYPE(ClassLiteral)
2679 Handle<String> name() const { return raw_name_->string(); }
2680 const AstRawString* raw_name() const { return raw_name_; }
2681 Scope* scope() const { return scope_; }
2682 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2683 Expression* extends() const { return extends_; }
2684 FunctionLiteral* constructor() const { return constructor_; }
2685 ZoneList<Property*>* properties() const { return properties_; }
2686 int start_position() const { return position(); }
2687 int end_position() const { return end_position_; }
2689 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2690 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2691 BailoutId ExitId() { return BailoutId(local_id(2)); }
2692 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2694 // Return an AST id for a property that is used in simulate instructions.
2695 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2697 // Unlike other AST nodes, this number of bailout IDs allocated for an
2698 // ClassLiteral can vary, so num_ids() is not a static method.
2699 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2701 // Object literals need one feedback slot for each non-trivial value, as well
2702 // as some slots for home objects.
2703 FeedbackVectorRequirements ComputeFeedbackRequirements(
2704 Isolate* isolate, const ICSlotCache* cache) override;
2705 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2706 ICSlotCache* cache) override {
2709 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2710 FeedbackVectorICSlot GetNthSlot(int n) const {
2711 return FeedbackVectorICSlot(slot_.ToInt() + n);
2714 // If value needs a home object, returns a valid feedback vector ic slot
2715 // given by slot_index, and increments slot_index.
2716 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2717 int* slot_index) const;
2720 int slot_count() const { return slot_count_; }
2724 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2725 VariableProxy* class_variable_proxy, Expression* extends,
2726 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2727 int start_position, int end_position)
2728 : Expression(zone, start_position),
2731 class_variable_proxy_(class_variable_proxy),
2733 constructor_(constructor),
2734 properties_(properties),
2735 end_position_(end_position),
2739 slot_(FeedbackVectorICSlot::Invalid()) {
2742 static int parent_num_ids() { return Expression::num_ids(); }
2745 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2747 const AstRawString* raw_name_;
2749 VariableProxy* class_variable_proxy_;
2750 Expression* extends_;
2751 FunctionLiteral* constructor_;
2752 ZoneList<Property*>* properties_;
2755 // slot_count_ helps validate that the logic to allocate ic slots and the
2756 // logic to use them are in sync.
2759 FeedbackVectorICSlot slot_;
2763 class NativeFunctionLiteral final : public Expression {
2765 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2767 Handle<String> name() const { return name_->string(); }
2768 v8::Extension* extension() const { return extension_; }
2771 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2772 v8::Extension* extension, int pos)
2773 : Expression(zone, pos), name_(name), extension_(extension) {}
2776 const AstRawString* name_;
2777 v8::Extension* extension_;
2781 class ThisFunction final : public Expression {
2783 DECLARE_NODE_TYPE(ThisFunction)
2786 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2790 class SuperPropertyReference final : public Expression {
2792 DECLARE_NODE_TYPE(SuperPropertyReference)
2794 VariableProxy* this_var() const { return this_var_; }
2795 Expression* home_object() const { return home_object_; }
2798 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2799 Expression* home_object, int pos)
2800 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2801 DCHECK(this_var->is_this());
2802 DCHECK(home_object->IsProperty());
2806 VariableProxy* this_var_;
2807 Expression* home_object_;
2811 class SuperCallReference final : public Expression {
2813 DECLARE_NODE_TYPE(SuperCallReference)
2815 VariableProxy* this_var() const { return this_var_; }
2816 VariableProxy* new_target_var() const { return new_target_var_; }
2817 VariableProxy* this_function_var() const { return this_function_var_; }
2820 SuperCallReference(Zone* zone, VariableProxy* this_var,
2821 VariableProxy* new_target_var,
2822 VariableProxy* this_function_var, int pos)
2823 : Expression(zone, pos),
2824 this_var_(this_var),
2825 new_target_var_(new_target_var),
2826 this_function_var_(this_function_var) {
2827 DCHECK(this_var->is_this());
2828 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo("new.target"));
2829 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2833 VariableProxy* this_var_;
2834 VariableProxy* new_target_var_;
2835 VariableProxy* this_function_var_;
2839 #undef DECLARE_NODE_TYPE
2842 // ----------------------------------------------------------------------------
2843 // Regular expressions
2846 class RegExpVisitor BASE_EMBEDDED {
2848 virtual ~RegExpVisitor() { }
2849 #define MAKE_CASE(Name) \
2850 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2851 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2856 class RegExpTree : public ZoneObject {
2858 static const int kInfinity = kMaxInt;
2859 virtual ~RegExpTree() {}
2860 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2861 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2862 RegExpNode* on_success) = 0;
2863 virtual bool IsTextElement() { return false; }
2864 virtual bool IsAnchoredAtStart() { return false; }
2865 virtual bool IsAnchoredAtEnd() { return false; }
2866 virtual int min_match() = 0;
2867 virtual int max_match() = 0;
2868 // Returns the interval of registers used for captures within this
2870 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2871 virtual void AppendToText(RegExpText* text, Zone* zone);
2872 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2873 #define MAKE_ASTYPE(Name) \
2874 virtual RegExp##Name* As##Name(); \
2875 virtual bool Is##Name();
2876 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2881 class RegExpDisjunction final : public RegExpTree {
2883 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2884 void* Accept(RegExpVisitor* visitor, void* data) override;
2885 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2886 RegExpNode* on_success) override;
2887 RegExpDisjunction* AsDisjunction() override;
2888 Interval CaptureRegisters() override;
2889 bool IsDisjunction() override;
2890 bool IsAnchoredAtStart() override;
2891 bool IsAnchoredAtEnd() override;
2892 int min_match() override { return min_match_; }
2893 int max_match() override { return max_match_; }
2894 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2896 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2897 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2898 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2899 ZoneList<RegExpTree*>* alternatives_;
2905 class RegExpAlternative final : public RegExpTree {
2907 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2908 void* Accept(RegExpVisitor* visitor, void* data) override;
2909 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2910 RegExpNode* on_success) override;
2911 RegExpAlternative* AsAlternative() override;
2912 Interval CaptureRegisters() override;
2913 bool IsAlternative() override;
2914 bool IsAnchoredAtStart() override;
2915 bool IsAnchoredAtEnd() override;
2916 int min_match() override { return min_match_; }
2917 int max_match() override { return max_match_; }
2918 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2920 ZoneList<RegExpTree*>* nodes_;
2926 class RegExpAssertion final : public RegExpTree {
2928 enum AssertionType {
2936 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2937 void* Accept(RegExpVisitor* visitor, void* data) override;
2938 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2939 RegExpNode* on_success) override;
2940 RegExpAssertion* AsAssertion() override;
2941 bool IsAssertion() override;
2942 bool IsAnchoredAtStart() override;
2943 bool IsAnchoredAtEnd() override;
2944 int min_match() override { return 0; }
2945 int max_match() override { return 0; }
2946 AssertionType assertion_type() { return assertion_type_; }
2948 AssertionType assertion_type_;
2952 class CharacterSet final BASE_EMBEDDED {
2954 explicit CharacterSet(uc16 standard_set_type)
2956 standard_set_type_(standard_set_type) {}
2957 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2959 standard_set_type_(0) {}
2960 ZoneList<CharacterRange>* ranges(Zone* zone);
2961 uc16 standard_set_type() { return standard_set_type_; }
2962 void set_standard_set_type(uc16 special_set_type) {
2963 standard_set_type_ = special_set_type;
2965 bool is_standard() { return standard_set_type_ != 0; }
2966 void Canonicalize();
2968 ZoneList<CharacterRange>* ranges_;
2969 // If non-zero, the value represents a standard set (e.g., all whitespace
2970 // characters) without having to expand the ranges.
2971 uc16 standard_set_type_;
2975 class RegExpCharacterClass final : public RegExpTree {
2977 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2979 is_negated_(is_negated) { }
2980 explicit RegExpCharacterClass(uc16 type)
2982 is_negated_(false) { }
2983 void* Accept(RegExpVisitor* visitor, void* data) override;
2984 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2985 RegExpNode* on_success) override;
2986 RegExpCharacterClass* AsCharacterClass() override;
2987 bool IsCharacterClass() override;
2988 bool IsTextElement() override { return true; }
2989 int min_match() override { return 1; }
2990 int max_match() override { return 1; }
2991 void AppendToText(RegExpText* text, Zone* zone) override;
2992 CharacterSet character_set() { return set_; }
2993 // TODO(lrn): Remove need for complex version if is_standard that
2994 // recognizes a mangled standard set and just do { return set_.is_special(); }
2995 bool is_standard(Zone* zone);
2996 // Returns a value representing the standard character set if is_standard()
2998 // Currently used values are:
2999 // s : unicode whitespace
3000 // S : unicode non-whitespace
3001 // w : ASCII word character (digit, letter, underscore)
3002 // W : non-ASCII word character
3004 // D : non-ASCII digit
3005 // . : non-unicode non-newline
3006 // * : All characters
3007 uc16 standard_type() { return set_.standard_set_type(); }
3008 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3009 bool is_negated() { return is_negated_; }
3017 class RegExpAtom final : public RegExpTree {
3019 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3020 void* Accept(RegExpVisitor* visitor, void* data) override;
3021 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3022 RegExpNode* on_success) override;
3023 RegExpAtom* AsAtom() override;
3024 bool IsAtom() override;
3025 bool IsTextElement() override { return true; }
3026 int min_match() override { return data_.length(); }
3027 int max_match() override { return data_.length(); }
3028 void AppendToText(RegExpText* text, Zone* zone) override;
3029 Vector<const uc16> data() { return data_; }
3030 int length() { return data_.length(); }
3032 Vector<const uc16> data_;
3036 class RegExpText final : public RegExpTree {
3038 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3039 void* Accept(RegExpVisitor* visitor, void* data) override;
3040 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3041 RegExpNode* on_success) override;
3042 RegExpText* AsText() override;
3043 bool IsText() override;
3044 bool IsTextElement() override { return true; }
3045 int min_match() override { return length_; }
3046 int max_match() override { return length_; }
3047 void AppendToText(RegExpText* text, Zone* zone) override;
3048 void AddElement(TextElement elm, Zone* zone) {
3049 elements_.Add(elm, zone);
3050 length_ += elm.length();
3052 ZoneList<TextElement>* elements() { return &elements_; }
3054 ZoneList<TextElement> elements_;
3059 class RegExpQuantifier final : public RegExpTree {
3061 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3062 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3066 min_match_(min * body->min_match()),
3067 quantifier_type_(type) {
3068 if (max > 0 && body->max_match() > kInfinity / max) {
3069 max_match_ = kInfinity;
3071 max_match_ = max * body->max_match();
3074 void* Accept(RegExpVisitor* visitor, void* data) override;
3075 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3076 RegExpNode* on_success) override;
3077 static RegExpNode* ToNode(int min,
3081 RegExpCompiler* compiler,
3082 RegExpNode* on_success,
3083 bool not_at_start = false);
3084 RegExpQuantifier* AsQuantifier() override;
3085 Interval CaptureRegisters() override;
3086 bool IsQuantifier() override;
3087 int min_match() override { return min_match_; }
3088 int max_match() override { return max_match_; }
3089 int min() { return min_; }
3090 int max() { return max_; }
3091 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3092 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3093 bool is_greedy() { return quantifier_type_ == GREEDY; }
3094 RegExpTree* body() { return body_; }
3102 QuantifierType quantifier_type_;
3106 class RegExpCapture final : public RegExpTree {
3108 explicit RegExpCapture(RegExpTree* body, int index)
3109 : body_(body), index_(index) { }
3110 void* Accept(RegExpVisitor* visitor, void* data) override;
3111 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3112 RegExpNode* on_success) override;
3113 static RegExpNode* ToNode(RegExpTree* body,
3115 RegExpCompiler* compiler,
3116 RegExpNode* on_success);
3117 RegExpCapture* AsCapture() override;
3118 bool IsAnchoredAtStart() override;
3119 bool IsAnchoredAtEnd() override;
3120 Interval CaptureRegisters() override;
3121 bool IsCapture() override;
3122 int min_match() override { return body_->min_match(); }
3123 int max_match() override { return body_->max_match(); }
3124 RegExpTree* body() { return body_; }
3125 int index() { return index_; }
3126 static int StartRegister(int index) { return index * 2; }
3127 static int EndRegister(int index) { return index * 2 + 1; }
3135 class RegExpLookahead final : public RegExpTree {
3137 RegExpLookahead(RegExpTree* body,
3142 is_positive_(is_positive),
3143 capture_count_(capture_count),
3144 capture_from_(capture_from) { }
3146 void* Accept(RegExpVisitor* visitor, void* data) override;
3147 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3148 RegExpNode* on_success) override;
3149 RegExpLookahead* AsLookahead() override;
3150 Interval CaptureRegisters() override;
3151 bool IsLookahead() override;
3152 bool IsAnchoredAtStart() override;
3153 int min_match() override { return 0; }
3154 int max_match() override { return 0; }
3155 RegExpTree* body() { return body_; }
3156 bool is_positive() { return is_positive_; }
3157 int capture_count() { return capture_count_; }
3158 int capture_from() { return capture_from_; }
3168 class RegExpBackReference final : public RegExpTree {
3170 explicit RegExpBackReference(RegExpCapture* capture)
3171 : capture_(capture) { }
3172 void* Accept(RegExpVisitor* visitor, void* data) override;
3173 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3174 RegExpNode* on_success) override;
3175 RegExpBackReference* AsBackReference() override;
3176 bool IsBackReference() override;
3177 int min_match() override { return 0; }
3178 int max_match() override { return capture_->max_match(); }
3179 int index() { return capture_->index(); }
3180 RegExpCapture* capture() { return capture_; }
3182 RegExpCapture* capture_;
3186 class RegExpEmpty final : public RegExpTree {
3189 void* Accept(RegExpVisitor* visitor, void* data) override;
3190 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3191 RegExpNode* on_success) override;
3192 RegExpEmpty* AsEmpty() override;
3193 bool IsEmpty() override;
3194 int min_match() override { return 0; }
3195 int max_match() override { return 0; }
3199 // ----------------------------------------------------------------------------
3201 // - leaf node visitors are abstract.
3203 class AstVisitor BASE_EMBEDDED {
3206 virtual ~AstVisitor() {}
3208 // Stack overflow check and dynamic dispatch.
3209 virtual void Visit(AstNode* node) = 0;
3211 // Iteration left-to-right.
3212 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3213 virtual void VisitStatements(ZoneList<Statement*>* statements);
3214 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3216 // Individual AST nodes.
3217 #define DEF_VISIT(type) \
3218 virtual void Visit##type(type* node) = 0;
3219 AST_NODE_LIST(DEF_VISIT)
3224 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3226 void Visit(AstNode* node) final { \
3227 if (!CheckStackOverflow()) node->Accept(this); \
3230 void SetStackOverflow() { stack_overflow_ = true; } \
3231 void ClearStackOverflow() { stack_overflow_ = false; } \
3232 bool HasStackOverflow() const { return stack_overflow_; } \
3234 bool CheckStackOverflow() { \
3235 if (stack_overflow_) return true; \
3236 StackLimitCheck check(isolate_); \
3237 if (!check.HasOverflowed()) return false; \
3238 stack_overflow_ = true; \
3243 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3244 isolate_ = isolate; \
3246 stack_overflow_ = false; \
3248 Zone* zone() { return zone_; } \
3249 Isolate* isolate() { return isolate_; } \
3251 Isolate* isolate_; \
3253 bool stack_overflow_
3256 // ----------------------------------------------------------------------------
3259 class AstNodeFactory final BASE_EMBEDDED {
3261 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3262 : zone_(ast_value_factory->zone()),
3263 ast_value_factory_(ast_value_factory) {}
3265 VariableDeclaration* NewVariableDeclaration(
3266 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3267 bool is_class_declaration = false, int declaration_group_start = -1) {
3269 VariableDeclaration(zone_, proxy, mode, scope, pos,
3270 is_class_declaration, declaration_group_start);
3273 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3275 FunctionLiteral* fun,
3278 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3281 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3282 const AstRawString* import_name,
3283 const AstRawString* module_specifier,
3284 Scope* scope, int pos) {
3285 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3286 module_specifier, scope, pos);
3289 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3292 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3295 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3296 bool ignore_completion_value, int pos) {
3298 Block(zone_, labels, capacity, ignore_completion_value, pos);
3301 #define STATEMENT_WITH_LABELS(NodeType) \
3302 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3303 return new (zone_) NodeType(zone_, labels, pos); \
3305 STATEMENT_WITH_LABELS(DoWhileStatement)
3306 STATEMENT_WITH_LABELS(WhileStatement)
3307 STATEMENT_WITH_LABELS(ForStatement)
3308 STATEMENT_WITH_LABELS(SwitchStatement)
3309 #undef STATEMENT_WITH_LABELS
3311 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3312 ZoneList<const AstRawString*>* labels,
3314 switch (visit_mode) {
3315 case ForEachStatement::ENUMERATE: {
3316 return new (zone_) ForInStatement(zone_, labels, pos);
3318 case ForEachStatement::ITERATE: {
3319 return new (zone_) ForOfStatement(zone_, labels, pos);
3326 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3327 return new (zone_) ExpressionStatement(zone_, expression, pos);
3330 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3331 return new (zone_) ContinueStatement(zone_, target, pos);
3334 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3335 return new (zone_) BreakStatement(zone_, target, pos);
3338 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3339 return new (zone_) ReturnStatement(zone_, expression, pos);
3342 WithStatement* NewWithStatement(Scope* scope,
3343 Expression* expression,
3344 Statement* statement,
3346 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3349 IfStatement* NewIfStatement(Expression* condition,
3350 Statement* then_statement,
3351 Statement* else_statement,
3354 IfStatement(zone_, condition, then_statement, else_statement, pos);
3357 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3359 Block* catch_block, int pos) {
3361 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3364 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3365 Block* finally_block, int pos) {
3367 TryFinallyStatement(zone_, try_block, finally_block, pos);
3370 DebuggerStatement* NewDebuggerStatement(int pos) {
3371 return new (zone_) DebuggerStatement(zone_, pos);
3374 EmptyStatement* NewEmptyStatement(int pos) {
3375 return new(zone_) EmptyStatement(zone_, pos);
3378 CaseClause* NewCaseClause(
3379 Expression* label, ZoneList<Statement*>* statements, int pos) {
3380 return new (zone_) CaseClause(zone_, label, statements, pos);
3383 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3385 Literal(zone_, ast_value_factory_->NewString(string), pos);
3388 // A JavaScript symbol (ECMA-262 edition 6).
3389 Literal* NewSymbolLiteral(const char* name, int pos) {
3390 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3393 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3395 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3398 Literal* NewSmiLiteral(int number, int pos) {
3399 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3402 Literal* NewBooleanLiteral(bool b, int pos) {
3403 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3406 Literal* NewNullLiteral(int pos) {
3407 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3410 Literal* NewUndefinedLiteral(int pos) {
3411 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3414 Literal* NewTheHoleLiteral(int pos) {
3415 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3418 ObjectLiteral* NewObjectLiteral(
3419 ZoneList<ObjectLiteral::Property*>* properties,
3421 int boilerplate_properties,
3425 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3426 boilerplate_properties, has_function,
3430 ObjectLiteral::Property* NewObjectLiteralProperty(
3431 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3432 bool is_static, bool is_computed_name) {
3434 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3437 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3440 bool is_computed_name) {
3441 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3442 is_static, is_computed_name);
3445 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3446 const AstRawString* flags,
3450 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3454 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3459 ArrayLiteral(zone_, values, -1, literal_index, is_strong, pos);
3462 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3463 int first_spread_index, int literal_index,
3464 bool is_strong, int pos) {
3465 return new (zone_) ArrayLiteral(zone_, values, first_spread_index,
3466 literal_index, is_strong, pos);
3469 VariableProxy* NewVariableProxy(Variable* var,
3470 int start_position = RelocInfo::kNoPosition,
3471 int end_position = RelocInfo::kNoPosition) {
3472 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3475 VariableProxy* NewVariableProxy(const AstRawString* name,
3476 Variable::Kind variable_kind,
3477 int start_position = RelocInfo::kNoPosition,
3478 int end_position = RelocInfo::kNoPosition) {
3479 DCHECK_NOT_NULL(name);
3481 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3484 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3485 return new (zone_) Property(zone_, obj, key, pos);
3488 Call* NewCall(Expression* expression,
3489 ZoneList<Expression*>* arguments,
3491 return new (zone_) Call(zone_, expression, arguments, pos);
3494 CallNew* NewCallNew(Expression* expression,
3495 ZoneList<Expression*>* arguments,
3497 return new (zone_) CallNew(zone_, expression, arguments, pos);
3500 CallRuntime* NewCallRuntime(const AstRawString* name,
3501 const Runtime::Function* function,
3502 ZoneList<Expression*>* arguments,
3504 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3507 UnaryOperation* NewUnaryOperation(Token::Value op,
3508 Expression* expression,
3510 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3513 BinaryOperation* NewBinaryOperation(Token::Value op,
3517 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3520 CountOperation* NewCountOperation(Token::Value op,
3524 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3527 CompareOperation* NewCompareOperation(Token::Value op,
3531 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3534 Spread* NewSpread(Expression* expression, int pos) {
3535 return new (zone_) Spread(zone_, expression, pos);
3538 Conditional* NewConditional(Expression* condition,
3539 Expression* then_expression,
3540 Expression* else_expression,
3542 return new (zone_) Conditional(zone_, condition, then_expression,
3543 else_expression, position);
3546 Assignment* NewAssignment(Token::Value op,
3550 DCHECK(Token::IsAssignmentOp(op));
3551 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3552 if (assign->is_compound()) {
3553 DCHECK(Token::IsAssignmentOp(op));
3554 assign->binary_operation_ =
3555 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3560 Yield* NewYield(Expression *generator_object,
3561 Expression* expression,
3562 Yield::Kind yield_kind,
3564 if (!expression) expression = NewUndefinedLiteral(pos);
3566 Yield(zone_, generator_object, expression, yield_kind, pos);
3569 Throw* NewThrow(Expression* exception, int pos) {
3570 return new (zone_) Throw(zone_, exception, pos);
3573 FunctionLiteral* NewFunctionLiteral(
3574 const AstRawString* name, AstValueFactory* ast_value_factory,
3575 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3576 int expected_property_count, int parameter_count,
3577 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3578 FunctionLiteral::FunctionType function_type,
3579 FunctionLiteral::IsFunctionFlag is_function,
3580 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3582 return new (zone_) FunctionLiteral(
3583 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3584 expected_property_count, parameter_count, function_type,
3585 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3589 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3590 VariableProxy* proxy, Expression* extends,
3591 FunctionLiteral* constructor,
3592 ZoneList<ObjectLiteral::Property*>* properties,
3593 int start_position, int end_position) {
3595 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3596 properties, start_position, end_position);
3599 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3600 v8::Extension* extension,
3602 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3605 ThisFunction* NewThisFunction(int pos) {
3606 return new (zone_) ThisFunction(zone_, pos);
3609 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3610 Expression* home_object,
3613 SuperPropertyReference(zone_, this_var, home_object, pos);
3616 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3617 VariableProxy* new_target_var,
3618 VariableProxy* this_function_var,
3620 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3621 this_function_var, pos);
3626 AstValueFactory* ast_value_factory_;
3630 } } // namespace v8::internal