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

Side by Side Diff: src/ast.h

Issue 669155: Add an assigned variables analysis.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' 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 | Annotate | Revision Log
« 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 virtual Expression* AsExpression() { return NULL; } 130 virtual Expression* AsExpression() { return NULL; }
131 virtual Literal* AsLiteral() { return NULL; } 131 virtual Literal* AsLiteral() { return NULL; }
132 virtual Slot* AsSlot() { return NULL; } 132 virtual Slot* AsSlot() { return NULL; }
133 virtual VariableProxy* AsVariableProxy() { return NULL; } 133 virtual VariableProxy* AsVariableProxy() { return NULL; }
134 virtual Property* AsProperty() { return NULL; } 134 virtual Property* AsProperty() { return NULL; }
135 virtual Call* AsCall() { return NULL; } 135 virtual Call* AsCall() { return NULL; }
136 virtual TargetCollector* AsTargetCollector() { return NULL; } 136 virtual TargetCollector* AsTargetCollector() { return NULL; }
137 virtual BreakableStatement* AsBreakableStatement() { return NULL; } 137 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
138 virtual IterationStatement* AsIterationStatement() { return NULL; } 138 virtual IterationStatement* AsIterationStatement() { return NULL; }
139 virtual UnaryOperation* AsUnaryOperation() { return NULL; } 139 virtual UnaryOperation* AsUnaryOperation() { return NULL; }
140 virtual CountOperation* AsCountOperation() { return NULL; }
140 virtual BinaryOperation* AsBinaryOperation() { return NULL; } 141 virtual BinaryOperation* AsBinaryOperation() { return NULL; }
141 virtual Assignment* AsAssignment() { return NULL; } 142 virtual Assignment* AsAssignment() { return NULL; }
142 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; } 143 virtual FunctionLiteral* AsFunctionLiteral() { return NULL; }
143 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } 144 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
144 virtual ObjectLiteral* AsObjectLiteral() { return NULL; } 145 virtual ObjectLiteral* AsObjectLiteral() { return NULL; }
145 virtual ArrayLiteral* AsArrayLiteral() { return NULL; } 146 virtual ArrayLiteral* AsArrayLiteral() { return NULL; }
146 virtual CompareOperation* AsCompareOperation() { return NULL; } 147 virtual CompareOperation* AsCompareOperation() { return NULL; }
147 148
148 int num() { return num_; } 149 int num() { return num_; }
149 void set_num(int n) { num_ = n; } 150 void set_num(int n) { num_ = n; }
150 151
151 private: 152 private:
152 // Support for ast node numbering. 153 // Support for ast node numbering.
153 int num_; 154 int num_;
154 }; 155 };
155 156
156 157
157 class Statement: public AstNode { 158 class Statement: public AstNode {
158 public: 159 public:
159 Statement() : statement_pos_(RelocInfo::kNoPosition) {} 160 Statement() : statement_pos_(RelocInfo::kNoPosition) {}
160 161
161 virtual Statement* AsStatement() { return this; } 162 virtual Statement* AsStatement() { return this; }
162 virtual ReturnStatement* AsReturnStatement() { return NULL; } 163 virtual ReturnStatement* AsReturnStatement() { return NULL; }
163 164
165 virtual Assignment* StatementAsSimpleAssignment() { return NULL; }
166 virtual CountOperation* StatementAsCountOperation() { return NULL; }
167
164 bool IsEmpty() { return AsEmptyStatement() != NULL; } 168 bool IsEmpty() { return AsEmptyStatement() != NULL; }
165 169
166 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } 170 void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; }
167 int statement_pos() const { return statement_pos_; } 171 int statement_pos() const { return statement_pos_; }
168 172
169 private: 173 private:
170 int statement_pos_; 174 int statement_pos_;
171 }; 175 };
172 176
173 177
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 318
315 class Block: public BreakableStatement { 319 class Block: public BreakableStatement {
316 public: 320 public:
317 Block(ZoneStringList* labels, int capacity, bool is_initializer_block) 321 Block(ZoneStringList* labels, int capacity, bool is_initializer_block)
318 : BreakableStatement(labels, TARGET_FOR_NAMED_ONLY), 322 : BreakableStatement(labels, TARGET_FOR_NAMED_ONLY),
319 statements_(capacity), 323 statements_(capacity),
320 is_initializer_block_(is_initializer_block) { } 324 is_initializer_block_(is_initializer_block) { }
321 325
322 virtual void Accept(AstVisitor* v); 326 virtual void Accept(AstVisitor* v);
323 327
328 virtual Assignment* StatementAsSimpleAssignment() {
329 if (statements_.length() != 1) return NULL;
330 return statements_[0]->StatementAsSimpleAssignment();
331 }
332
333 virtual CountOperation* StatementAsCountOperation() {
334 if (statements_.length() != 1) return NULL;
335 return statements_[0]->StatementAsCountOperation();
336 }
337
324 void AddStatement(Statement* statement) { statements_.Add(statement); } 338 void AddStatement(Statement* statement) { statements_.Add(statement); }
325 339
326 ZoneList<Statement*>* statements() { return &statements_; } 340 ZoneList<Statement*>* statements() { return &statements_; }
327 bool is_initializer_block() const { return is_initializer_block_; } 341 bool is_initializer_block() const { return is_initializer_block_; }
328 342
329 private: 343 private:
330 ZoneList<Statement*> statements_; 344 ZoneList<Statement*> statements_;
331 bool is_initializer_block_; 345 bool is_initializer_block_;
332 }; 346 };
333 347
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 }; 449 };
436 450
437 451
438 class ForStatement: public IterationStatement { 452 class ForStatement: public IterationStatement {
439 public: 453 public:
440 explicit ForStatement(ZoneStringList* labels) 454 explicit ForStatement(ZoneStringList* labels)
441 : IterationStatement(labels), 455 : IterationStatement(labels),
442 init_(NULL), 456 init_(NULL),
443 cond_(NULL), 457 cond_(NULL),
444 next_(NULL), 458 next_(NULL),
445 may_have_function_literal_(true) { 459 may_have_function_literal_(true),
446 } 460 loop_variable_(NULL) {}
447 461
448 void Initialize(Statement* init, 462 void Initialize(Statement* init,
449 Expression* cond, 463 Expression* cond,
450 Statement* next, 464 Statement* next,
451 Statement* body) { 465 Statement* body) {
452 IterationStatement::Initialize(body); 466 IterationStatement::Initialize(body);
453 init_ = init; 467 init_ = init;
454 cond_ = cond; 468 cond_ = cond;
455 next_ = next; 469 next_ = next;
456 } 470 }
457 471
458 virtual void Accept(AstVisitor* v); 472 virtual void Accept(AstVisitor* v);
459 473
460 Statement* init() const { return init_; } 474 Statement* init() const { return init_; }
461 Expression* cond() const { return cond_; } 475 Expression* cond() const { return cond_; }
462 Statement* next() const { return next_; } 476 Statement* next() const { return next_; }
463 bool may_have_function_literal() const { 477 bool may_have_function_literal() const {
464 return may_have_function_literal_; 478 return may_have_function_literal_;
465 } 479 }
466 480
481 bool is_fast_smi_loop() { return loop_variable_ != NULL; }
482 Variable* loop_variable() { return loop_variable_; }
483 void set_loop_variable(Variable* var) { loop_variable_ = var; }
484
467 private: 485 private:
468 Statement* init_; 486 Statement* init_;
469 Expression* cond_; 487 Expression* cond_;
470 Statement* next_; 488 Statement* next_;
471 // True if there is a function literal subexpression in the condition. 489 // True if there is a function literal subexpression in the condition.
472 bool may_have_function_literal_; 490 bool may_have_function_literal_;
491 Variable* loop_variable_;
473 492
474 friend class AstOptimizer; 493 friend class AstOptimizer;
475 }; 494 };
476 495
477 496
478 class ForInStatement: public IterationStatement { 497 class ForInStatement: public IterationStatement {
479 public: 498 public:
480 explicit ForInStatement(ZoneStringList* labels) 499 explicit ForInStatement(ZoneStringList* labels)
481 : IterationStatement(labels), each_(NULL), enumerable_(NULL) { } 500 : IterationStatement(labels), each_(NULL), enumerable_(NULL) { }
482 501
(...skipping 17 matching lines...) Expand all
500 class ExpressionStatement: public Statement { 519 class ExpressionStatement: public Statement {
501 public: 520 public:
502 explicit ExpressionStatement(Expression* expression) 521 explicit ExpressionStatement(Expression* expression)
503 : expression_(expression) { } 522 : expression_(expression) { }
504 523
505 virtual void Accept(AstVisitor* v); 524 virtual void Accept(AstVisitor* v);
506 525
507 // Type testing & conversion. 526 // Type testing & conversion.
508 virtual ExpressionStatement* AsExpressionStatement() { return this; } 527 virtual ExpressionStatement* AsExpressionStatement() { return this; }
509 528
529 virtual Assignment* StatementAsSimpleAssignment();
530 virtual CountOperation* StatementAsCountOperation();
531
510 void set_expression(Expression* e) { expression_ = e; } 532 void set_expression(Expression* e) { expression_ = e; }
511 Expression* expression() { return expression_; } 533 Expression* expression() { return expression_; }
512 534
513 private: 535 private:
514 Expression* expression_; 536 Expression* expression_;
515 }; 537 };
516 538
517 539
518 class ContinueStatement: public Statement { 540 class ContinueStatement: public Statement {
519 public: 541 public:
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 return var_ == NULL ? true : var_->IsValidLeftHandSide(); 979 return var_ == NULL ? true : var_->IsValidLeftHandSide();
958 } 980 }
959 981
960 virtual bool IsLeaf() { 982 virtual bool IsLeaf() {
961 ASSERT(var_ != NULL); // Variable must be resolved. 983 ASSERT(var_ != NULL); // Variable must be resolved.
962 return var()->is_global() || var()->rewrite()->IsLeaf(); 984 return var()->is_global() || var()->rewrite()->IsLeaf();
963 } 985 }
964 986
965 // Reading from a mutable variable is a side effect, but 'this' is 987 // Reading from a mutable variable is a side effect, but 'this' is
966 // immutable. 988 // immutable.
967 virtual bool IsTrivial() { return is_this(); } 989 virtual bool IsTrivial() { return is_trivial_; }
968 990
969 bool IsVariable(Handle<String> n) { 991 bool IsVariable(Handle<String> n) {
970 return !is_this() && name().is_identical_to(n); 992 return !is_this() && name().is_identical_to(n);
971 } 993 }
972 994
973 bool IsArguments() { 995 bool IsArguments() {
974 Variable* variable = AsVariable(); 996 Variable* variable = AsVariable();
975 return (variable == NULL) ? false : variable->is_arguments(); 997 return (variable == NULL) ? false : variable->is_arguments();
976 } 998 }
977 999
978 Handle<String> name() const { return name_; } 1000 Handle<String> name() const { return name_; }
979 Variable* var() const { return var_; } 1001 Variable* var() const { return var_; }
980 bool is_this() const { return is_this_; } 1002 bool is_this() const { return is_this_; }
981 bool inside_with() const { return inside_with_; } 1003 bool inside_with() const { return inside_with_; }
1004 bool is_trivial() { return is_trivial_; }
1005 void set_is_trivial(bool b) { is_trivial_ = b; }
982 1006
983 // Bind this proxy to the variable var. 1007 // Bind this proxy to the variable var.
984 void BindTo(Variable* var); 1008 void BindTo(Variable* var);
985 1009
986 protected: 1010 protected:
987 Handle<String> name_; 1011 Handle<String> name_;
988 Variable* var_; // resolved variable, or NULL 1012 Variable* var_; // resolved variable, or NULL
989 bool is_this_; 1013 bool is_this_;
990 bool inside_with_; 1014 bool inside_with_;
1015 bool is_trivial_;
991 1016
992 VariableProxy(Handle<String> name, bool is_this, bool inside_with); 1017 VariableProxy(Handle<String> name, bool is_this, bool inside_with);
993 explicit VariableProxy(bool is_this); 1018 explicit VariableProxy(bool is_this);
994 1019
995 friend class Scope; 1020 friend class Scope;
996 }; 1021 };
997 1022
998 1023
999 class VariableProxySentinel: public VariableProxy { 1024 class VariableProxySentinel: public VariableProxy {
1000 public: 1025 public:
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
1239 1264
1240 class CountOperation: public Expression { 1265 class CountOperation: public Expression {
1241 public: 1266 public:
1242 CountOperation(bool is_prefix, Token::Value op, Expression* expression) 1267 CountOperation(bool is_prefix, Token::Value op, Expression* expression)
1243 : is_prefix_(is_prefix), op_(op), expression_(expression) { 1268 : is_prefix_(is_prefix), op_(op), expression_(expression) {
1244 ASSERT(Token::IsCountOp(op)); 1269 ASSERT(Token::IsCountOp(op));
1245 } 1270 }
1246 1271
1247 virtual void Accept(AstVisitor* v); 1272 virtual void Accept(AstVisitor* v);
1248 1273
1274 virtual CountOperation* AsCountOperation() { return this; }
1275
1249 bool is_prefix() const { return is_prefix_; } 1276 bool is_prefix() const { return is_prefix_; }
1250 bool is_postfix() const { return !is_prefix_; } 1277 bool is_postfix() const { return !is_prefix_; }
1251 Token::Value op() const { return op_; } 1278 Token::Value op() const { return op_; }
1252 Token::Value binary_op() { 1279 Token::Value binary_op() {
1253 return op_ == Token::INC ? Token::ADD : Token::SUB; 1280 return op_ == Token::INC ? Token::ADD : Token::SUB;
1254 } 1281 }
1255 Expression* expression() const { return expression_; } 1282 Expression* expression() const { return expression_; }
1256 1283
1257 virtual void MarkAsStatement() { is_prefix_ = true; } 1284 virtual void MarkAsStatement() { is_prefix_ = true; }
1258 1285
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 public: 1344 public:
1318 Assignment(Token::Value op, Expression* target, Expression* value, int pos) 1345 Assignment(Token::Value op, Expression* target, Expression* value, int pos)
1319 : op_(op), target_(target), value_(value), pos_(pos), 1346 : op_(op), target_(target), value_(value), pos_(pos),
1320 block_start_(false), block_end_(false) { 1347 block_start_(false), block_end_(false) {
1321 ASSERT(Token::IsAssignmentOp(op)); 1348 ASSERT(Token::IsAssignmentOp(op));
1322 } 1349 }
1323 1350
1324 virtual void Accept(AstVisitor* v); 1351 virtual void Accept(AstVisitor* v);
1325 virtual Assignment* AsAssignment() { return this; } 1352 virtual Assignment* AsAssignment() { return this; }
1326 1353
1354 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
1355
1327 Token::Value binary_op() const; 1356 Token::Value binary_op() const;
1328 1357
1329 Token::Value op() const { return op_; } 1358 Token::Value op() const { return op_; }
1330 Expression* target() const { return target_; } 1359 Expression* target() const { return target_; }
1331 Expression* value() const { return value_; } 1360 Expression* value() const { return value_; }
1332 int position() { return pos_; } 1361 int position() { return pos_; }
1333 // This check relies on the definition order of token in token.h. 1362 // This check relies on the definition order of token in token.h.
1334 bool is_compound() const { return op() > Token::ASSIGN; } 1363 bool is_compound() const { return op() > Token::ASSIGN; }
1335 1364
1336 // An initialization block is a series of statments of the form 1365 // An initialization block is a series of statments of the form
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
1876 #undef DEF_VISIT 1905 #undef DEF_VISIT
1877 1906
1878 private: 1907 private:
1879 bool stack_overflow_; 1908 bool stack_overflow_;
1880 }; 1909 };
1881 1910
1882 1911
1883 } } // namespace v8::internal 1912 } } // namespace v8::internal
1884 1913
1885 #endif // V8_AST_H_ 1914 #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