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 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 53 | bool AssignedVariablesAnalyzer::Analyze() { |
| 54 | Scope* scope = fun_->scope(); |
| 55 | int variables = scope->num_parameters() + scope->num_stack_slots(); |
| 56 | if (variables == 0) return true; |
| 57 | av_.ExpandTo(variables); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 58 | VisitStatements(fun_->body()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 59 | return !HasStackOverflow(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 63 | Variable* AssignedVariablesAnalyzer::FindSmiLoopVariable(ForStatement* stmt) { |
| 64 | // The loop must have all necessary parts. |
| 65 | if (stmt->init() == NULL || stmt->cond() == NULL || stmt->next() == NULL) { |
| 66 | return NULL; |
| 67 | } |
| 68 | // The initialization statement has to be a simple assignment. |
| 69 | Assignment* init = stmt->init()->StatementAsSimpleAssignment(); |
| 70 | if (init == NULL) return NULL; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 71 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 72 | // We only deal with local variables. |
| 73 | Variable* loop_var = init->target()->AsVariableProxy()->AsVariable(); |
| 74 | if (loop_var == NULL || !loop_var->IsStackAllocated()) return NULL; |
| 75 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 76 | // Don't try to get clever with const or dynamic variables. |
| 77 | if (loop_var->mode() != Variable::VAR) return NULL; |
| 78 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 79 | // The initial value has to be a smi. |
| 80 | Literal* init_lit = init->value()->AsLiteral(); |
| 81 | if (init_lit == NULL || !init_lit->handle()->IsSmi()) return NULL; |
| 82 | int init_value = Smi::cast(*init_lit->handle())->value(); |
| 83 | |
| 84 | // The condition must be a compare of variable with <, <=, >, or >=. |
| 85 | CompareOperation* cond = stmt->cond()->AsCompareOperation(); |
| 86 | if (cond == NULL) return NULL; |
| 87 | if (cond->op() != Token::LT |
| 88 | && cond->op() != Token::LTE |
| 89 | && cond->op() != Token::GT |
| 90 | && cond->op() != Token::GTE) return NULL; |
| 91 | |
| 92 | // The lhs must be the same variable as in the init expression. |
| 93 | if (cond->left()->AsVariableProxy()->AsVariable() != loop_var) return NULL; |
| 94 | |
| 95 | // The rhs must be a smi. |
| 96 | Literal* term_lit = cond->right()->AsLiteral(); |
| 97 | if (term_lit == NULL || !term_lit->handle()->IsSmi()) return NULL; |
| 98 | int term_value = Smi::cast(*term_lit->handle())->value(); |
| 99 | |
| 100 | // The count operation updates the same variable as in the init expression. |
| 101 | CountOperation* update = stmt->next()->StatementAsCountOperation(); |
| 102 | if (update == NULL) return NULL; |
| 103 | if (update->expression()->AsVariableProxy()->AsVariable() != loop_var) { |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | // The direction of the count operation must agree with the start and the end |
| 108 | // value. We currently do not allow the initial value to be the same as the |
| 109 | // terminal value. This _would_ be ok as long as the loop body never executes |
| 110 | // or executes exactly one time. |
| 111 | if (init_value == term_value) return NULL; |
| 112 | if (init_value < term_value && update->op() != Token::INC) return NULL; |
| 113 | if (init_value > term_value && update->op() != Token::DEC) return NULL; |
| 114 | |
| 115 | // Check that the update operation cannot overflow the smi range. This can |
| 116 | // occur in the two cases where the loop bound is equal to the largest or |
| 117 | // smallest smi. |
| 118 | if (update->op() == Token::INC && term_value == Smi::kMaxValue) return NULL; |
| 119 | if (update->op() == Token::DEC && term_value == Smi::kMinValue) return NULL; |
| 120 | |
| 121 | // Found a smi loop variable. |
| 122 | return loop_var; |
| 123 | } |
| 124 | |
| 125 | int AssignedVariablesAnalyzer::BitIndex(Variable* var) { |
| 126 | ASSERT(var != NULL); |
| 127 | ASSERT(var->IsStackAllocated()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 128 | Slot* slot = var->AsSlot(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 129 | if (slot->type() == Slot::PARAMETER) { |
| 130 | return slot->index(); |
| 131 | } else { |
| 132 | return fun_->scope()->num_parameters() + slot->index(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 137 | void AssignedVariablesAnalyzer::RecordAssignedVar(Variable* var) { |
| 138 | ASSERT(var != NULL); |
| 139 | if (var->IsStackAllocated()) { |
| 140 | av_.Add(BitIndex(var)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 145 | void AssignedVariablesAnalyzer::MarkIfTrivial(Expression* expr) { |
| 146 | Variable* var = expr->AsVariableProxy()->AsVariable(); |
| 147 | if (var != NULL && |
| 148 | var->IsStackAllocated() && |
| 149 | !var->is_arguments() && |
| 150 | var->mode() != Variable::CONST && |
| 151 | (var->is_this() || !av_.Contains(BitIndex(var)))) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 152 | expr->AsVariableProxy()->MarkAsTrivial(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 157 | void AssignedVariablesAnalyzer::ProcessExpression(Expression* expr) { |
| 158 | BitVector saved_av(av_); |
| 159 | av_.Clear(); |
| 160 | Visit(expr); |
| 161 | av_.Union(saved_av); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 164 | void AssignedVariablesAnalyzer::VisitBlock(Block* stmt) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 165 | VisitStatements(stmt->statements()); |
| 166 | } |
| 167 | |
| 168 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 169 | void AssignedVariablesAnalyzer::VisitExpressionStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 170 | ExpressionStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 171 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 175 | void AssignedVariablesAnalyzer::VisitEmptyStatement(EmptyStatement* stmt) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 176 | // Do nothing. |
| 177 | } |
| 178 | |
| 179 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 180 | void AssignedVariablesAnalyzer::VisitIfStatement(IfStatement* stmt) { |
| 181 | ProcessExpression(stmt->condition()); |
| 182 | Visit(stmt->then_statement()); |
| 183 | Visit(stmt->else_statement()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 187 | void AssignedVariablesAnalyzer::VisitContinueStatement( |
| 188 | ContinueStatement* stmt) { |
| 189 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 193 | void AssignedVariablesAnalyzer::VisitBreakStatement(BreakStatement* stmt) { |
| 194 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 198 | void AssignedVariablesAnalyzer::VisitReturnStatement(ReturnStatement* stmt) { |
| 199 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 203 | void AssignedVariablesAnalyzer::VisitWithEnterStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 204 | WithEnterStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 205 | ProcessExpression(stmt->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 209 | void AssignedVariablesAnalyzer::VisitWithExitStatement( |
| 210 | WithExitStatement* stmt) { |
| 211 | // Nothing to do. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 215 | void AssignedVariablesAnalyzer::VisitSwitchStatement(SwitchStatement* stmt) { |
| 216 | BitVector result(av_); |
| 217 | av_.Clear(); |
| 218 | Visit(stmt->tag()); |
| 219 | result.Union(av_); |
| 220 | for (int i = 0; i < stmt->cases()->length(); i++) { |
| 221 | CaseClause* clause = stmt->cases()->at(i); |
| 222 | if (!clause->is_default()) { |
| 223 | av_.Clear(); |
| 224 | Visit(clause->label()); |
| 225 | result.Union(av_); |
| 226 | } |
| 227 | VisitStatements(clause->statements()); |
| 228 | } |
| 229 | av_.Union(result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 233 | void AssignedVariablesAnalyzer::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 234 | ProcessExpression(stmt->cond()); |
| 235 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 239 | void AssignedVariablesAnalyzer::VisitWhileStatement(WhileStatement* stmt) { |
| 240 | ProcessExpression(stmt->cond()); |
| 241 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 245 | void AssignedVariablesAnalyzer::VisitForStatement(ForStatement* stmt) { |
| 246 | if (stmt->init() != NULL) Visit(stmt->init()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 247 | if (stmt->cond() != NULL) ProcessExpression(stmt->cond()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 248 | if (stmt->next() != NULL) Visit(stmt->next()); |
| 249 | |
| 250 | // Process loop body. After visiting the loop body av_ contains |
| 251 | // the assigned variables of the loop body. |
| 252 | BitVector saved_av(av_); |
| 253 | av_.Clear(); |
| 254 | Visit(stmt->body()); |
| 255 | |
| 256 | Variable* var = FindSmiLoopVariable(stmt); |
| 257 | if (var != NULL && !av_.Contains(BitIndex(var))) { |
| 258 | stmt->set_loop_variable(var); |
| 259 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 260 | av_.Union(saved_av); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 264 | void AssignedVariablesAnalyzer::VisitForInStatement(ForInStatement* stmt) { |
| 265 | ProcessExpression(stmt->each()); |
| 266 | ProcessExpression(stmt->enumerable()); |
| 267 | Visit(stmt->body()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 271 | void AssignedVariablesAnalyzer::VisitTryCatchStatement( |
| 272 | TryCatchStatement* stmt) { |
| 273 | Visit(stmt->try_block()); |
| 274 | Visit(stmt->catch_block()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 278 | void AssignedVariablesAnalyzer::VisitTryFinallyStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 279 | TryFinallyStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 280 | Visit(stmt->try_block()); |
| 281 | Visit(stmt->finally_block()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 285 | void AssignedVariablesAnalyzer::VisitDebuggerStatement( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 286 | DebuggerStatement* stmt) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 287 | // Nothing to do. |
| 288 | } |
| 289 | |
| 290 | |
| 291 | void AssignedVariablesAnalyzer::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 292 | // Nothing to do. |
| 293 | ASSERT(av_.IsEmpty()); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | void AssignedVariablesAnalyzer::VisitSharedFunctionInfoLiteral( |
| 298 | SharedFunctionInfoLiteral* expr) { |
| 299 | // Nothing to do. |
| 300 | ASSERT(av_.IsEmpty()); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | void AssignedVariablesAnalyzer::VisitConditional(Conditional* expr) { |
| 305 | ASSERT(av_.IsEmpty()); |
| 306 | |
| 307 | Visit(expr->condition()); |
| 308 | |
| 309 | BitVector result(av_); |
| 310 | av_.Clear(); |
| 311 | Visit(expr->then_expression()); |
| 312 | result.Union(av_); |
| 313 | |
| 314 | av_.Clear(); |
| 315 | Visit(expr->else_expression()); |
| 316 | av_.Union(result); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void AssignedVariablesAnalyzer::VisitSlot(Slot* expr) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 321 | UNREACHABLE(); |
| 322 | } |
| 323 | |
| 324 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 325 | void AssignedVariablesAnalyzer::VisitVariableProxy(VariableProxy* expr) { |
| 326 | // Nothing to do. |
| 327 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 331 | void AssignedVariablesAnalyzer::VisitLiteral(Literal* expr) { |
| 332 | // Nothing to do. |
| 333 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 337 | void AssignedVariablesAnalyzer::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 338 | // Nothing to do. |
| 339 | ASSERT(av_.IsEmpty()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 343 | void AssignedVariablesAnalyzer::VisitObjectLiteral(ObjectLiteral* expr) { |
| 344 | ASSERT(av_.IsEmpty()); |
| 345 | BitVector result(av_.length()); |
| 346 | for (int i = 0; i < expr->properties()->length(); i++) { |
| 347 | Visit(expr->properties()->at(i)->value()); |
| 348 | result.Union(av_); |
| 349 | av_.Clear(); |
| 350 | } |
| 351 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 355 | void AssignedVariablesAnalyzer::VisitArrayLiteral(ArrayLiteral* expr) { |
| 356 | ASSERT(av_.IsEmpty()); |
| 357 | BitVector result(av_.length()); |
| 358 | for (int i = 0; i < expr->values()->length(); i++) { |
| 359 | Visit(expr->values()->at(i)); |
| 360 | result.Union(av_); |
| 361 | av_.Clear(); |
| 362 | } |
| 363 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 367 | void AssignedVariablesAnalyzer::VisitCatchExtensionObject( |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 368 | CatchExtensionObject* expr) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 369 | ASSERT(av_.IsEmpty()); |
| 370 | Visit(expr->key()); |
| 371 | ProcessExpression(expr->value()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 375 | void AssignedVariablesAnalyzer::VisitAssignment(Assignment* expr) { |
| 376 | ASSERT(av_.IsEmpty()); |
| 377 | |
| 378 | // There are three kinds of assignments: variable assignments, property |
| 379 | // assignments, and reference errors (invalid left-hand sides). |
| 380 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 381 | Property* prop = expr->target()->AsProperty(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 382 | ASSERT(var == NULL || prop == NULL); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 383 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 384 | if (var != NULL) { |
| 385 | MarkIfTrivial(expr->value()); |
| 386 | Visit(expr->value()); |
| 387 | if (expr->is_compound()) { |
| 388 | // Left-hand side occurs also as an rvalue. |
| 389 | MarkIfTrivial(expr->target()); |
| 390 | ProcessExpression(expr->target()); |
| 391 | } |
| 392 | RecordAssignedVar(var); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 393 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 394 | } else if (prop != NULL) { |
| 395 | MarkIfTrivial(expr->value()); |
| 396 | Visit(expr->value()); |
| 397 | if (!prop->key()->IsPropertyName()) { |
| 398 | MarkIfTrivial(prop->key()); |
| 399 | ProcessExpression(prop->key()); |
| 400 | } |
| 401 | MarkIfTrivial(prop->obj()); |
| 402 | ProcessExpression(prop->obj()); |
| 403 | |
| 404 | } else { |
| 405 | Visit(expr->target()); |
| 406 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 410 | void AssignedVariablesAnalyzer::VisitThrow(Throw* expr) { |
| 411 | ASSERT(av_.IsEmpty()); |
| 412 | Visit(expr->exception()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 416 | void AssignedVariablesAnalyzer::VisitProperty(Property* expr) { |
| 417 | ASSERT(av_.IsEmpty()); |
| 418 | if (!expr->key()->IsPropertyName()) { |
| 419 | MarkIfTrivial(expr->key()); |
| 420 | Visit(expr->key()); |
| 421 | } |
| 422 | MarkIfTrivial(expr->obj()); |
| 423 | ProcessExpression(expr->obj()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 427 | void AssignedVariablesAnalyzer::VisitCall(Call* expr) { |
| 428 | ASSERT(av_.IsEmpty()); |
| 429 | Visit(expr->expression()); |
| 430 | BitVector result(av_); |
| 431 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 432 | av_.Clear(); |
| 433 | Visit(expr->arguments()->at(i)); |
| 434 | result.Union(av_); |
| 435 | } |
| 436 | av_ = result; |
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::VisitCallNew(CallNew* expr) { |
| 441 | ASSERT(av_.IsEmpty()); |
| 442 | Visit(expr->expression()); |
| 443 | BitVector result(av_); |
| 444 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 445 | av_.Clear(); |
| 446 | Visit(expr->arguments()->at(i)); |
| 447 | result.Union(av_); |
| 448 | } |
| 449 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 453 | void AssignedVariablesAnalyzer::VisitCallRuntime(CallRuntime* expr) { |
| 454 | ASSERT(av_.IsEmpty()); |
| 455 | BitVector result(av_); |
| 456 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 457 | av_.Clear(); |
| 458 | Visit(expr->arguments()->at(i)); |
| 459 | result.Union(av_); |
| 460 | } |
| 461 | av_ = result; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 465 | void AssignedVariablesAnalyzer::VisitUnaryOperation(UnaryOperation* expr) { |
| 466 | ASSERT(av_.IsEmpty()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 467 | MarkIfTrivial(expr->expression()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 468 | Visit(expr->expression()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 472 | void AssignedVariablesAnalyzer::VisitIncrementOperation( |
| 473 | IncrementOperation* expr) { |
| 474 | UNREACHABLE(); |
| 475 | } |
| 476 | |
| 477 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 478 | void AssignedVariablesAnalyzer::VisitCountOperation(CountOperation* expr) { |
| 479 | ASSERT(av_.IsEmpty()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 480 | if (expr->is_prefix()) MarkIfTrivial(expr->expression()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 481 | Visit(expr->expression()); |
| 482 | |
| 483 | Variable* var = expr->expression()->AsVariableProxy()->AsVariable(); |
| 484 | if (var != NULL) RecordAssignedVar(var); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 488 | void AssignedVariablesAnalyzer::VisitBinaryOperation(BinaryOperation* expr) { |
| 489 | ASSERT(av_.IsEmpty()); |
| 490 | MarkIfTrivial(expr->right()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 491 | Visit(expr->right()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 492 | MarkIfTrivial(expr->left()); |
| 493 | ProcessExpression(expr->left()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 497 | void AssignedVariablesAnalyzer::VisitCompareOperation(CompareOperation* expr) { |
| 498 | ASSERT(av_.IsEmpty()); |
| 499 | MarkIfTrivial(expr->right()); |
| 500 | Visit(expr->right()); |
| 501 | MarkIfTrivial(expr->left()); |
| 502 | ProcessExpression(expr->left()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 506 | void AssignedVariablesAnalyzer::VisitCompareToNull(CompareToNull* expr) { |
| 507 | ASSERT(av_.IsEmpty()); |
| 508 | MarkIfTrivial(expr->expression()); |
| 509 | Visit(expr->expression()); |
| 510 | } |
| 511 | |
| 512 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 513 | void AssignedVariablesAnalyzer::VisitThisFunction(ThisFunction* expr) { |
| 514 | // Nothing to do. |
| 515 | ASSERT(av_.IsEmpty()); |
| 516 | } |
| 517 | |
| 518 | |
| 519 | void AssignedVariablesAnalyzer::VisitDeclaration(Declaration* decl) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 520 | UNREACHABLE(); |
| 521 | } |
| 522 | |
| 523 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 524 | } } // namespace v8::internal |