OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "v8.h" |
| 29 |
| 30 #include "codegen-inl.h" |
| 31 #include "fast-codegen.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 #define __ ACCESS_MASM(masm()) |
| 37 |
| 38 void FastCodeGenerator::EmitLoadReceiver(Register reg) { |
| 39 // Offset 2 is due to return address and saved frame pointer. |
| 40 int index = 2 + function()->scope()->num_parameters(); |
| 41 __ movq(reg, Operand(rbp, index * kPointerSize)); |
| 42 } |
| 43 |
| 44 |
| 45 void FastCodeGenerator::EmitReceiverMapCheck() { |
| 46 Comment cmnt(masm(), ";; MapCheck(this)"); |
| 47 if (FLAG_print_ir) { |
| 48 PrintF("MapCheck(this)\n"); |
| 49 } |
| 50 |
| 51 EmitLoadReceiver(rdx); |
| 52 __ JumpIfSmi(rdx, bailout()); |
| 53 |
| 54 ASSERT(has_receiver() && receiver()->IsHeapObject()); |
| 55 Handle<HeapObject> object = Handle<HeapObject>::cast(receiver()); |
| 56 Handle<Map> map(object->map()); |
| 57 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset), map); |
| 58 __ j(not_equal, bailout()); |
| 59 } |
| 60 |
| 61 |
| 62 void FastCodeGenerator::EmitGlobalVariableLoad(Handle<String> name) { |
| 63 // Compile global variable accesses as load IC calls. The only live |
| 64 // registers are rsi (context) and possibly rdx (this). Both are also |
| 65 // saved in the stack and rsi is preserved by the call. |
| 66 __ push(CodeGenerator::GlobalObject()); |
| 67 __ Move(rcx, name); |
| 68 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize)); |
| 69 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT); |
| 70 if (has_this_properties()) { |
| 71 // Restore this. |
| 72 EmitLoadReceiver(rdx); |
| 73 } else { |
| 74 __ nop(); // Not test rax, indicates IC has no inlined code at call site. |
| 75 } |
| 76 } |
| 77 |
| 78 |
| 79 void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) { |
| 80 LookupResult lookup; |
| 81 receiver()->Lookup(*name, &lookup); |
| 82 |
| 83 ASSERT(lookup.holder() == *receiver()); |
| 84 ASSERT(lookup.type() == FIELD); |
| 85 Handle<Map> map(Handle<HeapObject>::cast(receiver())->map()); |
| 86 int index = lookup.GetFieldIndex() - map->inobject_properties(); |
| 87 int offset = index * kPointerSize; |
| 88 |
| 89 // Negative offsets are inobject properties. |
| 90 if (offset < 0) { |
| 91 offset += map->instance_size(); |
| 92 __ movq(rcx, rdx); // Copy receiver for write barrier. |
| 93 } else { |
| 94 offset += FixedArray::kHeaderSize; |
| 95 __ movq(rcx, FieldOperand(rdx, JSObject::kPropertiesOffset)); |
| 96 } |
| 97 // Perform the store. |
| 98 __ movq(FieldOperand(rcx, offset), rax); |
| 99 // Preserve value from write barrier in case it's needed. |
| 100 __ movq(rbx, rax); |
| 101 __ RecordWrite(rcx, offset, rbx, rdi); |
| 102 } |
| 103 |
| 104 |
| 105 void FastCodeGenerator::Generate(FunctionLiteral* fun, CompilationInfo* info) { |
| 106 ASSERT(function_ == NULL); |
| 107 ASSERT(info_ == NULL); |
| 108 function_ = fun; |
| 109 info_ = info; |
| 110 |
| 111 // Save the caller's frame pointer and set up our own. |
| 112 Comment prologue_cmnt(masm(), ";; Prologue"); |
| 113 __ push(rbp); |
| 114 __ movq(rbp, rsp); |
| 115 __ push(rsi); // Context. |
| 116 __ push(rdi); // Closure. |
| 117 // Note that we keep a live register reference to esi (context) at this |
| 118 // point. |
| 119 |
| 120 // Receiver (this) is allocated to rdx if there are this properties. |
| 121 if (has_this_properties()) EmitReceiverMapCheck(); |
| 122 |
| 123 VisitStatements(fun->body()); |
| 124 |
| 125 Comment return_cmnt(masm(), ";; Return(<undefined>)"); |
| 126 __ LoadRoot(rax, Heap::kUndefinedValueRootIndex); |
| 127 |
| 128 Comment epilogue_cmnt(masm(), ";; Epilogue"); |
| 129 __ movq(rsp, rbp); |
| 130 __ pop(rbp); |
| 131 __ ret((fun->scope()->num_parameters() + 1) * kPointerSize); |
| 132 |
| 133 __ bind(&bailout_); |
| 134 } |
| 135 |
| 136 |
| 137 #undef __ |
| 138 |
| 139 |
| 140 } } // namespace v8::internal |
OLD | NEW |