sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 "v8.h" |
| 29 | |
| 30 | #include "data-flow.h" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 31 | #include "scopes.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 37 | #ifdef DEBUG |
| 38 | void BitVector::Print() { |
| 39 | bool first = true; |
| 40 | PrintF("{"); |
| 41 | for (int i = 0; i < length(); i++) { |
| 42 | if (Contains(i)) { |
| 43 | if (!first) PrintF(","); |
| 44 | first = false; |
| 45 | PrintF("%d"); |
| 46 | } |
| 47 | } |
| 48 | PrintF("}"); |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 53 | void AstLabeler::Label(CompilationInfo* info) { |
| 54 | info_ = info; |
| 55 | VisitStatements(info_->function()->body()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | void AstLabeler::VisitStatements(ZoneList<Statement*>* stmts) { |
| 60 | for (int i = 0, len = stmts->length(); i < len; i++) { |
| 61 | Visit(stmts->at(i)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void AstLabeler::VisitDeclarations(ZoneList<Declaration*>* decls) { |
| 67 | UNREACHABLE(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void AstLabeler::VisitBlock(Block* stmt) { |
| 72 | VisitStatements(stmt->statements()); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void AstLabeler::VisitExpressionStatement( |
| 77 | ExpressionStatement* stmt) { |
| 78 | Visit(stmt->expression()); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void AstLabeler::VisitEmptyStatement(EmptyStatement* stmt) { |
| 83 | // Do nothing. |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void AstLabeler::VisitIfStatement(IfStatement* stmt) { |
| 88 | UNREACHABLE(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void AstLabeler::VisitContinueStatement(ContinueStatement* stmt) { |
| 93 | UNREACHABLE(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void AstLabeler::VisitBreakStatement(BreakStatement* stmt) { |
| 98 | UNREACHABLE(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void AstLabeler::VisitReturnStatement(ReturnStatement* stmt) { |
| 103 | UNREACHABLE(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void AstLabeler::VisitWithEnterStatement( |
| 108 | WithEnterStatement* stmt) { |
| 109 | UNREACHABLE(); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void AstLabeler::VisitWithExitStatement(WithExitStatement* stmt) { |
| 114 | UNREACHABLE(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void AstLabeler::VisitSwitchStatement(SwitchStatement* stmt) { |
| 119 | UNREACHABLE(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void AstLabeler::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 124 | UNREACHABLE(); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void AstLabeler::VisitWhileStatement(WhileStatement* stmt) { |
| 129 | UNREACHABLE(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void AstLabeler::VisitForStatement(ForStatement* stmt) { |
| 134 | UNREACHABLE(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void AstLabeler::VisitForInStatement(ForInStatement* stmt) { |
| 139 | UNREACHABLE(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void AstLabeler::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 144 | UNREACHABLE(); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | void AstLabeler::VisitTryFinallyStatement( |
| 149 | TryFinallyStatement* stmt) { |
| 150 | UNREACHABLE(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void AstLabeler::VisitDebuggerStatement( |
| 155 | DebuggerStatement* stmt) { |
| 156 | UNREACHABLE(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void AstLabeler::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 161 | UNREACHABLE(); |
| 162 | } |
| 163 | |
| 164 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 165 | void AstLabeler::VisitSharedFunctionInfoLiteral( |
| 166 | SharedFunctionInfoLiteral* expr) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 167 | UNREACHABLE(); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | void AstLabeler::VisitConditional(Conditional* expr) { |
| 172 | UNREACHABLE(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void AstLabeler::VisitSlot(Slot* expr) { |
| 177 | UNREACHABLE(); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void AstLabeler::VisitVariableProxy(VariableProxy* expr) { |
| 182 | expr->set_num(next_number_++); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 183 | Variable* var = expr->var(); |
| 184 | if (var->is_global() && !var->is_this()) { |
| 185 | info_->set_has_globals(true); |
| 186 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | void AstLabeler::VisitLiteral(Literal* expr) { |
| 191 | UNREACHABLE(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void AstLabeler::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 196 | UNREACHABLE(); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void AstLabeler::VisitObjectLiteral(ObjectLiteral* expr) { |
| 201 | UNREACHABLE(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | void AstLabeler::VisitArrayLiteral(ArrayLiteral* expr) { |
| 206 | UNREACHABLE(); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void AstLabeler::VisitCatchExtensionObject( |
| 211 | CatchExtensionObject* expr) { |
| 212 | UNREACHABLE(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void AstLabeler::VisitAssignment(Assignment* expr) { |
| 217 | Property* prop = expr->target()->AsProperty(); |
| 218 | ASSERT(prop != NULL); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 219 | ASSERT(prop->key()->IsPropertyName()); |
| 220 | VariableProxy* proxy = prop->obj()->AsVariableProxy(); |
| 221 | USE(proxy); |
| 222 | ASSERT(proxy != NULL && proxy->var()->is_this()); |
| 223 | info()->set_has_this_properties(true); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 224 | |
| 225 | prop->obj()->set_num(AstNode::kNoNumber); |
| 226 | prop->key()->set_num(AstNode::kNoNumber); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 227 | Visit(expr->value()); |
| 228 | expr->set_num(next_number_++); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void AstLabeler::VisitThrow(Throw* expr) { |
| 233 | UNREACHABLE(); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void AstLabeler::VisitProperty(Property* expr) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 238 | ASSERT(expr->key()->IsPropertyName()); |
| 239 | VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 240 | USE(proxy); |
| 241 | ASSERT(proxy != NULL && proxy->var()->is_this()); |
| 242 | info()->set_has_this_properties(true); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 243 | |
| 244 | expr->obj()->set_num(AstNode::kNoNumber); |
| 245 | expr->key()->set_num(AstNode::kNoNumber); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 246 | expr->set_num(next_number_++); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | void AstLabeler::VisitCall(Call* expr) { |
| 251 | UNREACHABLE(); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void AstLabeler::VisitCallNew(CallNew* expr) { |
| 256 | UNREACHABLE(); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | void AstLabeler::VisitCallRuntime(CallRuntime* expr) { |
| 261 | UNREACHABLE(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | void AstLabeler::VisitUnaryOperation(UnaryOperation* expr) { |
| 266 | UNREACHABLE(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | void AstLabeler::VisitCountOperation(CountOperation* expr) { |
| 271 | UNREACHABLE(); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | void AstLabeler::VisitBinaryOperation(BinaryOperation* expr) { |
| 276 | Visit(expr->left()); |
| 277 | Visit(expr->right()); |
| 278 | expr->set_num(next_number_++); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void AstLabeler::VisitCompareOperation(CompareOperation* expr) { |
| 283 | UNREACHABLE(); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void AstLabeler::VisitThisFunction(ThisFunction* expr) { |
| 288 | UNREACHABLE(); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void AstLabeler::VisitDeclaration(Declaration* decl) { |
| 293 | UNREACHABLE(); |
| 294 | } |
| 295 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 296 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 297 | AssignedVariablesAnalyzer::AssignedVariablesAnalyzer(FunctionLiteral* fun) |
| 298 | : fun_(fun), |
| 299 | av_(fun->scope()->num_parameters() + fun->scope()->num_stack_slots()) {} |
| 300 | |
| 301 | |
| 302 | void AssignedVariablesAnalyzer::Analyze() { |
| 303 | ASSERT(av_.length() > 0); |
| 304 | VisitStatements(fun_->body()); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | Variable* AssignedVariablesAnalyzer::FindSmiLoopVariable(ForStatement* stmt) { |
| 309 | // The loop must have all necessary parts. |
| 310 | if (stmt->init() == NULL || stmt->cond() == NULL || stmt->next() == NULL) { |
| 311 | return NULL; |
| 312 | } |
| 313 | // The initialization statement has to be a simple assignment. |
| 314 | Assignment* init = stmt->init()->StatementAsSimpleAssignment(); |
| 315 | if (init == NULL) return NULL; |
| 316 | |
| 317 | // We only deal with local variables. |
| 318 | Variable* loop_var = init->target()->AsVariableProxy()->AsVariable(); |
| 319 | if (loop_var == NULL || !loop_var->IsStackAllocated()) return NULL; |
| 320 | |
| 321 | // The initial value has to be a smi. |
| 322 | Literal* init_lit = init->value()->AsLiteral(); |
| 323 | if (init_lit == NULL || !init_lit->handle()->IsSmi()) return NULL; |
| 324 | int init_value = Smi::cast(*init_lit->handle())->value(); |
| 325 | |
| 326 | // The condition must be a compare of variable with <, <=, >, or >=. |
| 327 | CompareOperation* cond = stmt->cond()->AsCompareOperation(); |
| 328 | if (cond == NULL) return NULL; |
| 329 | if (cond->op() != Token::LT |
| 330 | && cond->op() != Token::LTE |
| 331 | && cond->op() != Token::GT |
| 332 | && cond->op() != Token::GTE) return NULL; |
| 333 | |
| 334 | // The lhs must be the same variable as in the init expression. |
| 335 | if (cond->left()->AsVariableProxy()->AsVariable() != loop_var) return NULL; |
| 336 | |
| 337 | // The rhs must be a smi. |
| 338 | Literal* term_lit = cond->right()->AsLiteral(); |
| 339 | if (term_lit == NULL || !term_lit->handle()->IsSmi()) return NULL; |
| 340 | int term_value = Smi::cast(*term_lit->handle())->value(); |
| 341 | |
| 342 | // The count operation updates the same variable as in the init expression. |
| 343 | CountOperation* update = stmt->next()->StatementAsCountOperation(); |
| 344 | if (update == NULL) return NULL; |
| 345 | if (update->expression()->AsVariableProxy()->AsVariable() != loop_var) { |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | // The direction of the count operation must agree with the start and the end |
| 350 | // value. We currently do not allow the initial value to be the same as the |
| 351 | // terminal value. This _would_ be ok as long as the loop body never executes |
| 352 | // or executes exactly one time. |
| 353 | if (init_value == term_value) return NULL; |
| 354 | if (init_value < term_value && update->op() != Token::INC) return NULL; |
| 355 | if (init_value > term_value && update->op() != Token::DEC) return NULL; |
| 356 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 357 | // Check that the update operation cannot overflow the smi range. This can |
| 358 | // occur in the two cases where the loop bound is equal to the largest or |
| 359 | // smallest smi. |
| 360 | if (update->op() == Token::INC && term_value == Smi::kMaxValue) return NULL; |
| 361 | if (update->op() == Token::DEC && term_value == Smi::kMinValue) return NULL; |
| 362 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 363 | // Found a smi loop variable. |
| 364 | return loop_var; |
| 365 | } |
| 366 | |
| 367 | int AssignedVariablesAnalyzer::BitIndex(Variable* var) { |
| 368 | ASSERT(var != NULL); |
| 369 | ASSERT(var->IsStackAllocated()); |
| 370 | Slot* slot = var->slot(); |
| 371 | if (slot->type() == Slot::PARAMETER) { |
| 372 | return slot->index(); |
| 373 | } else { |
| 374 | return fun_->scope()->num_parameters() + slot->index(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void AssignedVariablesAnalyzer::RecordAssignedVar(Variable* var) { |
| 380 | ASSERT(var != NULL); |
| 381 | if (var->IsStackAllocated()) { |
| 382 | av_.Add(BitIndex(var)); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void AssignedVariablesAnalyzer::MarkIfTrivial(Expression* expr) { |
| 388 | Variable* var = expr->AsVariableProxy()->AsVariable(); |
| 389 | if (var != NULL && |
| 390 | var->IsStackAllocated() && |
| 391 | !var->is_arguments() && |
| 392 | var->mode() != Variable::CONST && |
| 393 | (var->is_this() || !av_.Contains(BitIndex(var)))) { |
| 394 | expr->AsVariableProxy()->set_is_trivial(true); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | void AssignedVariablesAnalyzer::ProcessExpression(Expression* expr) { |
| 400 | BitVector saved_av(av_); |
| 401 | av_.Clear(); |
| 402 | Visit(expr); |
| 403 | av_.Union(saved_av); |
| 404 | } |
| 405 | |
| 406 | void AssignedVariablesAnalyzer::VisitBlock(Block* stmt) { |
| 407 | VisitStatements(stmt->statements()); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | void AssignedVariablesAnalyzer::VisitExpressionStatement( |
| 412 | ExpressionStatement* stmt) { |
| 413 | ProcessExpression(stmt->expression()); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | void AssignedVariablesAnalyzer::VisitEmptyStatement(EmptyStatement* stmt) { |
| 418 | // Do nothing. |
| 419 | } |
| 420 | |
| 421 | |
| 422 | void AssignedVariablesAnalyzer::VisitIfStatement(IfStatement* stmt) { |
| 423 | ProcessExpression(stmt->condition()); |
| 424 | Visit(stmt->then_statement()); |
| 425 | Visit(stmt->else_statement()); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | void AssignedVariablesAnalyzer::VisitContinueStatement( |
| 430 | ContinueStatement* stmt) { |
| 431 | // Nothing to do. |
| 432 | } |
| 433 | |
| 434 | |
| 435 | void AssignedVariablesAnalyzer::VisitBreakStatement(BreakStatement* stmt) { |
| 436 | // Nothing to do. |
| 437 | } |
| 438 | |
| 439 | |
| 440 | void AssignedVariablesAnalyzer::VisitReturnStatement(ReturnStatement* stmt) { |
| 441 | ProcessExpression(stmt->expression()); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | void AssignedVariablesAnalyzer::VisitWithEnterStatement( |
| 446 | WithEnterStatement* stmt) { |
| 447 | ProcessExpression(stmt->expression()); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void AssignedVariablesAnalyzer::VisitWithExitStatement( |
| 452 | WithExitStatement* stmt) { |
| 453 | // Nothing to do. |
| 454 | } |
| 455 | |
| 456 | |
| 457 | void AssignedVariablesAnalyzer::VisitSwitchStatement(SwitchStatement* stmt) { |
| 458 | BitVector result(av_); |
| 459 | av_.Clear(); |
| 460 | Visit(stmt->tag()); |
| 461 | result.Union(av_); |
| 462 | for (int i = 0; i < stmt->cases()->length(); i++) { |
| 463 | CaseClause* clause = stmt->cases()->at(i); |
| 464 | if (!clause->is_default()) { |
| 465 | av_.Clear(); |
| 466 | Visit(clause->label()); |
| 467 | result.Union(av_); |
| 468 | } |
| 469 | VisitStatements(clause->statements()); |
| 470 | } |
| 471 | av_.Union(result); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void AssignedVariablesAnalyzer::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 476 | ProcessExpression(stmt->cond()); |
| 477 | Visit(stmt->body()); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void AssignedVariablesAnalyzer::VisitWhileStatement(WhileStatement* stmt) { |
| 482 | ProcessExpression(stmt->cond()); |
| 483 | Visit(stmt->body()); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void AssignedVariablesAnalyzer::VisitForStatement(ForStatement* stmt) { |
| 488 | if (stmt->init() != NULL) Visit(stmt->init()); |
| 489 | |
| 490 | if (stmt->cond() != NULL) ProcessExpression(stmt->cond()); |
| 491 | |
| 492 | if (stmt->next() != NULL) Visit(stmt->next()); |
| 493 | |
| 494 | // Process loop body. After visiting the loop body av_ contains |
| 495 | // the assigned variables of the loop body. |
| 496 | BitVector saved_av(av_); |
| 497 | av_.Clear(); |
| 498 | Visit(stmt->body()); |
| 499 | |
| 500 | Variable* var = FindSmiLoopVariable(stmt); |
| 501 | if (var != NULL && !av_.Contains(BitIndex(var))) { |
| 502 | stmt->set_loop_variable(var); |
| 503 | } |
| 504 | |
| 505 | av_.Union(saved_av); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | void AssignedVariablesAnalyzer::VisitForInStatement(ForInStatement* stmt) { |
| 510 | ProcessExpression(stmt->each()); |
| 511 | ProcessExpression(stmt->enumerable()); |
| 512 | Visit(stmt->body()); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | void AssignedVariablesAnalyzer::VisitTryCatchStatement( |
| 517 | TryCatchStatement* stmt) { |
| 518 | Visit(stmt->try_block()); |
| 519 | Visit(stmt->catch_block()); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | void AssignedVariablesAnalyzer::VisitTryFinallyStatement( |
| 524 | TryFinallyStatement* stmt) { |
| 525 | Visit(stmt->try_block()); |
| 526 | Visit(stmt->finally_block()); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | void AssignedVariablesAnalyzer::VisitDebuggerStatement( |
| 531 | DebuggerStatement* stmt) { |
| 532 | // Nothing to do. |
| 533 | } |
| 534 | |
| 535 | |
| 536 | void AssignedVariablesAnalyzer::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 537 | // Nothing to do. |
| 538 | ASSERT(av_.IsEmpty()); |
| 539 | } |
| 540 | |
| 541 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 542 | void AssignedVariablesAnalyzer::VisitSharedFunctionInfoLiteral( |
| 543 | SharedFunctionInfoLiteral* expr) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 544 | // Nothing to do. |
| 545 | ASSERT(av_.IsEmpty()); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void AssignedVariablesAnalyzer::VisitConditional(Conditional* expr) { |
| 550 | ASSERT(av_.IsEmpty()); |
| 551 | |
| 552 | Visit(expr->condition()); |
| 553 | |
| 554 | BitVector result(av_); |
| 555 | av_.Clear(); |
| 556 | Visit(expr->then_expression()); |
| 557 | result.Union(av_); |
| 558 | |
| 559 | av_.Clear(); |
| 560 | Visit(expr->else_expression()); |
| 561 | av_.Union(result); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | void AssignedVariablesAnalyzer::VisitSlot(Slot* expr) { |
| 566 | UNREACHABLE(); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | void AssignedVariablesAnalyzer::VisitVariableProxy(VariableProxy* expr) { |
| 571 | // Nothing to do. |
| 572 | ASSERT(av_.IsEmpty()); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | void AssignedVariablesAnalyzer::VisitLiteral(Literal* expr) { |
| 577 | // Nothing to do. |
| 578 | ASSERT(av_.IsEmpty()); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | void AssignedVariablesAnalyzer::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 583 | // Nothing to do. |
| 584 | ASSERT(av_.IsEmpty()); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | void AssignedVariablesAnalyzer::VisitObjectLiteral(ObjectLiteral* expr) { |
| 589 | ASSERT(av_.IsEmpty()); |
| 590 | BitVector result(av_.length()); |
| 591 | for (int i = 0; i < expr->properties()->length(); i++) { |
| 592 | Visit(expr->properties()->at(i)->value()); |
| 593 | result.Union(av_); |
| 594 | av_.Clear(); |
| 595 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 596 | av_ = result; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
| 600 | void AssignedVariablesAnalyzer::VisitArrayLiteral(ArrayLiteral* expr) { |
| 601 | ASSERT(av_.IsEmpty()); |
| 602 | BitVector result(av_.length()); |
| 603 | for (int i = 0; i < expr->values()->length(); i++) { |
| 604 | Visit(expr->values()->at(i)); |
| 605 | result.Union(av_); |
| 606 | av_.Clear(); |
| 607 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 608 | av_ = result; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
| 612 | void AssignedVariablesAnalyzer::VisitCatchExtensionObject( |
| 613 | CatchExtensionObject* expr) { |
| 614 | ASSERT(av_.IsEmpty()); |
| 615 | Visit(expr->key()); |
| 616 | ProcessExpression(expr->value()); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | void AssignedVariablesAnalyzer::VisitAssignment(Assignment* expr) { |
| 621 | ASSERT(av_.IsEmpty()); |
| 622 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 623 | // There are three kinds of assignments: variable assignments, property |
| 624 | // assignments, and reference errors (invalid left-hand sides). |
| 625 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 626 | Property* prop = expr->target()->AsProperty(); |
| 627 | ASSERT(var == NULL || prop == NULL); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 628 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 629 | if (var != NULL) { |
| 630 | MarkIfTrivial(expr->value()); |
| 631 | Visit(expr->value()); |
| 632 | if (expr->is_compound()) { |
| 633 | // Left-hand side occurs also as an rvalue. |
| 634 | MarkIfTrivial(expr->target()); |
| 635 | ProcessExpression(expr->target()); |
| 636 | } |
| 637 | RecordAssignedVar(var); |
| 638 | |
| 639 | } else if (prop != NULL) { |
| 640 | MarkIfTrivial(expr->value()); |
| 641 | Visit(expr->value()); |
| 642 | if (!prop->key()->IsPropertyName()) { |
| 643 | MarkIfTrivial(prop->key()); |
| 644 | ProcessExpression(prop->key()); |
| 645 | } |
| 646 | MarkIfTrivial(prop->obj()); |
| 647 | ProcessExpression(prop->obj()); |
| 648 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 649 | } else { |
| 650 | Visit(expr->target()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | void AssignedVariablesAnalyzer::VisitThrow(Throw* expr) { |
| 656 | ASSERT(av_.IsEmpty()); |
| 657 | Visit(expr->exception()); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | void AssignedVariablesAnalyzer::VisitProperty(Property* expr) { |
| 662 | ASSERT(av_.IsEmpty()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 663 | if (!expr->key()->IsPropertyName()) { |
| 664 | MarkIfTrivial(expr->key()); |
| 665 | Visit(expr->key()); |
| 666 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 667 | MarkIfTrivial(expr->obj()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 668 | ProcessExpression(expr->obj()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
| 672 | void AssignedVariablesAnalyzer::VisitCall(Call* expr) { |
| 673 | ASSERT(av_.IsEmpty()); |
| 674 | Visit(expr->expression()); |
| 675 | BitVector result(av_); |
| 676 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 677 | av_.Clear(); |
| 678 | Visit(expr->arguments()->at(i)); |
| 679 | result.Union(av_); |
| 680 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 681 | av_ = result; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | |
| 685 | void AssignedVariablesAnalyzer::VisitCallNew(CallNew* expr) { |
| 686 | ASSERT(av_.IsEmpty()); |
| 687 | Visit(expr->expression()); |
| 688 | BitVector result(av_); |
| 689 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 690 | av_.Clear(); |
| 691 | Visit(expr->arguments()->at(i)); |
| 692 | result.Union(av_); |
| 693 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 694 | av_ = result; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
| 698 | void AssignedVariablesAnalyzer::VisitCallRuntime(CallRuntime* expr) { |
| 699 | ASSERT(av_.IsEmpty()); |
| 700 | BitVector result(av_); |
| 701 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 702 | av_.Clear(); |
| 703 | Visit(expr->arguments()->at(i)); |
| 704 | result.Union(av_); |
| 705 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 706 | av_ = result; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
| 710 | void AssignedVariablesAnalyzer::VisitUnaryOperation(UnaryOperation* expr) { |
| 711 | ASSERT(av_.IsEmpty()); |
| 712 | Visit(expr->expression()); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | void AssignedVariablesAnalyzer::VisitCountOperation(CountOperation* expr) { |
| 717 | ASSERT(av_.IsEmpty()); |
| 718 | |
| 719 | Visit(expr->expression()); |
| 720 | |
| 721 | Variable* var = expr->expression()->AsVariableProxy()->AsVariable(); |
| 722 | if (var != NULL) RecordAssignedVar(var); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | void AssignedVariablesAnalyzer::VisitBinaryOperation(BinaryOperation* expr) { |
| 727 | ASSERT(av_.IsEmpty()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 728 | MarkIfTrivial(expr->right()); |
| 729 | Visit(expr->right()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 730 | MarkIfTrivial(expr->left()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 731 | ProcessExpression(expr->left()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
| 735 | void AssignedVariablesAnalyzer::VisitCompareOperation(CompareOperation* expr) { |
| 736 | ASSERT(av_.IsEmpty()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 737 | MarkIfTrivial(expr->right()); |
| 738 | Visit(expr->right()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 739 | MarkIfTrivial(expr->left()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame^] | 740 | ProcessExpression(expr->left()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
| 744 | void AssignedVariablesAnalyzer::VisitThisFunction(ThisFunction* expr) { |
| 745 | // Nothing to do. |
| 746 | ASSERT(av_.IsEmpty()); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void AssignedVariablesAnalyzer::VisitDeclaration(Declaration* decl) { |
| 751 | UNREACHABLE(); |
| 752 | } |
| 753 | |
| 754 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 755 | } } // namespace v8::internal |