Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: src/ast.h

Issue 1159005: Initial support for marking live code. (Closed)
Patch Set: Addressed review comments. Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/ast.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 virtual UnaryOperation* AsUnaryOperation() { return NULL; } 145 virtual UnaryOperation* AsUnaryOperation() { return NULL; }
146 virtual CountOperation* AsCountOperation() { return NULL; } 146 virtual CountOperation* AsCountOperation() { return NULL; }
147 virtual BinaryOperation* AsBinaryOperation() { return NULL; } 147 virtual BinaryOperation* AsBinaryOperation() { return NULL; }
148 virtual Assignment* AsAssignment() { return NULL; } 148 virtual Assignment* AsAssignment() { return NULL; }
149 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; } 149 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; }
150 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } 150 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
151 virtual ObjectLiteral* AsObjectLiteral() { return NULL; } 151 virtual ObjectLiteral* AsObjectLiteral() { return NULL; }
152 virtual ArrayLiteral* AsArrayLiteral() { return NULL; } 152 virtual ArrayLiteral* AsArrayLiteral() { return NULL; }
153 virtual CompareOperation* AsCompareOperation() { return NULL; } 153 virtual CompareOperation* AsCompareOperation() { return NULL; }
154 154
155 // True if the AST node is critical (its execution is needed or externally
156 // visible in some way).
157 virtual bool IsCritical() { UNREACHABLE(); return true; }
158
155 int num() { return num_; } 159 int num() { return num_; }
156 void set_num(int n) { num_ = n; } 160 void set_num(int n) { num_ = n; }
157 161
158 private: 162 private:
159 // Support for ast node numbering. 163 // Support for ast node numbering.
160 int num_; 164 int num_;
161 }; 165 };
162 166
163 167
164 class Statement: public AstNode { 168 class Statement: public AstNode {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 }; 208 };
205 209
206 Expression() : bitfields_(0) {} 210 Expression() : bitfields_(0) {}
207 211
208 explicit Expression(Expression* other); 212 explicit Expression(Expression* other);
209 213
210 virtual Expression* AsExpression() { return this; } 214 virtual Expression* AsExpression() { return this; }
211 215
212 virtual bool IsValidLeftHandSide() { return false; } 216 virtual bool IsValidLeftHandSide() { return false; }
213 217
214 virtual Variable* AssignedVar() { return NULL; } 218 virtual Variable* AssignedVariable() { return NULL; }
215 219
216 // Symbols that cannot be parsed as array indices are considered property 220 // Symbols that cannot be parsed as array indices are considered property
217 // names. We do not treat symbols that can be array indexes as property 221 // names. We do not treat symbols that can be array indexes as property
218 // names because [] for string objects is handled only by keyed ICs. 222 // names because [] for string objects is handled only by keyed ICs.
219 virtual bool IsPropertyName() { return false; } 223 virtual bool IsPropertyName() { return false; }
220 224
221 // True if the expression does not have (evaluated) subexpressions. 225 // True if the expression does not have (evaluated) subexpressions.
222 // Function literals are leaves because their subexpressions are not 226 // Function literals are leaves because their subexpressions are not
223 // evaluated. 227 // evaluated.
224 virtual bool IsLeaf() { return false; } 228 virtual bool IsLeaf() { return false; }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 } 280 }
277 281
278 // How many bitwise logical or shift operators are used in this expression? 282 // How many bitwise logical or shift operators are used in this expression?
279 int num_bit_ops() { return NumBitOpsField::decode(bitfields_); } 283 int num_bit_ops() { return NumBitOpsField::decode(bitfields_); }
280 void set_num_bit_ops(int num_bit_ops) { 284 void set_num_bit_ops(int num_bit_ops) {
281 bitfields_ &= ~NumBitOpsField::mask(); 285 bitfields_ &= ~NumBitOpsField::mask();
282 num_bit_ops = Min(num_bit_ops, kMaxNumBitOps); 286 num_bit_ops = Min(num_bit_ops, kMaxNumBitOps);
283 bitfields_ |= NumBitOpsField::encode(num_bit_ops); 287 bitfields_ |= NumBitOpsField::encode(num_bit_ops);
284 } 288 }
285 289
290 // Functions used for dead-code elimination. Predicate is true if the
291 // expression is not dead code.
292 int is_live() const { return LiveField::decode(bitfields_); }
293 void mark_as_live() { bitfields_ |= LiveField::encode(true); }
294
295 // Mark non-live children as live and push them on a stack for further
296 // processing.
297 virtual void ProcessNonLiveChildren(
298 List<AstNode*>* stack,
299 ZoneList<Expression*>* body_definitions,
300 int variable_count) {
301 }
302
286 private: 303 private:
287 static const int kMaxNumBitOps = (1 << 5) - 1; 304 static const int kMaxNumBitOps = (1 << 5) - 1;
288 305
289 uint32_t bitfields_; 306 uint32_t bitfields_;
290 StaticType type_; 307 StaticType type_;
291 308
292 // Using template BitField<type, start, size>. 309 // Using template BitField<type, start, size>.
293 class SideEffectFreeField : public BitField<bool, 0, 1> {}; 310 class SideEffectFreeField : public BitField<bool, 0, 1> {};
294 class NoNegativeZeroField : public BitField<bool, 1, 1> {}; 311 class NoNegativeZeroField : public BitField<bool, 1, 1> {};
295 class ToInt32Field : public BitField<bool, 2, 1> {}; 312 class ToInt32Field : public BitField<bool, 2, 1> {};
296 class NumBitOpsField : public BitField<int, 3, 5> {}; 313 class NumBitOpsField : public BitField<int, 3, 5> {};
297 class LoopConditionField: public BitField<bool, 8, 1> {}; 314 class LoopConditionField: public BitField<bool, 8, 1> {};
315 class LiveField: public BitField<bool, 9, 1> {};
298 }; 316 };
299 317
300 318
301 /** 319 /**
302 * A sentinel used during pre parsing that represents some expression 320 * A sentinel used during pre parsing that represents some expression
303 * that is a valid left hand side without having to actually build 321 * that is a valid left hand side without having to actually build
304 * the expression. 322 * the expression.
305 */ 323 */
306 class ValidLeftHandSideSentinel: public Expression { 324 class ValidLeftHandSideSentinel: public Expression {
307 public: 325 public:
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 if (handle_->IsSymbol()) { 892 if (handle_->IsSymbol()) {
875 uint32_t ignored; 893 uint32_t ignored;
876 return !String::cast(*handle_)->AsArrayIndex(&ignored); 894 return !String::cast(*handle_)->AsArrayIndex(&ignored);
877 } 895 }
878 return false; 896 return false;
879 } 897 }
880 898
881 virtual bool IsLeaf() { return true; } 899 virtual bool IsLeaf() { return true; }
882 virtual bool IsTrivial() { return true; } 900 virtual bool IsTrivial() { return true; }
883 virtual bool IsPrimitive(); 901 virtual bool IsPrimitive();
902 virtual bool IsCritical();
903 virtual void ProcessNonLiveChildren(
904 List<AstNode*>* stack,
905 ZoneList<Expression*>* body_definitions,
906 int variable_count);
884 907
885 // Identity testers. 908 // Identity testers.
886 bool IsNull() const { return handle_.is_identical_to(Factory::null_value()); } 909 bool IsNull() const { return handle_.is_identical_to(Factory::null_value()); }
887 bool IsTrue() const { return handle_.is_identical_to(Factory::true_value()); } 910 bool IsTrue() const { return handle_.is_identical_to(Factory::true_value()); }
888 bool IsFalse() const { 911 bool IsFalse() const {
889 return handle_.is_identical_to(Factory::false_value()); 912 return handle_.is_identical_to(Factory::false_value());
890 } 913 }
891 914
892 Handle<Object> handle() const { return handle_; } 915 Handle<Object> handle() const { return handle_; }
893 916
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 virtual bool IsLeaf() { 1103 virtual bool IsLeaf() {
1081 ASSERT(var_ != NULL); // Variable must be resolved. 1104 ASSERT(var_ != NULL); // Variable must be resolved.
1082 return var()->is_global() || var()->rewrite()->IsLeaf(); 1105 return var()->is_global() || var()->rewrite()->IsLeaf();
1083 } 1106 }
1084 1107
1085 // Reading from a mutable variable is a side effect, but 'this' is 1108 // Reading from a mutable variable is a side effect, but 'this' is
1086 // immutable. 1109 // immutable.
1087 virtual bool IsTrivial() { return is_trivial_; } 1110 virtual bool IsTrivial() { return is_trivial_; }
1088 1111
1089 virtual bool IsPrimitive(); 1112 virtual bool IsPrimitive();
1113 virtual bool IsCritical();
1114 virtual void ProcessNonLiveChildren(
1115 List<AstNode*>* stack,
1116 ZoneList<Expression*>* body_definitions,
1117 int variable_count);
1090 1118
1091 void SetIsPrimitive(bool value) { is_primitive_ = value; } 1119 void SetIsPrimitive(bool value) { is_primitive_ = value; }
1092 1120
1093 bool IsVariable(Handle<String> n) { 1121 bool IsVariable(Handle<String> n) {
1094 return !is_this() && name().is_identical_to(n); 1122 return !is_this() && name().is_identical_to(n);
1095 } 1123 }
1096 1124
1097 bool IsArguments() { 1125 bool IsArguments() {
1098 Variable* variable = AsVariable(); 1126 Variable* variable = AsVariable();
1099 return (variable == NULL) ? false : variable->is_arguments(); 1127 return (variable == NULL) ? false : variable->is_arguments();
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1217 Property(Property* other, Expression* obj, Expression* key); 1245 Property(Property* other, Expression* obj, Expression* key);
1218 1246
1219 virtual void Accept(AstVisitor* v); 1247 virtual void Accept(AstVisitor* v);
1220 1248
1221 // Type testing & conversion 1249 // Type testing & conversion
1222 virtual Property* AsProperty() { return this; } 1250 virtual Property* AsProperty() { return this; }
1223 1251
1224 virtual bool IsValidLeftHandSide() { return true; } 1252 virtual bool IsValidLeftHandSide() { return true; }
1225 1253
1226 virtual bool IsPrimitive(); 1254 virtual bool IsPrimitive();
1255 virtual bool IsCritical();
1256 virtual void ProcessNonLiveChildren(
1257 List<AstNode*>* stack,
1258 ZoneList<Expression*>* body_definitions,
1259 int variable_count);
1227 1260
1228 Expression* obj() const { return obj_; } 1261 Expression* obj() const { return obj_; }
1229 Expression* key() const { return key_; } 1262 Expression* key() const { return key_; }
1230 int position() const { return pos_; } 1263 int position() const { return pos_; }
1231 bool is_synthetic() const { return type_ == SYNTHETIC; } 1264 bool is_synthetic() const { return type_ == SYNTHETIC; }
1232 1265
1233 // Returns a property singleton property access on 'this'. Used 1266 // Returns a property singleton property access on 'this'. Used
1234 // during preparsing. 1267 // during preparsing.
1235 static Property* this_property() { return &this_property_; } 1268 static Property* this_property() { return &this_property_; }
1236 1269
(...skipping 14 matching lines...) Expand all
1251 : expression_(expression), arguments_(arguments), pos_(pos) { } 1284 : expression_(expression), arguments_(arguments), pos_(pos) { }
1252 1285
1253 Call(Call* other, Expression* expression, ZoneList<Expression*>* arguments); 1286 Call(Call* other, Expression* expression, ZoneList<Expression*>* arguments);
1254 1287
1255 virtual void Accept(AstVisitor* v); 1288 virtual void Accept(AstVisitor* v);
1256 1289
1257 // Type testing and conversion. 1290 // Type testing and conversion.
1258 virtual Call* AsCall() { return this; } 1291 virtual Call* AsCall() { return this; }
1259 1292
1260 virtual bool IsPrimitive(); 1293 virtual bool IsPrimitive();
1294 virtual bool IsCritical();
1295 virtual void ProcessNonLiveChildren(
1296 List<AstNode*>* stack,
1297 ZoneList<Expression*>* body_definitions,
1298 int variable_count);
1261 1299
1262 Expression* expression() const { return expression_; } 1300 Expression* expression() const { return expression_; }
1263 ZoneList<Expression*>* arguments() const { return arguments_; } 1301 ZoneList<Expression*>* arguments() const { return arguments_; }
1264 int position() { return pos_; } 1302 int position() { return pos_; }
1265 1303
1266 static Call* sentinel() { return &sentinel_; } 1304 static Call* sentinel() { return &sentinel_; }
1267 1305
1268 private: 1306 private:
1269 Expression* expression_; 1307 Expression* expression_;
1270 ZoneList<Expression*>* arguments_; 1308 ZoneList<Expression*>* arguments_;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 } 1367 }
1330 1368
1331 UnaryOperation(UnaryOperation* other, Expression* expression); 1369 UnaryOperation(UnaryOperation* other, Expression* expression);
1332 1370
1333 virtual void Accept(AstVisitor* v); 1371 virtual void Accept(AstVisitor* v);
1334 1372
1335 // Type testing & conversion 1373 // Type testing & conversion
1336 virtual UnaryOperation* AsUnaryOperation() { return this; } 1374 virtual UnaryOperation* AsUnaryOperation() { return this; }
1337 1375
1338 virtual bool IsPrimitive(); 1376 virtual bool IsPrimitive();
1377 virtual bool IsCritical();
1378 virtual void ProcessNonLiveChildren(
1379 List<AstNode*>* stack,
1380 ZoneList<Expression*>* body_definitions,
1381 int variable_count);
1339 1382
1340 Token::Value op() const { return op_; } 1383 Token::Value op() const { return op_; }
1341 Expression* expression() const { return expression_; } 1384 Expression* expression() const { return expression_; }
1342 1385
1343 private: 1386 private:
1344 Token::Value op_; 1387 Token::Value op_;
1345 Expression* expression_; 1388 Expression* expression_;
1346 }; 1389 };
1347 1390
1348 1391
1349 class BinaryOperation: public Expression { 1392 class BinaryOperation: public Expression {
1350 public: 1393 public:
1351 BinaryOperation(Token::Value op, Expression* left, Expression* right) 1394 BinaryOperation(Token::Value op, Expression* left, Expression* right)
1352 : op_(op), left_(left), right_(right) { 1395 : op_(op), left_(left), right_(right) {
1353 ASSERT(Token::IsBinaryOp(op)); 1396 ASSERT(Token::IsBinaryOp(op));
1354 } 1397 }
1355 1398
1356 BinaryOperation(BinaryOperation* other, Expression* left, Expression* right); 1399 BinaryOperation(BinaryOperation* other, Expression* left, Expression* right);
1357 1400
1358 virtual void Accept(AstVisitor* v); 1401 virtual void Accept(AstVisitor* v);
1359 1402
1360 // Type testing & conversion 1403 // Type testing & conversion
1361 virtual BinaryOperation* AsBinaryOperation() { return this; } 1404 virtual BinaryOperation* AsBinaryOperation() { return this; }
1362 1405
1363 virtual bool IsPrimitive(); 1406 virtual bool IsPrimitive();
1407 virtual bool IsCritical();
1408 virtual void ProcessNonLiveChildren(
1409 List<AstNode*>* stack,
1410 ZoneList<Expression*>* body_definitions,
1411 int variable_count);
1364 1412
1365 // True iff the result can be safely overwritten (to avoid allocation). 1413 // True iff the result can be safely overwritten (to avoid allocation).
1366 // False for operations that can return one of their operands. 1414 // False for operations that can return one of their operands.
1367 bool ResultOverwriteAllowed() { 1415 bool ResultOverwriteAllowed() {
1368 switch (op_) { 1416 switch (op_) {
1369 case Token::COMMA: 1417 case Token::COMMA:
1370 case Token::OR: 1418 case Token::OR:
1371 case Token::AND: 1419 case Token::AND:
1372 return false; 1420 return false;
1373 case Token::BIT_OR: 1421 case Token::BIT_OR:
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1405 : is_prefix_(is_prefix), op_(op), expression_(expression) { 1453 : is_prefix_(is_prefix), op_(op), expression_(expression) {
1406 ASSERT(Token::IsCountOp(op)); 1454 ASSERT(Token::IsCountOp(op));
1407 } 1455 }
1408 1456
1409 CountOperation(CountOperation* other, Expression* expression); 1457 CountOperation(CountOperation* other, Expression* expression);
1410 1458
1411 virtual void Accept(AstVisitor* v); 1459 virtual void Accept(AstVisitor* v);
1412 1460
1413 virtual CountOperation* AsCountOperation() { return this; } 1461 virtual CountOperation* AsCountOperation() { return this; }
1414 1462
1415 virtual Variable* AssignedVar() { 1463 virtual Variable* AssignedVariable() {
1416 return expression()->AsVariableProxy()->AsVariable(); 1464 return expression()->AsVariableProxy()->AsVariable();
1417 } 1465 }
1418 1466
1419 virtual bool IsPrimitive(); 1467 virtual bool IsPrimitive();
1468 virtual bool IsCritical();
1469 virtual void ProcessNonLiveChildren(
1470 List<AstNode*>* stack,
1471 ZoneList<Expression*>* body_definitions,
1472 int variable_count);
1420 1473
1421 bool is_prefix() const { return is_prefix_; } 1474 bool is_prefix() const { return is_prefix_; }
1422 bool is_postfix() const { return !is_prefix_; } 1475 bool is_postfix() const { return !is_prefix_; }
1423 Token::Value op() const { return op_; } 1476 Token::Value op() const { return op_; }
1424 Token::Value binary_op() { 1477 Token::Value binary_op() {
1425 return op_ == Token::INC ? Token::ADD : Token::SUB; 1478 return op_ == Token::INC ? Token::ADD : Token::SUB;
1426 } 1479 }
1427 Expression* expression() const { return expression_; } 1480 Expression* expression() const { return expression_; }
1428 1481
1429 virtual void MarkAsStatement() { is_prefix_ = true; } 1482 virtual void MarkAsStatement() { is_prefix_ = true; }
(...skipping 12 matching lines...) Expand all
1442 ASSERT(Token::IsCompareOp(op)); 1495 ASSERT(Token::IsCompareOp(op));
1443 } 1496 }
1444 1497
1445 CompareOperation(CompareOperation* other, 1498 CompareOperation(CompareOperation* other,
1446 Expression* left, 1499 Expression* left,
1447 Expression* right); 1500 Expression* right);
1448 1501
1449 virtual void Accept(AstVisitor* v); 1502 virtual void Accept(AstVisitor* v);
1450 1503
1451 virtual bool IsPrimitive(); 1504 virtual bool IsPrimitive();
1505 virtual bool IsCritical();
1506 virtual void ProcessNonLiveChildren(
1507 List<AstNode*>* stack,
1508 ZoneList<Expression*>* body_definitions,
1509 int variable_count);
1452 1510
1453 Token::Value op() const { return op_; } 1511 Token::Value op() const { return op_; }
1454 Expression* left() const { return left_; } 1512 Expression* left() const { return left_; }
1455 Expression* right() const { return right_; } 1513 Expression* right() const { return right_; }
1456 1514
1457 // Type testing & conversion 1515 // Type testing & conversion
1458 virtual CompareOperation* AsCompareOperation() { return this; } 1516 virtual CompareOperation* AsCompareOperation() { return this; }
1459 1517
1460 private: 1518 private:
1461 Token::Value op_; 1519 Token::Value op_;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 block_start_(false), block_end_(false) { 1553 block_start_(false), block_end_(false) {
1496 ASSERT(Token::IsAssignmentOp(op)); 1554 ASSERT(Token::IsAssignmentOp(op));
1497 } 1555 }
1498 1556
1499 Assignment(Assignment* other, Expression* target, Expression* value); 1557 Assignment(Assignment* other, Expression* target, Expression* value);
1500 1558
1501 virtual void Accept(AstVisitor* v); 1559 virtual void Accept(AstVisitor* v);
1502 virtual Assignment* AsAssignment() { return this; } 1560 virtual Assignment* AsAssignment() { return this; }
1503 1561
1504 virtual bool IsPrimitive(); 1562 virtual bool IsPrimitive();
1563 virtual bool IsCritical();
1564 virtual void ProcessNonLiveChildren(
1565 List<AstNode*>* stack,
1566 ZoneList<Expression*>* body_definitions,
1567 int variable_count);
1505 1568
1506 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; } 1569 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
1507 1570
1508 virtual Variable* AssignedVar() { 1571 virtual Variable* AssignedVariable() {
1509 return target()->AsVariableProxy()->AsVariable(); 1572 return target()->AsVariableProxy()->AsVariable();
1510 } 1573 }
1511 1574
1512 Token::Value binary_op() const; 1575 Token::Value binary_op() const;
1513 1576
1514 Token::Value op() const { return op_; } 1577 Token::Value op() const { return op_; }
1515 Expression* target() const { return target_; } 1578 Expression* target() const { return target_; }
1516 Expression* value() const { return value_; } 1579 Expression* value() const { return value_; }
1517 int position() { return pos_; } 1580 int position() { return pos_; }
1518 // This check relies on the definition order of token in token.h. 1581 // This check relies on the definition order of token in token.h.
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
2092 2155
2093 // Holds the result of copying an expression. 2156 // Holds the result of copying an expression.
2094 Expression* expr_; 2157 Expression* expr_;
2095 // Holds the result of copying a statement. 2158 // Holds the result of copying a statement.
2096 Statement* stmt_; 2159 Statement* stmt_;
2097 }; 2160 };
2098 2161
2099 } } // namespace v8::internal 2162 } } // namespace v8::internal
2100 2163
2101 #endif // V8_AST_H_ 2164 #endif // V8_AST_H_
OLDNEW
« no previous file with comments | « no previous file | src/ast.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698