OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 }; | 1402 }; |
1403 | 1403 |
1404 | 1404 |
1405 // Base class for literals that needs space in the corresponding JSFunction. | 1405 // Base class for literals that needs space in the corresponding JSFunction. |
1406 class MaterializedLiteral : public Expression { | 1406 class MaterializedLiteral : public Expression { |
1407 public: | 1407 public: |
1408 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; } | 1408 virtual MaterializedLiteral* AsMaterializedLiteral() { return this; } |
1409 | 1409 |
1410 int literal_index() { return literal_index_; } | 1410 int literal_index() { return literal_index_; } |
1411 | 1411 |
| 1412 protected: |
| 1413 MaterializedLiteral(Isolate* isolate, |
| 1414 int literal_index, |
| 1415 int pos) |
| 1416 : Expression(isolate, pos), |
| 1417 literal_index_(literal_index), |
| 1418 is_simple_(false) {} |
| 1419 |
1412 // A materialized literal is simple if the values consist of only | 1420 // A materialized literal is simple if the values consist of only |
1413 // constants and simple object and array literals. | 1421 // constants and simple object and array literals. |
1414 bool is_simple() const { return is_simple_; } | 1422 bool is_simple() const { return is_simple_; } |
| 1423 void set_is_simple(bool is_simple) { is_simple_ = is_simple; } |
| 1424 friend class CompileTimeValue; |
1415 | 1425 |
1416 int depth() const { return depth_; } | 1426 // Populate the constant properties/elements fixed array. |
| 1427 void BuildConstants(Isolate* isolate, int* depth); |
| 1428 friend class ArrayLiteral; |
| 1429 friend class ObjectLiteral; |
1417 | 1430 |
1418 protected: | 1431 // If the expression is a literal, return the literal value; |
1419 MaterializedLiteral(Isolate* isolate, | 1432 // if the expression is a materialized literal and is simple return a |
1420 int literal_index, | 1433 // compile time value as encoded by CompileTimeValue::GetValue(). |
1421 bool is_simple, | 1434 // Otherwise, return undefined literal as the placeholder |
1422 int depth, | 1435 // in the object literal boilerplate. |
1423 int pos) | 1436 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate); |
1424 : Expression(isolate, pos), | |
1425 literal_index_(literal_index), | |
1426 is_simple_(is_simple), | |
1427 depth_(depth) {} | |
1428 | 1437 |
1429 private: | 1438 private: |
1430 int literal_index_; | 1439 int literal_index_; |
1431 bool is_simple_; | 1440 bool is_simple_; |
1432 int depth_; | |
1433 }; | 1441 }; |
1434 | 1442 |
1435 | 1443 |
1436 // Property is used for passing information | 1444 // Property is used for passing information |
1437 // about an object literal's properties from the parser | 1445 // about an object literal's properties from the parser |
1438 // to the code generator. | 1446 // to the code generator. |
1439 class ObjectLiteralProperty V8_FINAL : public ZoneObject { | 1447 class ObjectLiteralProperty V8_FINAL : public ZoneObject { |
1440 public: | 1448 public: |
1441 enum Kind { | 1449 enum Kind { |
1442 CONSTANT, // Property with constant value (compile time). | 1450 CONSTANT, // Property with constant value (compile time). |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1486 DECLARE_NODE_TYPE(ObjectLiteral) | 1494 DECLARE_NODE_TYPE(ObjectLiteral) |
1487 | 1495 |
1488 Handle<FixedArray> constant_properties() const { | 1496 Handle<FixedArray> constant_properties() const { |
1489 return constant_properties_; | 1497 return constant_properties_; |
1490 } | 1498 } |
1491 ZoneList<Property*>* properties() const { return properties_; } | 1499 ZoneList<Property*>* properties() const { return properties_; } |
1492 bool fast_elements() const { return fast_elements_; } | 1500 bool fast_elements() const { return fast_elements_; } |
1493 bool may_store_doubles() const { return may_store_doubles_; } | 1501 bool may_store_doubles() const { return may_store_doubles_; } |
1494 bool has_function() const { return has_function_; } | 1502 bool has_function() const { return has_function_; } |
1495 | 1503 |
| 1504 // Decide if a property should be in the object boilerplate. |
| 1505 static bool IsBoilerplateProperty(Property* property); |
| 1506 |
| 1507 // Populate the constant properties fixed array. |
| 1508 void BuildConstantProperties(Isolate* isolate, int* depth = NULL); |
| 1509 |
1496 // Mark all computed expressions that are bound to a key that | 1510 // Mark all computed expressions that are bound to a key that |
1497 // is shadowed by a later occurrence of the same key. For the | 1511 // is shadowed by a later occurrence of the same key. For the |
1498 // marked expressions, no store code is emitted. | 1512 // marked expressions, no store code is emitted. |
1499 void CalculateEmitStore(Zone* zone); | 1513 void CalculateEmitStore(Zone* zone); |
1500 | 1514 |
1501 enum Flags { | 1515 enum Flags { |
1502 kNoFlags = 0, | 1516 kNoFlags = 0, |
1503 kFastElements = 1, | 1517 kFastElements = 1, |
1504 kHasFunction = 1 << 1 | 1518 kHasFunction = 1 << 1 |
1505 }; | 1519 }; |
1506 | 1520 |
1507 struct Accessors: public ZoneObject { | 1521 struct Accessors: public ZoneObject { |
1508 Accessors() : getter(NULL), setter(NULL) { } | 1522 Accessors() : getter(NULL), setter(NULL) { } |
1509 Expression* getter; | 1523 Expression* getter; |
1510 Expression* setter; | 1524 Expression* setter; |
1511 }; | 1525 }; |
1512 | 1526 |
1513 protected: | 1527 protected: |
1514 ObjectLiteral(Isolate* isolate, | 1528 ObjectLiteral(Isolate* isolate, |
1515 Handle<FixedArray> constant_properties, | |
1516 ZoneList<Property*>* properties, | 1529 ZoneList<Property*>* properties, |
1517 int literal_index, | 1530 int literal_index, |
1518 bool is_simple, | 1531 int boilerplate_properties, |
1519 bool fast_elements, | |
1520 int depth, | |
1521 bool may_store_doubles, | |
1522 bool has_function, | 1532 bool has_function, |
1523 int pos) | 1533 int pos) |
1524 : MaterializedLiteral(isolate, literal_index, is_simple, depth, pos), | 1534 : MaterializedLiteral(isolate, literal_index, pos), |
1525 constant_properties_(constant_properties), | |
1526 properties_(properties), | 1535 properties_(properties), |
1527 fast_elements_(fast_elements), | 1536 boilerplate_properties_(boilerplate_properties), |
1528 may_store_doubles_(may_store_doubles), | 1537 fast_elements_(false), |
| 1538 may_store_doubles_(false), |
1529 has_function_(has_function) {} | 1539 has_function_(has_function) {} |
1530 | 1540 |
1531 private: | 1541 private: |
1532 Handle<FixedArray> constant_properties_; | 1542 Handle<FixedArray> constant_properties_; |
1533 ZoneList<Property*>* properties_; | 1543 ZoneList<Property*>* properties_; |
| 1544 int boilerplate_properties_; |
1534 bool fast_elements_; | 1545 bool fast_elements_; |
1535 bool may_store_doubles_; | 1546 bool may_store_doubles_; |
1536 bool has_function_; | 1547 bool has_function_; |
1537 }; | 1548 }; |
1538 | 1549 |
1539 | 1550 |
1540 // Node for capturing a regexp literal. | 1551 // Node for capturing a regexp literal. |
1541 class RegExpLiteral V8_FINAL : public MaterializedLiteral { | 1552 class RegExpLiteral V8_FINAL : public MaterializedLiteral { |
1542 public: | 1553 public: |
1543 DECLARE_NODE_TYPE(RegExpLiteral) | 1554 DECLARE_NODE_TYPE(RegExpLiteral) |
1544 | 1555 |
1545 Handle<String> pattern() const { return pattern_; } | 1556 Handle<String> pattern() const { return pattern_; } |
1546 Handle<String> flags() const { return flags_; } | 1557 Handle<String> flags() const { return flags_; } |
1547 | 1558 |
1548 protected: | 1559 protected: |
1549 RegExpLiteral(Isolate* isolate, | 1560 RegExpLiteral(Isolate* isolate, |
1550 Handle<String> pattern, | 1561 Handle<String> pattern, |
1551 Handle<String> flags, | 1562 Handle<String> flags, |
1552 int literal_index, | 1563 int literal_index, |
1553 int pos) | 1564 int pos) |
1554 : MaterializedLiteral(isolate, literal_index, false, 1, pos), | 1565 : MaterializedLiteral(isolate, literal_index, pos), |
1555 pattern_(pattern), | 1566 pattern_(pattern), |
1556 flags_(flags) {} | 1567 flags_(flags) {} |
1557 | 1568 |
1558 private: | 1569 private: |
1559 Handle<String> pattern_; | 1570 Handle<String> pattern_; |
1560 Handle<String> flags_; | 1571 Handle<String> flags_; |
1561 }; | 1572 }; |
1562 | 1573 |
1563 | 1574 |
1564 // An array literal has a literals object that is used | 1575 // An array literal has a literals object that is used |
1565 // for minimizing the work when constructing it at runtime. | 1576 // for minimizing the work when constructing it at runtime. |
1566 class ArrayLiteral V8_FINAL : public MaterializedLiteral { | 1577 class ArrayLiteral V8_FINAL : public MaterializedLiteral { |
1567 public: | 1578 public: |
1568 DECLARE_NODE_TYPE(ArrayLiteral) | 1579 DECLARE_NODE_TYPE(ArrayLiteral) |
1569 | 1580 |
1570 Handle<FixedArray> constant_elements() const { return constant_elements_; } | 1581 Handle<FixedArray> constant_elements() const { return constant_elements_; } |
1571 ZoneList<Expression*>* values() const { return values_; } | 1582 ZoneList<Expression*>* values() const { return values_; } |
1572 | 1583 |
1573 // Return an AST id for an element that is used in simulate instructions. | 1584 // Return an AST id for an element that is used in simulate instructions. |
1574 BailoutId GetIdForElement(int i) { | 1585 BailoutId GetIdForElement(int i) { |
1575 return BailoutId(first_element_id_.ToInt() + i); | 1586 return BailoutId(first_element_id_.ToInt() + i); |
1576 } | 1587 } |
1577 | 1588 |
| 1589 // Populate the constant elements fixed array. |
| 1590 void BuildConstantElements(Isolate* isolate, int* depth = NULL); |
| 1591 |
1578 protected: | 1592 protected: |
1579 ArrayLiteral(Isolate* isolate, | 1593 ArrayLiteral(Isolate* isolate, |
1580 Handle<FixedArray> constant_elements, | |
1581 ZoneList<Expression*>* values, | 1594 ZoneList<Expression*>* values, |
1582 int literal_index, | 1595 int literal_index, |
1583 bool is_simple, | |
1584 int depth, | |
1585 int pos) | 1596 int pos) |
1586 : MaterializedLiteral(isolate, literal_index, is_simple, depth, pos), | 1597 : MaterializedLiteral(isolate, literal_index, pos), |
1587 constant_elements_(constant_elements), | |
1588 values_(values), | 1598 values_(values), |
1589 first_element_id_(ReserveIdRange(isolate, values->length())) {} | 1599 first_element_id_(ReserveIdRange(isolate, values->length())) {} |
1590 | 1600 |
1591 private: | 1601 private: |
1592 Handle<FixedArray> constant_elements_; | 1602 Handle<FixedArray> constant_elements_; |
1593 ZoneList<Expression*>* values_; | 1603 ZoneList<Expression*>* values_; |
1594 const BailoutId first_element_id_; | 1604 const BailoutId first_element_id_; |
1595 }; | 1605 }; |
1596 | 1606 |
1597 | 1607 |
(...skipping 1461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3059 Literal* NewLiteral(Handle<Object> handle, int pos) { | 3069 Literal* NewLiteral(Handle<Object> handle, int pos) { |
3060 Literal* lit = new(zone_) Literal(isolate_, handle, pos); | 3070 Literal* lit = new(zone_) Literal(isolate_, handle, pos); |
3061 VISIT_AND_RETURN(Literal, lit) | 3071 VISIT_AND_RETURN(Literal, lit) |
3062 } | 3072 } |
3063 | 3073 |
3064 Literal* NewNumberLiteral(double number, int pos) { | 3074 Literal* NewNumberLiteral(double number, int pos) { |
3065 return NewLiteral(isolate_->factory()->NewNumber(number, TENURED), pos); | 3075 return NewLiteral(isolate_->factory()->NewNumber(number, TENURED), pos); |
3066 } | 3076 } |
3067 | 3077 |
3068 ObjectLiteral* NewObjectLiteral( | 3078 ObjectLiteral* NewObjectLiteral( |
3069 Handle<FixedArray> constant_properties, | |
3070 ZoneList<ObjectLiteral::Property*>* properties, | 3079 ZoneList<ObjectLiteral::Property*>* properties, |
3071 int literal_index, | 3080 int literal_index, |
3072 bool is_simple, | 3081 int boilerplate_properties, |
3073 bool fast_elements, | |
3074 int depth, | |
3075 bool may_store_doubles, | |
3076 bool has_function, | 3082 bool has_function, |
3077 int pos) { | 3083 int pos) { |
3078 ObjectLiteral* lit = new(zone_) ObjectLiteral( | 3084 ObjectLiteral* lit = new(zone_) ObjectLiteral( |
3079 isolate_, constant_properties, properties, literal_index, | 3085 isolate_, properties, literal_index, boilerplate_properties, |
3080 is_simple, fast_elements, depth, may_store_doubles, has_function, pos); | 3086 has_function, pos); |
3081 VISIT_AND_RETURN(ObjectLiteral, lit) | 3087 VISIT_AND_RETURN(ObjectLiteral, lit) |
3082 } | 3088 } |
3083 | 3089 |
3084 ObjectLiteral::Property* NewObjectLiteralProperty(bool is_getter, | 3090 ObjectLiteral::Property* NewObjectLiteralProperty(bool is_getter, |
3085 FunctionLiteral* value, | 3091 FunctionLiteral* value, |
3086 int pos) { | 3092 int pos) { |
3087 ObjectLiteral::Property* prop = | 3093 ObjectLiteral::Property* prop = |
3088 new(zone_) ObjectLiteral::Property(is_getter, value); | 3094 new(zone_) ObjectLiteral::Property(is_getter, value); |
3089 prop->set_key(NewLiteral(value->name(), pos)); | 3095 prop->set_key(NewLiteral(value->name(), pos)); |
3090 return prop; // Not an AST node, will not be visited. | 3096 return prop; // Not an AST node, will not be visited. |
3091 } | 3097 } |
3092 | 3098 |
3093 RegExpLiteral* NewRegExpLiteral(Handle<String> pattern, | 3099 RegExpLiteral* NewRegExpLiteral(Handle<String> pattern, |
3094 Handle<String> flags, | 3100 Handle<String> flags, |
3095 int literal_index, | 3101 int literal_index, |
3096 int pos) { | 3102 int pos) { |
3097 RegExpLiteral* lit = | 3103 RegExpLiteral* lit = |
3098 new(zone_) RegExpLiteral(isolate_, pattern, flags, literal_index, pos); | 3104 new(zone_) RegExpLiteral(isolate_, pattern, flags, literal_index, pos); |
3099 VISIT_AND_RETURN(RegExpLiteral, lit); | 3105 VISIT_AND_RETURN(RegExpLiteral, lit); |
3100 } | 3106 } |
3101 | 3107 |
3102 ArrayLiteral* NewArrayLiteral(Handle<FixedArray> constant_elements, | 3108 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values, |
3103 ZoneList<Expression*>* values, | |
3104 int literal_index, | 3109 int literal_index, |
3105 bool is_simple, | |
3106 int depth, | |
3107 int pos) { | 3110 int pos) { |
3108 ArrayLiteral* lit = new(zone_) ArrayLiteral( | 3111 ArrayLiteral* lit = new(zone_) ArrayLiteral( |
3109 isolate_, constant_elements, values, literal_index, is_simple, | 3112 isolate_, values, literal_index, pos); |
3110 depth, pos); | |
3111 VISIT_AND_RETURN(ArrayLiteral, lit) | 3113 VISIT_AND_RETURN(ArrayLiteral, lit) |
3112 } | 3114 } |
3113 | 3115 |
3114 VariableProxy* NewVariableProxy(Variable* var, | 3116 VariableProxy* NewVariableProxy(Variable* var, |
3115 int pos = RelocInfo::kNoPosition) { | 3117 int pos = RelocInfo::kNoPosition) { |
3116 VariableProxy* proxy = new(zone_) VariableProxy(isolate_, var, pos); | 3118 VariableProxy* proxy = new(zone_) VariableProxy(isolate_, var, pos); |
3117 VISIT_AND_RETURN(VariableProxy, proxy) | 3119 VISIT_AND_RETURN(VariableProxy, proxy) |
3118 } | 3120 } |
3119 | 3121 |
3120 VariableProxy* NewVariableProxy(Handle<String> name, | 3122 VariableProxy* NewVariableProxy(Handle<String> name, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3265 private: | 3267 private: |
3266 Isolate* isolate_; | 3268 Isolate* isolate_; |
3267 Zone* zone_; | 3269 Zone* zone_; |
3268 Visitor visitor_; | 3270 Visitor visitor_; |
3269 }; | 3271 }; |
3270 | 3272 |
3271 | 3273 |
3272 } } // namespace v8::internal | 3274 } } // namespace v8::internal |
3273 | 3275 |
3274 #endif // V8_AST_H_ | 3276 #endif // V8_AST_H_ |
OLD | NEW |