1a9919b5aa52c0343d60cac4cffd4dcf2ee4d24e
[platform/framework/web/crosswalk.git] / src / v8 / src / ast.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "ast.h"
29
30 #include <cmath>  // For isfinite.
31 #include "builtins.h"
32 #include "code-stubs.h"
33 #include "contexts.h"
34 #include "conversions.h"
35 #include "hashmap.h"
36 #include "parser.h"
37 #include "property-details.h"
38 #include "property.h"
39 #include "scopes.h"
40 #include "string-stream.h"
41 #include "type-info.h"
42
43 namespace v8 {
44 namespace internal {
45
46 // ----------------------------------------------------------------------------
47 // All the Accept member functions for each syntax tree node type.
48
49 #define DECL_ACCEPT(type)                                       \
50   void type::Accept(AstVisitor* v) { v->Visit##type(this); }
51 AST_NODE_LIST(DECL_ACCEPT)
52 #undef DECL_ACCEPT
53
54
55 // ----------------------------------------------------------------------------
56 // Implementation of other node functionality.
57
58
59 bool Expression::IsSmiLiteral() {
60   return AsLiteral() != NULL && AsLiteral()->value()->IsSmi();
61 }
62
63
64 bool Expression::IsStringLiteral() {
65   return AsLiteral() != NULL && AsLiteral()->value()->IsString();
66 }
67
68
69 bool Expression::IsNullLiteral() {
70   return AsLiteral() != NULL && AsLiteral()->value()->IsNull();
71 }
72
73
74 bool Expression::IsUndefinedLiteral(Isolate* isolate) {
75   VariableProxy* var_proxy = AsVariableProxy();
76   if (var_proxy == NULL) return false;
77   Variable* var = var_proxy->var();
78   // The global identifier "undefined" is immutable. Everything
79   // else could be reassigned.
80   return var != NULL && var->location() == Variable::UNALLOCATED &&
81          var_proxy->name()->Equals(isolate->heap()->undefined_string());
82 }
83
84
85 VariableProxy::VariableProxy(Zone* zone, Variable* var, int position)
86     : Expression(zone, position),
87       name_(var->name()),
88       var_(NULL),  // Will be set by the call to BindTo.
89       is_this_(var->is_this()),
90       is_trivial_(false),
91       is_lvalue_(false),
92       interface_(var->interface()) {
93   BindTo(var);
94 }
95
96
97 VariableProxy::VariableProxy(Zone* zone,
98                              Handle<String> name,
99                              bool is_this,
100                              Interface* interface,
101                              int position)
102     : Expression(zone, position),
103       name_(name),
104       var_(NULL),
105       is_this_(is_this),
106       is_trivial_(false),
107       is_lvalue_(false),
108       interface_(interface) {
109   // Names must be canonicalized for fast equality checks.
110   ASSERT(name->IsInternalizedString());
111 }
112
113
114 void VariableProxy::BindTo(Variable* var) {
115   ASSERT(var_ == NULL);  // must be bound only once
116   ASSERT(var != NULL);  // must bind
117   ASSERT(!FLAG_harmony_modules || interface_->IsUnified(var->interface()));
118   ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name()));
119   // Ideally CONST-ness should match. However, this is very hard to achieve
120   // because we don't know the exact semantics of conflicting (const and
121   // non-const) multiple variable declarations, const vars introduced via
122   // eval() etc.  Const-ness and variable declarations are a complete mess
123   // in JS. Sigh...
124   var_ = var;
125   var->set_is_used(true);
126 }
127
128
129 Assignment::Assignment(Zone* zone,
130                        Token::Value op,
131                        Expression* target,
132                        Expression* value,
133                        int pos)
134     : Expression(zone, pos),
135       op_(op),
136       target_(target),
137       value_(value),
138       binary_operation_(NULL),
139       assignment_id_(GetNextId(zone)),
140       is_uninitialized_(false),
141       store_mode_(STANDARD_STORE) { }
142
143
144 Token::Value Assignment::binary_op() const {
145   switch (op_) {
146     case Token::ASSIGN_BIT_OR: return Token::BIT_OR;
147     case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR;
148     case Token::ASSIGN_BIT_AND: return Token::BIT_AND;
149     case Token::ASSIGN_SHL: return Token::SHL;
150     case Token::ASSIGN_SAR: return Token::SAR;
151     case Token::ASSIGN_SHR: return Token::SHR;
152     case Token::ASSIGN_ADD: return Token::ADD;
153     case Token::ASSIGN_SUB: return Token::SUB;
154     case Token::ASSIGN_MUL: return Token::MUL;
155     case Token::ASSIGN_DIV: return Token::DIV;
156     case Token::ASSIGN_MOD: return Token::MOD;
157     default: UNREACHABLE();
158   }
159   return Token::ILLEGAL;
160 }
161
162
163 bool FunctionLiteral::AllowsLazyCompilation() {
164   return scope()->AllowsLazyCompilation();
165 }
166
167
168 bool FunctionLiteral::AllowsLazyCompilationWithoutContext() {
169   return scope()->AllowsLazyCompilationWithoutContext();
170 }
171
172
173 int FunctionLiteral::start_position() const {
174   return scope()->start_position();
175 }
176
177
178 int FunctionLiteral::end_position() const {
179   return scope()->end_position();
180 }
181
182
183 LanguageMode FunctionLiteral::language_mode() const {
184   return scope()->language_mode();
185 }
186
187
188 void FunctionLiteral::InitializeSharedInfo(
189     Handle<Code> unoptimized_code) {
190   for (RelocIterator it(*unoptimized_code); !it.done(); it.next()) {
191     RelocInfo* rinfo = it.rinfo();
192     if (rinfo->rmode() != RelocInfo::EMBEDDED_OBJECT) continue;
193     Object* obj = rinfo->target_object();
194     if (obj->IsSharedFunctionInfo()) {
195       SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj);
196       if (shared->start_position() == start_position()) {
197         shared_info_ = Handle<SharedFunctionInfo>(shared);
198         break;
199       }
200     }
201   }
202 }
203
204
205 ObjectLiteralProperty::ObjectLiteralProperty(
206     Zone* zone, Literal* key, Expression* value) {
207   emit_store_ = true;
208   key_ = key;
209   value_ = value;
210   Object* k = *key->value();
211   if (k->IsInternalizedString() &&
212       zone->isolate()->heap()->proto_string()->Equals(String::cast(k))) {
213     kind_ = PROTOTYPE;
214   } else if (value_->AsMaterializedLiteral() != NULL) {
215     kind_ = MATERIALIZED_LITERAL;
216   } else if (value_->AsLiteral() != NULL) {
217     kind_ = CONSTANT;
218   } else {
219     kind_ = COMPUTED;
220   }
221 }
222
223
224 ObjectLiteralProperty::ObjectLiteralProperty(
225     Zone* zone, bool is_getter, FunctionLiteral* value) {
226   emit_store_ = true;
227   value_ = value;
228   kind_ = is_getter ? GETTER : SETTER;
229 }
230
231
232 bool ObjectLiteral::Property::IsCompileTimeValue() {
233   return kind_ == CONSTANT ||
234       (kind_ == MATERIALIZED_LITERAL &&
235        CompileTimeValue::IsCompileTimeValue(value_));
236 }
237
238
239 void ObjectLiteral::Property::set_emit_store(bool emit_store) {
240   emit_store_ = emit_store;
241 }
242
243
244 bool ObjectLiteral::Property::emit_store() {
245   return emit_store_;
246 }
247
248
249 void ObjectLiteral::CalculateEmitStore(Zone* zone) {
250   ZoneAllocationPolicy allocator(zone);
251
252   ZoneHashMap table(Literal::Match, ZoneHashMap::kDefaultHashMapCapacity,
253                     allocator);
254   for (int i = properties()->length() - 1; i >= 0; i--) {
255     ObjectLiteral::Property* property = properties()->at(i);
256     Literal* literal = property->key();
257     if (literal->value()->IsNull()) continue;
258     uint32_t hash = literal->Hash();
259     // If the key of a computed property is in the table, do not emit
260     // a store for the property later.
261     if ((property->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL ||
262          property->kind() == ObjectLiteral::Property::COMPUTED) &&
263         table.Lookup(literal, hash, false, allocator) != NULL) {
264       property->set_emit_store(false);
265     } else {
266       // Add key to the table.
267       table.Lookup(literal, hash, true, allocator);
268     }
269   }
270 }
271
272
273 bool ObjectLiteral::IsBoilerplateProperty(ObjectLiteral::Property* property) {
274   return property != NULL &&
275          property->kind() != ObjectLiteral::Property::PROTOTYPE;
276 }
277
278
279 void ObjectLiteral::BuildConstantProperties(Isolate* isolate) {
280   if (!constant_properties_.is_null()) return;
281
282   // Allocate a fixed array to hold all the constant properties.
283   Handle<FixedArray> constant_properties = isolate->factory()->NewFixedArray(
284       boilerplate_properties_ * 2, TENURED);
285
286   int position = 0;
287   // Accumulate the value in local variables and store it at the end.
288   bool is_simple = true;
289   int depth_acc = 1;
290   uint32_t max_element_index = 0;
291   uint32_t elements = 0;
292   for (int i = 0; i < properties()->length(); i++) {
293     ObjectLiteral::Property* property = properties()->at(i);
294     if (!IsBoilerplateProperty(property)) {
295       is_simple = false;
296       continue;
297     }
298     MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
299     if (m_literal != NULL) {
300       m_literal->BuildConstants(isolate);
301       if (m_literal->depth() >= depth_acc) depth_acc = m_literal->depth() + 1;
302     }
303
304     // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined
305     // value for COMPUTED properties, the real value is filled in at
306     // runtime. The enumeration order is maintained.
307     Handle<Object> key = property->key()->value();
308     Handle<Object> value = GetBoilerplateValue(property->value(), isolate);
309
310     // Ensure objects that may, at any point in time, contain fields with double
311     // representation are always treated as nested objects. This is true for
312     // computed fields (value is undefined), and smi and double literals
313     // (value->IsNumber()).
314     // TODO(verwaest): Remove once we can store them inline.
315     if (FLAG_track_double_fields &&
316         (value->IsNumber() || value->IsUninitialized())) {
317       may_store_doubles_ = true;
318     }
319
320     is_simple = is_simple && !value->IsUninitialized();
321
322     // Keep track of the number of elements in the object literal and
323     // the largest element index.  If the largest element index is
324     // much larger than the number of elements, creating an object
325     // literal with fast elements will be a waste of space.
326     uint32_t element_index = 0;
327     if (key->IsString()
328         && Handle<String>::cast(key)->AsArrayIndex(&element_index)
329         && element_index > max_element_index) {
330       max_element_index = element_index;
331       elements++;
332     } else if (key->IsSmi()) {
333       int key_value = Smi::cast(*key)->value();
334       if (key_value > 0
335           && static_cast<uint32_t>(key_value) > max_element_index) {
336         max_element_index = key_value;
337       }
338       elements++;
339     }
340
341     // Add name, value pair to the fixed array.
342     constant_properties->set(position++, *key);
343     constant_properties->set(position++, *value);
344   }
345
346   constant_properties_ = constant_properties;
347   fast_elements_ =
348       (max_element_index <= 32) || ((2 * elements) >= max_element_index);
349   set_is_simple(is_simple);
350   set_depth(depth_acc);
351 }
352
353
354 void ArrayLiteral::BuildConstantElements(Isolate* isolate) {
355   if (!constant_elements_.is_null()) return;
356
357   // Allocate a fixed array to hold all the object literals.
358   Handle<JSArray> array =
359       isolate->factory()->NewJSArray(0, FAST_HOLEY_SMI_ELEMENTS);
360   isolate->factory()->SetElementsCapacityAndLength(
361       array, values()->length(), values()->length());
362
363   // Fill in the literals.
364   bool is_simple = true;
365   int depth_acc = 1;
366   bool is_holey = false;
367   for (int i = 0, n = values()->length(); i < n; i++) {
368     Expression* element = values()->at(i);
369     MaterializedLiteral* m_literal = element->AsMaterializedLiteral();
370     if (m_literal != NULL) {
371       m_literal->BuildConstants(isolate);
372       if (m_literal->depth() + 1 > depth_acc) {
373         depth_acc = m_literal->depth() + 1;
374       }
375     }
376     Handle<Object> boilerplate_value = GetBoilerplateValue(element, isolate);
377     if (boilerplate_value->IsTheHole()) {
378       is_holey = true;
379     } else if (boilerplate_value->IsUninitialized()) {
380       is_simple = false;
381       JSObject::SetOwnElement(
382           array, i, handle(Smi::FromInt(0), isolate), kNonStrictMode);
383     } else {
384       JSObject::SetOwnElement(array, i, boilerplate_value, kNonStrictMode);
385     }
386   }
387
388   Handle<FixedArrayBase> element_values(array->elements());
389
390   // Simple and shallow arrays can be lazily copied, we transform the
391   // elements array to a copy-on-write array.
392   if (is_simple && depth_acc == 1 && values()->length() > 0 &&
393       array->HasFastSmiOrObjectElements()) {
394     element_values->set_map(isolate->heap()->fixed_cow_array_map());
395   }
396
397   // Remember both the literal's constant values as well as the ElementsKind
398   // in a 2-element FixedArray.
399   Handle<FixedArray> literals = isolate->factory()->NewFixedArray(2, TENURED);
400
401   ElementsKind kind = array->GetElementsKind();
402   kind = is_holey ? GetHoleyElementsKind(kind) : GetPackedElementsKind(kind);
403
404   literals->set(0, Smi::FromInt(kind));
405   literals->set(1, *element_values);
406
407   constant_elements_ = literals;
408   set_is_simple(is_simple);
409   set_depth(depth_acc);
410 }
411
412
413 Handle<Object> MaterializedLiteral::GetBoilerplateValue(Expression* expression,
414                                                         Isolate* isolate) {
415   if (expression->AsLiteral() != NULL) {
416     return expression->AsLiteral()->value();
417   }
418   if (CompileTimeValue::IsCompileTimeValue(expression)) {
419     return CompileTimeValue::GetValue(isolate, expression);
420   }
421   return isolate->factory()->uninitialized_value();
422 }
423
424
425 void MaterializedLiteral::BuildConstants(Isolate* isolate) {
426   if (IsArrayLiteral()) {
427     return AsArrayLiteral()->BuildConstantElements(isolate);
428   }
429   if (IsObjectLiteral()) {
430     return AsObjectLiteral()->BuildConstantProperties(isolate);
431   }
432   ASSERT(IsRegExpLiteral());
433   ASSERT(depth() >= 1);  // Depth should be initialized.
434 }
435
436
437 void TargetCollector::AddTarget(Label* target, Zone* zone) {
438   // Add the label to the collector, but discard duplicates.
439   int length = targets_.length();
440   for (int i = 0; i < length; i++) {
441     if (targets_[i] == target) return;
442   }
443   targets_.Add(target, zone);
444 }
445
446
447 void UnaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
448   // TODO(olivf) If this Operation is used in a test context, then the
449   // expression has a ToBoolean stub and we want to collect the type
450   // information. However the GraphBuilder expects it to be on the instruction
451   // corresponding to the TestContext, therefore we have to store it here and
452   // not on the operand.
453   set_to_boolean_types(oracle->ToBooleanTypes(expression()->test_id()));
454 }
455
456
457 void BinaryOperation::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
458   // TODO(olivf) If this Operation is used in a test context, then the right
459   // hand side has a ToBoolean stub and we want to collect the type information.
460   // However the GraphBuilder expects it to be on the instruction corresponding
461   // to the TestContext, therefore we have to store it here and not on the
462   // right hand operand.
463   set_to_boolean_types(oracle->ToBooleanTypes(right()->test_id()));
464 }
465
466
467 bool BinaryOperation::ResultOverwriteAllowed() {
468   switch (op_) {
469     case Token::COMMA:
470     case Token::OR:
471     case Token::AND:
472       return false;
473     case Token::BIT_OR:
474     case Token::BIT_XOR:
475     case Token::BIT_AND:
476     case Token::SHL:
477     case Token::SAR:
478     case Token::SHR:
479     case Token::ADD:
480     case Token::SUB:
481     case Token::MUL:
482     case Token::DIV:
483     case Token::MOD:
484       return true;
485     default:
486       UNREACHABLE();
487   }
488   return false;
489 }
490
491
492 static bool IsTypeof(Expression* expr) {
493   UnaryOperation* maybe_unary = expr->AsUnaryOperation();
494   return maybe_unary != NULL && maybe_unary->op() == Token::TYPEOF;
495 }
496
497
498 // Check for the pattern: typeof <expression> equals <string literal>.
499 static bool MatchLiteralCompareTypeof(Expression* left,
500                                       Token::Value op,
501                                       Expression* right,
502                                       Expression** expr,
503                                       Handle<String>* check) {
504   if (IsTypeof(left) && right->IsStringLiteral() && Token::IsEqualityOp(op)) {
505     *expr = left->AsUnaryOperation()->expression();
506     *check = Handle<String>::cast(right->AsLiteral()->value());
507     return true;
508   }
509   return false;
510 }
511
512
513 bool CompareOperation::IsLiteralCompareTypeof(Expression** expr,
514                                               Handle<String>* check) {
515   return MatchLiteralCompareTypeof(left_, op_, right_, expr, check) ||
516       MatchLiteralCompareTypeof(right_, op_, left_, expr, check);
517 }
518
519
520 static bool IsVoidOfLiteral(Expression* expr) {
521   UnaryOperation* maybe_unary = expr->AsUnaryOperation();
522   return maybe_unary != NULL &&
523       maybe_unary->op() == Token::VOID &&
524       maybe_unary->expression()->AsLiteral() != NULL;
525 }
526
527
528 // Check for the pattern: void <literal> equals <expression> or
529 // undefined equals <expression>
530 static bool MatchLiteralCompareUndefined(Expression* left,
531                                          Token::Value op,
532                                          Expression* right,
533                                          Expression** expr,
534                                          Isolate* isolate) {
535   if (IsVoidOfLiteral(left) && Token::IsEqualityOp(op)) {
536     *expr = right;
537     return true;
538   }
539   if (left->IsUndefinedLiteral(isolate) && Token::IsEqualityOp(op)) {
540     *expr = right;
541     return true;
542   }
543   return false;
544 }
545
546
547 bool CompareOperation::IsLiteralCompareUndefined(
548     Expression** expr, Isolate* isolate) {
549   return MatchLiteralCompareUndefined(left_, op_, right_, expr, isolate) ||
550       MatchLiteralCompareUndefined(right_, op_, left_, expr, isolate);
551 }
552
553
554 // Check for the pattern: null equals <expression>
555 static bool MatchLiteralCompareNull(Expression* left,
556                                     Token::Value op,
557                                     Expression* right,
558                                     Expression** expr) {
559   if (left->IsNullLiteral() && Token::IsEqualityOp(op)) {
560     *expr = right;
561     return true;
562   }
563   return false;
564 }
565
566
567 bool CompareOperation::IsLiteralCompareNull(Expression** expr) {
568   return MatchLiteralCompareNull(left_, op_, right_, expr) ||
569       MatchLiteralCompareNull(right_, op_, left_, expr);
570 }
571
572
573 // ----------------------------------------------------------------------------
574 // Inlining support
575
576 bool Declaration::IsInlineable() const {
577   return proxy()->var()->IsStackAllocated();
578 }
579
580 bool FunctionDeclaration::IsInlineable() const {
581   return false;
582 }
583
584
585 // ----------------------------------------------------------------------------
586 // Recording of type feedback
587
588 // TODO(rossberg): all RecordTypeFeedback functions should disappear
589 // once we use the common type field in the AST consistently.
590
591 void Expression::RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) {
592   to_boolean_types_ = oracle->ToBooleanTypes(test_id());
593 }
594
595
596 Call::CallType Call::GetCallType(Isolate* isolate) const {
597   VariableProxy* proxy = expression()->AsVariableProxy();
598   if (proxy != NULL) {
599     if (proxy->var()->is_possibly_eval(isolate)) {
600       return POSSIBLY_EVAL_CALL;
601     } else if (proxy->var()->IsUnallocated()) {
602       return GLOBAL_CALL;
603     } else if (proxy->var()->IsLookupSlot()) {
604       return LOOKUP_SLOT_CALL;
605     }
606   }
607
608   Property* property = expression()->AsProperty();
609   return property != NULL ? PROPERTY_CALL : OTHER_CALL;
610 }
611
612
613 bool Call::ComputeGlobalTarget(Handle<GlobalObject> global,
614                                LookupResult* lookup) {
615   target_ = Handle<JSFunction>::null();
616   cell_ = Handle<Cell>::null();
617   ASSERT(lookup->IsFound() &&
618          lookup->type() == NORMAL &&
619          lookup->holder() == *global);
620   cell_ = Handle<Cell>(global->GetPropertyCell(lookup));
621   if (cell_->value()->IsJSFunction()) {
622     Handle<JSFunction> candidate(JSFunction::cast(cell_->value()));
623     // If the function is in new space we assume it's more likely to
624     // change and thus prefer the general IC code.
625     if (!lookup->isolate()->heap()->InNewSpace(*candidate)) {
626       target_ = candidate;
627       return true;
628     }
629   }
630   return false;
631 }
632
633
634 void CallNew::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
635   allocation_site_ =
636       oracle->GetCallNewAllocationSite(CallNewFeedbackId());
637   is_monomorphic_ = oracle->CallNewIsMonomorphic(CallNewFeedbackId());
638   if (is_monomorphic_) {
639     target_ = oracle->GetCallNewTarget(CallNewFeedbackId());
640     if (!allocation_site_.is_null()) {
641       elements_kind_ = allocation_site_->GetElementsKind();
642     }
643   }
644 }
645
646
647 void ObjectLiteral::Property::RecordTypeFeedback(TypeFeedbackOracle* oracle) {
648   TypeFeedbackId id = key()->LiteralFeedbackId();
649   SmallMapList maps;
650   oracle->CollectReceiverTypes(id, &maps);
651   receiver_type_ = maps.length() == 1 ? maps.at(0)
652                                       : Handle<Map>::null();
653 }
654
655
656 // ----------------------------------------------------------------------------
657 // Implementation of AstVisitor
658
659 void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) {
660   for (int i = 0; i < declarations->length(); i++) {
661     Visit(declarations->at(i));
662   }
663 }
664
665
666 void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) {
667   for (int i = 0; i < statements->length(); i++) {
668     Statement* stmt = statements->at(i);
669     Visit(stmt);
670     if (stmt->IsJump()) break;
671   }
672 }
673
674
675 void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) {
676   for (int i = 0; i < expressions->length(); i++) {
677     // The variable statement visiting code may pass NULL expressions
678     // to this code. Maybe this should be handled by introducing an
679     // undefined expression or literal?  Revisit this code if this
680     // changes
681     Expression* expression = expressions->at(i);
682     if (expression != NULL) Visit(expression);
683   }
684 }
685
686
687 // ----------------------------------------------------------------------------
688 // Regular expressions
689
690 #define MAKE_ACCEPT(Name)                                            \
691   void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) {   \
692     return visitor->Visit##Name(this, data);                         \
693   }
694 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT)
695 #undef MAKE_ACCEPT
696
697 #define MAKE_TYPE_CASE(Name)                                         \
698   RegExp##Name* RegExpTree::As##Name() {                             \
699     return NULL;                                                     \
700   }                                                                  \
701   bool RegExpTree::Is##Name() { return false; }
702 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
703 #undef MAKE_TYPE_CASE
704
705 #define MAKE_TYPE_CASE(Name)                                        \
706   RegExp##Name* RegExp##Name::As##Name() {                          \
707     return this;                                                    \
708   }                                                                 \
709   bool RegExp##Name::Is##Name() { return true; }
710 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
711 #undef MAKE_TYPE_CASE
712
713
714 static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) {
715   Interval result = Interval::Empty();
716   for (int i = 0; i < children->length(); i++)
717     result = result.Union(children->at(i)->CaptureRegisters());
718   return result;
719 }
720
721
722 Interval RegExpAlternative::CaptureRegisters() {
723   return ListCaptureRegisters(nodes());
724 }
725
726
727 Interval RegExpDisjunction::CaptureRegisters() {
728   return ListCaptureRegisters(alternatives());
729 }
730
731
732 Interval RegExpLookahead::CaptureRegisters() {
733   return body()->CaptureRegisters();
734 }
735
736
737 Interval RegExpCapture::CaptureRegisters() {
738   Interval self(StartRegister(index()), EndRegister(index()));
739   return self.Union(body()->CaptureRegisters());
740 }
741
742
743 Interval RegExpQuantifier::CaptureRegisters() {
744   return body()->CaptureRegisters();
745 }
746
747
748 bool RegExpAssertion::IsAnchoredAtStart() {
749   return assertion_type() == RegExpAssertion::START_OF_INPUT;
750 }
751
752
753 bool RegExpAssertion::IsAnchoredAtEnd() {
754   return assertion_type() == RegExpAssertion::END_OF_INPUT;
755 }
756
757
758 bool RegExpAlternative::IsAnchoredAtStart() {
759   ZoneList<RegExpTree*>* nodes = this->nodes();
760   for (int i = 0; i < nodes->length(); i++) {
761     RegExpTree* node = nodes->at(i);
762     if (node->IsAnchoredAtStart()) { return true; }
763     if (node->max_match() > 0) { return false; }
764   }
765   return false;
766 }
767
768
769 bool RegExpAlternative::IsAnchoredAtEnd() {
770   ZoneList<RegExpTree*>* nodes = this->nodes();
771   for (int i = nodes->length() - 1; i >= 0; i--) {
772     RegExpTree* node = nodes->at(i);
773     if (node->IsAnchoredAtEnd()) { return true; }
774     if (node->max_match() > 0) { return false; }
775   }
776   return false;
777 }
778
779
780 bool RegExpDisjunction::IsAnchoredAtStart() {
781   ZoneList<RegExpTree*>* alternatives = this->alternatives();
782   for (int i = 0; i < alternatives->length(); i++) {
783     if (!alternatives->at(i)->IsAnchoredAtStart())
784       return false;
785   }
786   return true;
787 }
788
789
790 bool RegExpDisjunction::IsAnchoredAtEnd() {
791   ZoneList<RegExpTree*>* alternatives = this->alternatives();
792   for (int i = 0; i < alternatives->length(); i++) {
793     if (!alternatives->at(i)->IsAnchoredAtEnd())
794       return false;
795   }
796   return true;
797 }
798
799
800 bool RegExpLookahead::IsAnchoredAtStart() {
801   return is_positive() && body()->IsAnchoredAtStart();
802 }
803
804
805 bool RegExpCapture::IsAnchoredAtStart() {
806   return body()->IsAnchoredAtStart();
807 }
808
809
810 bool RegExpCapture::IsAnchoredAtEnd() {
811   return body()->IsAnchoredAtEnd();
812 }
813
814
815 // Convert regular expression trees to a simple sexp representation.
816 // This representation should be different from the input grammar
817 // in as many cases as possible, to make it more difficult for incorrect
818 // parses to look as correct ones which is likely if the input and
819 // output formats are alike.
820 class RegExpUnparser V8_FINAL : public RegExpVisitor {
821  public:
822   explicit RegExpUnparser(Zone* zone);
823   void VisitCharacterRange(CharacterRange that);
824   SmartArrayPointer<const char> ToString() { return stream_.ToCString(); }
825 #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*,          \
826                                                   void* data) V8_OVERRIDE;
827   FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
828 #undef MAKE_CASE
829  private:
830   StringStream* stream() { return &stream_; }
831   HeapStringAllocator alloc_;
832   StringStream stream_;
833   Zone* zone_;
834 };
835
836
837 RegExpUnparser::RegExpUnparser(Zone* zone) : stream_(&alloc_), zone_(zone) {
838 }
839
840
841 void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) {
842   stream()->Add("(|");
843   for (int i = 0; i <  that->alternatives()->length(); i++) {
844     stream()->Add(" ");
845     that->alternatives()->at(i)->Accept(this, data);
846   }
847   stream()->Add(")");
848   return NULL;
849 }
850
851
852 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
853   stream()->Add("(:");
854   for (int i = 0; i <  that->nodes()->length(); i++) {
855     stream()->Add(" ");
856     that->nodes()->at(i)->Accept(this, data);
857   }
858   stream()->Add(")");
859   return NULL;
860 }
861
862
863 void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
864   stream()->Add("%k", that.from());
865   if (!that.IsSingleton()) {
866     stream()->Add("-%k", that.to());
867   }
868 }
869
870
871
872 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
873                                           void* data) {
874   if (that->is_negated())
875     stream()->Add("^");
876   stream()->Add("[");
877   for (int i = 0; i < that->ranges(zone_)->length(); i++) {
878     if (i > 0) stream()->Add(" ");
879     VisitCharacterRange(that->ranges(zone_)->at(i));
880   }
881   stream()->Add("]");
882   return NULL;
883 }
884
885
886 void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) {
887   switch (that->assertion_type()) {
888     case RegExpAssertion::START_OF_INPUT:
889       stream()->Add("@^i");
890       break;
891     case RegExpAssertion::END_OF_INPUT:
892       stream()->Add("@$i");
893       break;
894     case RegExpAssertion::START_OF_LINE:
895       stream()->Add("@^l");
896       break;
897     case RegExpAssertion::END_OF_LINE:
898       stream()->Add("@$l");
899        break;
900     case RegExpAssertion::BOUNDARY:
901       stream()->Add("@b");
902       break;
903     case RegExpAssertion::NON_BOUNDARY:
904       stream()->Add("@B");
905       break;
906   }
907   return NULL;
908 }
909
910
911 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
912   stream()->Add("'");
913   Vector<const uc16> chardata = that->data();
914   for (int i = 0; i < chardata.length(); i++) {
915     stream()->Add("%k", chardata[i]);
916   }
917   stream()->Add("'");
918   return NULL;
919 }
920
921
922 void* RegExpUnparser::VisitText(RegExpText* that, void* data) {
923   if (that->elements()->length() == 1) {
924     that->elements()->at(0).tree()->Accept(this, data);
925   } else {
926     stream()->Add("(!");
927     for (int i = 0; i < that->elements()->length(); i++) {
928       stream()->Add(" ");
929       that->elements()->at(i).tree()->Accept(this, data);
930     }
931     stream()->Add(")");
932   }
933   return NULL;
934 }
935
936
937 void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) {
938   stream()->Add("(# %i ", that->min());
939   if (that->max() == RegExpTree::kInfinity) {
940     stream()->Add("- ");
941   } else {
942     stream()->Add("%i ", that->max());
943   }
944   stream()->Add(that->is_greedy() ? "g " : that->is_possessive() ? "p " : "n ");
945   that->body()->Accept(this, data);
946   stream()->Add(")");
947   return NULL;
948 }
949
950
951 void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) {
952   stream()->Add("(^ ");
953   that->body()->Accept(this, data);
954   stream()->Add(")");
955   return NULL;
956 }
957
958
959 void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) {
960   stream()->Add("(-> ");
961   stream()->Add(that->is_positive() ? "+ " : "- ");
962   that->body()->Accept(this, data);
963   stream()->Add(")");
964   return NULL;
965 }
966
967
968 void* RegExpUnparser::VisitBackReference(RegExpBackReference* that,
969                                          void* data) {
970   stream()->Add("(<- %i)", that->index());
971   return NULL;
972 }
973
974
975 void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) {
976   stream()->Put('%');
977   return NULL;
978 }
979
980
981 SmartArrayPointer<const char> RegExpTree::ToString(Zone* zone) {
982   RegExpUnparser unparser(zone);
983   Accept(&unparser, NULL);
984   return unparser.ToString();
985 }
986
987
988 RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives)
989     : alternatives_(alternatives) {
990   ASSERT(alternatives->length() > 1);
991   RegExpTree* first_alternative = alternatives->at(0);
992   min_match_ = first_alternative->min_match();
993   max_match_ = first_alternative->max_match();
994   for (int i = 1; i < alternatives->length(); i++) {
995     RegExpTree* alternative = alternatives->at(i);
996     min_match_ = Min(min_match_, alternative->min_match());
997     max_match_ = Max(max_match_, alternative->max_match());
998   }
999 }
1000
1001
1002 static int IncreaseBy(int previous, int increase) {
1003   if (RegExpTree::kInfinity - previous < increase) {
1004     return RegExpTree::kInfinity;
1005   } else {
1006     return previous + increase;
1007   }
1008 }
1009
1010 RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes)
1011     : nodes_(nodes) {
1012   ASSERT(nodes->length() > 1);
1013   min_match_ = 0;
1014   max_match_ = 0;
1015   for (int i = 0; i < nodes->length(); i++) {
1016     RegExpTree* node = nodes->at(i);
1017     int node_min_match = node->min_match();
1018     min_match_ = IncreaseBy(min_match_, node_min_match);
1019     int node_max_match = node->max_match();
1020     max_match_ = IncreaseBy(max_match_, node_max_match);
1021   }
1022 }
1023
1024
1025 CaseClause::CaseClause(Zone* zone,
1026                        Expression* label,
1027                        ZoneList<Statement*>* statements,
1028                        int pos)
1029     : Expression(zone, pos),
1030       label_(label),
1031       statements_(statements),
1032       compare_type_(Type::None(zone)),
1033       compare_id_(AstNode::GetNextId(zone)),
1034       entry_id_(AstNode::GetNextId(zone)) {
1035 }
1036
1037
1038 #define REGULAR_NODE(NodeType) \
1039   void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1040     increase_node_count(); \
1041   }
1042 #define DONT_OPTIMIZE_NODE(NodeType) \
1043   void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1044     increase_node_count(); \
1045     set_dont_optimize_reason(k##NodeType); \
1046     add_flag(kDontInline); \
1047     add_flag(kDontSelfOptimize); \
1048   }
1049 #define DONT_SELFOPTIMIZE_NODE(NodeType) \
1050   void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1051     increase_node_count(); \
1052     add_flag(kDontSelfOptimize); \
1053   }
1054 #define DONT_CACHE_NODE(NodeType) \
1055   void AstConstructionVisitor::Visit##NodeType(NodeType* node) { \
1056     increase_node_count(); \
1057     set_dont_optimize_reason(k##NodeType); \
1058     add_flag(kDontInline); \
1059     add_flag(kDontSelfOptimize); \
1060     add_flag(kDontCache); \
1061   }
1062
1063 REGULAR_NODE(VariableDeclaration)
1064 REGULAR_NODE(FunctionDeclaration)
1065 REGULAR_NODE(Block)
1066 REGULAR_NODE(ExpressionStatement)
1067 REGULAR_NODE(EmptyStatement)
1068 REGULAR_NODE(IfStatement)
1069 REGULAR_NODE(ContinueStatement)
1070 REGULAR_NODE(BreakStatement)
1071 REGULAR_NODE(ReturnStatement)
1072 REGULAR_NODE(SwitchStatement)
1073 REGULAR_NODE(CaseClause)
1074 REGULAR_NODE(Conditional)
1075 REGULAR_NODE(Literal)
1076 REGULAR_NODE(ArrayLiteral)
1077 REGULAR_NODE(ObjectLiteral)
1078 REGULAR_NODE(RegExpLiteral)
1079 REGULAR_NODE(FunctionLiteral)
1080 REGULAR_NODE(Assignment)
1081 REGULAR_NODE(Throw)
1082 REGULAR_NODE(Property)
1083 REGULAR_NODE(UnaryOperation)
1084 REGULAR_NODE(CountOperation)
1085 REGULAR_NODE(BinaryOperation)
1086 REGULAR_NODE(CompareOperation)
1087 REGULAR_NODE(ThisFunction)
1088 REGULAR_NODE(Call)
1089 REGULAR_NODE(CallNew)
1090 // In theory, for VariableProxy we'd have to add:
1091 // if (node->var()->IsLookupSlot()) add_flag(kDontInline);
1092 // But node->var() is usually not bound yet at VariableProxy creation time, and
1093 // LOOKUP variables only result from constructs that cannot be inlined anyway.
1094 REGULAR_NODE(VariableProxy)
1095
1096 // We currently do not optimize any modules.
1097 DONT_OPTIMIZE_NODE(ModuleDeclaration)
1098 DONT_OPTIMIZE_NODE(ImportDeclaration)
1099 DONT_OPTIMIZE_NODE(ExportDeclaration)
1100 DONT_OPTIMIZE_NODE(ModuleVariable)
1101 DONT_OPTIMIZE_NODE(ModulePath)
1102 DONT_OPTIMIZE_NODE(ModuleUrl)
1103 DONT_OPTIMIZE_NODE(ModuleStatement)
1104 DONT_OPTIMIZE_NODE(Yield)
1105 DONT_OPTIMIZE_NODE(WithStatement)
1106 DONT_OPTIMIZE_NODE(TryCatchStatement)
1107 DONT_OPTIMIZE_NODE(TryFinallyStatement)
1108 DONT_OPTIMIZE_NODE(DebuggerStatement)
1109 DONT_OPTIMIZE_NODE(NativeFunctionLiteral)
1110
1111 DONT_SELFOPTIMIZE_NODE(DoWhileStatement)
1112 DONT_SELFOPTIMIZE_NODE(WhileStatement)
1113 DONT_SELFOPTIMIZE_NODE(ForStatement)
1114 DONT_SELFOPTIMIZE_NODE(ForInStatement)
1115 DONT_SELFOPTIMIZE_NODE(ForOfStatement)
1116
1117 DONT_CACHE_NODE(ModuleLiteral)
1118
1119 void AstConstructionVisitor::VisitCallRuntime(CallRuntime* node) {
1120   increase_node_count();
1121   if (node->is_jsruntime()) {
1122     // Don't try to inline JS runtime calls because we don't (currently) even
1123     // optimize them.
1124     add_flag(kDontInline);
1125   } else if (node->function()->intrinsic_type == Runtime::INLINE &&
1126       (node->name()->IsOneByteEqualTo(
1127           STATIC_ASCII_VECTOR("_ArgumentsLength")) ||
1128        node->name()->IsOneByteEqualTo(STATIC_ASCII_VECTOR("_Arguments")))) {
1129     // Don't inline the %_ArgumentsLength or %_Arguments because their
1130     // implementation will not work.  There is no stack frame to get them
1131     // from.
1132     add_flag(kDontInline);
1133   }
1134 }
1135
1136 #undef REGULAR_NODE
1137 #undef DONT_OPTIMIZE_NODE
1138 #undef DONT_SELFOPTIMIZE_NODE
1139 #undef DONT_CACHE_NODE
1140
1141
1142 Handle<String> Literal::ToString() {
1143   if (value_->IsString()) return Handle<String>::cast(value_);
1144   ASSERT(value_->IsNumber());
1145   char arr[100];
1146   Vector<char> buffer(arr, ARRAY_SIZE(arr));
1147   const char* str;
1148   if (value_->IsSmi()) {
1149     // Optimization only, the heap number case would subsume this.
1150     OS::SNPrintF(buffer, "%d", Smi::cast(*value_)->value());
1151     str = arr;
1152   } else {
1153     str = DoubleToCString(value_->Number(), buffer);
1154   }
1155   return isolate_->factory()->NewStringFromAscii(CStrVector(str));
1156 }
1157
1158
1159 } }  // namespace v8::internal