| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/ast.h" | 7 #include "src/ast.h" |
| 8 #include "src/ast-numbering.h" | 8 #include "src/ast-numbering.h" |
| 9 #include "src/code-factory.h" | 9 #include "src/code-factory.h" |
| 10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
| (...skipping 1362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1373 SetStatementPosition(stmt); | 1373 SetStatementPosition(stmt); |
| 1374 // The try block adds a handler to the exception handler chain before | 1374 // The try block adds a handler to the exception handler chain before |
| 1375 // entering, and removes it again when exiting normally. If an exception | 1375 // entering, and removes it again when exiting normally. If an exception |
| 1376 // is thrown during execution of the try block, the handler is consumed | 1376 // is thrown during execution of the try block, the handler is consumed |
| 1377 // and control is passed to the catch block with the exception in the | 1377 // and control is passed to the catch block with the exception in the |
| 1378 // result register. | 1378 // result register. |
| 1379 | 1379 |
| 1380 Label try_entry, handler_entry, exit; | 1380 Label try_entry, handler_entry, exit; |
| 1381 __ jmp(&try_entry); | 1381 __ jmp(&try_entry); |
| 1382 __ bind(&handler_entry); | 1382 __ bind(&handler_entry); |
| 1383 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); | |
| 1384 // Exception handler code, the exception is in the result register. | 1383 // Exception handler code, the exception is in the result register. |
| 1385 // Extend the context before executing the catch block. | 1384 // Extend the context before executing the catch block. |
| 1386 { Comment cmnt(masm_, "[ Extend catch context"); | 1385 { Comment cmnt(masm_, "[ Extend catch context"); |
| 1387 __ Push(stmt->variable()->name()); | 1386 __ Push(stmt->variable()->name()); |
| 1388 __ Push(result_register()); | 1387 __ Push(result_register()); |
| 1389 PushFunctionArgumentForContextAllocation(); | 1388 PushFunctionArgumentForContextAllocation(); |
| 1390 __ CallRuntime(Runtime::kPushCatchContext, 3); | 1389 __ CallRuntime(Runtime::kPushCatchContext, 3); |
| 1391 StoreToFrameField(StandardFrameConstants::kContextOffset, | 1390 StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1392 context_register()); | 1391 context_register()); |
| 1393 } | 1392 } |
| 1394 | 1393 |
| 1395 Scope* saved_scope = scope(); | 1394 Scope* saved_scope = scope(); |
| 1396 scope_ = stmt->scope(); | 1395 scope_ = stmt->scope(); |
| 1397 DCHECK(scope_->declarations()->is_empty()); | 1396 DCHECK(scope_->declarations()->is_empty()); |
| 1398 { WithOrCatch catch_body(this); | 1397 { WithOrCatch catch_body(this); |
| 1399 Visit(stmt->catch_block()); | 1398 Visit(stmt->catch_block()); |
| 1400 } | 1399 } |
| 1401 // Restore the context. | 1400 // Restore the context. |
| 1402 LoadContextField(context_register(), Context::PREVIOUS_INDEX); | 1401 LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1403 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); | 1402 StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 1404 scope_ = saved_scope; | 1403 scope_ = saved_scope; |
| 1405 __ jmp(&exit); | 1404 __ jmp(&exit); |
| 1406 | 1405 |
| 1407 // Try block code. Sets up the exception handler chain. | 1406 // Try block code. Sets up the exception handler chain. |
| 1408 __ bind(&try_entry); | 1407 __ bind(&try_entry); |
| 1409 __ PushTryHandler(StackHandler::CATCH, stmt->index()); | 1408 EnterTryBlock(stmt->index(), &handler_entry); |
| 1410 { TryCatch try_body(this); | 1409 { TryCatch try_body(this); |
| 1411 Visit(stmt->try_block()); | 1410 Visit(stmt->try_block()); |
| 1412 } | 1411 } |
| 1413 __ PopTryHandler(); | 1412 ExitTryBlock(stmt->index()); |
| 1414 __ bind(&exit); | 1413 __ bind(&exit); |
| 1415 } | 1414 } |
| 1416 | 1415 |
| 1417 | 1416 |
| 1418 void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { | 1417 void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1419 Comment cmnt(masm_, "[ TryFinallyStatement"); | 1418 Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1420 SetStatementPosition(stmt); | 1419 SetStatementPosition(stmt); |
| 1421 // Try finally is compiled by setting up a try-handler on the stack while | 1420 // Try finally is compiled by setting up a try-handler on the stack while |
| 1422 // executing the try body, and removing it again afterwards. | 1421 // executing the try body, and removing it again afterwards. |
| 1423 // | 1422 // |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1437 // The finally block must assume a return address on top of the stack | 1436 // The finally block must assume a return address on top of the stack |
| 1438 // (or in the link register on ARM chips) and a value (return value or | 1437 // (or in the link register on ARM chips) and a value (return value or |
| 1439 // exception) in the result register (rax/eax/r0), both of which must | 1438 // exception) in the result register (rax/eax/r0), both of which must |
| 1440 // be preserved. The return address isn't GC-safe, so it should be | 1439 // be preserved. The return address isn't GC-safe, so it should be |
| 1441 // cooked before GC. | 1440 // cooked before GC. |
| 1442 Label try_entry, handler_entry, finally_entry; | 1441 Label try_entry, handler_entry, finally_entry; |
| 1443 | 1442 |
| 1444 // Jump to try-handler setup and try-block code. | 1443 // Jump to try-handler setup and try-block code. |
| 1445 __ jmp(&try_entry); | 1444 __ jmp(&try_entry); |
| 1446 __ bind(&handler_entry); | 1445 __ bind(&handler_entry); |
| 1447 handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); | |
| 1448 // Exception handler code. This code is only executed when an exception | 1446 // Exception handler code. This code is only executed when an exception |
| 1449 // is thrown. The exception is in the result register, and must be | 1447 // is thrown. The exception is in the result register, and must be |
| 1450 // preserved by the finally block. Call the finally block and then | 1448 // preserved by the finally block. Call the finally block and then |
| 1451 // rethrow the exception if it returns. | 1449 // rethrow the exception if it returns. |
| 1452 __ Call(&finally_entry); | 1450 __ Call(&finally_entry); |
| 1453 __ Push(result_register()); | 1451 __ Push(result_register()); |
| 1454 __ CallRuntime(Runtime::kReThrow, 1); | 1452 __ CallRuntime(Runtime::kReThrow, 1); |
| 1455 | 1453 |
| 1456 // Finally block implementation. | 1454 // Finally block implementation. |
| 1457 __ bind(&finally_entry); | 1455 __ bind(&finally_entry); |
| 1458 EnterFinallyBlock(); | 1456 EnterFinallyBlock(); |
| 1459 { Finally finally_body(this); | 1457 { Finally finally_body(this); |
| 1460 Visit(stmt->finally_block()); | 1458 Visit(stmt->finally_block()); |
| 1461 } | 1459 } |
| 1462 ExitFinallyBlock(); // Return to the calling code. | 1460 ExitFinallyBlock(); // Return to the calling code. |
| 1463 | 1461 |
| 1464 // Set up try handler. | 1462 // Set up try handler. |
| 1465 __ bind(&try_entry); | 1463 __ bind(&try_entry); |
| 1466 __ PushTryHandler(StackHandler::FINALLY, stmt->index()); | 1464 EnterTryBlock(stmt->index(), &handler_entry); |
| 1467 { TryFinally try_body(this, &finally_entry); | 1465 { TryFinally try_body(this, &finally_entry); |
| 1468 Visit(stmt->try_block()); | 1466 Visit(stmt->try_block()); |
| 1469 } | 1467 } |
| 1470 __ PopTryHandler(); | 1468 ExitTryBlock(stmt->index()); |
| 1471 // Execute the finally block on the way out. Clobber the unpredictable | 1469 // Execute the finally block on the way out. Clobber the unpredictable |
| 1472 // value in the result register with one that's safe for GC because the | 1470 // value in the result register with one that's safe for GC because the |
| 1473 // finally block will unconditionally preserve the result register on the | 1471 // finally block will unconditionally preserve the result register on the |
| 1474 // stack. | 1472 // stack. |
| 1475 ClearAccumulator(); | 1473 ClearAccumulator(); |
| 1476 __ Call(&finally_entry); | 1474 __ Call(&finally_entry); |
| 1477 } | 1475 } |
| 1478 | 1476 |
| 1479 | 1477 |
| 1480 void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { | 1478 void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1615 | 1613 |
| 1616 | 1614 |
| 1617 void FullCodeGenerator::VisitThrow(Throw* expr) { | 1615 void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1618 Comment cmnt(masm_, "[ Throw"); | 1616 Comment cmnt(masm_, "[ Throw"); |
| 1619 VisitForStackValue(expr->exception()); | 1617 VisitForStackValue(expr->exception()); |
| 1620 __ CallRuntime(Runtime::kThrow, 1); | 1618 __ CallRuntime(Runtime::kThrow, 1); |
| 1621 // Never returns here. | 1619 // Never returns here. |
| 1622 } | 1620 } |
| 1623 | 1621 |
| 1624 | 1622 |
| 1625 FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit( | 1623 void FullCodeGenerator::EnterTryBlock(int index, Label* handler) { |
| 1626 int* stack_depth, | 1624 handler_table()->SetRangeStart(index, masm()->pc_offset()); |
| 1627 int* context_length) { | 1625 handler_table()->SetRangeHandler(index, handler->pos()); |
| 1626 |
| 1627 // Determine expression stack depth of try statement. |
| 1628 int stack_depth = info_->scope()->num_stack_slots(); // Include stack locals. |
| 1629 for (NestedStatement* current = nesting_stack_; current != NULL; /*nop*/) { |
| 1630 current = current->AccumulateDepth(&stack_depth); |
| 1631 } |
| 1632 handler_table()->SetRangeDepth(index, stack_depth); |
| 1633 |
| 1634 // Push context onto operand stack. |
| 1635 STATIC_ASSERT(TryBlockConstant::kElementCount == 1); |
| 1636 __ Push(context_register()); |
| 1637 } |
| 1638 |
| 1639 |
| 1640 void FullCodeGenerator::ExitTryBlock(int index) { |
| 1641 handler_table()->SetRangeEnd(index, masm()->pc_offset()); |
| 1642 |
| 1643 // Drop context from operand stack. |
| 1644 __ Drop(TryBlockConstant::kElementCount); |
| 1645 } |
| 1646 |
| 1647 |
| 1648 FullCodeGenerator::NestedStatement* FullCodeGenerator::TryFinally::Exit( |
| 1649 int* stack_depth, int* context_length) { |
| 1628 // The macros used here must preserve the result register. | 1650 // The macros used here must preserve the result register. |
| 1629 __ Drop(*stack_depth); | 1651 |
| 1630 __ PopTryHandler(); | 1652 // Because the handler block contains the context of the finally |
| 1653 // code, we can restore it directly from there for the finally code |
| 1654 // rather than iteratively unwinding contexts via their previous |
| 1655 // links. |
| 1656 if (*context_length > 0) { |
| 1657 __ Drop(*stack_depth); // Down to the handler block. |
| 1658 // Restore the context to its dedicated register and the stack. |
| 1659 STATIC_ASSERT(TryFinally::kElementCount == 1); |
| 1660 __ Pop(codegen_->context_register()); |
| 1661 codegen_->StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1662 codegen_->context_register()); |
| 1663 } else { |
| 1664 // Down to the handler block and also drop context. |
| 1665 __ Drop(*stack_depth + kElementCount); |
| 1666 } |
| 1667 __ Call(finally_entry_); |
| 1668 |
| 1631 *stack_depth = 0; | 1669 *stack_depth = 0; |
| 1670 *context_length = 0; |
| 1632 return previous_; | 1671 return previous_; |
| 1633 } | 1672 } |
| 1634 | 1673 |
| 1635 | 1674 |
| 1636 bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) { | 1675 bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) { |
| 1637 Expression* sub_expr; | 1676 Expression* sub_expr; |
| 1638 Handle<String> check; | 1677 Handle<String> check; |
| 1639 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) { | 1678 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) { |
| 1640 EmitLiteralCompareTypeof(expr, sub_expr, check); | 1679 EmitLiteralCompareTypeof(expr, sub_expr, check); |
| 1641 return true; | 1680 return true; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1784 } | 1823 } |
| 1785 codegen_->PrepareForBailoutForId(exit_id_, NO_REGISTERS); | 1824 codegen_->PrepareForBailoutForId(exit_id_, NO_REGISTERS); |
| 1786 codegen_->scope_ = saved_scope_; | 1825 codegen_->scope_ = saved_scope_; |
| 1787 } | 1826 } |
| 1788 | 1827 |
| 1789 | 1828 |
| 1790 #undef __ | 1829 #undef __ |
| 1791 | 1830 |
| 1792 | 1831 |
| 1793 } } // namespace v8::internal | 1832 } } // namespace v8::internal |
| OLD | NEW |