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.
8 #include "src/assembler.h"
9 #include "src/ast-value-factory.h"
10 #include "src/bailout-reason.h"
11 #include "src/base/flags.h"
12 #include "src/base/smart-pointers.h"
13 #include "src/factory.h"
14 #include "src/isolate.h"
16 #include "src/modules.h"
17 #include "src/regexp/jsregexp.h"
18 #include "src/runtime/runtime.h"
19 #include "src/small-pointer-list.h"
20 #include "src/token.h"
21 #include "src/types.h"
22 #include "src/utils.h"
23 #include "src/variables.h"
28 // The abstract syntax tree is an intermediate, light-weight
29 // representation of the parsed JavaScript code suitable for
30 // compilation to native code.
32 // Nodes are allocated in a separate zone, which allows faster
33 // allocation and constant-time deallocation of the entire syntax
37 // ----------------------------------------------------------------------------
38 // Nodes of the abstract syntax tree. Only concrete classes are
41 #define DECLARATION_NODE_LIST(V) \
42 V(VariableDeclaration) \
43 V(FunctionDeclaration) \
44 V(ImportDeclaration) \
47 #define STATEMENT_NODE_LIST(V) \
49 V(ExpressionStatement) \
52 V(ContinueStatement) \
62 V(TryCatchStatement) \
63 V(TryFinallyStatement) \
66 #define EXPRESSION_NODE_LIST(V) \
69 V(NativeFunctionLiteral) \
89 V(SuperPropertyReference) \
90 V(SuperCallReference) \
93 #define AST_NODE_LIST(V) \
94 DECLARATION_NODE_LIST(V) \
95 STATEMENT_NODE_LIST(V) \
96 EXPRESSION_NODE_LIST(V)
98 // Forward declarations
103 class BreakableStatement;
105 class IterationStatement;
106 class MaterializedLiteral;
108 class TypeFeedbackOracle;
110 class RegExpAlternative;
111 class RegExpAssertion;
113 class RegExpBackReference;
115 class RegExpCharacterClass;
116 class RegExpCompiler;
117 class RegExpDisjunction;
119 class RegExpLookahead;
120 class RegExpQuantifier;
123 #define DEF_FORWARD_DECLARATION(type) class type;
124 AST_NODE_LIST(DEF_FORWARD_DECLARATION)
125 #undef DEF_FORWARD_DECLARATION
128 // Typedef only introduced to avoid unreadable code.
129 typedef ZoneList<Handle<String>> ZoneStringList;
130 typedef ZoneList<Handle<Object>> ZoneObjectList;
133 #define DECLARE_NODE_TYPE(type) \
134 void Accept(AstVisitor* v) override; \
135 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
136 friend class AstNodeFactory;
139 class FeedbackVectorRequirements {
141 FeedbackVectorRequirements(int slots, int ic_slots)
142 : slots_(slots), ic_slots_(ic_slots) {}
144 int slots() const { return slots_; }
145 int ic_slots() const { return ic_slots_; }
155 explicit ICSlotCache(Zone* zone)
157 hash_map_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
158 ZoneAllocationPolicy(zone)) {}
160 void Put(Variable* variable, FeedbackVectorICSlot slot) {
161 ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
162 variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
163 entry->value = reinterpret_cast<void*>(slot.ToInt());
166 ZoneHashMap::Entry* Get(Variable* variable) const {
167 return hash_map_.Lookup(variable, ComputePointerHash(variable));
172 ZoneHashMap hash_map_;
176 class AstProperties final BASE_EMBEDDED {
180 kDontSelfOptimize = 1 << 0,
181 kDontCrankshaft = 1 << 1
184 typedef base::Flags<Flag> Flags;
186 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
188 Flags& flags() { return flags_; }
189 Flags flags() const { return flags_; }
190 int node_count() { return node_count_; }
191 void add_node_count(int count) { node_count_ += count; }
193 int slots() const { return spec_.slots(); }
194 void increase_slots(int count) { spec_.increase_slots(count); }
196 int ic_slots() const { return spec_.ic_slots(); }
197 void increase_ic_slots(int count) { spec_.increase_ic_slots(count); }
198 void SetKind(int ic_slot, Code::Kind kind) { spec_.SetKind(ic_slot, kind); }
199 const ZoneFeedbackVectorSpec* get_spec() const { return &spec_; }
204 ZoneFeedbackVectorSpec spec_;
207 DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
210 class AstNode: public ZoneObject {
212 #define DECLARE_TYPE_ENUM(type) k##type,
214 AST_NODE_LIST(DECLARE_TYPE_ENUM)
217 #undef DECLARE_TYPE_ENUM
219 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
221 explicit AstNode(int position): position_(position) {}
222 virtual ~AstNode() {}
224 virtual void Accept(AstVisitor* v) = 0;
225 virtual NodeType node_type() const = 0;
226 int position() const { return position_; }
228 // Type testing & conversion functions overridden by concrete subclasses.
229 #define DECLARE_NODE_FUNCTIONS(type) \
230 bool Is##type() const { return node_type() == AstNode::k##type; } \
232 return Is##type() ? reinterpret_cast<type*>(this) : NULL; \
234 const type* As##type() const { \
235 return Is##type() ? reinterpret_cast<const type*>(this) : NULL; \
237 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
238 #undef DECLARE_NODE_FUNCTIONS
240 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
241 virtual IterationStatement* AsIterationStatement() { return NULL; }
242 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
244 // The interface for feedback slots, with default no-op implementations for
245 // node types which don't actually have this. Note that this is conceptually
246 // not really nice, but multiple inheritance would introduce yet another
247 // vtable entry per node, something we don't want for space reasons.
248 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
249 Isolate* isolate, const ICSlotCache* cache) {
250 return FeedbackVectorRequirements(0, 0);
252 virtual void SetFirstFeedbackSlot(FeedbackVectorSlot slot) { UNREACHABLE(); }
253 virtual void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
254 ICSlotCache* cache) {
257 // Each ICSlot stores a kind of IC which the participating node should know.
258 virtual Code::Kind FeedbackICSlotKind(int index) {
260 return Code::NUMBER_OF_KINDS;
264 // Hidden to prevent accidental usage. It would have to load the
265 // current zone from the TLS.
266 void* operator new(size_t size);
268 friend class CaseClause; // Generates AST IDs.
274 class Statement : public AstNode {
276 explicit Statement(Zone* zone, int position) : AstNode(position) {}
278 bool IsEmpty() { return AsEmptyStatement() != NULL; }
279 virtual bool IsJump() const { return false; }
283 class SmallMapList final {
286 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
288 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
289 void Clear() { list_.Clear(); }
290 void Sort() { list_.Sort(); }
292 bool is_empty() const { return list_.is_empty(); }
293 int length() const { return list_.length(); }
295 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
296 if (!Map::TryUpdate(map).ToHandle(&map)) return;
297 for (int i = 0; i < length(); ++i) {
298 if (at(i).is_identical_to(map)) return;
303 void FilterForPossibleTransitions(Map* root_map) {
304 for (int i = list_.length() - 1; i >= 0; i--) {
305 if (at(i)->FindRootMap() != root_map) {
306 list_.RemoveElement(list_.at(i));
311 void Add(Handle<Map> handle, Zone* zone) {
312 list_.Add(handle.location(), zone);
315 Handle<Map> at(int i) const {
316 return Handle<Map>(list_.at(i));
319 Handle<Map> first() const { return at(0); }
320 Handle<Map> last() const { return at(length() - 1); }
323 // The list stores pointers to Map*, that is Map**, so it's GC safe.
324 SmallPointerList<Map*> list_;
326 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
330 class Expression : public AstNode {
333 // Not assigned a context yet, or else will not be visited during
336 // Evaluated for its side effects.
338 // Evaluated for its value (and side effects).
340 // Evaluated for control flow (and side effects).
344 // True iff the expression is a valid reference expression.
345 virtual bool IsValidReferenceExpression() const { return false; }
347 // Helpers for ToBoolean conversion.
348 virtual bool ToBooleanIsTrue() const { return false; }
349 virtual bool ToBooleanIsFalse() const { return false; }
351 // Symbols that cannot be parsed as array indices are considered property
352 // names. We do not treat symbols that can be array indexes as property
353 // names because [] for string objects is handled only by keyed ICs.
354 virtual bool IsPropertyName() const { return false; }
356 // True iff the expression is a literal represented as a smi.
357 bool IsSmiLiteral() const;
359 // True iff the expression is a string literal.
360 bool IsStringLiteral() const;
362 // True iff the expression is the null literal.
363 bool IsNullLiteral() const;
365 // True if we can prove that the expression is the undefined literal.
366 bool IsUndefinedLiteral(Isolate* isolate) const;
368 // True iff the expression is a valid target for an assignment.
369 bool IsValidReferenceExpressionOrThis() const;
371 // Expression type bounds
372 Bounds bounds() const { return bounds_; }
373 void set_bounds(Bounds bounds) { bounds_ = bounds; }
375 // Type feedback information for assignments and properties.
376 virtual bool IsMonomorphic() {
380 virtual SmallMapList* GetReceiverTypes() {
384 virtual KeyedAccessStoreMode GetStoreMode() const {
386 return STANDARD_STORE;
388 virtual IcCheckType GetKeyType() const {
393 // TODO(rossberg): this should move to its own AST node eventually.
394 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
395 uint16_t to_boolean_types() const {
396 return ToBooleanTypesField::decode(bit_field_);
399 void set_base_id(int id) { base_id_ = id; }
400 static int num_ids() { return parent_num_ids() + 2; }
401 BailoutId id() const { return BailoutId(local_id(0)); }
402 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
405 Expression(Zone* zone, int pos)
407 base_id_(BailoutId::None().ToInt()),
408 bounds_(Bounds::Unbounded(zone)),
410 static int parent_num_ids() { return 0; }
411 void set_to_boolean_types(uint16_t types) {
412 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
415 int base_id() const {
416 DCHECK(!BailoutId(base_id_).IsNone());
421 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
425 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
427 // Ends with 16-bit field; deriving classes in turn begin with
428 // 16-bit fields for optimum packing efficiency.
432 class BreakableStatement : public Statement {
435 TARGET_FOR_ANONYMOUS,
436 TARGET_FOR_NAMED_ONLY
439 // The labels associated with this statement. May be NULL;
440 // if it is != NULL, guaranteed to contain at least one entry.
441 ZoneList<const AstRawString*>* labels() const { return labels_; }
443 // Type testing & conversion.
444 BreakableStatement* AsBreakableStatement() final { return this; }
447 Label* break_target() { return &break_target_; }
450 bool is_target_for_anonymous() const {
451 return breakable_type_ == TARGET_FOR_ANONYMOUS;
454 void set_base_id(int id) { base_id_ = id; }
455 static int num_ids() { return parent_num_ids() + 2; }
456 BailoutId EntryId() const { return BailoutId(local_id(0)); }
457 BailoutId ExitId() const { return BailoutId(local_id(1)); }
460 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
461 BreakableType breakable_type, int position)
462 : Statement(zone, position),
464 breakable_type_(breakable_type),
465 base_id_(BailoutId::None().ToInt()) {
466 DCHECK(labels == NULL || labels->length() > 0);
468 static int parent_num_ids() { return 0; }
470 int base_id() const {
471 DCHECK(!BailoutId(base_id_).IsNone());
476 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
478 ZoneList<const AstRawString*>* labels_;
479 BreakableType breakable_type_;
485 class Block final : public BreakableStatement {
487 DECLARE_NODE_TYPE(Block)
489 void AddStatement(Statement* statement, Zone* zone) {
490 statements_.Add(statement, zone);
493 ZoneList<Statement*>* statements() { return &statements_; }
494 bool ignore_completion_value() const { return ignore_completion_value_; }
496 static int num_ids() { return parent_num_ids() + 1; }
497 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
499 bool IsJump() const override {
500 return !statements_.is_empty() && statements_.last()->IsJump()
501 && labels() == NULL; // Good enough as an approximation...
504 Scope* scope() const { return scope_; }
505 void set_scope(Scope* scope) { scope_ = scope; }
508 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
509 bool ignore_completion_value, int pos)
510 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
511 statements_(capacity, zone),
512 ignore_completion_value_(ignore_completion_value),
514 static int parent_num_ids() { return BreakableStatement::num_ids(); }
517 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
519 ZoneList<Statement*> statements_;
520 bool ignore_completion_value_;
525 class Declaration : public AstNode {
527 VariableProxy* proxy() const { return proxy_; }
528 VariableMode mode() const { return mode_; }
529 Scope* scope() const { return scope_; }
530 virtual InitializationFlag initialization() const = 0;
531 virtual bool IsInlineable() const;
534 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
536 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
537 DCHECK(IsDeclaredVariableMode(mode));
542 VariableProxy* proxy_;
544 // Nested scope from which the declaration originated.
549 class VariableDeclaration final : public Declaration {
551 DECLARE_NODE_TYPE(VariableDeclaration)
553 InitializationFlag initialization() const override {
554 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
557 bool is_class_declaration() const { return is_class_declaration_; }
559 // VariableDeclarations can be grouped into consecutive declaration
560 // groups. Each VariableDeclaration is associated with the start position of
561 // the group it belongs to. The positions are used for strong mode scope
562 // checks for classes and functions.
563 int declaration_group_start() const { return declaration_group_start_; }
566 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
567 Scope* scope, int pos, bool is_class_declaration = false,
568 int declaration_group_start = -1)
569 : Declaration(zone, proxy, mode, scope, pos),
570 is_class_declaration_(is_class_declaration),
571 declaration_group_start_(declaration_group_start) {}
573 bool is_class_declaration_;
574 int declaration_group_start_;
578 class FunctionDeclaration final : public Declaration {
580 DECLARE_NODE_TYPE(FunctionDeclaration)
582 FunctionLiteral* fun() const { return fun_; }
583 InitializationFlag initialization() const override {
584 return kCreatedInitialized;
586 bool IsInlineable() const override;
589 FunctionDeclaration(Zone* zone,
590 VariableProxy* proxy,
592 FunctionLiteral* fun,
595 : Declaration(zone, proxy, mode, scope, pos),
597 DCHECK(mode == VAR || mode == LET || mode == CONST);
602 FunctionLiteral* fun_;
606 class ImportDeclaration final : public Declaration {
608 DECLARE_NODE_TYPE(ImportDeclaration)
610 const AstRawString* import_name() const { return import_name_; }
611 const AstRawString* module_specifier() const { return module_specifier_; }
612 void set_module_specifier(const AstRawString* module_specifier) {
613 DCHECK(module_specifier_ == NULL);
614 module_specifier_ = module_specifier;
616 InitializationFlag initialization() const override {
617 return kNeedsInitialization;
621 ImportDeclaration(Zone* zone, VariableProxy* proxy,
622 const AstRawString* import_name,
623 const AstRawString* module_specifier, Scope* scope, int pos)
624 : Declaration(zone, proxy, IMPORT, scope, pos),
625 import_name_(import_name),
626 module_specifier_(module_specifier) {}
629 const AstRawString* import_name_;
630 const AstRawString* module_specifier_;
634 class ExportDeclaration final : public Declaration {
636 DECLARE_NODE_TYPE(ExportDeclaration)
638 InitializationFlag initialization() const override {
639 return kCreatedInitialized;
643 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
644 : Declaration(zone, proxy, LET, scope, pos) {}
648 class Module : public AstNode {
650 ModuleDescriptor* descriptor() const { return descriptor_; }
651 Block* body() const { return body_; }
654 Module(Zone* zone, int pos)
655 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
656 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
657 : AstNode(pos), descriptor_(descriptor), body_(body) {}
660 ModuleDescriptor* descriptor_;
665 class IterationStatement : public BreakableStatement {
667 // Type testing & conversion.
668 IterationStatement* AsIterationStatement() final { return this; }
670 Statement* body() const { return body_; }
672 static int num_ids() { return parent_num_ids() + 1; }
673 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
674 virtual BailoutId ContinueId() const = 0;
675 virtual BailoutId StackCheckId() const = 0;
678 Label* continue_target() { return &continue_target_; }
681 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
682 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
684 static int parent_num_ids() { return BreakableStatement::num_ids(); }
685 void Initialize(Statement* body) { body_ = body; }
688 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
691 Label continue_target_;
695 class DoWhileStatement final : public IterationStatement {
697 DECLARE_NODE_TYPE(DoWhileStatement)
699 void Initialize(Expression* cond, Statement* body) {
700 IterationStatement::Initialize(body);
704 Expression* cond() const { return cond_; }
706 static int num_ids() { return parent_num_ids() + 2; }
707 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
708 BailoutId StackCheckId() const override { return BackEdgeId(); }
709 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
712 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
713 : IterationStatement(zone, labels, pos), cond_(NULL) {}
714 static int parent_num_ids() { return IterationStatement::num_ids(); }
717 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
723 class WhileStatement final : public IterationStatement {
725 DECLARE_NODE_TYPE(WhileStatement)
727 void Initialize(Expression* cond, Statement* body) {
728 IterationStatement::Initialize(body);
732 Expression* cond() const { return cond_; }
734 static int num_ids() { return parent_num_ids() + 1; }
735 BailoutId ContinueId() const override { return EntryId(); }
736 BailoutId StackCheckId() const override { return BodyId(); }
737 BailoutId BodyId() const { return BailoutId(local_id(0)); }
740 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
741 : IterationStatement(zone, labels, pos), cond_(NULL) {}
742 static int parent_num_ids() { return IterationStatement::num_ids(); }
745 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
751 class ForStatement final : public IterationStatement {
753 DECLARE_NODE_TYPE(ForStatement)
755 void Initialize(Statement* init,
759 IterationStatement::Initialize(body);
765 Statement* init() const { return init_; }
766 Expression* cond() const { return cond_; }
767 Statement* next() const { return next_; }
769 static int num_ids() { return parent_num_ids() + 2; }
770 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
771 BailoutId StackCheckId() const override { return BodyId(); }
772 BailoutId BodyId() const { return BailoutId(local_id(1)); }
775 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
776 : IterationStatement(zone, labels, pos),
780 static int parent_num_ids() { return IterationStatement::num_ids(); }
783 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
791 class ForEachStatement : public IterationStatement {
794 ENUMERATE, // for (each in subject) body;
795 ITERATE // for (each of subject) body;
798 void Initialize(Expression* each, Expression* subject, Statement* body) {
799 IterationStatement::Initialize(body);
804 Expression* each() const { return each_; }
805 Expression* subject() const { return subject_; }
807 FeedbackVectorRequirements ComputeFeedbackRequirements(
808 Isolate* isolate, const ICSlotCache* cache) override;
809 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
810 ICSlotCache* cache) override {
813 Code::Kind FeedbackICSlotKind(int index) override;
814 FeedbackVectorICSlot EachFeedbackSlot() const { return each_slot_; }
817 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
818 : IterationStatement(zone, labels, pos),
821 each_slot_(FeedbackVectorICSlot::Invalid()) {}
825 Expression* subject_;
826 FeedbackVectorICSlot each_slot_;
830 class ForInStatement final : public ForEachStatement {
832 DECLARE_NODE_TYPE(ForInStatement)
834 Expression* enumerable() const {
838 // Type feedback information.
839 FeedbackVectorRequirements ComputeFeedbackRequirements(
840 Isolate* isolate, const ICSlotCache* cache) override {
841 FeedbackVectorRequirements base =
842 ForEachStatement::ComputeFeedbackRequirements(isolate, cache);
843 DCHECK(base.slots() == 0 && base.ic_slots() <= 1);
844 return FeedbackVectorRequirements(1, base.ic_slots());
846 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
847 for_in_feedback_slot_ = slot;
850 FeedbackVectorSlot ForInFeedbackSlot() {
851 DCHECK(!for_in_feedback_slot_.IsInvalid());
852 return for_in_feedback_slot_;
855 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
856 ForInType for_in_type() const { return for_in_type_; }
857 void set_for_in_type(ForInType type) { for_in_type_ = type; }
859 static int num_ids() { return parent_num_ids() + 6; }
860 BailoutId BodyId() const { return BailoutId(local_id(0)); }
861 BailoutId PrepareId() const { return BailoutId(local_id(1)); }
862 BailoutId EnumId() const { return BailoutId(local_id(2)); }
863 BailoutId ToObjectId() const { return BailoutId(local_id(3)); }
864 BailoutId FilterId() const { return BailoutId(local_id(4)); }
865 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
866 BailoutId ContinueId() const override { return EntryId(); }
867 BailoutId StackCheckId() const override { return BodyId(); }
870 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
871 : ForEachStatement(zone, labels, pos),
872 for_in_type_(SLOW_FOR_IN),
873 for_in_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
874 static int parent_num_ids() { return ForEachStatement::num_ids(); }
877 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
879 ForInType for_in_type_;
880 FeedbackVectorSlot for_in_feedback_slot_;
884 class ForOfStatement final : public ForEachStatement {
886 DECLARE_NODE_TYPE(ForOfStatement)
888 void Initialize(Expression* each,
891 Expression* assign_iterator,
892 Expression* next_result,
893 Expression* result_done,
894 Expression* assign_each) {
895 ForEachStatement::Initialize(each, subject, body);
896 assign_iterator_ = assign_iterator;
897 next_result_ = next_result;
898 result_done_ = result_done;
899 assign_each_ = assign_each;
902 Expression* iterable() const {
906 // iterator = subject[Symbol.iterator]()
907 Expression* assign_iterator() const {
908 return assign_iterator_;
911 // result = iterator.next() // with type check
912 Expression* next_result() const {
917 Expression* result_done() const {
921 // each = result.value
922 Expression* assign_each() const {
926 BailoutId ContinueId() const override { return EntryId(); }
927 BailoutId StackCheckId() const override { return BackEdgeId(); }
929 static int num_ids() { return parent_num_ids() + 1; }
930 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
933 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
934 : ForEachStatement(zone, labels, pos),
935 assign_iterator_(NULL),
938 assign_each_(NULL) {}
939 static int parent_num_ids() { return ForEachStatement::num_ids(); }
942 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
944 Expression* assign_iterator_;
945 Expression* next_result_;
946 Expression* result_done_;
947 Expression* assign_each_;
951 class ExpressionStatement final : public Statement {
953 DECLARE_NODE_TYPE(ExpressionStatement)
955 void set_expression(Expression* e) { expression_ = e; }
956 Expression* expression() const { return expression_; }
957 bool IsJump() const override { return expression_->IsThrow(); }
960 ExpressionStatement(Zone* zone, Expression* expression, int pos)
961 : Statement(zone, pos), expression_(expression) { }
964 Expression* expression_;
968 class JumpStatement : public Statement {
970 bool IsJump() const final { return true; }
973 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
977 class ContinueStatement final : public JumpStatement {
979 DECLARE_NODE_TYPE(ContinueStatement)
981 IterationStatement* target() const { return target_; }
984 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
985 : JumpStatement(zone, pos), target_(target) { }
988 IterationStatement* target_;
992 class BreakStatement final : public JumpStatement {
994 DECLARE_NODE_TYPE(BreakStatement)
996 BreakableStatement* target() const { return target_; }
999 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
1000 : JumpStatement(zone, pos), target_(target) { }
1003 BreakableStatement* target_;
1007 class ReturnStatement final : public JumpStatement {
1009 DECLARE_NODE_TYPE(ReturnStatement)
1011 Expression* expression() const { return expression_; }
1014 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
1015 : JumpStatement(zone, pos), expression_(expression) { }
1018 Expression* expression_;
1022 class WithStatement final : public Statement {
1024 DECLARE_NODE_TYPE(WithStatement)
1026 Scope* scope() { return scope_; }
1027 Expression* expression() const { return expression_; }
1028 Statement* statement() const { return statement_; }
1030 void set_base_id(int id) { base_id_ = id; }
1031 static int num_ids() { return parent_num_ids() + 1; }
1032 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1035 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1036 Statement* statement, int pos)
1037 : Statement(zone, pos),
1039 expression_(expression),
1040 statement_(statement),
1041 base_id_(BailoutId::None().ToInt()) {}
1042 static int parent_num_ids() { return 0; }
1044 int base_id() const {
1045 DCHECK(!BailoutId(base_id_).IsNone());
1050 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1053 Expression* expression_;
1054 Statement* statement_;
1059 class CaseClause final : public Expression {
1061 DECLARE_NODE_TYPE(CaseClause)
1063 bool is_default() const { return label_ == NULL; }
1064 Expression* label() const {
1065 CHECK(!is_default());
1068 Label* body_target() { return &body_target_; }
1069 ZoneList<Statement*>* statements() const { return statements_; }
1071 static int num_ids() { return parent_num_ids() + 2; }
1072 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1073 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1075 Type* compare_type() { return compare_type_; }
1076 void set_compare_type(Type* type) { compare_type_ = type; }
1079 static int parent_num_ids() { return Expression::num_ids(); }
1082 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1084 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1088 ZoneList<Statement*>* statements_;
1089 Type* compare_type_;
1093 class SwitchStatement final : public BreakableStatement {
1095 DECLARE_NODE_TYPE(SwitchStatement)
1097 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1102 Expression* tag() const { return tag_; }
1103 ZoneList<CaseClause*>* cases() const { return cases_; }
1106 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1107 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1113 ZoneList<CaseClause*>* cases_;
1117 // If-statements always have non-null references to their then- and
1118 // else-parts. When parsing if-statements with no explicit else-part,
1119 // the parser implicitly creates an empty statement. Use the
1120 // HasThenStatement() and HasElseStatement() functions to check if a
1121 // given if-statement has a then- or an else-part containing code.
1122 class IfStatement final : public Statement {
1124 DECLARE_NODE_TYPE(IfStatement)
1126 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1127 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1129 Expression* condition() const { return condition_; }
1130 Statement* then_statement() const { return then_statement_; }
1131 Statement* else_statement() const { return else_statement_; }
1133 bool IsJump() const override {
1134 return HasThenStatement() && then_statement()->IsJump()
1135 && HasElseStatement() && else_statement()->IsJump();
1138 void set_base_id(int id) { base_id_ = id; }
1139 static int num_ids() { return parent_num_ids() + 3; }
1140 BailoutId IfId() const { return BailoutId(local_id(0)); }
1141 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1142 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1145 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1146 Statement* else_statement, int pos)
1147 : Statement(zone, pos),
1148 condition_(condition),
1149 then_statement_(then_statement),
1150 else_statement_(else_statement),
1151 base_id_(BailoutId::None().ToInt()) {}
1152 static int parent_num_ids() { return 0; }
1154 int base_id() const {
1155 DCHECK(!BailoutId(base_id_).IsNone());
1160 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1162 Expression* condition_;
1163 Statement* then_statement_;
1164 Statement* else_statement_;
1169 class TryStatement : public Statement {
1171 Block* try_block() const { return try_block_; }
1173 void set_base_id(int id) { base_id_ = id; }
1174 static int num_ids() { return parent_num_ids() + 1; }
1175 BailoutId HandlerId() const { return BailoutId(local_id(0)); }
1178 TryStatement(Zone* zone, Block* try_block, int pos)
1179 : Statement(zone, pos),
1180 try_block_(try_block),
1181 base_id_(BailoutId::None().ToInt()) {}
1182 static int parent_num_ids() { return 0; }
1184 int base_id() const {
1185 DCHECK(!BailoutId(base_id_).IsNone());
1190 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1197 class TryCatchStatement final : public TryStatement {
1199 DECLARE_NODE_TYPE(TryCatchStatement)
1201 Scope* scope() { return scope_; }
1202 Variable* variable() { return variable_; }
1203 Block* catch_block() const { return catch_block_; }
1206 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
1207 Variable* variable, Block* catch_block, int pos)
1208 : TryStatement(zone, try_block, pos),
1210 variable_(variable),
1211 catch_block_(catch_block) {}
1215 Variable* variable_;
1216 Block* catch_block_;
1220 class TryFinallyStatement final : public TryStatement {
1222 DECLARE_NODE_TYPE(TryFinallyStatement)
1224 Block* finally_block() const { return finally_block_; }
1227 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1229 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1232 Block* finally_block_;
1236 class DebuggerStatement final : public Statement {
1238 DECLARE_NODE_TYPE(DebuggerStatement)
1240 void set_base_id(int id) { base_id_ = id; }
1241 static int num_ids() { return parent_num_ids() + 1; }
1242 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1245 explicit DebuggerStatement(Zone* zone, int pos)
1246 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1247 static int parent_num_ids() { return 0; }
1249 int base_id() const {
1250 DCHECK(!BailoutId(base_id_).IsNone());
1255 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1261 class EmptyStatement final : public Statement {
1263 DECLARE_NODE_TYPE(EmptyStatement)
1266 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1270 class Literal final : public Expression {
1272 DECLARE_NODE_TYPE(Literal)
1274 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1276 Handle<String> AsPropertyName() {
1277 DCHECK(IsPropertyName());
1278 return Handle<String>::cast(value());
1281 const AstRawString* AsRawPropertyName() {
1282 DCHECK(IsPropertyName());
1283 return value_->AsString();
1286 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1287 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1289 Handle<Object> value() const { return value_->value(); }
1290 const AstValue* raw_value() const { return value_; }
1292 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1293 // only for string and number literals!
1295 static bool Match(void* literal1, void* literal2);
1297 static int num_ids() { return parent_num_ids() + 1; }
1298 TypeFeedbackId LiteralFeedbackId() const {
1299 return TypeFeedbackId(local_id(0));
1303 Literal(Zone* zone, const AstValue* value, int position)
1304 : Expression(zone, position), value_(value) {}
1305 static int parent_num_ids() { return Expression::num_ids(); }
1308 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1310 const AstValue* value_;
1314 class AstLiteralReindexer;
1316 // Base class for literals that needs space in the corresponding JSFunction.
1317 class MaterializedLiteral : public Expression {
1319 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; }
1321 int literal_index() { return literal_index_; }
1324 // only callable after initialization.
1325 DCHECK(depth_ >= 1);
1329 bool is_strong() const { return is_strong_; }
1332 MaterializedLiteral(Zone* zone, int literal_index, bool is_strong, int pos)
1333 : Expression(zone, pos),
1334 literal_index_(literal_index),
1336 is_strong_(is_strong),
1339 // A materialized literal is simple if the values consist of only
1340 // constants and simple object and array literals.
1341 bool is_simple() const { return is_simple_; }
1342 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1343 friend class CompileTimeValue;
1345 void set_depth(int depth) {
1350 // Populate the constant properties/elements fixed array.
1351 void BuildConstants(Isolate* isolate);
1352 friend class ArrayLiteral;
1353 friend class ObjectLiteral;
1355 // If the expression is a literal, return the literal value;
1356 // if the expression is a materialized literal and is simple return a
1357 // compile time value as encoded by CompileTimeValue::GetValue().
1358 // Otherwise, return undefined literal as the placeholder
1359 // in the object literal boilerplate.
1360 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1368 friend class AstLiteralReindexer;
1372 // Property is used for passing information
1373 // about an object literal's properties from the parser
1374 // to the code generator.
1375 class ObjectLiteralProperty final : public ZoneObject {
1378 CONSTANT, // Property with constant value (compile time).
1379 COMPUTED, // Property with computed value (execution time).
1380 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1381 GETTER, SETTER, // Property is an accessor function.
1382 PROTOTYPE // Property is __proto__.
1385 Expression* key() { return key_; }
1386 Expression* value() { return value_; }
1387 Kind kind() { return kind_; }
1389 // Type feedback information.
1390 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1391 Handle<Map> GetReceiverType() { return receiver_type_; }
1393 bool IsCompileTimeValue();
1395 void set_emit_store(bool emit_store);
1398 bool is_static() const { return is_static_; }
1399 bool is_computed_name() const { return is_computed_name_; }
1401 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1404 friend class AstNodeFactory;
1406 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1407 bool is_static, bool is_computed_name);
1408 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1409 Expression* value, bool is_static,
1410 bool is_computed_name);
1418 bool is_computed_name_;
1419 Handle<Map> receiver_type_;
1423 // An object literal has a boilerplate object that is used
1424 // for minimizing the work when constructing it at runtime.
1425 class ObjectLiteral final : public MaterializedLiteral {
1427 typedef ObjectLiteralProperty Property;
1429 DECLARE_NODE_TYPE(ObjectLiteral)
1431 Handle<FixedArray> constant_properties() const {
1432 return constant_properties_;
1434 int properties_count() const { return constant_properties_->length() / 2; }
1435 ZoneList<Property*>* properties() const { return properties_; }
1436 bool fast_elements() const { return fast_elements_; }
1437 bool may_store_doubles() const { return may_store_doubles_; }
1438 bool has_function() const { return has_function_; }
1439 bool has_elements() const { return has_elements_; }
1441 // Decide if a property should be in the object boilerplate.
1442 static bool IsBoilerplateProperty(Property* property);
1444 // Populate the constant properties fixed array.
1445 void BuildConstantProperties(Isolate* isolate);
1447 // Mark all computed expressions that are bound to a key that
1448 // is shadowed by a later occurrence of the same key. For the
1449 // marked expressions, no store code is emitted.
1450 void CalculateEmitStore(Zone* zone);
1452 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1453 int ComputeFlags(bool disable_mementos = false) const {
1454 int flags = fast_elements() ? kFastElements : kNoFlags;
1455 flags |= has_function() ? kHasFunction : kNoFlags;
1456 if (depth() == 1 && !has_elements() && !may_store_doubles()) {
1457 flags |= kShallowProperties;
1459 if (disable_mementos) {
1460 flags |= kDisableMementos;
1471 kHasFunction = 1 << 1,
1472 kShallowProperties = 1 << 2,
1473 kDisableMementos = 1 << 3,
1477 struct Accessors: public ZoneObject {
1478 Accessors() : getter(NULL), setter(NULL) {}
1483 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1485 // Return an AST id for a property that is used in simulate instructions.
1486 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 1)); }
1488 // Unlike other AST nodes, this number of bailout IDs allocated for an
1489 // ObjectLiteral can vary, so num_ids() is not a static method.
1490 int num_ids() const { return parent_num_ids() + 1 + properties()->length(); }
1492 // Object literals need one feedback slot for each non-trivial value, as well
1493 // as some slots for home objects.
1494 FeedbackVectorRequirements ComputeFeedbackRequirements(
1495 Isolate* isolate, const ICSlotCache* cache) override;
1496 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1497 ICSlotCache* cache) override {
1500 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
1501 FeedbackVectorICSlot GetNthSlot(int n) const {
1502 return FeedbackVectorICSlot(slot_.ToInt() + n);
1505 // If value needs a home object, returns a valid feedback vector ic slot
1506 // given by slot_index, and increments slot_index.
1507 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
1508 int* slot_index) const;
1511 int slot_count() const { return slot_count_; }
1515 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
1516 int boilerplate_properties, bool has_function, bool is_strong,
1518 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1519 properties_(properties),
1520 boilerplate_properties_(boilerplate_properties),
1521 fast_elements_(false),
1522 has_elements_(false),
1523 may_store_doubles_(false),
1524 has_function_(has_function),
1528 slot_(FeedbackVectorICSlot::Invalid()) {
1530 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1533 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1534 Handle<FixedArray> constant_properties_;
1535 ZoneList<Property*>* properties_;
1536 int boilerplate_properties_;
1537 bool fast_elements_;
1539 bool may_store_doubles_;
1542 // slot_count_ helps validate that the logic to allocate ic slots and the
1543 // logic to use them are in sync.
1546 FeedbackVectorICSlot slot_;
1550 // Node for capturing a regexp literal.
1551 class RegExpLiteral final : public MaterializedLiteral {
1553 DECLARE_NODE_TYPE(RegExpLiteral)
1555 Handle<String> pattern() const { return pattern_->string(); }
1556 Handle<String> flags() const { return flags_->string(); }
1559 RegExpLiteral(Zone* zone, const AstRawString* pattern,
1560 const AstRawString* flags, int literal_index, bool is_strong,
1562 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1569 const AstRawString* pattern_;
1570 const AstRawString* flags_;
1574 // An array literal has a literals object that is used
1575 // for minimizing the work when constructing it at runtime.
1576 class ArrayLiteral final : public MaterializedLiteral {
1578 DECLARE_NODE_TYPE(ArrayLiteral)
1580 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1581 ElementsKind constant_elements_kind() const {
1582 DCHECK_EQ(2, constant_elements_->length());
1583 return static_cast<ElementsKind>(
1584 Smi::cast(constant_elements_->get(0))->value());
1587 ZoneList<Expression*>* values() const { return values_; }
1589 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1591 // Return an AST id for an element that is used in simulate instructions.
1592 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1594 // Unlike other AST nodes, this number of bailout IDs allocated for an
1595 // ArrayLiteral can vary, so num_ids() is not a static method.
1596 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1598 // Populate the constant elements fixed array.
1599 void BuildConstantElements(Isolate* isolate);
1601 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1602 int ComputeFlags(bool disable_mementos = false) const {
1603 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1604 if (disable_mementos) {
1605 flags |= kDisableMementos;
1615 kShallowElements = 1,
1616 kDisableMementos = 1 << 1,
1621 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
1622 int first_spread_index, int literal_index, bool is_strong,
1624 : MaterializedLiteral(zone, literal_index, is_strong, pos),
1626 first_spread_index_(first_spread_index) {}
1627 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1630 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1632 Handle<FixedArray> constant_elements_;
1633 ZoneList<Expression*>* values_;
1634 int first_spread_index_;
1638 class VariableProxy final : public Expression {
1640 DECLARE_NODE_TYPE(VariableProxy)
1642 bool IsValidReferenceExpression() const override { return !is_this(); }
1644 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1646 Handle<String> name() const { return raw_name()->string(); }
1647 const AstRawString* raw_name() const {
1648 return is_resolved() ? var_->raw_name() : raw_name_;
1651 Variable* var() const {
1652 DCHECK(is_resolved());
1655 void set_var(Variable* v) {
1656 DCHECK(!is_resolved());
1661 bool is_this() const { return IsThisField::decode(bit_field_); }
1663 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1664 void set_is_assigned() {
1665 bit_field_ = IsAssignedField::update(bit_field_, true);
1668 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1669 void set_is_resolved() {
1670 bit_field_ = IsResolvedField::update(bit_field_, true);
1673 int end_position() const { return end_position_; }
1675 // Bind this proxy to the variable var.
1676 void BindTo(Variable* var);
1678 bool UsesVariableFeedbackSlot() const {
1679 return var()->IsUnallocated() || var()->IsLookupSlot();
1682 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1683 Isolate* isolate, const ICSlotCache* cache) override;
1685 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1686 ICSlotCache* cache) override;
1687 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
1688 FeedbackVectorICSlot VariableFeedbackSlot() {
1689 return variable_feedback_slot_;
1692 static int num_ids() { return parent_num_ids() + 1; }
1693 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1696 VariableProxy(Zone* zone, Variable* var, int start_position,
1699 VariableProxy(Zone* zone, const AstRawString* name,
1700 Variable::Kind variable_kind, int start_position,
1702 static int parent_num_ids() { return Expression::num_ids(); }
1703 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1705 class IsThisField : public BitField8<bool, 0, 1> {};
1706 class IsAssignedField : public BitField8<bool, 1, 1> {};
1707 class IsResolvedField : public BitField8<bool, 2, 1> {};
1709 // Start with 16-bit (or smaller) field, which should get packed together
1710 // with Expression's trailing 16-bit field.
1712 FeedbackVectorICSlot variable_feedback_slot_;
1714 const AstRawString* raw_name_; // if !is_resolved_
1715 Variable* var_; // if is_resolved_
1717 // Position is stored in the AstNode superclass, but VariableProxy needs to
1718 // know its end position too (for error messages). It cannot be inferred from
1719 // the variable name length because it can contain escapes.
1724 // Left-hand side can only be a property, a global or a (parameter or local)
1730 NAMED_SUPER_PROPERTY,
1731 KEYED_SUPER_PROPERTY
1735 class Property final : public Expression {
1737 DECLARE_NODE_TYPE(Property)
1739 bool IsValidReferenceExpression() const override { return true; }
1741 Expression* obj() const { return obj_; }
1742 Expression* key() const { return key_; }
1744 static int num_ids() { return parent_num_ids() + 1; }
1745 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1747 bool IsStringAccess() const {
1748 return IsStringAccessField::decode(bit_field_);
1751 // Type feedback information.
1752 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1753 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1754 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1755 IcCheckType GetKeyType() const override {
1756 return KeyTypeField::decode(bit_field_);
1758 bool IsUninitialized() const {
1759 return !is_for_call() && HasNoTypeInformation();
1761 bool HasNoTypeInformation() const {
1762 return GetInlineCacheState() == UNINITIALIZED;
1764 InlineCacheState GetInlineCacheState() const {
1765 return InlineCacheStateField::decode(bit_field_);
1767 void set_is_string_access(bool b) {
1768 bit_field_ = IsStringAccessField::update(bit_field_, b);
1770 void set_key_type(IcCheckType key_type) {
1771 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1773 void set_inline_cache_state(InlineCacheState state) {
1774 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1776 void mark_for_call() {
1777 bit_field_ = IsForCallField::update(bit_field_, true);
1779 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1781 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1783 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1784 Isolate* isolate, const ICSlotCache* cache) override {
1785 return FeedbackVectorRequirements(0, 1);
1787 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1788 ICSlotCache* cache) override {
1789 property_feedback_slot_ = slot;
1791 Code::Kind FeedbackICSlotKind(int index) override {
1792 return key()->IsPropertyName() ? Code::LOAD_IC : Code::KEYED_LOAD_IC;
1795 FeedbackVectorICSlot PropertyFeedbackSlot() const {
1796 return property_feedback_slot_;
1799 static LhsKind GetAssignType(Property* property) {
1800 if (property == NULL) return VARIABLE;
1801 bool super_access = property->IsSuperAccess();
1802 return (property->key()->IsPropertyName())
1803 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1804 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1808 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1809 : Expression(zone, pos),
1810 bit_field_(IsForCallField::encode(false) |
1811 IsStringAccessField::encode(false) |
1812 InlineCacheStateField::encode(UNINITIALIZED)),
1813 property_feedback_slot_(FeedbackVectorICSlot::Invalid()),
1816 static int parent_num_ids() { return Expression::num_ids(); }
1819 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1821 class IsForCallField : public BitField8<bool, 0, 1> {};
1822 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1823 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1824 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1826 FeedbackVectorICSlot property_feedback_slot_;
1829 SmallMapList receiver_types_;
1833 class Call final : public Expression {
1835 DECLARE_NODE_TYPE(Call)
1837 Expression* expression() const { return expression_; }
1838 ZoneList<Expression*>* arguments() const { return arguments_; }
1840 // Type feedback information.
1841 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1842 Isolate* isolate, const ICSlotCache* cache) override;
1843 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
1844 ICSlotCache* cache) override {
1847 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override { slot_ = slot; }
1848 Code::Kind FeedbackICSlotKind(int index) override { return Code::CALL_IC; }
1850 FeedbackVectorSlot CallFeedbackSlot() const { return slot_; }
1852 FeedbackVectorICSlot CallFeedbackICSlot() const { return ic_slot_; }
1854 SmallMapList* GetReceiverTypes() override {
1855 if (expression()->IsProperty()) {
1856 return expression()->AsProperty()->GetReceiverTypes();
1861 bool IsMonomorphic() override {
1862 if (expression()->IsProperty()) {
1863 return expression()->AsProperty()->IsMonomorphic();
1865 return !target_.is_null();
1868 bool global_call() const {
1869 VariableProxy* proxy = expression_->AsVariableProxy();
1870 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1873 bool known_global_function() const {
1874 return global_call() && !target_.is_null();
1877 Handle<JSFunction> target() { return target_; }
1879 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1881 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1883 set_is_uninitialized(false);
1885 void set_target(Handle<JSFunction> target) { target_ = target; }
1886 void set_allocation_site(Handle<AllocationSite> site) {
1887 allocation_site_ = site;
1890 static int num_ids() { return parent_num_ids() + 3; }
1891 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1892 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1893 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1895 bool is_uninitialized() const {
1896 return IsUninitializedField::decode(bit_field_);
1898 void set_is_uninitialized(bool b) {
1899 bit_field_ = IsUninitializedField::update(bit_field_, b);
1911 // Helpers to determine how to handle the call.
1912 CallType GetCallType(Isolate* isolate) const;
1913 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1914 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1917 // Used to assert that the FullCodeGenerator records the return site.
1918 bool return_is_recorded_;
1922 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1924 : Expression(zone, pos),
1925 ic_slot_(FeedbackVectorICSlot::Invalid()),
1926 slot_(FeedbackVectorSlot::Invalid()),
1927 expression_(expression),
1928 arguments_(arguments),
1929 bit_field_(IsUninitializedField::encode(false)) {
1930 if (expression->IsProperty()) {
1931 expression->AsProperty()->mark_for_call();
1934 static int parent_num_ids() { return Expression::num_ids(); }
1937 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1939 FeedbackVectorICSlot ic_slot_;
1940 FeedbackVectorSlot slot_;
1941 Expression* expression_;
1942 ZoneList<Expression*>* arguments_;
1943 Handle<JSFunction> target_;
1944 Handle<AllocationSite> allocation_site_;
1945 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1950 class CallNew final : public Expression {
1952 DECLARE_NODE_TYPE(CallNew)
1954 Expression* expression() const { return expression_; }
1955 ZoneList<Expression*>* arguments() const { return arguments_; }
1957 // Type feedback information.
1958 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
1959 Isolate* isolate, const ICSlotCache* cache) override {
1960 return FeedbackVectorRequirements(FLAG_pretenuring_call_new ? 2 : 1, 0);
1962 void SetFirstFeedbackSlot(FeedbackVectorSlot slot) override {
1963 callnew_feedback_slot_ = slot;
1966 FeedbackVectorSlot CallNewFeedbackSlot() {
1967 DCHECK(!callnew_feedback_slot_.IsInvalid());
1968 return callnew_feedback_slot_;
1970 FeedbackVectorSlot AllocationSiteFeedbackSlot() {
1971 DCHECK(FLAG_pretenuring_call_new);
1972 return CallNewFeedbackSlot().next();
1975 bool IsMonomorphic() override { return is_monomorphic_; }
1976 Handle<JSFunction> target() const { return target_; }
1977 Handle<AllocationSite> allocation_site() const {
1978 return allocation_site_;
1981 static int num_ids() { return parent_num_ids() + 1; }
1982 static int feedback_slots() { return 1; }
1983 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1985 void set_allocation_site(Handle<AllocationSite> site) {
1986 allocation_site_ = site;
1988 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
1989 void set_target(Handle<JSFunction> target) { target_ = target; }
1990 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1992 is_monomorphic_ = true;
1996 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1998 : Expression(zone, pos),
1999 expression_(expression),
2000 arguments_(arguments),
2001 is_monomorphic_(false),
2002 callnew_feedback_slot_(FeedbackVectorSlot::Invalid()) {}
2004 static int parent_num_ids() { return Expression::num_ids(); }
2007 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2009 Expression* expression_;
2010 ZoneList<Expression*>* arguments_;
2011 bool is_monomorphic_;
2012 Handle<JSFunction> target_;
2013 Handle<AllocationSite> allocation_site_;
2014 FeedbackVectorSlot callnew_feedback_slot_;
2018 // The CallRuntime class does not represent any official JavaScript
2019 // language construct. Instead it is used to call a C or JS function
2020 // with a set of arguments. This is used from the builtins that are
2021 // implemented in JavaScript (see "v8natives.js").
2022 class CallRuntime final : public Expression {
2024 DECLARE_NODE_TYPE(CallRuntime)
2026 Handle<String> name() const { return raw_name_->string(); }
2027 const AstRawString* raw_name() const { return raw_name_; }
2028 const Runtime::Function* function() const { return function_; }
2029 ZoneList<Expression*>* arguments() const { return arguments_; }
2030 bool is_jsruntime() const { return function_ == NULL; }
2032 // Type feedback information.
2033 bool HasCallRuntimeFeedbackSlot() const { return is_jsruntime(); }
2034 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2035 Isolate* isolate, const ICSlotCache* cache) override {
2036 return FeedbackVectorRequirements(0, HasCallRuntimeFeedbackSlot() ? 1 : 0);
2038 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2039 ICSlotCache* cache) override {
2040 callruntime_feedback_slot_ = slot;
2042 Code::Kind FeedbackICSlotKind(int index) override { return Code::LOAD_IC; }
2044 FeedbackVectorICSlot CallRuntimeFeedbackSlot() {
2045 DCHECK(!HasCallRuntimeFeedbackSlot() ||
2046 !callruntime_feedback_slot_.IsInvalid());
2047 return callruntime_feedback_slot_;
2050 static int num_ids() { return parent_num_ids() + 1; }
2051 BailoutId CallId() { return BailoutId(local_id(0)); }
2054 CallRuntime(Zone* zone, const AstRawString* name,
2055 const Runtime::Function* function,
2056 ZoneList<Expression*>* arguments, int pos)
2057 : Expression(zone, pos),
2059 function_(function),
2060 arguments_(arguments),
2061 callruntime_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2062 static int parent_num_ids() { return Expression::num_ids(); }
2065 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2067 const AstRawString* raw_name_;
2068 const Runtime::Function* function_;
2069 ZoneList<Expression*>* arguments_;
2070 FeedbackVectorICSlot callruntime_feedback_slot_;
2074 class UnaryOperation final : public Expression {
2076 DECLARE_NODE_TYPE(UnaryOperation)
2078 Token::Value op() const { return op_; }
2079 Expression* expression() const { return expression_; }
2081 // For unary not (Token::NOT), the AST ids where true and false will
2082 // actually be materialized, respectively.
2083 static int num_ids() { return parent_num_ids() + 2; }
2084 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2085 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2087 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2090 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2091 : Expression(zone, pos), op_(op), expression_(expression) {
2092 DCHECK(Token::IsUnaryOp(op));
2094 static int parent_num_ids() { return Expression::num_ids(); }
2097 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2100 Expression* expression_;
2104 class BinaryOperation final : public Expression {
2106 DECLARE_NODE_TYPE(BinaryOperation)
2108 Token::Value op() const { return static_cast<Token::Value>(op_); }
2109 Expression* left() const { return left_; }
2110 Expression* right() const { return right_; }
2111 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2112 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2113 allocation_site_ = allocation_site;
2116 // The short-circuit logical operations need an AST ID for their
2117 // right-hand subexpression.
2118 static int num_ids() { return parent_num_ids() + 2; }
2119 BailoutId RightId() const { return BailoutId(local_id(0)); }
2121 TypeFeedbackId BinaryOperationFeedbackId() const {
2122 return TypeFeedbackId(local_id(1));
2124 Maybe<int> fixed_right_arg() const {
2125 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2127 void set_fixed_right_arg(Maybe<int> arg) {
2128 has_fixed_right_arg_ = arg.IsJust();
2129 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2132 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2135 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2136 Expression* right, int pos)
2137 : Expression(zone, pos),
2138 op_(static_cast<byte>(op)),
2139 has_fixed_right_arg_(false),
2140 fixed_right_arg_value_(0),
2143 DCHECK(Token::IsBinaryOp(op));
2145 static int parent_num_ids() { return Expression::num_ids(); }
2148 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2150 const byte op_; // actually Token::Value
2151 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2152 // type for the RHS. Currenty it's actually a Maybe<int>
2153 bool has_fixed_right_arg_;
2154 int fixed_right_arg_value_;
2157 Handle<AllocationSite> allocation_site_;
2161 class CountOperation final : public Expression {
2163 DECLARE_NODE_TYPE(CountOperation)
2165 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2166 bool is_postfix() const { return !is_prefix(); }
2168 Token::Value op() const { return TokenField::decode(bit_field_); }
2169 Token::Value binary_op() {
2170 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2173 Expression* expression() const { return expression_; }
2175 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2176 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2177 IcCheckType GetKeyType() const override {
2178 return KeyTypeField::decode(bit_field_);
2180 KeyedAccessStoreMode GetStoreMode() const override {
2181 return StoreModeField::decode(bit_field_);
2183 Type* type() const { return type_; }
2184 void set_key_type(IcCheckType type) {
2185 bit_field_ = KeyTypeField::update(bit_field_, type);
2187 void set_store_mode(KeyedAccessStoreMode mode) {
2188 bit_field_ = StoreModeField::update(bit_field_, mode);
2190 void set_type(Type* type) { type_ = type; }
2192 static int num_ids() { return parent_num_ids() + 4; }
2193 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2194 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2195 TypeFeedbackId CountBinOpFeedbackId() const {
2196 return TypeFeedbackId(local_id(2));
2198 TypeFeedbackId CountStoreFeedbackId() const {
2199 return TypeFeedbackId(local_id(3));
2202 FeedbackVectorRequirements ComputeFeedbackRequirements(
2203 Isolate* isolate, const ICSlotCache* cache) override;
2204 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2205 ICSlotCache* cache) override {
2208 Code::Kind FeedbackICSlotKind(int index) override;
2209 FeedbackVectorICSlot CountSlot() const { return slot_; }
2212 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2214 : Expression(zone, pos),
2216 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2217 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2220 slot_(FeedbackVectorICSlot::Invalid()) {}
2221 static int parent_num_ids() { return Expression::num_ids(); }
2224 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2226 class IsPrefixField : public BitField16<bool, 0, 1> {};
2227 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2228 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2229 class TokenField : public BitField16<Token::Value, 6, 8> {};
2231 // Starts with 16-bit field, which should get packed together with
2232 // Expression's trailing 16-bit field.
2233 uint16_t bit_field_;
2235 Expression* expression_;
2236 SmallMapList receiver_types_;
2237 FeedbackVectorICSlot slot_;
2241 class CompareOperation final : public Expression {
2243 DECLARE_NODE_TYPE(CompareOperation)
2245 Token::Value op() const { return op_; }
2246 Expression* left() const { return left_; }
2247 Expression* right() const { return right_; }
2249 // Type feedback information.
2250 static int num_ids() { return parent_num_ids() + 1; }
2251 TypeFeedbackId CompareOperationFeedbackId() const {
2252 return TypeFeedbackId(local_id(0));
2254 Type* combined_type() const { return combined_type_; }
2255 void set_combined_type(Type* type) { combined_type_ = type; }
2257 // Match special cases.
2258 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
2259 bool IsLiteralCompareUndefined(Expression** expr, Isolate* isolate);
2260 bool IsLiteralCompareNull(Expression** expr);
2263 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2264 Expression* right, int pos)
2265 : Expression(zone, pos),
2269 combined_type_(Type::None(zone)) {
2270 DCHECK(Token::IsCompareOp(op));
2272 static int parent_num_ids() { return Expression::num_ids(); }
2275 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2281 Type* combined_type_;
2285 class Spread final : public Expression {
2287 DECLARE_NODE_TYPE(Spread)
2289 Expression* expression() const { return expression_; }
2291 static int num_ids() { return parent_num_ids(); }
2294 Spread(Zone* zone, Expression* expression, int pos)
2295 : Expression(zone, pos), expression_(expression) {}
2296 static int parent_num_ids() { return Expression::num_ids(); }
2299 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2301 Expression* expression_;
2305 class Conditional final : public Expression {
2307 DECLARE_NODE_TYPE(Conditional)
2309 Expression* condition() const { return condition_; }
2310 Expression* then_expression() const { return then_expression_; }
2311 Expression* else_expression() const { return else_expression_; }
2313 static int num_ids() { return parent_num_ids() + 2; }
2314 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2315 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2318 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2319 Expression* else_expression, int position)
2320 : Expression(zone, position),
2321 condition_(condition),
2322 then_expression_(then_expression),
2323 else_expression_(else_expression) {}
2324 static int parent_num_ids() { return Expression::num_ids(); }
2327 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2329 Expression* condition_;
2330 Expression* then_expression_;
2331 Expression* else_expression_;
2335 class Assignment final : public Expression {
2337 DECLARE_NODE_TYPE(Assignment)
2339 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2341 Token::Value binary_op() const;
2343 Token::Value op() const { return TokenField::decode(bit_field_); }
2344 Expression* target() const { return target_; }
2345 Expression* value() const { return value_; }
2346 BinaryOperation* binary_operation() const { return binary_operation_; }
2348 // This check relies on the definition order of token in token.h.
2349 bool is_compound() const { return op() > Token::ASSIGN; }
2351 static int num_ids() { return parent_num_ids() + 2; }
2352 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2354 // Type feedback information.
2355 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2356 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2357 bool IsUninitialized() const {
2358 return IsUninitializedField::decode(bit_field_);
2360 bool HasNoTypeInformation() {
2361 return IsUninitializedField::decode(bit_field_);
2363 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2364 IcCheckType GetKeyType() const override {
2365 return KeyTypeField::decode(bit_field_);
2367 KeyedAccessStoreMode GetStoreMode() const override {
2368 return StoreModeField::decode(bit_field_);
2370 void set_is_uninitialized(bool b) {
2371 bit_field_ = IsUninitializedField::update(bit_field_, b);
2373 void set_key_type(IcCheckType key_type) {
2374 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2376 void set_store_mode(KeyedAccessStoreMode mode) {
2377 bit_field_ = StoreModeField::update(bit_field_, mode);
2380 FeedbackVectorRequirements ComputeFeedbackRequirements(
2381 Isolate* isolate, const ICSlotCache* cache) override;
2382 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2383 ICSlotCache* cache) override {
2386 Code::Kind FeedbackICSlotKind(int index) override;
2387 FeedbackVectorICSlot AssignmentSlot() const { return slot_; }
2390 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2392 static int parent_num_ids() { return Expression::num_ids(); }
2395 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2397 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2398 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2399 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 4> {};
2400 class TokenField : public BitField16<Token::Value, 6, 8> {};
2402 // Starts with 16-bit field, which should get packed together with
2403 // Expression's trailing 16-bit field.
2404 uint16_t bit_field_;
2405 Expression* target_;
2407 BinaryOperation* binary_operation_;
2408 SmallMapList receiver_types_;
2409 FeedbackVectorICSlot slot_;
2413 class Yield final : public Expression {
2415 DECLARE_NODE_TYPE(Yield)
2418 kInitial, // The initial yield that returns the unboxed generator object.
2419 kSuspend, // A normal yield: { value: EXPRESSION, done: false }
2420 kDelegating, // A yield*.
2421 kFinal // A return: { value: EXPRESSION, done: true }
2424 Expression* generator_object() const { return generator_object_; }
2425 Expression* expression() const { return expression_; }
2426 Kind yield_kind() const { return yield_kind_; }
2428 // Type feedback information.
2429 bool HasFeedbackSlots() const { return yield_kind() == kDelegating; }
2430 virtual FeedbackVectorRequirements ComputeFeedbackRequirements(
2431 Isolate* isolate, const ICSlotCache* cache) override {
2432 return FeedbackVectorRequirements(0, HasFeedbackSlots() ? 3 : 0);
2434 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2435 ICSlotCache* cache) override {
2436 yield_first_feedback_slot_ = slot;
2438 Code::Kind FeedbackICSlotKind(int index) override {
2439 return index == 0 ? Code::KEYED_LOAD_IC : Code::LOAD_IC;
2442 FeedbackVectorICSlot KeyedLoadFeedbackSlot() {
2443 DCHECK(!HasFeedbackSlots() || !yield_first_feedback_slot_.IsInvalid());
2444 return yield_first_feedback_slot_;
2447 FeedbackVectorICSlot DoneFeedbackSlot() {
2448 return KeyedLoadFeedbackSlot().next();
2451 FeedbackVectorICSlot ValueFeedbackSlot() { return DoneFeedbackSlot().next(); }
2454 Yield(Zone* zone, Expression* generator_object, Expression* expression,
2455 Kind yield_kind, int pos)
2456 : Expression(zone, pos),
2457 generator_object_(generator_object),
2458 expression_(expression),
2459 yield_kind_(yield_kind),
2460 yield_first_feedback_slot_(FeedbackVectorICSlot::Invalid()) {}
2463 Expression* generator_object_;
2464 Expression* expression_;
2466 FeedbackVectorICSlot yield_first_feedback_slot_;
2470 class Throw final : public Expression {
2472 DECLARE_NODE_TYPE(Throw)
2474 Expression* exception() const { return exception_; }
2477 Throw(Zone* zone, Expression* exception, int pos)
2478 : Expression(zone, pos), exception_(exception) {}
2481 Expression* exception_;
2485 class FunctionLiteral final : public Expression {
2488 ANONYMOUS_EXPRESSION,
2493 enum ParameterFlag {
2494 kNoDuplicateParameters = 0,
2495 kHasDuplicateParameters = 1
2498 enum IsFunctionFlag {
2503 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2505 enum ShouldBeUsedOnceHint { kShouldBeUsedOnce, kDontKnowIfShouldBeUsedOnce };
2507 enum ArityRestriction {
2513 DECLARE_NODE_TYPE(FunctionLiteral)
2515 Handle<String> name() const { return raw_name_->string(); }
2516 const AstRawString* raw_name() const { return raw_name_; }
2517 Scope* scope() const { return scope_; }
2518 ZoneList<Statement*>* body() const { return body_; }
2519 void set_function_token_position(int pos) { function_token_position_ = pos; }
2520 int function_token_position() const { return function_token_position_; }
2521 int start_position() const;
2522 int end_position() const;
2523 int SourceSize() const { return end_position() - start_position(); }
2524 bool is_expression() const { return IsExpression::decode(bitfield_); }
2525 bool is_anonymous() const { return IsAnonymous::decode(bitfield_); }
2526 LanguageMode language_mode() const;
2528 static bool NeedsHomeObject(Expression* expr);
2530 int materialized_literal_count() { return materialized_literal_count_; }
2531 int expected_property_count() { return expected_property_count_; }
2532 int parameter_count() { return parameter_count_; }
2534 bool AllowsLazyCompilation();
2535 bool AllowsLazyCompilationWithoutContext();
2537 Handle<String> debug_name() const {
2538 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2539 return raw_name_->string();
2541 return inferred_name();
2544 Handle<String> inferred_name() const {
2545 if (!inferred_name_.is_null()) {
2546 DCHECK(raw_inferred_name_ == NULL);
2547 return inferred_name_;
2549 if (raw_inferred_name_ != NULL) {
2550 return raw_inferred_name_->string();
2553 return Handle<String>();
2556 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2557 void set_inferred_name(Handle<String> inferred_name) {
2558 DCHECK(!inferred_name.is_null());
2559 inferred_name_ = inferred_name;
2560 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2561 raw_inferred_name_ = NULL;
2564 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2565 DCHECK(raw_inferred_name != NULL);
2566 raw_inferred_name_ = raw_inferred_name;
2567 DCHECK(inferred_name_.is_null());
2568 inferred_name_ = Handle<String>();
2571 bool pretenure() { return Pretenure::decode(bitfield_); }
2572 void set_pretenure() { bitfield_ |= Pretenure::encode(true); }
2574 bool has_duplicate_parameters() {
2575 return HasDuplicateParameters::decode(bitfield_);
2578 bool is_function() { return IsFunction::decode(bitfield_) == kIsFunction; }
2580 // This is used as a heuristic on when to eagerly compile a function
2581 // literal. We consider the following constructs as hints that the
2582 // function will be called immediately:
2583 // - (function() { ... })();
2584 // - var x = function() { ... }();
2585 bool should_eager_compile() const {
2586 return EagerCompileHintBit::decode(bitfield_) == kShouldEagerCompile;
2588 void set_should_eager_compile() {
2589 bitfield_ = EagerCompileHintBit::update(bitfield_, kShouldEagerCompile);
2592 // A hint that we expect this function to be called (exactly) once,
2593 // i.e. we suspect it's an initialization function.
2594 bool should_be_used_once_hint() const {
2595 return ShouldBeUsedOnceHintBit::decode(bitfield_) == kShouldBeUsedOnce;
2597 void set_should_be_used_once_hint() {
2598 bitfield_ = ShouldBeUsedOnceHintBit::update(bitfield_, kShouldBeUsedOnce);
2601 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2603 int ast_node_count() { return ast_properties_.node_count(); }
2604 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2605 void set_ast_properties(AstProperties* ast_properties) {
2606 ast_properties_ = *ast_properties;
2608 const ZoneFeedbackVectorSpec* feedback_vector_spec() const {
2609 return ast_properties_.get_spec();
2611 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2612 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2613 void set_dont_optimize_reason(BailoutReason reason) {
2614 dont_optimize_reason_ = reason;
2618 FunctionLiteral(Zone* zone, const AstRawString* name,
2619 AstValueFactory* ast_value_factory, Scope* scope,
2620 ZoneList<Statement*>* body, int materialized_literal_count,
2621 int expected_property_count, int parameter_count,
2622 FunctionType function_type,
2623 ParameterFlag has_duplicate_parameters,
2624 IsFunctionFlag is_function,
2625 EagerCompileHint eager_compile_hint, FunctionKind kind,
2627 : Expression(zone, position),
2631 raw_inferred_name_(ast_value_factory->empty_string()),
2632 ast_properties_(zone),
2633 dont_optimize_reason_(kNoReason),
2634 materialized_literal_count_(materialized_literal_count),
2635 expected_property_count_(expected_property_count),
2636 parameter_count_(parameter_count),
2637 function_token_position_(RelocInfo::kNoPosition) {
2638 bitfield_ = IsExpression::encode(function_type != DECLARATION) |
2639 IsAnonymous::encode(function_type == ANONYMOUS_EXPRESSION) |
2640 Pretenure::encode(false) |
2641 HasDuplicateParameters::encode(has_duplicate_parameters) |
2642 IsFunction::encode(is_function) |
2643 EagerCompileHintBit::encode(eager_compile_hint) |
2644 FunctionKindBits::encode(kind) |
2645 ShouldBeUsedOnceHintBit::encode(kDontKnowIfShouldBeUsedOnce);
2646 DCHECK(IsValidFunctionKind(kind));
2650 const AstRawString* raw_name_;
2651 Handle<String> name_;
2653 ZoneList<Statement*>* body_;
2654 const AstString* raw_inferred_name_;
2655 Handle<String> inferred_name_;
2656 AstProperties ast_properties_;
2657 BailoutReason dont_optimize_reason_;
2659 int materialized_literal_count_;
2660 int expected_property_count_;
2661 int parameter_count_;
2662 int function_token_position_;
2665 class IsExpression : public BitField<bool, 0, 1> {};
2666 class IsAnonymous : public BitField<bool, 1, 1> {};
2667 class Pretenure : public BitField<bool, 2, 1> {};
2668 class HasDuplicateParameters : public BitField<ParameterFlag, 3, 1> {};
2669 class IsFunction : public BitField<IsFunctionFlag, 4, 1> {};
2670 class EagerCompileHintBit : public BitField<EagerCompileHint, 5, 1> {};
2671 class FunctionKindBits : public BitField<FunctionKind, 6, 8> {};
2672 class ShouldBeUsedOnceHintBit : public BitField<ShouldBeUsedOnceHint, 15, 1> {
2677 class ClassLiteral final : public Expression {
2679 typedef ObjectLiteralProperty Property;
2681 DECLARE_NODE_TYPE(ClassLiteral)
2683 Handle<String> name() const { return raw_name_->string(); }
2684 const AstRawString* raw_name() const { return raw_name_; }
2685 Scope* scope() const { return scope_; }
2686 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2687 Expression* extends() const { return extends_; }
2688 FunctionLiteral* constructor() const { return constructor_; }
2689 ZoneList<Property*>* properties() const { return properties_; }
2690 int start_position() const { return position(); }
2691 int end_position() const { return end_position_; }
2693 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2694 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2695 BailoutId ExitId() { return BailoutId(local_id(2)); }
2696 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
2698 // Return an AST id for a property that is used in simulate instructions.
2699 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 4)); }
2701 // Unlike other AST nodes, this number of bailout IDs allocated for an
2702 // ClassLiteral can vary, so num_ids() is not a static method.
2703 int num_ids() const { return parent_num_ids() + 4 + properties()->length(); }
2705 // Object literals need one feedback slot for each non-trivial value, as well
2706 // as some slots for home objects.
2707 FeedbackVectorRequirements ComputeFeedbackRequirements(
2708 Isolate* isolate, const ICSlotCache* cache) override;
2709 void SetFirstFeedbackICSlot(FeedbackVectorICSlot slot,
2710 ICSlotCache* cache) override {
2713 Code::Kind FeedbackICSlotKind(int index) override { return Code::STORE_IC; }
2714 FeedbackVectorICSlot GetNthSlot(int n) const {
2715 return FeedbackVectorICSlot(slot_.ToInt() + n);
2718 // If value needs a home object, returns a valid feedback vector ic slot
2719 // given by slot_index, and increments slot_index.
2720 FeedbackVectorICSlot SlotForHomeObject(Expression* value,
2721 int* slot_index) const;
2724 int slot_count() const { return slot_count_; }
2728 ClassLiteral(Zone* zone, const AstRawString* name, Scope* scope,
2729 VariableProxy* class_variable_proxy, Expression* extends,
2730 FunctionLiteral* constructor, ZoneList<Property*>* properties,
2731 int start_position, int end_position)
2732 : Expression(zone, start_position),
2735 class_variable_proxy_(class_variable_proxy),
2737 constructor_(constructor),
2738 properties_(properties),
2739 end_position_(end_position),
2743 slot_(FeedbackVectorICSlot::Invalid()) {
2746 static int parent_num_ids() { return Expression::num_ids(); }
2749 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2751 const AstRawString* raw_name_;
2753 VariableProxy* class_variable_proxy_;
2754 Expression* extends_;
2755 FunctionLiteral* constructor_;
2756 ZoneList<Property*>* properties_;
2759 // slot_count_ helps validate that the logic to allocate ic slots and the
2760 // logic to use them are in sync.
2763 FeedbackVectorICSlot slot_;
2767 class NativeFunctionLiteral final : public Expression {
2769 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2771 Handle<String> name() const { return name_->string(); }
2772 v8::Extension* extension() const { return extension_; }
2775 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2776 v8::Extension* extension, int pos)
2777 : Expression(zone, pos), name_(name), extension_(extension) {}
2780 const AstRawString* name_;
2781 v8::Extension* extension_;
2785 class ThisFunction final : public Expression {
2787 DECLARE_NODE_TYPE(ThisFunction)
2790 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2794 class SuperPropertyReference final : public Expression {
2796 DECLARE_NODE_TYPE(SuperPropertyReference)
2798 VariableProxy* this_var() const { return this_var_; }
2799 Expression* home_object() const { return home_object_; }
2802 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2803 Expression* home_object, int pos)
2804 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2805 DCHECK(this_var->is_this());
2806 DCHECK(home_object->IsProperty());
2810 VariableProxy* this_var_;
2811 Expression* home_object_;
2815 class SuperCallReference final : public Expression {
2817 DECLARE_NODE_TYPE(SuperCallReference)
2819 VariableProxy* this_var() const { return this_var_; }
2820 VariableProxy* new_target_var() const { return new_target_var_; }
2821 VariableProxy* this_function_var() const { return this_function_var_; }
2824 SuperCallReference(Zone* zone, VariableProxy* this_var,
2825 VariableProxy* new_target_var,
2826 VariableProxy* this_function_var, int pos)
2827 : Expression(zone, pos),
2828 this_var_(this_var),
2829 new_target_var_(new_target_var),
2830 this_function_var_(this_function_var) {
2831 DCHECK(this_var->is_this());
2832 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2833 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2837 VariableProxy* this_var_;
2838 VariableProxy* new_target_var_;
2839 VariableProxy* this_function_var_;
2843 #undef DECLARE_NODE_TYPE
2846 // ----------------------------------------------------------------------------
2847 // Regular expressions
2850 class RegExpVisitor BASE_EMBEDDED {
2852 virtual ~RegExpVisitor() { }
2853 #define MAKE_CASE(Name) \
2854 virtual void* Visit##Name(RegExp##Name*, void* data) = 0;
2855 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
2860 class RegExpTree : public ZoneObject {
2862 static const int kInfinity = kMaxInt;
2863 virtual ~RegExpTree() {}
2864 virtual void* Accept(RegExpVisitor* visitor, void* data) = 0;
2865 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2866 RegExpNode* on_success) = 0;
2867 virtual bool IsTextElement() { return false; }
2868 virtual bool IsAnchoredAtStart() { return false; }
2869 virtual bool IsAnchoredAtEnd() { return false; }
2870 virtual int min_match() = 0;
2871 virtual int max_match() = 0;
2872 // Returns the interval of registers used for captures within this
2874 virtual Interval CaptureRegisters() { return Interval::Empty(); }
2875 virtual void AppendToText(RegExpText* text, Zone* zone);
2876 std::ostream& Print(std::ostream& os, Zone* zone); // NOLINT
2877 #define MAKE_ASTYPE(Name) \
2878 virtual RegExp##Name* As##Name(); \
2879 virtual bool Is##Name();
2880 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE)
2885 class RegExpDisjunction final : public RegExpTree {
2887 explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives);
2888 void* Accept(RegExpVisitor* visitor, void* data) override;
2889 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2890 RegExpNode* on_success) override;
2891 RegExpDisjunction* AsDisjunction() override;
2892 Interval CaptureRegisters() override;
2893 bool IsDisjunction() override;
2894 bool IsAnchoredAtStart() override;
2895 bool IsAnchoredAtEnd() override;
2896 int min_match() override { return min_match_; }
2897 int max_match() override { return max_match_; }
2898 ZoneList<RegExpTree*>* alternatives() { return alternatives_; }
2900 bool SortConsecutiveAtoms(RegExpCompiler* compiler);
2901 void RationalizeConsecutiveAtoms(RegExpCompiler* compiler);
2902 void FixSingleCharacterDisjunctions(RegExpCompiler* compiler);
2903 ZoneList<RegExpTree*>* alternatives_;
2909 class RegExpAlternative final : public RegExpTree {
2911 explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes);
2912 void* Accept(RegExpVisitor* visitor, void* data) override;
2913 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2914 RegExpNode* on_success) override;
2915 RegExpAlternative* AsAlternative() override;
2916 Interval CaptureRegisters() override;
2917 bool IsAlternative() override;
2918 bool IsAnchoredAtStart() override;
2919 bool IsAnchoredAtEnd() override;
2920 int min_match() override { return min_match_; }
2921 int max_match() override { return max_match_; }
2922 ZoneList<RegExpTree*>* nodes() { return nodes_; }
2924 ZoneList<RegExpTree*>* nodes_;
2930 class RegExpAssertion final : public RegExpTree {
2932 enum AssertionType {
2940 explicit RegExpAssertion(AssertionType type) : assertion_type_(type) { }
2941 void* Accept(RegExpVisitor* visitor, void* data) override;
2942 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2943 RegExpNode* on_success) override;
2944 RegExpAssertion* AsAssertion() override;
2945 bool IsAssertion() override;
2946 bool IsAnchoredAtStart() override;
2947 bool IsAnchoredAtEnd() override;
2948 int min_match() override { return 0; }
2949 int max_match() override { return 0; }
2950 AssertionType assertion_type() { return assertion_type_; }
2952 AssertionType assertion_type_;
2956 class CharacterSet final BASE_EMBEDDED {
2958 explicit CharacterSet(uc16 standard_set_type)
2960 standard_set_type_(standard_set_type) {}
2961 explicit CharacterSet(ZoneList<CharacterRange>* ranges)
2963 standard_set_type_(0) {}
2964 ZoneList<CharacterRange>* ranges(Zone* zone);
2965 uc16 standard_set_type() { return standard_set_type_; }
2966 void set_standard_set_type(uc16 special_set_type) {
2967 standard_set_type_ = special_set_type;
2969 bool is_standard() { return standard_set_type_ != 0; }
2970 void Canonicalize();
2972 ZoneList<CharacterRange>* ranges_;
2973 // If non-zero, the value represents a standard set (e.g., all whitespace
2974 // characters) without having to expand the ranges.
2975 uc16 standard_set_type_;
2979 class RegExpCharacterClass final : public RegExpTree {
2981 RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated)
2983 is_negated_(is_negated) { }
2984 explicit RegExpCharacterClass(uc16 type)
2986 is_negated_(false) { }
2987 void* Accept(RegExpVisitor* visitor, void* data) override;
2988 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
2989 RegExpNode* on_success) override;
2990 RegExpCharacterClass* AsCharacterClass() override;
2991 bool IsCharacterClass() override;
2992 bool IsTextElement() override { return true; }
2993 int min_match() override { return 1; }
2994 int max_match() override { return 1; }
2995 void AppendToText(RegExpText* text, Zone* zone) override;
2996 CharacterSet character_set() { return set_; }
2997 // TODO(lrn): Remove need for complex version if is_standard that
2998 // recognizes a mangled standard set and just do { return set_.is_special(); }
2999 bool is_standard(Zone* zone);
3000 // Returns a value representing the standard character set if is_standard()
3002 // Currently used values are:
3003 // s : unicode whitespace
3004 // S : unicode non-whitespace
3005 // w : ASCII word character (digit, letter, underscore)
3006 // W : non-ASCII word character
3008 // D : non-ASCII digit
3009 // . : non-unicode non-newline
3010 // * : All characters
3011 uc16 standard_type() { return set_.standard_set_type(); }
3012 ZoneList<CharacterRange>* ranges(Zone* zone) { return set_.ranges(zone); }
3013 bool is_negated() { return is_negated_; }
3021 class RegExpAtom final : public RegExpTree {
3023 explicit RegExpAtom(Vector<const uc16> data) : data_(data) { }
3024 void* Accept(RegExpVisitor* visitor, void* data) override;
3025 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3026 RegExpNode* on_success) override;
3027 RegExpAtom* AsAtom() override;
3028 bool IsAtom() override;
3029 bool IsTextElement() override { return true; }
3030 int min_match() override { return data_.length(); }
3031 int max_match() override { return data_.length(); }
3032 void AppendToText(RegExpText* text, Zone* zone) override;
3033 Vector<const uc16> data() { return data_; }
3034 int length() { return data_.length(); }
3036 Vector<const uc16> data_;
3040 class RegExpText final : public RegExpTree {
3042 explicit RegExpText(Zone* zone) : elements_(2, zone), length_(0) {}
3043 void* Accept(RegExpVisitor* visitor, void* data) override;
3044 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3045 RegExpNode* on_success) override;
3046 RegExpText* AsText() override;
3047 bool IsText() override;
3048 bool IsTextElement() override { return true; }
3049 int min_match() override { return length_; }
3050 int max_match() override { return length_; }
3051 void AppendToText(RegExpText* text, Zone* zone) override;
3052 void AddElement(TextElement elm, Zone* zone) {
3053 elements_.Add(elm, zone);
3054 length_ += elm.length();
3056 ZoneList<TextElement>* elements() { return &elements_; }
3058 ZoneList<TextElement> elements_;
3063 class RegExpQuantifier final : public RegExpTree {
3065 enum QuantifierType { GREEDY, NON_GREEDY, POSSESSIVE };
3066 RegExpQuantifier(int min, int max, QuantifierType type, RegExpTree* body)
3070 min_match_(min * body->min_match()),
3071 quantifier_type_(type) {
3072 if (max > 0 && body->max_match() > kInfinity / max) {
3073 max_match_ = kInfinity;
3075 max_match_ = max * body->max_match();
3078 void* Accept(RegExpVisitor* visitor, void* data) override;
3079 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3080 RegExpNode* on_success) override;
3081 static RegExpNode* ToNode(int min,
3085 RegExpCompiler* compiler,
3086 RegExpNode* on_success,
3087 bool not_at_start = false);
3088 RegExpQuantifier* AsQuantifier() override;
3089 Interval CaptureRegisters() override;
3090 bool IsQuantifier() override;
3091 int min_match() override { return min_match_; }
3092 int max_match() override { return max_match_; }
3093 int min() { return min_; }
3094 int max() { return max_; }
3095 bool is_possessive() { return quantifier_type_ == POSSESSIVE; }
3096 bool is_non_greedy() { return quantifier_type_ == NON_GREEDY; }
3097 bool is_greedy() { return quantifier_type_ == GREEDY; }
3098 RegExpTree* body() { return body_; }
3106 QuantifierType quantifier_type_;
3110 class RegExpCapture final : public RegExpTree {
3112 explicit RegExpCapture(RegExpTree* body, int index)
3113 : body_(body), index_(index) { }
3114 void* Accept(RegExpVisitor* visitor, void* data) override;
3115 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3116 RegExpNode* on_success) override;
3117 static RegExpNode* ToNode(RegExpTree* body,
3119 RegExpCompiler* compiler,
3120 RegExpNode* on_success);
3121 RegExpCapture* AsCapture() override;
3122 bool IsAnchoredAtStart() override;
3123 bool IsAnchoredAtEnd() override;
3124 Interval CaptureRegisters() override;
3125 bool IsCapture() override;
3126 int min_match() override { return body_->min_match(); }
3127 int max_match() override { return body_->max_match(); }
3128 RegExpTree* body() { return body_; }
3129 int index() { return index_; }
3130 static int StartRegister(int index) { return index * 2; }
3131 static int EndRegister(int index) { return index * 2 + 1; }
3139 class RegExpLookahead final : public RegExpTree {
3141 RegExpLookahead(RegExpTree* body,
3146 is_positive_(is_positive),
3147 capture_count_(capture_count),
3148 capture_from_(capture_from) { }
3150 void* Accept(RegExpVisitor* visitor, void* data) override;
3151 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3152 RegExpNode* on_success) override;
3153 RegExpLookahead* AsLookahead() override;
3154 Interval CaptureRegisters() override;
3155 bool IsLookahead() override;
3156 bool IsAnchoredAtStart() override;
3157 int min_match() override { return 0; }
3158 int max_match() override { return 0; }
3159 RegExpTree* body() { return body_; }
3160 bool is_positive() { return is_positive_; }
3161 int capture_count() { return capture_count_; }
3162 int capture_from() { return capture_from_; }
3172 class RegExpBackReference final : public RegExpTree {
3174 explicit RegExpBackReference(RegExpCapture* capture)
3175 : capture_(capture) { }
3176 void* Accept(RegExpVisitor* visitor, void* data) override;
3177 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3178 RegExpNode* on_success) override;
3179 RegExpBackReference* AsBackReference() override;
3180 bool IsBackReference() override;
3181 int min_match() override { return 0; }
3182 int max_match() override { return capture_->max_match(); }
3183 int index() { return capture_->index(); }
3184 RegExpCapture* capture() { return capture_; }
3186 RegExpCapture* capture_;
3190 class RegExpEmpty final : public RegExpTree {
3193 void* Accept(RegExpVisitor* visitor, void* data) override;
3194 virtual RegExpNode* ToNode(RegExpCompiler* compiler,
3195 RegExpNode* on_success) override;
3196 RegExpEmpty* AsEmpty() override;
3197 bool IsEmpty() override;
3198 int min_match() override { return 0; }
3199 int max_match() override { return 0; }
3203 // ----------------------------------------------------------------------------
3205 // - leaf node visitors are abstract.
3207 class AstVisitor BASE_EMBEDDED {
3210 virtual ~AstVisitor() {}
3212 // Stack overflow check and dynamic dispatch.
3213 virtual void Visit(AstNode* node) = 0;
3215 // Iteration left-to-right.
3216 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
3217 virtual void VisitStatements(ZoneList<Statement*>* statements);
3218 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
3220 // Individual AST nodes.
3221 #define DEF_VISIT(type) \
3222 virtual void Visit##type(type* node) = 0;
3223 AST_NODE_LIST(DEF_VISIT)
3228 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
3230 void Visit(AstNode* node) final { \
3231 if (!CheckStackOverflow()) node->Accept(this); \
3234 void SetStackOverflow() { stack_overflow_ = true; } \
3235 void ClearStackOverflow() { stack_overflow_ = false; } \
3236 bool HasStackOverflow() const { return stack_overflow_; } \
3238 bool CheckStackOverflow() { \
3239 if (stack_overflow_) return true; \
3240 StackLimitCheck check(isolate_); \
3241 if (!check.HasOverflowed()) return false; \
3242 stack_overflow_ = true; \
3247 void InitializeAstVisitor(Isolate* isolate, Zone* zone) { \
3248 isolate_ = isolate; \
3250 stack_overflow_ = false; \
3252 Zone* zone() { return zone_; } \
3253 Isolate* isolate() { return isolate_; } \
3255 Isolate* isolate_; \
3257 bool stack_overflow_
3260 // ----------------------------------------------------------------------------
3263 class AstNodeFactory final BASE_EMBEDDED {
3265 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3266 : zone_(ast_value_factory->zone()),
3267 ast_value_factory_(ast_value_factory) {}
3269 VariableDeclaration* NewVariableDeclaration(
3270 VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
3271 bool is_class_declaration = false, int declaration_group_start = -1) {
3273 VariableDeclaration(zone_, proxy, mode, scope, pos,
3274 is_class_declaration, declaration_group_start);
3277 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3279 FunctionLiteral* fun,
3282 return new (zone_) FunctionDeclaration(zone_, proxy, mode, fun, scope, pos);
3285 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3286 const AstRawString* import_name,
3287 const AstRawString* module_specifier,
3288 Scope* scope, int pos) {
3289 return new (zone_) ImportDeclaration(zone_, proxy, import_name,
3290 module_specifier, scope, pos);
3293 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3296 return new (zone_) ExportDeclaration(zone_, proxy, scope, pos);
3299 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3300 bool ignore_completion_value, int pos) {
3302 Block(zone_, labels, capacity, ignore_completion_value, pos);
3305 #define STATEMENT_WITH_LABELS(NodeType) \
3306 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3307 return new (zone_) NodeType(zone_, labels, pos); \
3309 STATEMENT_WITH_LABELS(DoWhileStatement)
3310 STATEMENT_WITH_LABELS(WhileStatement)
3311 STATEMENT_WITH_LABELS(ForStatement)
3312 STATEMENT_WITH_LABELS(SwitchStatement)
3313 #undef STATEMENT_WITH_LABELS
3315 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3316 ZoneList<const AstRawString*>* labels,
3318 switch (visit_mode) {
3319 case ForEachStatement::ENUMERATE: {
3320 return new (zone_) ForInStatement(zone_, labels, pos);
3322 case ForEachStatement::ITERATE: {
3323 return new (zone_) ForOfStatement(zone_, labels, pos);
3330 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3331 return new (zone_) ExpressionStatement(zone_, expression, pos);
3334 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3335 return new (zone_) ContinueStatement(zone_, target, pos);
3338 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3339 return new (zone_) BreakStatement(zone_, target, pos);
3342 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3343 return new (zone_) ReturnStatement(zone_, expression, pos);
3346 WithStatement* NewWithStatement(Scope* scope,
3347 Expression* expression,
3348 Statement* statement,
3350 return new (zone_) WithStatement(zone_, scope, expression, statement, pos);
3353 IfStatement* NewIfStatement(Expression* condition,
3354 Statement* then_statement,
3355 Statement* else_statement,
3358 IfStatement(zone_, condition, then_statement, else_statement, pos);
3361 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3363 Block* catch_block, int pos) {
3365 TryCatchStatement(zone_, try_block, scope, variable, catch_block, pos);
3368 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3369 Block* finally_block, int pos) {
3371 TryFinallyStatement(zone_, try_block, finally_block, pos);
3374 DebuggerStatement* NewDebuggerStatement(int pos) {
3375 return new (zone_) DebuggerStatement(zone_, pos);
3378 EmptyStatement* NewEmptyStatement(int pos) {
3379 return new(zone_) EmptyStatement(zone_, pos);
3382 CaseClause* NewCaseClause(
3383 Expression* label, ZoneList<Statement*>* statements, int pos) {
3384 return new (zone_) CaseClause(zone_, label, statements, pos);
3387 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3389 Literal(zone_, ast_value_factory_->NewString(string), pos);
3392 // A JavaScript symbol (ECMA-262 edition 6).
3393 Literal* NewSymbolLiteral(const char* name, int pos) {
3394 return new (zone_) Literal(zone_, ast_value_factory_->NewSymbol(name), pos);
3397 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3399 Literal(zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3402 Literal* NewSmiLiteral(int number, int pos) {
3403 return new (zone_) Literal(zone_, ast_value_factory_->NewSmi(number), pos);
3406 Literal* NewBooleanLiteral(bool b, int pos) {
3407 return new (zone_) Literal(zone_, ast_value_factory_->NewBoolean(b), pos);
3410 Literal* NewNullLiteral(int pos) {
3411 return new (zone_) Literal(zone_, ast_value_factory_->NewNull(), pos);
3414 Literal* NewUndefinedLiteral(int pos) {
3415 return new (zone_) Literal(zone_, ast_value_factory_->NewUndefined(), pos);
3418 Literal* NewTheHoleLiteral(int pos) {
3419 return new (zone_) Literal(zone_, ast_value_factory_->NewTheHole(), pos);
3422 ObjectLiteral* NewObjectLiteral(
3423 ZoneList<ObjectLiteral::Property*>* properties,
3425 int boilerplate_properties,
3429 return new (zone_) ObjectLiteral(zone_, properties, literal_index,
3430 boilerplate_properties, has_function,
3434 ObjectLiteral::Property* NewObjectLiteralProperty(
3435 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3436 bool is_static, bool is_computed_name) {
3438 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3441 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3444 bool is_computed_name) {
3445 return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3446 is_static, is_computed_name);
3449 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern,
3450 const AstRawString* flags,
3454 return new (zone_) RegExpLiteral(zone_, pattern, flags, literal_index,
3458 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3463 ArrayLiteral(zone_, values, -1, literal_index, is_strong, pos);
3466 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3467 int first_spread_index, int literal_index,
3468 bool is_strong, int pos) {
3469 return new (zone_) ArrayLiteral(zone_, values, first_spread_index,
3470 literal_index, is_strong, pos);
3473 VariableProxy* NewVariableProxy(Variable* var,
3474 int start_position = RelocInfo::kNoPosition,
3475 int end_position = RelocInfo::kNoPosition) {
3476 return new (zone_) VariableProxy(zone_, var, start_position, end_position);
3479 VariableProxy* NewVariableProxy(const AstRawString* name,
3480 Variable::Kind variable_kind,
3481 int start_position = RelocInfo::kNoPosition,
3482 int end_position = RelocInfo::kNoPosition) {
3483 DCHECK_NOT_NULL(name);
3485 VariableProxy(zone_, name, variable_kind, start_position, end_position);
3488 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3489 return new (zone_) Property(zone_, obj, key, pos);
3492 Call* NewCall(Expression* expression,
3493 ZoneList<Expression*>* arguments,
3495 return new (zone_) Call(zone_, expression, arguments, pos);
3498 CallNew* NewCallNew(Expression* expression,
3499 ZoneList<Expression*>* arguments,
3501 return new (zone_) CallNew(zone_, expression, arguments, pos);
3504 CallRuntime* NewCallRuntime(const AstRawString* name,
3505 const Runtime::Function* function,
3506 ZoneList<Expression*>* arguments,
3508 return new (zone_) CallRuntime(zone_, name, function, arguments, pos);
3511 UnaryOperation* NewUnaryOperation(Token::Value op,
3512 Expression* expression,
3514 return new (zone_) UnaryOperation(zone_, op, expression, pos);
3517 BinaryOperation* NewBinaryOperation(Token::Value op,
3521 return new (zone_) BinaryOperation(zone_, op, left, right, pos);
3524 CountOperation* NewCountOperation(Token::Value op,
3528 return new (zone_) CountOperation(zone_, op, is_prefix, expr, pos);
3531 CompareOperation* NewCompareOperation(Token::Value op,
3535 return new (zone_) CompareOperation(zone_, op, left, right, pos);
3538 Spread* NewSpread(Expression* expression, int pos) {
3539 return new (zone_) Spread(zone_, expression, pos);
3542 Conditional* NewConditional(Expression* condition,
3543 Expression* then_expression,
3544 Expression* else_expression,
3546 return new (zone_) Conditional(zone_, condition, then_expression,
3547 else_expression, position);
3550 Assignment* NewAssignment(Token::Value op,
3554 DCHECK(Token::IsAssignmentOp(op));
3555 Assignment* assign = new (zone_) Assignment(zone_, op, target, value, pos);
3556 if (assign->is_compound()) {
3557 DCHECK(Token::IsAssignmentOp(op));
3558 assign->binary_operation_ =
3559 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3564 Yield* NewYield(Expression *generator_object,
3565 Expression* expression,
3566 Yield::Kind yield_kind,
3568 if (!expression) expression = NewUndefinedLiteral(pos);
3570 Yield(zone_, generator_object, expression, yield_kind, pos);
3573 Throw* NewThrow(Expression* exception, int pos) {
3574 return new (zone_) Throw(zone_, exception, pos);
3577 FunctionLiteral* NewFunctionLiteral(
3578 const AstRawString* name, AstValueFactory* ast_value_factory,
3579 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3580 int expected_property_count, int parameter_count,
3581 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3582 FunctionLiteral::FunctionType function_type,
3583 FunctionLiteral::IsFunctionFlag is_function,
3584 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3586 return new (zone_) FunctionLiteral(
3587 zone_, name, ast_value_factory, scope, body, materialized_literal_count,
3588 expected_property_count, parameter_count, function_type,
3589 has_duplicate_parameters, is_function, eager_compile_hint, kind,
3593 ClassLiteral* NewClassLiteral(const AstRawString* name, Scope* scope,
3594 VariableProxy* proxy, Expression* extends,
3595 FunctionLiteral* constructor,
3596 ZoneList<ObjectLiteral::Property*>* properties,
3597 int start_position, int end_position) {
3599 ClassLiteral(zone_, name, scope, proxy, extends, constructor,
3600 properties, start_position, end_position);
3603 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3604 v8::Extension* extension,
3606 return new (zone_) NativeFunctionLiteral(zone_, name, extension, pos);
3609 ThisFunction* NewThisFunction(int pos) {
3610 return new (zone_) ThisFunction(zone_, pos);
3613 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3614 Expression* home_object,
3617 SuperPropertyReference(zone_, this_var, home_object, pos);
3620 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3621 VariableProxy* new_target_var,
3622 VariableProxy* this_function_var,
3624 return new (zone_) SuperCallReference(zone_, this_var, new_target_var,
3625 this_function_var, pos);
3630 AstValueFactory* ast_value_factory_;
3634 } } // namespace v8::internal