OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, | 73 CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script, |
74 bool is_eval) | 74 bool is_eval) |
75 : is_eval_(is_eval), | 75 : is_eval_(is_eval), |
76 script_(script), | 76 script_(script), |
77 deferred_(8), | 77 deferred_(8), |
78 masm_(new MacroAssembler(NULL, buffer_size)), | 78 masm_(new MacroAssembler(NULL, buffer_size)), |
79 scope_(NULL), | 79 scope_(NULL), |
80 frame_(NULL), | 80 frame_(NULL), |
81 allocator_(NULL), | 81 allocator_(NULL), |
82 state_(NULL), | 82 state_(NULL), |
83 break_stack_height_(0), | |
84 loop_nesting_(0), | 83 loop_nesting_(0), |
85 function_return_is_shadowed_(false), | 84 function_return_is_shadowed_(false), |
86 in_spilled_code_(false) { | 85 in_spilled_code_(false) { |
87 } | 86 } |
88 | 87 |
89 | 88 |
90 // Calling conventions: | 89 // Calling conventions: |
91 // ebp: caller's frame pointer | 90 // ebp: caller's frame pointer |
92 // esp: stack pointer | 91 // esp: stack pointer |
93 // edi: called JS function | 92 // edi: called JS function |
94 // esi: callee's context | 93 // esi: callee's context |
95 | 94 |
96 void CodeGenerator::GenCode(FunctionLiteral* fun) { | 95 void CodeGenerator::GenCode(FunctionLiteral* fun) { |
97 // Record the position for debugging purposes. | 96 // Record the position for debugging purposes. |
98 CodeForFunctionPosition(fun); | 97 CodeForFunctionPosition(fun); |
99 | 98 |
100 ZoneList<Statement*>* body = fun->body(); | 99 ZoneList<Statement*>* body = fun->body(); |
101 | 100 |
102 // Initialize state. | 101 // Initialize state. |
103 ASSERT(scope_ == NULL); | 102 ASSERT(scope_ == NULL); |
104 scope_ = fun->scope(); | 103 scope_ = fun->scope(); |
105 ASSERT(allocator_ == NULL); | 104 ASSERT(allocator_ == NULL); |
106 RegisterAllocator register_allocator(this); | 105 RegisterAllocator register_allocator(this); |
107 allocator_ = ®ister_allocator; | 106 allocator_ = ®ister_allocator; |
108 ASSERT(frame_ == NULL); | 107 ASSERT(frame_ == NULL); |
109 frame_ = new VirtualFrame(this); | 108 frame_ = new VirtualFrame(this); |
110 function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL); | |
111 function_return_is_shadowed_ = false; | |
112 set_in_spilled_code(false); | 109 set_in_spilled_code(false); |
113 | 110 |
114 // Adjust for function-level loop nesting. | 111 // Adjust for function-level loop nesting. |
115 loop_nesting_ += fun->loop_nesting(); | 112 loop_nesting_ += fun->loop_nesting(); |
116 | 113 |
117 { | 114 { |
118 CodeGenState state(this); | 115 CodeGenState state(this); |
119 | 116 |
120 // Entry: | 117 // Entry: |
121 // Stack: receiver, arguments, return address. | 118 // Stack: receiver, arguments, return address. |
122 // ebp: caller's frame pointer | 119 // ebp: caller's frame pointer |
123 // esp: stack pointer | 120 // esp: stack pointer |
124 // edi: called JS function | 121 // edi: called JS function |
125 // esi: callee's context | 122 // esi: callee's context |
126 allocator_->Initialize(); | 123 allocator_->Initialize(); |
127 frame_->Enter(); | 124 frame_->Enter(); |
128 // tos: code slot | 125 |
129 #ifdef DEBUG | 126 #ifdef DEBUG |
130 if (strlen(FLAG_stop_at) > 0 && | 127 if (strlen(FLAG_stop_at) > 0 && |
131 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { | 128 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) { |
132 frame_->SpillAll(); | 129 frame_->SpillAll(); |
133 __ int3(); | 130 __ int3(); |
134 } | 131 } |
135 #endif | 132 #endif |
136 | 133 |
137 // Allocate space for locals and initialize them. | 134 // Allocate space for locals and initialize them. |
138 frame_->AllocateStackSlots(scope_->num_stack_slots()); | 135 frame_->AllocateStackSlots(scope_->num_stack_slots()); |
| 136 // Initialize the function return target after the locals are set |
| 137 // up, because it needs the expected frame height from the frame. |
| 138 function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL); |
| 139 function_return_is_shadowed_ = false; |
139 | 140 |
140 // Allocate the arguments object and copy the parameters into it. | 141 // Allocate the arguments object and copy the parameters into it. |
141 if (scope_->arguments() != NULL) { | 142 if (scope_->arguments() != NULL) { |
142 ASSERT(scope_->arguments_shadow() != NULL); | 143 ASSERT(scope_->arguments_shadow() != NULL); |
143 Comment cmnt(masm_, "[ Allocate arguments object"); | 144 Comment cmnt(masm_, "[ Allocate arguments object"); |
144 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); | 145 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT); |
145 frame_->PushFunction(); | 146 frame_->PushFunction(); |
146 frame_->PushReceiverSlotAddress(); | 147 frame_->PushReceiverSlotAddress(); |
147 frame_->Push(Smi::FromInt(scope_->num_parameters())); | 148 frame_->Push(Smi::FromInt(scope_->num_parameters())); |
148 Result answer = frame_->CallStub(&stub, 3); | 149 Result answer = frame_->CallStub(&stub, 3); |
(...skipping 1405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1554 for (int i = 0; has_valid_frame() && i < statements->length(); i++) { | 1555 for (int i = 0; has_valid_frame() && i < statements->length(); i++) { |
1555 Visit(statements->at(i)); | 1556 Visit(statements->at(i)); |
1556 } | 1557 } |
1557 } | 1558 } |
1558 | 1559 |
1559 | 1560 |
1560 void CodeGenerator::VisitBlock(Block* node) { | 1561 void CodeGenerator::VisitBlock(Block* node) { |
1561 ASSERT(!in_spilled_code()); | 1562 ASSERT(!in_spilled_code()); |
1562 Comment cmnt(masm_, "[ Block"); | 1563 Comment cmnt(masm_, "[ Block"); |
1563 CodeForStatementPosition(node); | 1564 CodeForStatementPosition(node); |
1564 node->set_break_stack_height(break_stack_height_); | |
1565 node->break_target()->Initialize(this); | 1565 node->break_target()->Initialize(this); |
1566 VisitStatements(node->statements()); | 1566 VisitStatements(node->statements()); |
1567 if (node->break_target()->is_linked()) { | 1567 if (node->break_target()->is_linked()) { |
1568 node->break_target()->Bind(); | 1568 node->break_target()->Bind(); |
1569 } | 1569 } |
1570 } | 1570 } |
1571 | 1571 |
1572 | 1572 |
1573 void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { | 1573 void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) { |
1574 frame_->Push(pairs); | 1574 frame_->Push(pairs); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1756 frame_->Drop(); | 1756 frame_->Drop(); |
1757 } | 1757 } |
1758 } | 1758 } |
1759 | 1759 |
1760 if (exit.is_linked()) { | 1760 if (exit.is_linked()) { |
1761 exit.Bind(); | 1761 exit.Bind(); |
1762 } | 1762 } |
1763 } | 1763 } |
1764 | 1764 |
1765 | 1765 |
1766 void CodeGenerator::CleanStack(int num_bytes) { | |
1767 ASSERT(num_bytes % kPointerSize == 0); | |
1768 frame_->Drop(num_bytes / kPointerSize); | |
1769 } | |
1770 | |
1771 | |
1772 void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { | 1766 void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { |
1773 ASSERT(!in_spilled_code()); | 1767 ASSERT(!in_spilled_code()); |
1774 Comment cmnt(masm_, "[ ContinueStatement"); | 1768 Comment cmnt(masm_, "[ ContinueStatement"); |
1775 CodeForStatementPosition(node); | 1769 CodeForStatementPosition(node); |
1776 CleanStack(break_stack_height_ - node->target()->break_stack_height()); | |
1777 node->target()->continue_target()->Jump(); | 1770 node->target()->continue_target()->Jump(); |
1778 } | 1771 } |
1779 | 1772 |
1780 | 1773 |
1781 void CodeGenerator::VisitBreakStatement(BreakStatement* node) { | 1774 void CodeGenerator::VisitBreakStatement(BreakStatement* node) { |
1782 ASSERT(!in_spilled_code()); | 1775 ASSERT(!in_spilled_code()); |
1783 Comment cmnt(masm_, "[ BreakStatement"); | 1776 Comment cmnt(masm_, "[ BreakStatement"); |
1784 CodeForStatementPosition(node); | 1777 CodeForStatementPosition(node); |
1785 CleanStack(break_stack_height_ - node->target()->break_stack_height()); | |
1786 node->target()->break_target()->Jump(); | 1778 node->target()->break_target()->Jump(); |
1787 } | 1779 } |
1788 | 1780 |
1789 | 1781 |
1790 void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { | 1782 void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { |
1791 ASSERT(!in_spilled_code()); | 1783 ASSERT(!in_spilled_code()); |
1792 Comment cmnt(masm_, "[ ReturnStatement"); | 1784 Comment cmnt(masm_, "[ ReturnStatement"); |
1793 | 1785 |
1794 if (function_return_is_shadowed_) { | 1786 if (function_return_is_shadowed_) { |
1795 // If the function return is shadowed, we spill all information | 1787 // If the function return is shadowed, we spill all information |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2013 } | 2005 } |
2014 | 2006 |
2015 delete start_frame; | 2007 delete start_frame; |
2016 } | 2008 } |
2017 | 2009 |
2018 | 2010 |
2019 void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { | 2011 void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { |
2020 ASSERT(!in_spilled_code()); | 2012 ASSERT(!in_spilled_code()); |
2021 Comment cmnt(masm_, "[ SwitchStatement"); | 2013 Comment cmnt(masm_, "[ SwitchStatement"); |
2022 CodeForStatementPosition(node); | 2014 CodeForStatementPosition(node); |
2023 node->set_break_stack_height(break_stack_height_); | |
2024 node->break_target()->Initialize(this); | 2015 node->break_target()->Initialize(this); |
2025 | 2016 |
2026 // Compile the switch value. | 2017 // Compile the switch value. |
2027 Load(node->tag()); | 2018 Load(node->tag()); |
2028 | 2019 |
2029 if (TryGenerateFastCaseSwitchStatement(node)) { | 2020 if (TryGenerateFastCaseSwitchStatement(node)) { |
2030 return; | 2021 return; |
2031 } | 2022 } |
2032 | 2023 |
2033 ZoneList<CaseClause*>* cases = node->cases(); | 2024 ZoneList<CaseClause*>* cases = node->cases(); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2141 if (node->break_target()->is_linked()) { | 2132 if (node->break_target()->is_linked()) { |
2142 node->break_target()->Bind(); | 2133 node->break_target()->Bind(); |
2143 } | 2134 } |
2144 } | 2135 } |
2145 | 2136 |
2146 | 2137 |
2147 void CodeGenerator::VisitLoopStatement(LoopStatement* node) { | 2138 void CodeGenerator::VisitLoopStatement(LoopStatement* node) { |
2148 ASSERT(!in_spilled_code()); | 2139 ASSERT(!in_spilled_code()); |
2149 Comment cmnt(masm_, "[ LoopStatement"); | 2140 Comment cmnt(masm_, "[ LoopStatement"); |
2150 CodeForStatementPosition(node); | 2141 CodeForStatementPosition(node); |
2151 node->set_break_stack_height(break_stack_height_); | |
2152 node->break_target()->Initialize(this); | 2142 node->break_target()->Initialize(this); |
2153 | 2143 |
2154 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a | 2144 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a |
2155 // known result for the test expression, with no side effects. | 2145 // known result for the test expression, with no side effects. |
2156 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; | 2146 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW; |
2157 if (node->cond() == NULL) { | 2147 if (node->cond() == NULL) { |
2158 ASSERT(node->type() == LoopStatement::FOR_LOOP); | 2148 ASSERT(node->type() == LoopStatement::FOR_LOOP); |
2159 info = ALWAYS_TRUE; | 2149 info = ALWAYS_TRUE; |
2160 } else { | 2150 } else { |
2161 Literal* lit = node->cond()->AsLiteral(); | 2151 Literal* lit = node->cond()->AsLiteral(); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2464 DecrementLoopNesting(); | 2454 DecrementLoopNesting(); |
2465 } | 2455 } |
2466 | 2456 |
2467 | 2457 |
2468 void CodeGenerator::VisitForInStatement(ForInStatement* node) { | 2458 void CodeGenerator::VisitForInStatement(ForInStatement* node) { |
2469 ASSERT(!in_spilled_code()); | 2459 ASSERT(!in_spilled_code()); |
2470 VirtualFrame::SpilledScope spilled_scope(this); | 2460 VirtualFrame::SpilledScope spilled_scope(this); |
2471 Comment cmnt(masm_, "[ ForInStatement"); | 2461 Comment cmnt(masm_, "[ ForInStatement"); |
2472 CodeForStatementPosition(node); | 2462 CodeForStatementPosition(node); |
2473 | 2463 |
2474 // We keep stuff on the stack while the body is executing. | |
2475 // Record it, so that a break/continue crossing this statement | |
2476 // can restore the stack. | |
2477 const int kForInStackSize = 5 * kPointerSize; | |
2478 break_stack_height_ += kForInStackSize; | |
2479 node->set_break_stack_height(break_stack_height_); | |
2480 node->break_target()->Initialize(this); | |
2481 node->continue_target()->Initialize(this); | |
2482 | |
2483 JumpTarget primitive(this); | 2464 JumpTarget primitive(this); |
2484 JumpTarget jsobject(this); | 2465 JumpTarget jsobject(this); |
2485 JumpTarget fixed_array(this); | 2466 JumpTarget fixed_array(this); |
2486 JumpTarget entry(this, JumpTarget::BIDIRECTIONAL); | 2467 JumpTarget entry(this, JumpTarget::BIDIRECTIONAL); |
2487 JumpTarget end_del_check(this); | 2468 JumpTarget end_del_check(this); |
2488 JumpTarget exit(this); | 2469 JumpTarget exit(this); |
2489 | 2470 |
2490 // Get the object to enumerate over (converted to JSObject). | 2471 // Get the object to enumerate over (converted to JSObject). |
2491 LoadAndSpill(node->enumerable()); | 2472 LoadAndSpill(node->enumerable()); |
2492 | 2473 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2561 frame_->EmitPush(eax); // <- slot 2 | 2542 frame_->EmitPush(eax); // <- slot 2 |
2562 | 2543 |
2563 // Push the length of the array and the initial index onto the stack. | 2544 // Push the length of the array and the initial index onto the stack. |
2564 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset)); | 2545 __ mov(eax, FieldOperand(eax, FixedArray::kLengthOffset)); |
2565 __ shl(eax, kSmiTagSize); | 2546 __ shl(eax, kSmiTagSize); |
2566 frame_->EmitPush(eax); // <- slot 1 | 2547 frame_->EmitPush(eax); // <- slot 1 |
2567 frame_->EmitPush(Immediate(Smi::FromInt(0))); // <- slot 0 | 2548 frame_->EmitPush(Immediate(Smi::FromInt(0))); // <- slot 0 |
2568 | 2549 |
2569 // Condition. | 2550 // Condition. |
2570 entry.Bind(); | 2551 entry.Bind(); |
| 2552 // Grab the current frame's height for the break and continue |
| 2553 // targets only after all the state is pushed on the frame. |
| 2554 node->break_target()->Initialize(this); |
| 2555 node->continue_target()->Initialize(this); |
| 2556 |
2571 __ mov(eax, frame_->ElementAt(0)); // load the current count | 2557 __ mov(eax, frame_->ElementAt(0)); // load the current count |
2572 __ cmp(eax, frame_->ElementAt(1)); // compare to the array length | 2558 __ cmp(eax, frame_->ElementAt(1)); // compare to the array length |
2573 node->break_target()->Branch(above_equal); | 2559 node->break_target()->Branch(above_equal); |
2574 | 2560 |
2575 // Get the i'th entry of the array. | 2561 // Get the i'th entry of the array. |
2576 __ mov(edx, frame_->ElementAt(2)); | 2562 __ mov(edx, frame_->ElementAt(2)); |
2577 __ mov(ebx, Operand(edx, eax, times_2, | 2563 __ mov(ebx, Operand(edx, eax, times_2, |
2578 FixedArray::kHeaderSize - kHeapObjectTag)); | 2564 FixedArray::kHeaderSize - kHeapObjectTag)); |
2579 | 2565 |
2580 // Get the expected map from the stack or a zero map in the | 2566 // Get the expected map from the stack or a zero map in the |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2643 __ add(Operand(eax), Immediate(Smi::FromInt(1))); | 2629 __ add(Operand(eax), Immediate(Smi::FromInt(1))); |
2644 frame_->EmitPush(eax); | 2630 frame_->EmitPush(eax); |
2645 entry.Jump(); | 2631 entry.Jump(); |
2646 | 2632 |
2647 // Cleanup. | 2633 // Cleanup. |
2648 node->break_target()->Bind(); | 2634 node->break_target()->Bind(); |
2649 frame_->Drop(5); | 2635 frame_->Drop(5); |
2650 | 2636 |
2651 // Exit. | 2637 // Exit. |
2652 exit.Bind(); | 2638 exit.Bind(); |
2653 | |
2654 break_stack_height_ -= kForInStackSize; | |
2655 } | 2639 } |
2656 | 2640 |
2657 | 2641 |
2658 void CodeGenerator::VisitTryCatch(TryCatch* node) { | 2642 void CodeGenerator::VisitTryCatch(TryCatch* node) { |
2659 ASSERT(!in_spilled_code()); | 2643 ASSERT(!in_spilled_code()); |
2660 VirtualFrame::SpilledScope spilled_scope(this); | 2644 VirtualFrame::SpilledScope spilled_scope(this); |
2661 Comment cmnt(masm_, "[ TryCatch"); | 2645 Comment cmnt(masm_, "[ TryCatch"); |
2662 CodeForStatementPosition(node); | 2646 CodeForStatementPosition(node); |
2663 | 2647 |
2664 JumpTarget try_block(this); | 2648 JumpTarget try_block(this); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2910 } | 2894 } |
2911 } | 2895 } |
2912 | 2896 |
2913 // --- Finally block --- | 2897 // --- Finally block --- |
2914 finally_block.Bind(); | 2898 finally_block.Bind(); |
2915 | 2899 |
2916 // Push the state on the stack. | 2900 // Push the state on the stack. |
2917 frame_->EmitPush(ecx); | 2901 frame_->EmitPush(ecx); |
2918 | 2902 |
2919 // We keep two elements on the stack - the (possibly faked) result | 2903 // We keep two elements on the stack - the (possibly faked) result |
2920 // and the state - while evaluating the finally block. Record it, so | 2904 // and the state - while evaluating the finally block. |
2921 // that a break/continue crossing this statement can restore the | 2905 // |
2922 // stack. | |
2923 const int kFinallyStackSize = 2 * kPointerSize; | |
2924 break_stack_height_ += kFinallyStackSize; | |
2925 | |
2926 // Generate code for the statements in the finally block. | 2906 // Generate code for the statements in the finally block. |
2927 VisitStatementsAndSpill(node->finally_block()->statements()); | 2907 VisitStatementsAndSpill(node->finally_block()->statements()); |
2928 | 2908 |
2929 break_stack_height_ -= kFinallyStackSize; | |
2930 if (has_valid_frame()) { | 2909 if (has_valid_frame()) { |
2931 JumpTarget exit(this); | 2910 JumpTarget exit(this); |
2932 // Restore state and return value or faked TOS. | 2911 // Restore state and return value or faked TOS. |
2933 frame_->EmitPop(ecx); | 2912 frame_->EmitPop(ecx); |
2934 frame_->EmitPop(eax); | 2913 frame_->EmitPop(eax); |
2935 | 2914 |
2936 // Generate code to jump to the right destination for all used | 2915 // Generate code to jump to the right destination for all used |
2937 // formerly shadowing targets. | 2916 // formerly shadowing targets. |
2938 for (int i = 0; i <= nof_escapes; i++) { | 2917 for (int i = 0; i <= nof_escapes; i++) { |
2939 if (shadows[i]->is_bound()) { | 2918 if (shadows[i]->is_bound()) { |
(...skipping 3999 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6939 | 6918 |
6940 // Slow-case: Go through the JavaScript implementation. | 6919 // Slow-case: Go through the JavaScript implementation. |
6941 __ bind(&slow); | 6920 __ bind(&slow); |
6942 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); | 6921 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION); |
6943 } | 6922 } |
6944 | 6923 |
6945 | 6924 |
6946 #undef __ | 6925 #undef __ |
6947 | 6926 |
6948 } } // namespace v8::internal | 6927 } } // namespace v8::internal |
OLD | NEW |