sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2009 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 "codegen-inl.h" |
| 31 | #include "compiler.h" |
| 32 | #include "full-codegen.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 33 | #include "scopes.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 34 | #include "stub-cache.h" |
| 35 | #include "debug.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 36 | #include "liveedit.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 37 | |
| 38 | namespace v8 { |
| 39 | namespace internal { |
| 40 | |
| 41 | #define BAILOUT(reason) \ |
| 42 | do { \ |
| 43 | if (FLAG_trace_bailout) { \ |
| 44 | PrintF("%s\n", reason); \ |
| 45 | } \ |
| 46 | has_supported_syntax_ = false; \ |
| 47 | return; \ |
| 48 | } while (false) |
| 49 | |
| 50 | |
| 51 | #define CHECK_BAILOUT \ |
| 52 | do { \ |
| 53 | if (!has_supported_syntax_) return; \ |
| 54 | } while (false) |
| 55 | |
| 56 | |
| 57 | void FullCodeGenSyntaxChecker::Check(FunctionLiteral* fun) { |
| 58 | Scope* scope = fun->scope(); |
| 59 | VisitDeclarations(scope->declarations()); |
| 60 | CHECK_BAILOUT; |
| 61 | |
| 62 | VisitStatements(fun->body()); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void FullCodeGenSyntaxChecker::VisitDeclarations( |
| 67 | ZoneList<Declaration*>* decls) { |
| 68 | for (int i = 0; i < decls->length(); i++) { |
| 69 | Visit(decls->at(i)); |
| 70 | CHECK_BAILOUT; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void FullCodeGenSyntaxChecker::VisitStatements(ZoneList<Statement*>* stmts) { |
| 76 | for (int i = 0, len = stmts->length(); i < len; i++) { |
| 77 | Visit(stmts->at(i)); |
| 78 | CHECK_BAILOUT; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void FullCodeGenSyntaxChecker::VisitDeclaration(Declaration* decl) { |
| 84 | Property* prop = decl->proxy()->AsProperty(); |
| 85 | if (prop != NULL) { |
| 86 | Visit(prop->obj()); |
| 87 | Visit(prop->key()); |
| 88 | } |
| 89 | |
| 90 | if (decl->fun() != NULL) { |
| 91 | Visit(decl->fun()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void FullCodeGenSyntaxChecker::VisitBlock(Block* stmt) { |
| 97 | VisitStatements(stmt->statements()); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | void FullCodeGenSyntaxChecker::VisitExpressionStatement( |
| 102 | ExpressionStatement* stmt) { |
| 103 | Visit(stmt->expression()); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void FullCodeGenSyntaxChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 108 | // Supported. |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void FullCodeGenSyntaxChecker::VisitIfStatement(IfStatement* stmt) { |
| 113 | Visit(stmt->condition()); |
| 114 | CHECK_BAILOUT; |
| 115 | Visit(stmt->then_statement()); |
| 116 | CHECK_BAILOUT; |
| 117 | Visit(stmt->else_statement()); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void FullCodeGenSyntaxChecker::VisitContinueStatement(ContinueStatement* stmt) { |
| 122 | // Supported. |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void FullCodeGenSyntaxChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 127 | // Supported. |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void FullCodeGenSyntaxChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 132 | Visit(stmt->expression()); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void FullCodeGenSyntaxChecker::VisitWithEnterStatement( |
| 137 | WithEnterStatement* stmt) { |
| 138 | Visit(stmt->expression()); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void FullCodeGenSyntaxChecker::VisitWithExitStatement(WithExitStatement* stmt) { |
| 143 | // Supported. |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void FullCodeGenSyntaxChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 148 | BAILOUT("SwitchStatement"); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void FullCodeGenSyntaxChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 153 | Visit(stmt->cond()); |
| 154 | CHECK_BAILOUT; |
| 155 | Visit(stmt->body()); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void FullCodeGenSyntaxChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 160 | Visit(stmt->cond()); |
| 161 | CHECK_BAILOUT; |
| 162 | Visit(stmt->body()); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void FullCodeGenSyntaxChecker::VisitForStatement(ForStatement* stmt) { |
| 167 | if (!FLAG_always_full_compiler) BAILOUT("ForStatement"); |
| 168 | if (stmt->init() != NULL) { |
| 169 | Visit(stmt->init()); |
| 170 | CHECK_BAILOUT; |
| 171 | } |
| 172 | if (stmt->cond() != NULL) { |
| 173 | Visit(stmt->cond()); |
| 174 | CHECK_BAILOUT; |
| 175 | } |
| 176 | Visit(stmt->body()); |
| 177 | if (stmt->next() != NULL) { |
| 178 | CHECK_BAILOUT; |
| 179 | Visit(stmt->next()); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | void FullCodeGenSyntaxChecker::VisitForInStatement(ForInStatement* stmt) { |
| 185 | BAILOUT("ForInStatement"); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void FullCodeGenSyntaxChecker::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 190 | Visit(stmt->try_block()); |
| 191 | CHECK_BAILOUT; |
| 192 | Visit(stmt->catch_block()); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void FullCodeGenSyntaxChecker::VisitTryFinallyStatement( |
| 197 | TryFinallyStatement* stmt) { |
| 198 | Visit(stmt->try_block()); |
| 199 | CHECK_BAILOUT; |
| 200 | Visit(stmt->finally_block()); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | void FullCodeGenSyntaxChecker::VisitDebuggerStatement( |
| 205 | DebuggerStatement* stmt) { |
| 206 | // Supported. |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void FullCodeGenSyntaxChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 211 | // Supported. |
| 212 | } |
| 213 | |
| 214 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 215 | void FullCodeGenSyntaxChecker::VisitSharedFunctionInfoLiteral( |
| 216 | SharedFunctionInfoLiteral* expr) { |
| 217 | BAILOUT("SharedFunctionInfoLiteral"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | void FullCodeGenSyntaxChecker::VisitConditional(Conditional* expr) { |
| 222 | Visit(expr->condition()); |
| 223 | CHECK_BAILOUT; |
| 224 | Visit(expr->then_expression()); |
| 225 | CHECK_BAILOUT; |
| 226 | Visit(expr->else_expression()); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | void FullCodeGenSyntaxChecker::VisitSlot(Slot* expr) { |
| 231 | UNREACHABLE(); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void FullCodeGenSyntaxChecker::VisitVariableProxy(VariableProxy* expr) { |
| 236 | // Supported. |
| 237 | } |
| 238 | |
| 239 | |
| 240 | void FullCodeGenSyntaxChecker::VisitLiteral(Literal* expr) { |
| 241 | // Supported. |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void FullCodeGenSyntaxChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 246 | // Supported. |
| 247 | } |
| 248 | |
| 249 | |
| 250 | void FullCodeGenSyntaxChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 251 | ZoneList<ObjectLiteral::Property*>* properties = expr->properties(); |
| 252 | |
| 253 | for (int i = 0, len = properties->length(); i < len; i++) { |
| 254 | ObjectLiteral::Property* property = properties->at(i); |
| 255 | if (property->IsCompileTimeValue()) continue; |
| 256 | Visit(property->key()); |
| 257 | CHECK_BAILOUT; |
| 258 | Visit(property->value()); |
| 259 | CHECK_BAILOUT; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void FullCodeGenSyntaxChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 265 | ZoneList<Expression*>* subexprs = expr->values(); |
| 266 | for (int i = 0, len = subexprs->length(); i < len; i++) { |
| 267 | Expression* subexpr = subexprs->at(i); |
| 268 | if (subexpr->AsLiteral() != NULL) continue; |
| 269 | if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue; |
| 270 | Visit(subexpr); |
| 271 | CHECK_BAILOUT; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void FullCodeGenSyntaxChecker::VisitCatchExtensionObject( |
| 277 | CatchExtensionObject* expr) { |
| 278 | Visit(expr->key()); |
| 279 | CHECK_BAILOUT; |
| 280 | Visit(expr->value()); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | void FullCodeGenSyntaxChecker::VisitAssignment(Assignment* expr) { |
| 285 | Token::Value op = expr->op(); |
| 286 | if (op == Token::INIT_CONST) BAILOUT("initialize constant"); |
| 287 | |
| 288 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 289 | Property* prop = expr->target()->AsProperty(); |
| 290 | ASSERT(var == NULL || prop == NULL); |
| 291 | if (var != NULL) { |
| 292 | if (var->mode() == Variable::CONST) BAILOUT("Assignment to const"); |
| 293 | // All other variables are supported. |
| 294 | } else if (prop != NULL) { |
| 295 | Visit(prop->obj()); |
| 296 | CHECK_BAILOUT; |
| 297 | Visit(prop->key()); |
| 298 | CHECK_BAILOUT; |
| 299 | } else { |
| 300 | // This is a throw reference error. |
| 301 | BAILOUT("non-variable/non-property assignment"); |
| 302 | } |
| 303 | |
| 304 | Visit(expr->value()); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | void FullCodeGenSyntaxChecker::VisitThrow(Throw* expr) { |
| 309 | Visit(expr->exception()); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | void FullCodeGenSyntaxChecker::VisitProperty(Property* expr) { |
| 314 | Visit(expr->obj()); |
| 315 | CHECK_BAILOUT; |
| 316 | Visit(expr->key()); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void FullCodeGenSyntaxChecker::VisitCall(Call* expr) { |
| 321 | Expression* fun = expr->expression(); |
| 322 | ZoneList<Expression*>* args = expr->arguments(); |
| 323 | Variable* var = fun->AsVariableProxy()->AsVariable(); |
| 324 | |
| 325 | // Check for supported calls |
| 326 | if (var != NULL && var->is_possibly_eval()) { |
| 327 | BAILOUT("call to the identifier 'eval'"); |
| 328 | } else if (var != NULL && !var->is_this() && var->is_global()) { |
| 329 | // Calls to global variables are supported. |
| 330 | } else if (var != NULL && var->slot() != NULL && |
| 331 | var->slot()->type() == Slot::LOOKUP) { |
| 332 | BAILOUT("call to a lookup slot"); |
| 333 | } else if (fun->AsProperty() != NULL) { |
| 334 | Property* prop = fun->AsProperty(); |
| 335 | Visit(prop->obj()); |
| 336 | CHECK_BAILOUT; |
| 337 | Visit(prop->key()); |
| 338 | CHECK_BAILOUT; |
| 339 | } else { |
| 340 | // Otherwise the call is supported if the function expression is. |
| 341 | Visit(fun); |
| 342 | } |
| 343 | // Check all arguments to the call. |
| 344 | for (int i = 0; i < args->length(); i++) { |
| 345 | Visit(args->at(i)); |
| 346 | CHECK_BAILOUT; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | |
| 351 | void FullCodeGenSyntaxChecker::VisitCallNew(CallNew* expr) { |
| 352 | Visit(expr->expression()); |
| 353 | CHECK_BAILOUT; |
| 354 | ZoneList<Expression*>* args = expr->arguments(); |
| 355 | // Check all arguments to the call |
| 356 | for (int i = 0; i < args->length(); i++) { |
| 357 | Visit(args->at(i)); |
| 358 | CHECK_BAILOUT; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void FullCodeGenSyntaxChecker::VisitCallRuntime(CallRuntime* expr) { |
| 364 | // Check for inline runtime call |
| 365 | if (expr->name()->Get(0) == '_' && |
| 366 | CodeGenerator::FindInlineRuntimeLUT(expr->name()) != NULL) { |
| 367 | BAILOUT("inlined runtime call"); |
| 368 | } |
| 369 | // Check all arguments to the call. (Relies on TEMP meaning STACK.) |
| 370 | for (int i = 0; i < expr->arguments()->length(); i++) { |
| 371 | Visit(expr->arguments()->at(i)); |
| 372 | CHECK_BAILOUT; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | |
| 377 | void FullCodeGenSyntaxChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 378 | switch (expr->op()) { |
| 379 | case Token::ADD: |
| 380 | case Token::BIT_NOT: |
| 381 | case Token::NOT: |
| 382 | case Token::SUB: |
| 383 | case Token::TYPEOF: |
| 384 | case Token::VOID: |
| 385 | Visit(expr->expression()); |
| 386 | break; |
| 387 | case Token::DELETE: |
| 388 | BAILOUT("UnaryOperation: DELETE"); |
| 389 | default: |
| 390 | UNREACHABLE(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | void FullCodeGenSyntaxChecker::VisitCountOperation(CountOperation* expr) { |
| 396 | Variable* var = expr->expression()->AsVariableProxy()->AsVariable(); |
| 397 | Property* prop = expr->expression()->AsProperty(); |
| 398 | ASSERT(var == NULL || prop == NULL); |
| 399 | if (var != NULL) { |
| 400 | // All global variables are supported. |
| 401 | if (!var->is_global()) { |
| 402 | ASSERT(var->slot() != NULL); |
| 403 | Slot::Type type = var->slot()->type(); |
| 404 | if (type == Slot::LOOKUP) { |
| 405 | BAILOUT("CountOperation with lookup slot"); |
| 406 | } |
| 407 | } |
| 408 | } else if (prop != NULL) { |
| 409 | Visit(prop->obj()); |
| 410 | CHECK_BAILOUT; |
| 411 | Visit(prop->key()); |
| 412 | CHECK_BAILOUT; |
| 413 | } else { |
| 414 | // This is a throw reference error. |
| 415 | BAILOUT("CountOperation non-variable/non-property expression"); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
| 420 | void FullCodeGenSyntaxChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 421 | Visit(expr->left()); |
| 422 | CHECK_BAILOUT; |
| 423 | Visit(expr->right()); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | void FullCodeGenSyntaxChecker::VisitCompareOperation(CompareOperation* expr) { |
| 428 | Visit(expr->left()); |
| 429 | CHECK_BAILOUT; |
| 430 | Visit(expr->right()); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | void FullCodeGenSyntaxChecker::VisitThisFunction(ThisFunction* expr) { |
| 435 | // Supported. |
| 436 | } |
| 437 | |
| 438 | #undef BAILOUT |
| 439 | #undef CHECK_BAILOUT |
| 440 | |
| 441 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 442 | void BreakableStatementChecker::Check(Statement* stmt) { |
| 443 | Visit(stmt); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | void BreakableStatementChecker::Check(Expression* expr) { |
| 448 | Visit(expr); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | void BreakableStatementChecker::VisitDeclaration(Declaration* decl) { |
| 453 | } |
| 454 | |
| 455 | |
| 456 | void BreakableStatementChecker::VisitBlock(Block* stmt) { |
| 457 | } |
| 458 | |
| 459 | |
| 460 | void BreakableStatementChecker::VisitExpressionStatement( |
| 461 | ExpressionStatement* stmt) { |
| 462 | // Check if expression is breakable. |
| 463 | Visit(stmt->expression()); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 468 | } |
| 469 | |
| 470 | |
| 471 | void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) { |
| 472 | // If the condition is breakable the if statement is breakable. |
| 473 | Visit(stmt->condition()); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | void BreakableStatementChecker::VisitContinueStatement( |
| 478 | ContinueStatement* stmt) { |
| 479 | } |
| 480 | |
| 481 | |
| 482 | void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 487 | // Return is breakable if the expression is. |
| 488 | Visit(stmt->expression()); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | void BreakableStatementChecker::VisitWithEnterStatement( |
| 493 | WithEnterStatement* stmt) { |
| 494 | Visit(stmt->expression()); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | void BreakableStatementChecker::VisitWithExitStatement( |
| 499 | WithExitStatement* stmt) { |
| 500 | } |
| 501 | |
| 502 | |
| 503 | void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 504 | // Switch statements breakable if the tag expression is. |
| 505 | Visit(stmt->tag()); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 510 | // Mark do while as breakable to avoid adding a break slot in front of it. |
| 511 | is_breakable_ = true; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 516 | // Mark while statements breakable if the condition expression is. |
| 517 | Visit(stmt->cond()); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) { |
| 522 | // Mark for statements breakable if the condition expression is. |
| 523 | if (stmt->cond() != NULL) { |
| 524 | Visit(stmt->cond()); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | |
| 529 | void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) { |
| 530 | // Mark for in statements breakable if the enumerable expression is. |
| 531 | Visit(stmt->enumerable()); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | void BreakableStatementChecker::VisitTryCatchStatement( |
| 536 | TryCatchStatement* stmt) { |
| 537 | // Mark try catch as breakable to avoid adding a break slot in front of it. |
| 538 | is_breakable_ = true; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | void BreakableStatementChecker::VisitTryFinallyStatement( |
| 543 | TryFinallyStatement* stmt) { |
| 544 | // Mark try finally as breakable to avoid adding a break slot in front of it. |
| 545 | is_breakable_ = true; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void BreakableStatementChecker::VisitDebuggerStatement( |
| 550 | DebuggerStatement* stmt) { |
| 551 | // The debugger statement is breakable. |
| 552 | is_breakable_ = true; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 557 | } |
| 558 | |
| 559 | |
| 560 | void BreakableStatementChecker::VisitSharedFunctionInfoLiteral( |
| 561 | SharedFunctionInfoLiteral* expr) { |
| 562 | } |
| 563 | |
| 564 | |
| 565 | void BreakableStatementChecker::VisitConditional(Conditional* expr) { |
| 566 | } |
| 567 | |
| 568 | |
| 569 | void BreakableStatementChecker::VisitSlot(Slot* expr) { |
| 570 | } |
| 571 | |
| 572 | |
| 573 | void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) { |
| 574 | } |
| 575 | |
| 576 | |
| 577 | void BreakableStatementChecker::VisitLiteral(Literal* expr) { |
| 578 | } |
| 579 | |
| 580 | |
| 581 | void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 582 | } |
| 583 | |
| 584 | |
| 585 | void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 586 | } |
| 587 | |
| 588 | |
| 589 | void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 590 | } |
| 591 | |
| 592 | |
| 593 | void BreakableStatementChecker::VisitCatchExtensionObject( |
| 594 | CatchExtensionObject* expr) { |
| 595 | } |
| 596 | |
| 597 | |
| 598 | void BreakableStatementChecker::VisitAssignment(Assignment* expr) { |
| 599 | // If assigning to a property (including a global property) the assignment is |
| 600 | // breakable. |
| 601 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 602 | Property* prop = expr->target()->AsProperty(); |
| 603 | if (prop != NULL || (var != NULL && var->is_global())) { |
| 604 | is_breakable_ = true; |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | // Otherwise the assignment is breakable if the assigned value is. |
| 609 | Visit(expr->value()); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | void BreakableStatementChecker::VisitThrow(Throw* expr) { |
| 614 | // Throw is breakable if the expression is. |
| 615 | Visit(expr->exception()); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | void BreakableStatementChecker::VisitProperty(Property* expr) { |
| 620 | // Property load is breakable. |
| 621 | is_breakable_ = true; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | void BreakableStatementChecker::VisitCall(Call* expr) { |
| 626 | // Function calls both through IC and call stub are breakable. |
| 627 | is_breakable_ = true; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | void BreakableStatementChecker::VisitCallNew(CallNew* expr) { |
| 632 | // Function calls through new are breakable. |
| 633 | is_breakable_ = true; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) { |
| 638 | } |
| 639 | |
| 640 | |
| 641 | void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 642 | Visit(expr->expression()); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) { |
| 647 | Visit(expr->expression()); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 652 | Visit(expr->left()); |
| 653 | Visit(expr->right()); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) { |
| 658 | Visit(expr->left()); |
| 659 | Visit(expr->right()); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) { |
| 664 | } |
| 665 | |
| 666 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 667 | #define __ ACCESS_MASM(masm()) |
| 668 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 669 | Handle<Code> FullCodeGenerator::MakeCode(CompilationInfo* info) { |
| 670 | Handle<Script> script = info->script(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 671 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 672 | int len = String::cast(script->source())->length(); |
| 673 | Counters::total_full_codegen_source_size.Increment(len); |
| 674 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 675 | CodeGenerator::MakeCodePrologue(info); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 676 | const int kInitialBufferSize = 4 * KB; |
| 677 | MacroAssembler masm(NULL, kInitialBufferSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 678 | |
| 679 | FullCodeGenerator cgen(&masm); |
| 680 | cgen.Generate(info, PRIMARY); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 681 | if (cgen.HasStackOverflow()) { |
| 682 | ASSERT(!Top::has_pending_exception()); |
| 683 | return Handle<Code>::null(); |
| 684 | } |
| 685 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 686 | return CodeGenerator::MakeCodeEpilogue(&masm, flags, info); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
| 690 | int FullCodeGenerator::SlotOffset(Slot* slot) { |
| 691 | ASSERT(slot != NULL); |
| 692 | // Offset is negative because higher indexes are at lower addresses. |
| 693 | int offset = -slot->index() * kPointerSize; |
| 694 | // Adjust by a (parameter or local) base offset. |
| 695 | switch (slot->type()) { |
| 696 | case Slot::PARAMETER: |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 697 | offset += (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 698 | break; |
| 699 | case Slot::LOCAL: |
| 700 | offset += JavaScriptFrameConstants::kLocal0Offset; |
| 701 | break; |
| 702 | case Slot::CONTEXT: |
| 703 | case Slot::LOOKUP: |
| 704 | UNREACHABLE(); |
| 705 | } |
| 706 | return offset; |
| 707 | } |
| 708 | |
| 709 | |
| 710 | void FullCodeGenerator::VisitDeclarations( |
| 711 | ZoneList<Declaration*>* declarations) { |
| 712 | int length = declarations->length(); |
| 713 | int globals = 0; |
| 714 | for (int i = 0; i < length; i++) { |
| 715 | Declaration* decl = declarations->at(i); |
| 716 | Variable* var = decl->proxy()->var(); |
| 717 | Slot* slot = var->slot(); |
| 718 | |
| 719 | // If it was not possible to allocate the variable at compile |
| 720 | // time, we need to "declare" it at runtime to make sure it |
| 721 | // actually exists in the local context. |
| 722 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 723 | VisitDeclaration(decl); |
| 724 | } else { |
| 725 | // Count global variables and functions for later processing |
| 726 | globals++; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Compute array of global variable and function declarations. |
| 731 | // Do nothing in case of no declared global functions or variables. |
| 732 | if (globals > 0) { |
| 733 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 734 | for (int j = 0, i = 0; i < length; i++) { |
| 735 | Declaration* decl = declarations->at(i); |
| 736 | Variable* var = decl->proxy()->var(); |
| 737 | Slot* slot = var->slot(); |
| 738 | |
| 739 | if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) { |
| 740 | array->set(j++, *(var->name())); |
| 741 | if (decl->fun() == NULL) { |
| 742 | if (var->mode() == Variable::CONST) { |
| 743 | // In case this is const property use the hole. |
| 744 | array->set_the_hole(j++); |
| 745 | } else { |
| 746 | array->set_undefined(j++); |
| 747 | } |
| 748 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 749 | Handle<SharedFunctionInfo> function = |
| 750 | Compiler::BuildFunctionInfo(decl->fun(), script(), this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 751 | // Check for stack-overflow exception. |
| 752 | if (HasStackOverflow()) return; |
| 753 | array->set(j++, *function); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | // Invoke the platform-dependent code generator to do the actual |
| 758 | // declaration the global variables and functions. |
| 759 | DeclareGlobals(array); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | |
| 764 | void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) { |
| 765 | if (FLAG_debug_info) { |
| 766 | CodeGenerator::RecordPositions(masm_, fun->start_position()); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | |
| 771 | void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) { |
| 772 | if (FLAG_debug_info) { |
| 773 | CodeGenerator::RecordPositions(masm_, fun->end_position()); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | |
| 778 | void FullCodeGenerator::SetStatementPosition(Statement* stmt) { |
| 779 | if (FLAG_debug_info) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 780 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 781 | if (!Debugger::IsDebuggerActive()) { |
| 782 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
| 783 | } else { |
| 784 | // Check if the statement will be breakable without adding a debug break |
| 785 | // slot. |
| 786 | BreakableStatementChecker checker; |
| 787 | checker.Check(stmt); |
| 788 | // Record the statement position right here if the statement is not |
| 789 | // breakable. For breakable statements the actual recording of the |
| 790 | // position will be postponed to the breakable code (typically an IC). |
| 791 | bool position_recorded = CodeGenerator::RecordPositions( |
| 792 | masm_, stmt->statement_pos(), !checker.is_breakable()); |
| 793 | // If the position recording did record a new position generate a debug |
| 794 | // break slot to make the statement breakable. |
| 795 | if (position_recorded) { |
| 796 | Debug::GenerateSlot(masm_); |
| 797 | } |
| 798 | } |
| 799 | #else |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 800 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 801 | #endif |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | |
| 806 | void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) { |
| 807 | if (FLAG_debug_info) { |
| 808 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 809 | if (!Debugger::IsDebuggerActive()) { |
| 810 | CodeGenerator::RecordPositions(masm_, pos); |
| 811 | } else { |
| 812 | // Check if the expression will be breakable without adding a debug break |
| 813 | // slot. |
| 814 | BreakableStatementChecker checker; |
| 815 | checker.Check(expr); |
| 816 | // Record a statement position right here if the expression is not |
| 817 | // breakable. For breakable expressions the actual recording of the |
| 818 | // position will be postponed to the breakable code (typically an IC). |
| 819 | // NOTE this will record a statement position for something which might |
| 820 | // not be a statement. As stepping in the debugger will only stop at |
| 821 | // statement positions this is used for e.g. the condition expression of |
| 822 | // a do while loop. |
| 823 | bool position_recorded = CodeGenerator::RecordPositions( |
| 824 | masm_, pos, !checker.is_breakable()); |
| 825 | // If the position recording did record a new position generate a debug |
| 826 | // break slot to make the statement breakable. |
| 827 | if (position_recorded) { |
| 828 | Debug::GenerateSlot(masm_); |
| 829 | } |
| 830 | } |
| 831 | #else |
| 832 | CodeGenerator::RecordPositions(masm_, pos); |
| 833 | #endif |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void FullCodeGenerator::SetStatementPosition(int pos) { |
| 839 | if (FLAG_debug_info) { |
| 840 | CodeGenerator::RecordPositions(masm_, pos); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | |
| 845 | void FullCodeGenerator::SetSourcePosition(int pos) { |
| 846 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 847 | masm_->RecordPosition(pos); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 852 | void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) { |
| 853 | Handle<String> name = expr->name(); |
| 854 | if (strcmp("_IsSmi", *name->ToCString()) == 0) { |
| 855 | EmitIsSmi(expr->arguments()); |
| 856 | } else if (strcmp("_IsNonNegativeSmi", *name->ToCString()) == 0) { |
| 857 | EmitIsNonNegativeSmi(expr->arguments()); |
| 858 | } else if (strcmp("_IsObject", *name->ToCString()) == 0) { |
| 859 | EmitIsObject(expr->arguments()); |
| 860 | } else if (strcmp("_IsUndetectableObject", *name->ToCString()) == 0) { |
| 861 | EmitIsUndetectableObject(expr->arguments()); |
| 862 | } else if (strcmp("_IsFunction", *name->ToCString()) == 0) { |
| 863 | EmitIsFunction(expr->arguments()); |
| 864 | } else if (strcmp("_IsArray", *name->ToCString()) == 0) { |
| 865 | EmitIsArray(expr->arguments()); |
| 866 | } else if (strcmp("_IsRegExp", *name->ToCString()) == 0) { |
| 867 | EmitIsRegExp(expr->arguments()); |
| 868 | } else if (strcmp("_IsConstructCall", *name->ToCString()) == 0) { |
| 869 | EmitIsConstructCall(expr->arguments()); |
| 870 | } else if (strcmp("_ObjectEquals", *name->ToCString()) == 0) { |
| 871 | EmitObjectEquals(expr->arguments()); |
| 872 | } else if (strcmp("_Arguments", *name->ToCString()) == 0) { |
| 873 | EmitArguments(expr->arguments()); |
| 874 | } else if (strcmp("_ArgumentsLength", *name->ToCString()) == 0) { |
| 875 | EmitArgumentsLength(expr->arguments()); |
| 876 | } else if (strcmp("_ClassOf", *name->ToCString()) == 0) { |
| 877 | EmitClassOf(expr->arguments()); |
| 878 | } else if (strcmp("_Log", *name->ToCString()) == 0) { |
| 879 | EmitLog(expr->arguments()); |
| 880 | } else if (strcmp("_RandomHeapNumber", *name->ToCString()) == 0) { |
| 881 | EmitRandomHeapNumber(expr->arguments()); |
| 882 | } else if (strcmp("_SubString", *name->ToCString()) == 0) { |
| 883 | EmitSubString(expr->arguments()); |
| 884 | } else if (strcmp("_RegExpExec", *name->ToCString()) == 0) { |
| 885 | EmitRegExpExec(expr->arguments()); |
| 886 | } else if (strcmp("_ValueOf", *name->ToCString()) == 0) { |
| 887 | EmitValueOf(expr->arguments()); |
| 888 | } else if (strcmp("_SetValueOf", *name->ToCString()) == 0) { |
| 889 | EmitSetValueOf(expr->arguments()); |
| 890 | } else if (strcmp("_NumberToString", *name->ToCString()) == 0) { |
| 891 | EmitNumberToString(expr->arguments()); |
| 892 | } else if (strcmp("_StringCharFromCode", *name->ToCString()) == 0) { |
| 893 | EmitStringCharFromCode(expr->arguments()); |
| 894 | } else if (strcmp("_StringCharCodeAt", *name->ToCString()) == 0) { |
| 895 | EmitStringCharCodeAt(expr->arguments()); |
| 896 | } else if (strcmp("_StringCharAt", *name->ToCString()) == 0) { |
| 897 | EmitStringCharAt(expr->arguments()); |
| 898 | } else if (strcmp("_StringAdd", *name->ToCString()) == 0) { |
| 899 | EmitStringAdd(expr->arguments()); |
| 900 | } else if (strcmp("_StringCompare", *name->ToCString()) == 0) { |
| 901 | EmitStringCompare(expr->arguments()); |
| 902 | } else if (strcmp("_MathPow", *name->ToCString()) == 0) { |
| 903 | EmitMathPow(expr->arguments()); |
| 904 | } else if (strcmp("_MathSin", *name->ToCString()) == 0) { |
| 905 | EmitMathSin(expr->arguments()); |
| 906 | } else if (strcmp("_MathCos", *name->ToCString()) == 0) { |
| 907 | EmitMathCos(expr->arguments()); |
| 908 | } else if (strcmp("_MathSqrt", *name->ToCString()) == 0) { |
| 909 | EmitMathSqrt(expr->arguments()); |
| 910 | } else if (strcmp("_CallFunction", *name->ToCString()) == 0) { |
| 911 | EmitCallFunction(expr->arguments()); |
| 912 | } else if (strcmp("_RegExpConstructResult", *name->ToCString()) == 0) { |
| 913 | EmitRegExpConstructResult(expr->arguments()); |
| 914 | } else if (strcmp("_SwapElements", *name->ToCString()) == 0) { |
| 915 | EmitSwapElements(expr->arguments()); |
| 916 | } else if (strcmp("_GetFromCache", *name->ToCString()) == 0) { |
| 917 | EmitGetFromCache(expr->arguments()); |
| 918 | } else { |
| 919 | UNREACHABLE(); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 924 | void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) { |
| 925 | Label eval_right, done; |
| 926 | |
| 927 | // Set up the appropriate context for the left subexpression based |
| 928 | // on the operation and our own context. Initially assume we can |
| 929 | // inherit both true and false labels from our context. |
| 930 | if (expr->op() == Token::OR) { |
| 931 | switch (context_) { |
| 932 | case Expression::kUninitialized: |
| 933 | UNREACHABLE(); |
| 934 | case Expression::kEffect: |
| 935 | VisitForControl(expr->left(), &done, &eval_right); |
| 936 | break; |
| 937 | case Expression::kValue: |
| 938 | VisitForValueControl(expr->left(), |
| 939 | location_, |
| 940 | &done, |
| 941 | &eval_right); |
| 942 | break; |
| 943 | case Expression::kTest: |
| 944 | VisitForControl(expr->left(), true_label_, &eval_right); |
| 945 | break; |
| 946 | case Expression::kValueTest: |
| 947 | VisitForValueControl(expr->left(), |
| 948 | location_, |
| 949 | true_label_, |
| 950 | &eval_right); |
| 951 | break; |
| 952 | case Expression::kTestValue: |
| 953 | VisitForControl(expr->left(), true_label_, &eval_right); |
| 954 | break; |
| 955 | } |
| 956 | } else { |
| 957 | ASSERT_EQ(Token::AND, expr->op()); |
| 958 | switch (context_) { |
| 959 | case Expression::kUninitialized: |
| 960 | UNREACHABLE(); |
| 961 | case Expression::kEffect: |
| 962 | VisitForControl(expr->left(), &eval_right, &done); |
| 963 | break; |
| 964 | case Expression::kValue: |
| 965 | VisitForControlValue(expr->left(), |
| 966 | location_, |
| 967 | &eval_right, |
| 968 | &done); |
| 969 | break; |
| 970 | case Expression::kTest: |
| 971 | VisitForControl(expr->left(), &eval_right, false_label_); |
| 972 | break; |
| 973 | case Expression::kValueTest: |
| 974 | VisitForControl(expr->left(), &eval_right, false_label_); |
| 975 | break; |
| 976 | case Expression::kTestValue: |
| 977 | VisitForControlValue(expr->left(), |
| 978 | location_, |
| 979 | &eval_right, |
| 980 | false_label_); |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | __ bind(&eval_right); |
| 986 | Visit(expr->right()); |
| 987 | |
| 988 | __ bind(&done); |
| 989 | } |
| 990 | |
| 991 | |
| 992 | void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 993 | Comment cmnt(masm_, "[ Block"); |
| 994 | Breakable nested_statement(this, stmt); |
| 995 | SetStatementPosition(stmt); |
| 996 | VisitStatements(stmt->statements()); |
| 997 | __ bind(nested_statement.break_target()); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 1002 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 1003 | SetStatementPosition(stmt); |
| 1004 | VisitForEffect(stmt->expression()); |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 1009 | Comment cmnt(masm_, "[ EmptyStatement"); |
| 1010 | SetStatementPosition(stmt); |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 1015 | Comment cmnt(masm_, "[ IfStatement"); |
| 1016 | SetStatementPosition(stmt); |
| 1017 | Label then_part, else_part, done; |
| 1018 | |
| 1019 | // Do not worry about optimizing for empty then or else bodies. |
| 1020 | VisitForControl(stmt->condition(), &then_part, &else_part); |
| 1021 | |
| 1022 | __ bind(&then_part); |
| 1023 | Visit(stmt->then_statement()); |
| 1024 | __ jmp(&done); |
| 1025 | |
| 1026 | __ bind(&else_part); |
| 1027 | Visit(stmt->else_statement()); |
| 1028 | |
| 1029 | __ bind(&done); |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 1034 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 1035 | SetStatementPosition(stmt); |
| 1036 | NestedStatement* current = nesting_stack_; |
| 1037 | int stack_depth = 0; |
| 1038 | while (!current->IsContinueTarget(stmt->target())) { |
| 1039 | stack_depth = current->Exit(stack_depth); |
| 1040 | current = current->outer(); |
| 1041 | } |
| 1042 | __ Drop(stack_depth); |
| 1043 | |
| 1044 | Iteration* loop = current->AsIteration(); |
| 1045 | __ jmp(loop->continue_target()); |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 1050 | Comment cmnt(masm_, "[ BreakStatement"); |
| 1051 | SetStatementPosition(stmt); |
| 1052 | NestedStatement* current = nesting_stack_; |
| 1053 | int stack_depth = 0; |
| 1054 | while (!current->IsBreakTarget(stmt->target())) { |
| 1055 | stack_depth = current->Exit(stack_depth); |
| 1056 | current = current->outer(); |
| 1057 | } |
| 1058 | __ Drop(stack_depth); |
| 1059 | |
| 1060 | Breakable* target = current->AsBreakable(); |
| 1061 | __ jmp(target->break_target()); |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 1066 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 1067 | SetStatementPosition(stmt); |
| 1068 | Expression* expr = stmt->expression(); |
| 1069 | VisitForValue(expr, kAccumulator); |
| 1070 | |
| 1071 | // Exit all nested statements. |
| 1072 | NestedStatement* current = nesting_stack_; |
| 1073 | int stack_depth = 0; |
| 1074 | while (current != NULL) { |
| 1075 | stack_depth = current->Exit(stack_depth); |
| 1076 | current = current->outer(); |
| 1077 | } |
| 1078 | __ Drop(stack_depth); |
| 1079 | |
| 1080 | EmitReturnSequence(stmt->statement_pos()); |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 1085 | Comment cmnt(masm_, "[ WithEnterStatement"); |
| 1086 | SetStatementPosition(stmt); |
| 1087 | |
| 1088 | VisitForValue(stmt->expression(), kStack); |
| 1089 | if (stmt->is_catch_block()) { |
| 1090 | __ CallRuntime(Runtime::kPushCatchContext, 1); |
| 1091 | } else { |
| 1092 | __ CallRuntime(Runtime::kPushContext, 1); |
| 1093 | } |
| 1094 | // Both runtime calls return the new context in both the context and the |
| 1095 | // result registers. |
| 1096 | |
| 1097 | // Update local stack frame context field. |
| 1098 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) { |
| 1103 | Comment cmnt(masm_, "[ WithExitStatement"); |
| 1104 | SetStatementPosition(stmt); |
| 1105 | |
| 1106 | // Pop context. |
| 1107 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1108 | // Update local stack frame context field. |
| 1109 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 1110 | } |
| 1111 | |
| 1112 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1113 | void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 1114 | Comment cmnt(masm_, "[ DoWhileStatement"); |
| 1115 | SetStatementPosition(stmt); |
| 1116 | Label body, stack_limit_hit, stack_check_success; |
| 1117 | |
| 1118 | Iteration loop_statement(this, stmt); |
| 1119 | increment_loop_depth(); |
| 1120 | |
| 1121 | __ bind(&body); |
| 1122 | Visit(stmt->body()); |
| 1123 | |
| 1124 | // Check stack before looping. |
| 1125 | __ StackLimitCheck(&stack_limit_hit); |
| 1126 | __ bind(&stack_check_success); |
| 1127 | |
| 1128 | __ bind(loop_statement.continue_target()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 1129 | |
| 1130 | // Record the position of the do while condition and make sure it is possible |
| 1131 | // to break on the condition. |
| 1132 | SetExpressionPosition(stmt->cond(), stmt->condition_position()); |
| 1133 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1134 | VisitForControl(stmt->cond(), &body, loop_statement.break_target()); |
| 1135 | |
| 1136 | __ bind(&stack_limit_hit); |
| 1137 | StackCheckStub stack_stub; |
| 1138 | __ CallStub(&stack_stub); |
| 1139 | __ jmp(&stack_check_success); |
| 1140 | |
| 1141 | __ bind(loop_statement.break_target()); |
| 1142 | |
| 1143 | decrement_loop_depth(); |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 1148 | Comment cmnt(masm_, "[ WhileStatement"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1149 | Label body, stack_limit_hit, stack_check_success; |
| 1150 | |
| 1151 | Iteration loop_statement(this, stmt); |
| 1152 | increment_loop_depth(); |
| 1153 | |
| 1154 | // Emit the test at the bottom of the loop. |
| 1155 | __ jmp(loop_statement.continue_target()); |
| 1156 | |
| 1157 | __ bind(&body); |
| 1158 | Visit(stmt->body()); |
| 1159 | |
| 1160 | __ bind(loop_statement.continue_target()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 1161 | // Emit the statement position here as this is where the while statement code |
| 1162 | // starts. |
| 1163 | SetStatementPosition(stmt); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1164 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1165 | // Check stack before looping. |
| 1166 | __ StackLimitCheck(&stack_limit_hit); |
| 1167 | __ bind(&stack_check_success); |
| 1168 | |
| 1169 | VisitForControl(stmt->cond(), &body, loop_statement.break_target()); |
| 1170 | |
| 1171 | __ bind(&stack_limit_hit); |
| 1172 | StackCheckStub stack_stub; |
| 1173 | __ CallStub(&stack_stub); |
| 1174 | __ jmp(&stack_check_success); |
| 1175 | |
| 1176 | __ bind(loop_statement.break_target()); |
| 1177 | decrement_loop_depth(); |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | void FullCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 1182 | Comment cmnt(masm_, "[ ForStatement"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1183 | Label test, body, stack_limit_hit, stack_check_success; |
| 1184 | |
| 1185 | Iteration loop_statement(this, stmt); |
| 1186 | if (stmt->init() != NULL) { |
| 1187 | Visit(stmt->init()); |
| 1188 | } |
| 1189 | |
| 1190 | increment_loop_depth(); |
| 1191 | // Emit the test at the bottom of the loop (even if empty). |
| 1192 | __ jmp(&test); |
| 1193 | |
| 1194 | __ bind(&body); |
| 1195 | Visit(stmt->body()); |
| 1196 | |
| 1197 | __ bind(loop_statement.continue_target()); |
| 1198 | |
| 1199 | SetStatementPosition(stmt); |
| 1200 | if (stmt->next() != NULL) { |
| 1201 | Visit(stmt->next()); |
| 1202 | } |
| 1203 | |
| 1204 | __ bind(&test); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 1205 | // Emit the statement position here as this is where the for statement code |
| 1206 | // starts. |
| 1207 | SetStatementPosition(stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Check stack before looping. |
| 1210 | __ StackLimitCheck(&stack_limit_hit); |
| 1211 | __ bind(&stack_check_success); |
| 1212 | |
| 1213 | if (stmt->cond() != NULL) { |
| 1214 | VisitForControl(stmt->cond(), &body, loop_statement.break_target()); |
| 1215 | } else { |
| 1216 | __ jmp(&body); |
| 1217 | } |
| 1218 | |
| 1219 | __ bind(&stack_limit_hit); |
| 1220 | StackCheckStub stack_stub; |
| 1221 | __ CallStub(&stack_stub); |
| 1222 | __ jmp(&stack_check_success); |
| 1223 | |
| 1224 | __ bind(loop_statement.break_target()); |
| 1225 | decrement_loop_depth(); |
| 1226 | } |
| 1227 | |
| 1228 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1229 | void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1230 | Comment cmnt(masm_, "[ TryCatchStatement"); |
| 1231 | SetStatementPosition(stmt); |
| 1232 | // The try block adds a handler to the exception handler chain |
| 1233 | // before entering, and removes it again when exiting normally. |
| 1234 | // If an exception is thrown during execution of the try block, |
| 1235 | // control is passed to the handler, which also consumes the handler. |
| 1236 | // At this point, the exception is in a register, and store it in |
| 1237 | // the temporary local variable (prints as ".catch-var") before |
| 1238 | // executing the catch block. The catch block has been rewritten |
| 1239 | // to introduce a new scope to bind the catch variable and to remove |
| 1240 | // that scope again afterwards. |
| 1241 | |
| 1242 | Label try_handler_setup, catch_entry, done; |
| 1243 | __ Call(&try_handler_setup); |
| 1244 | // Try handler code, exception in result register. |
| 1245 | |
| 1246 | // Store exception in local .catch variable before executing catch block. |
| 1247 | { |
| 1248 | // The catch variable is *always* a variable proxy for a local variable. |
| 1249 | Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable(); |
| 1250 | ASSERT_NOT_NULL(catch_var); |
| 1251 | Slot* variable_slot = catch_var->slot(); |
| 1252 | ASSERT_NOT_NULL(variable_slot); |
| 1253 | ASSERT_EQ(Slot::LOCAL, variable_slot->type()); |
| 1254 | StoreToFrameField(SlotOffset(variable_slot), result_register()); |
| 1255 | } |
| 1256 | |
| 1257 | Visit(stmt->catch_block()); |
| 1258 | __ jmp(&done); |
| 1259 | |
| 1260 | // Try block code. Sets up the exception handler chain. |
| 1261 | __ bind(&try_handler_setup); |
| 1262 | { |
| 1263 | TryCatch try_block(this, &catch_entry); |
| 1264 | __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER); |
| 1265 | Visit(stmt->try_block()); |
| 1266 | __ PopTryHandler(); |
| 1267 | } |
| 1268 | __ bind(&done); |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1273 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1274 | SetStatementPosition(stmt); |
| 1275 | // Try finally is compiled by setting up a try-handler on the stack while |
| 1276 | // executing the try body, and removing it again afterwards. |
| 1277 | // |
| 1278 | // The try-finally construct can enter the finally block in three ways: |
| 1279 | // 1. By exiting the try-block normally. This removes the try-handler and |
| 1280 | // calls the finally block code before continuing. |
| 1281 | // 2. By exiting the try-block with a function-local control flow transfer |
| 1282 | // (break/continue/return). The site of the, e.g., break removes the |
| 1283 | // try handler and calls the finally block code before continuing |
| 1284 | // its outward control transfer. |
| 1285 | // 3. by exiting the try-block with a thrown exception. |
| 1286 | // This can happen in nested function calls. It traverses the try-handler |
| 1287 | // chain and consumes the try-handler entry before jumping to the |
| 1288 | // handler code. The handler code then calls the finally-block before |
| 1289 | // rethrowing the exception. |
| 1290 | // |
| 1291 | // The finally block must assume a return address on top of the stack |
| 1292 | // (or in the link register on ARM chips) and a value (return value or |
| 1293 | // exception) in the result register (rax/eax/r0), both of which must |
| 1294 | // be preserved. The return address isn't GC-safe, so it should be |
| 1295 | // cooked before GC. |
| 1296 | Label finally_entry; |
| 1297 | Label try_handler_setup; |
| 1298 | |
| 1299 | // Setup the try-handler chain. Use a call to |
| 1300 | // Jump to try-handler setup and try-block code. Use call to put try-handler |
| 1301 | // address on stack. |
| 1302 | __ Call(&try_handler_setup); |
| 1303 | // Try handler code. Return address of call is pushed on handler stack. |
| 1304 | { |
| 1305 | // This code is only executed during stack-handler traversal when an |
| 1306 | // exception is thrown. The execption is in the result register, which |
| 1307 | // is retained by the finally block. |
| 1308 | // Call the finally block and then rethrow the exception. |
| 1309 | __ Call(&finally_entry); |
| 1310 | __ push(result_register()); |
| 1311 | __ CallRuntime(Runtime::kReThrow, 1); |
| 1312 | } |
| 1313 | |
| 1314 | __ bind(&finally_entry); |
| 1315 | { |
| 1316 | // Finally block implementation. |
| 1317 | Finally finally_block(this); |
| 1318 | EnterFinallyBlock(); |
| 1319 | Visit(stmt->finally_block()); |
| 1320 | ExitFinallyBlock(); // Return to the calling code. |
| 1321 | } |
| 1322 | |
| 1323 | __ bind(&try_handler_setup); |
| 1324 | { |
| 1325 | // Setup try handler (stack pointer registers). |
| 1326 | TryFinally try_block(this, &finally_entry); |
| 1327 | __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER); |
| 1328 | Visit(stmt->try_block()); |
| 1329 | __ PopTryHandler(); |
| 1330 | } |
| 1331 | // Execute the finally block on the way out. |
| 1332 | __ Call(&finally_entry); |
| 1333 | } |
| 1334 | |
| 1335 | |
| 1336 | void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1337 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1338 | Comment cmnt(masm_, "[ DebuggerStatement"); |
| 1339 | SetStatementPosition(stmt); |
| 1340 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1341 | __ DebugBreak(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1342 | // Ignore the return value. |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
| 1346 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1347 | void FullCodeGenerator::VisitConditional(Conditional* expr) { |
| 1348 | Comment cmnt(masm_, "[ Conditional"); |
| 1349 | Label true_case, false_case, done; |
| 1350 | VisitForControl(expr->condition(), &true_case, &false_case); |
| 1351 | |
| 1352 | __ bind(&true_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 1353 | SetExpressionPosition(expr->then_expression(), |
| 1354 | expr->then_expression_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1355 | Visit(expr->then_expression()); |
| 1356 | // If control flow falls through Visit, jump to done. |
| 1357 | if (context_ == Expression::kEffect || context_ == Expression::kValue) { |
| 1358 | __ jmp(&done); |
| 1359 | } |
| 1360 | |
| 1361 | __ bind(&false_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame^] | 1362 | SetExpressionPosition(expr->else_expression(), |
| 1363 | expr->else_expression_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1364 | Visit(expr->else_expression()); |
| 1365 | // If control flow falls through Visit, merge it with true case here. |
| 1366 | if (context_ == Expression::kEffect || context_ == Expression::kValue) { |
| 1367 | __ bind(&done); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | void FullCodeGenerator::VisitSlot(Slot* expr) { |
| 1373 | // Slots do not appear directly in the AST. |
| 1374 | UNREACHABLE(); |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | void FullCodeGenerator::VisitLiteral(Literal* expr) { |
| 1379 | Comment cmnt(masm_, "[ Literal"); |
| 1380 | Apply(context_, expr); |
| 1381 | } |
| 1382 | |
| 1383 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1384 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1385 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1386 | |
| 1387 | // Build the function boilerplate and instantiate it. |
| 1388 | Handle<SharedFunctionInfo> function_info = |
| 1389 | Compiler::BuildFunctionInfo(expr, script(), this); |
| 1390 | if (HasStackOverflow()) return; |
| 1391 | EmitNewClosure(function_info); |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | void FullCodeGenerator::VisitSharedFunctionInfoLiteral( |
| 1396 | SharedFunctionInfoLiteral* expr) { |
| 1397 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 1398 | EmitNewClosure(expr->shared_function_info()); |
| 1399 | } |
| 1400 | |
| 1401 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1402 | void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) { |
| 1403 | // Call runtime routine to allocate the catch extension object and |
| 1404 | // assign the exception value to the catch variable. |
| 1405 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
| 1406 | VisitForValue(expr->key(), kStack); |
| 1407 | VisitForValue(expr->value(), kStack); |
| 1408 | // Create catch extension object. |
| 1409 | __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
| 1410 | Apply(context_, result_register()); |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1415 | Comment cmnt(masm_, "[ Throw"); |
| 1416 | VisitForValue(expr->exception(), kStack); |
| 1417 | __ CallRuntime(Runtime::kThrow, 1); |
| 1418 | // Never returns here. |
| 1419 | } |
| 1420 | |
| 1421 | |
| 1422 | int FullCodeGenerator::TryFinally::Exit(int stack_depth) { |
| 1423 | // The macros used here must preserve the result register. |
| 1424 | __ Drop(stack_depth); |
| 1425 | __ PopTryHandler(); |
| 1426 | __ Call(finally_entry_); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | int FullCodeGenerator::TryCatch::Exit(int stack_depth) { |
| 1432 | // The macros used here must preserve the result register. |
| 1433 | __ Drop(stack_depth); |
| 1434 | __ PopTryHandler(); |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | #undef __ |
| 1439 | |
| 1440 | |
| 1441 | } } // namespace v8::internal |