OLD | NEW |
(Empty) | |
| 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 "debug.h" |
| 32 #include "fast-codegen.h" |
| 33 |
| 34 namespace v8 { |
| 35 namespace internal { |
| 36 |
| 37 #define __ ACCESS_MASM(masm_) |
| 38 |
| 39 // Generate code for a JS function. On entry to the function the receiver |
| 40 // and arguments have been pushed on the stack left to right, with the |
| 41 // return address on top of them. The actual argument count matches the |
| 42 // formal parameter count expected by the function. |
| 43 // |
| 44 // The live registers are: |
| 45 // o rdi: the JS function object being called (ie, ourselves) |
| 46 // o rsi: our context |
| 47 // o rbp: our caller's frame pointer |
| 48 // o rsp: stack pointer (pointing to return address) |
| 49 // |
| 50 // The function builds a JS frame. Please see JavaScriptFrameConstants in |
| 51 // frames-x64.h for its layout. |
| 52 void FastCodeGenerator::Generate(FunctionLiteral* fun) { |
| 53 function_ = fun; |
| 54 |
| 55 __ push(rbp); // Caller's frame pointer. |
| 56 __ movq(rbp, rsp); |
| 57 __ push(rsi); // Callee's context. |
| 58 __ push(rdi); // Callee's JS Function. |
| 59 |
| 60 { Comment cmnt(masm_, "[ Allocate locals"); |
| 61 int locals_count = fun->scope()->num_stack_slots(); |
| 62 for (int i = 0; i < locals_count; i++) { |
| 63 __ PushRoot(Heap::kUndefinedValueRootIndex); |
| 64 } |
| 65 } |
| 66 |
| 67 { Comment cmnt(masm_, "[ Stack check"); |
| 68 Label ok; |
| 69 __ CompareRoot(rsp, Heap::kStackLimitRootIndex); |
| 70 __ j(above_equal, &ok); |
| 71 StackCheckStub stub; |
| 72 __ CallStub(&stub); |
| 73 __ bind(&ok); |
| 74 } |
| 75 |
| 76 { Comment cmnt(masm_, "[ Body"); |
| 77 VisitStatements(fun->body()); |
| 78 } |
| 79 |
| 80 { Comment cmnt(masm_, "[ return <undefined>;"); |
| 81 // Emit a 'return undefined' in case control fell off the end of the |
| 82 // body. |
| 83 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex); |
| 84 __ RecordJSReturn(); |
| 85 // Do not use the leave instruction here because it is too short to |
| 86 // patch with the code required by the debugger. |
| 87 __ movq(rsp, rbp); |
| 88 __ pop(rbp); |
| 89 __ ret((fun->scope()->num_parameters() + 1) * kPointerSize); |
| 90 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 91 // Add padding that will be overwritten by a debugger breakpoint. We |
| 92 // have just generated "movq rsp, rbp; pop rbp; ret k" with length 7 |
| 93 // (3 + 1 + 3). |
| 94 const int kPadding = Debug::kX64JSReturnSequenceLength - 7; |
| 95 for (int i = 0; i < kPadding; ++i) { |
| 96 masm_->int3(); |
| 97 } |
| 98 #endif |
| 99 } |
| 100 } |
| 101 |
| 102 |
| 103 void FastCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 104 Comment cmnt(masm_, "[ ExpressionStatement"); |
| 105 Visit(stmt->expression()); |
| 106 __ pop(rax); |
| 107 } |
| 108 |
| 109 |
| 110 void FastCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 111 Comment cmnt(masm_, "[ ReturnStatement"); |
| 112 Visit(stmt->expression()); |
| 113 __ pop(rax); |
| 114 __ RecordJSReturn(); |
| 115 // Do not use the leave instruction here because it is too short to |
| 116 // patch with the code required by the debugger. |
| 117 __ movq(rsp, rbp); |
| 118 __ pop(rbp); |
| 119 __ ret((function_->scope()->num_parameters() + 1) * kPointerSize); |
| 120 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 121 // Add padding that will be overwritten by a debugger breakpoint. We |
| 122 // have just generated "movq rsp, rbp; pop rbp; ret k" with length 7 |
| 123 // (3 + 1 + 3). |
| 124 const int kPadding = Debug::kX64JSReturnSequenceLength - 7; |
| 125 for (int i = 0; i < kPadding; ++i) { |
| 126 masm_->int3(); |
| 127 } |
| 128 #endif |
| 129 } |
| 130 |
| 131 |
| 132 void FastCodeGenerator::VisitSlot(Slot* expr) { |
| 133 Comment cmnt(masm_, "[ Slot"); |
| 134 __ push(Operand(rbp, SlotOffset(expr))); |
| 135 } |
| 136 |
| 137 |
| 138 void FastCodeGenerator::VisitLiteral(Literal* expr) { |
| 139 Comment cmnt(masm_, "[ Literal"); |
| 140 __ Push(expr->handle()); |
| 141 } |
| 142 |
| 143 |
| 144 void FastCodeGenerator::VisitAssignment(Assignment* expr) { |
| 145 Comment cmnt(masm_, "[ Assignment"); |
| 146 ASSERT(expr->op() == Token::ASSIGN || expr->op() == Token::INIT_VAR); |
| 147 |
| 148 Visit(expr->value()); |
| 149 |
| 150 Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 151 ASSERT(var != NULL && var->slot() != NULL); |
| 152 __ movq(rax, Operand(rsp, 0)); |
| 153 __ movq(Operand(rbp, SlotOffset(var->slot())), rax); |
| 154 } |
| 155 |
| 156 |
| 157 } } // namespace v8::internal |
OLD | NEW |