Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 31 | #include "scopes.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [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 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 53 | void AstLabeler::Label(CompilationInfo* info) { |
| 54 | info_ = info; |
| 55 | VisitStatements(info_->function()->body()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 165 | void AstLabeler::VisitSharedFunctionInfoLiteral( |
| 166 | SharedFunctionInfoLiteral* expr) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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_++); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 183 | Variable* var = expr->var(); |
| 184 | if (var->is_global() && !var->is_this()) { |
| 185 | info_->set_has_globals(true); |
| 186 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +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); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 224 | |
| 225 | prop->obj()->set_num(AstNode::kNoNumber); |
| 226 | prop->key()->set_num(AstNode::kNoNumber); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +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); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 243 | |
| 244 | expr->obj()->set_num(AstNode::kNoNumber); |
| 245 | expr->key()->set_num(AstNode::kNoNumber); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 246 | expr->set_num(next_number_++); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +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 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 296 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [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()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 316 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | |
| 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 | |
| 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(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 379 | void AssignedVariablesAnalyzer::RecordAssignedVar(Variable* var) { |
| 380 | ASSERT(var != NULL); |
| 381 | if (var->IsStackAllocated()) { |
| 382 | av_.Add(BitIndex(var)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 399 | void AssignedVariablesAnalyzer::ProcessExpression(Expression* expr) { |
| 400 | BitVector saved_av(av_); |
| 401 | av_.Clear(); |
| 402 | Visit(expr); |
| 403 | av_.Union(saved_av); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 406 | void AssignedVariablesAnalyzer::VisitBlock(Block* stmt) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 407 | VisitStatements(stmt->statements()); |
| 408 | } |
| 409 | |
| 410 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 411 | void AssignedVariablesAnalyzer::VisitExpressionStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 412 | ExpressionStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 413 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 417 | void AssignedVariablesAnalyzer::VisitEmptyStatement(EmptyStatement* stmt) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 418 | // Do nothing. |
| 419 | } |
| 420 | |
| 421 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 422 | void AssignedVariablesAnalyzer::VisitIfStatement(IfStatement* stmt) { |
| 423 | ProcessExpression(stmt->condition()); |
| 424 | Visit(stmt->then_statement()); |
| 425 | Visit(stmt->else_statement()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 429 | void AssignedVariablesAnalyzer::VisitContinueStatement( |
| 430 | ContinueStatement* stmt) { |
| 431 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 435 | void AssignedVariablesAnalyzer::VisitBreakStatement(BreakStatement* stmt) { |
| 436 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 440 | void AssignedVariablesAnalyzer::VisitReturnStatement(ReturnStatement* stmt) { |
| 441 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 445 | void AssignedVariablesAnalyzer::VisitWithEnterStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 446 | WithEnterStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 447 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 451 | void AssignedVariablesAnalyzer::VisitWithExitStatement( |
| 452 | WithExitStatement* stmt) { |
| 453 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 475 | void AssignedVariablesAnalyzer::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 476 | ProcessExpression(stmt->cond()); |
| 477 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 481 | void AssignedVariablesAnalyzer::VisitWhileStatement(WhileStatement* stmt) { |
| 482 | ProcessExpression(stmt->cond()); |
| 483 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 509 | void AssignedVariablesAnalyzer::VisitForInStatement(ForInStatement* stmt) { |
| 510 | ProcessExpression(stmt->each()); |
| 511 | ProcessExpression(stmt->enumerable()); |
| 512 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 516 | void AssignedVariablesAnalyzer::VisitTryCatchStatement( |
| 517 | TryCatchStatement* stmt) { |
| 518 | Visit(stmt->try_block()); |
| 519 | Visit(stmt->catch_block()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 523 | void AssignedVariablesAnalyzer::VisitTryFinallyStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 524 | TryFinallyStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 525 | Visit(stmt->try_block()); |
| 526 | Visit(stmt->finally_block()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 530 | void AssignedVariablesAnalyzer::VisitDebuggerStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 531 | DebuggerStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 532 | // Nothing to do. |
| 533 | } |
| 534 | |
| 535 | |
| 536 | void AssignedVariablesAnalyzer::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 537 | // Nothing to do. |
| 538 | ASSERT(av_.IsEmpty()); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | void AssignedVariablesAnalyzer::VisitSharedFunctionInfoLiteral( |
| 543 | SharedFunctionInfoLiteral* expr) { |
| 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) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 566 | UNREACHABLE(); |
| 567 | } |
| 568 | |
| 569 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 570 | void AssignedVariablesAnalyzer::VisitVariableProxy(VariableProxy* expr) { |
| 571 | // Nothing to do. |
| 572 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 576 | void AssignedVariablesAnalyzer::VisitLiteral(Literal* expr) { |
| 577 | // Nothing to do. |
| 578 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 582 | void AssignedVariablesAnalyzer::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 583 | // Nothing to do. |
| 584 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | } |
| 596 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | } |
| 608 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 612 | void AssignedVariablesAnalyzer::VisitCatchExtensionObject( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 613 | CatchExtensionObject* expr) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 614 | ASSERT(av_.IsEmpty()); |
| 615 | Visit(expr->key()); |
| 616 | ProcessExpression(expr->value()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 620 | void AssignedVariablesAnalyzer::VisitAssignment(Assignment* expr) { |
| 621 | ASSERT(av_.IsEmpty()); |
| 622 | |
| 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(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 626 | Property* prop = expr->target()->AsProperty(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 627 | ASSERT(var == NULL || prop == NULL); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 628 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 638 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | |
| 649 | } else { |
| 650 | Visit(expr->target()); |
| 651 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 655 | void AssignedVariablesAnalyzer::VisitThrow(Throw* expr) { |
| 656 | ASSERT(av_.IsEmpty()); |
| 657 | Visit(expr->exception()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 661 | void AssignedVariablesAnalyzer::VisitProperty(Property* expr) { |
| 662 | ASSERT(av_.IsEmpty()); |
| 663 | if (!expr->key()->IsPropertyName()) { |
| 664 | MarkIfTrivial(expr->key()); |
| 665 | Visit(expr->key()); |
| 666 | } |
| 667 | MarkIfTrivial(expr->obj()); |
| 668 | ProcessExpression(expr->obj()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | } |
| 681 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | } |
| 694 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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 | } |
| 706 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 710 | void AssignedVariablesAnalyzer::VisitUnaryOperation(UnaryOperation* expr) { |
| 711 | ASSERT(av_.IsEmpty()); |
| 712 | Visit(expr->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 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); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 726 | void AssignedVariablesAnalyzer::VisitBinaryOperation(BinaryOperation* expr) { |
| 727 | ASSERT(av_.IsEmpty()); |
| 728 | MarkIfTrivial(expr->right()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 729 | Visit(expr->right()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 730 | MarkIfTrivial(expr->left()); |
| 731 | ProcessExpression(expr->left()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 735 | void AssignedVariablesAnalyzer::VisitCompareOperation(CompareOperation* expr) { |
| 736 | ASSERT(av_.IsEmpty()); |
| 737 | MarkIfTrivial(expr->right()); |
| 738 | Visit(expr->right()); |
| 739 | MarkIfTrivial(expr->left()); |
| 740 | ProcessExpression(expr->left()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 744 | void AssignedVariablesAnalyzer::VisitThisFunction(ThisFunction* expr) { |
| 745 | // Nothing to do. |
| 746 | ASSERT(av_.IsEmpty()); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void AssignedVariablesAnalyzer::VisitDeclaration(Declaration* decl) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 751 | UNREACHABLE(); |
| 752 | } |
| 753 | |
| 754 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 755 | } } // namespace v8::internal |