Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 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 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 30 | #if defined(V8_TARGET_ARCH_X64) |
| 31 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 32 | #include "codegen-inl.h" |
| 33 | #include "fast-codegen.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 34 | #include "scopes.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | #define __ ACCESS_MASM(masm()) |
| 40 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 41 | Register FastCodeGenerator::accumulator0() { return rax; } |
| 42 | Register FastCodeGenerator::accumulator1() { return rdx; } |
| 43 | Register FastCodeGenerator::scratch0() { return rcx; } |
| 44 | Register FastCodeGenerator::scratch1() { return rdi; } |
| 45 | Register FastCodeGenerator::receiver_reg() { return rbx; } |
| 46 | Register FastCodeGenerator::context_reg() { return rsi; } |
| 47 | |
| 48 | |
| 49 | void FastCodeGenerator::EmitLoadReceiver() { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 50 | // Offset 2 is due to return address and saved frame pointer. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 51 | int index = 2 + scope()->num_parameters(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 52 | __ movq(receiver_reg(), Operand(rbp, index * kPointerSize)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | void FastCodeGenerator::EmitGlobalVariableLoad(Handle<Object> cell) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 57 | ASSERT(!destination().is(no_reg)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 58 | ASSERT(cell->IsJSGlobalPropertyCell()); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 59 | |
| 60 | __ Move(destination(), cell); |
| 61 | __ movq(destination(), |
| 62 | FieldOperand(destination(), JSGlobalPropertyCell::kValueOffset)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 63 | if (FLAG_debug_code) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 64 | __ Cmp(destination(), Factory::the_hole_value()); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 65 | __ Check(not_equal, "DontDelete cells can't contain the hole"); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 66 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 67 | |
| 68 | // The loaded value is not known to be a smi. |
| 69 | clear_as_smi(destination()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) { |
| 74 | LookupResult lookup; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 75 | info()->receiver()->Lookup(*name, &lookup); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 76 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 77 | ASSERT(lookup.holder() == *info()->receiver()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 78 | ASSERT(lookup.type() == FIELD); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 79 | Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 80 | int index = lookup.GetFieldIndex() - map->inobject_properties(); |
| 81 | int offset = index * kPointerSize; |
| 82 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 83 | // We will emit the write barrier unless the stored value is statically |
| 84 | // known to be a smi. |
| 85 | bool needs_write_barrier = !is_smi(accumulator0()); |
| 86 | |
| 87 | // Perform the store. Negative offsets are inobject properties. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 88 | if (offset < 0) { |
| 89 | offset += map->instance_size(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 90 | __ movq(FieldOperand(receiver_reg(), offset), accumulator0()); |
| 91 | if (needs_write_barrier) { |
| 92 | // Preserve receiver from write barrier. |
| 93 | __ movq(scratch0(), receiver_reg()); |
| 94 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 95 | } else { |
| 96 | offset += FixedArray::kHeaderSize; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 97 | __ movq(scratch0(), |
| 98 | FieldOperand(receiver_reg(), JSObject::kPropertiesOffset)); |
| 99 | __ movq(FieldOperand(scratch0(), offset), accumulator0()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 100 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 101 | |
| 102 | if (needs_write_barrier) { |
| 103 | if (destination().is(no_reg)) { |
| 104 | // After RecordWrite accumulator0 is only accidently a smi, but it is |
| 105 | // already marked as not known to be one. |
| 106 | __ RecordWrite(scratch0(), offset, accumulator0(), scratch1()); |
| 107 | } else { |
| 108 | // Copy the value to the other accumulator to preserve a copy from the |
| 109 | // write barrier. One of the accumulators is available as a scratch |
| 110 | // register. Neither is a smi. |
| 111 | __ movq(accumulator1(), accumulator0()); |
| 112 | clear_as_smi(accumulator1()); |
| 113 | Register value_scratch = other_accumulator(destination()); |
| 114 | __ RecordWrite(scratch0(), offset, value_scratch, scratch1()); |
| 115 | } |
| 116 | } else if (destination().is(accumulator1())) { |
| 117 | __ movq(accumulator1(), accumulator0()); |
| 118 | // Is a smi because we do not need the write barrier. |
| 119 | set_as_smi(accumulator1()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void FastCodeGenerator::EmitThisPropertyLoad(Handle<String> name) { |
| 125 | ASSERT(!destination().is(no_reg)); |
| 126 | LookupResult lookup; |
| 127 | info()->receiver()->Lookup(*name, &lookup); |
| 128 | |
| 129 | ASSERT(lookup.holder() == *info()->receiver()); |
| 130 | ASSERT(lookup.type() == FIELD); |
| 131 | Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map()); |
| 132 | int index = lookup.GetFieldIndex() - map->inobject_properties(); |
| 133 | int offset = index * kPointerSize; |
| 134 | |
| 135 | // Perform the load. Negative offsets are inobject properties. |
| 136 | if (offset < 0) { |
| 137 | offset += map->instance_size(); |
| 138 | __ movq(destination(), FieldOperand(receiver_reg(), offset)); |
| 139 | } else { |
| 140 | offset += FixedArray::kHeaderSize; |
| 141 | __ movq(scratch0(), |
| 142 | FieldOperand(receiver_reg(), JSObject::kPropertiesOffset)); |
| 143 | __ movq(destination(), FieldOperand(scratch0(), offset)); |
| 144 | } |
| 145 | |
| 146 | // The loaded value is not known to be a smi. |
| 147 | clear_as_smi(destination()); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void FastCodeGenerator::EmitBitOr() { |
| 152 | if (is_smi(accumulator0()) && is_smi(accumulator1())) { |
| 153 | // If both operands are known to be a smi then there is no need to check |
| 154 | // the operands or result. |
| 155 | if (destination().is(no_reg)) { |
| 156 | __ or_(accumulator1(), accumulator0()); |
| 157 | } else { |
| 158 | // Leave the result in the destination register. Bitwise or is |
| 159 | // commutative. |
| 160 | __ or_(destination(), other_accumulator(destination())); |
| 161 | } |
| 162 | } else { |
| 163 | // Left is in accumulator1, right in accumulator0. |
| 164 | if (destination().is(accumulator0())) { |
| 165 | __ movq(scratch0(), accumulator0()); |
| 166 | __ or_(destination(), accumulator1()); // Or is commutative. |
| 167 | Label* bailout = |
| 168 | info()->AddBailout(accumulator1(), scratch0()); // Left, right. |
| 169 | __ JumpIfNotSmi(destination(), bailout); |
| 170 | } else if (destination().is(accumulator1())) { |
| 171 | __ movq(scratch0(), accumulator1()); |
| 172 | __ or_(destination(), accumulator0()); |
| 173 | Label* bailout = info()->AddBailout(scratch0(), accumulator0()); |
| 174 | __ JumpIfNotSmi(destination(), bailout); |
| 175 | } else { |
| 176 | ASSERT(destination().is(no_reg)); |
| 177 | __ movq(scratch0(), accumulator1()); |
| 178 | __ or_(scratch0(), accumulator0()); |
| 179 | Label* bailout = info()->AddBailout(accumulator1(), accumulator0()); |
| 180 | __ JumpIfNotSmi(scratch0(), bailout); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // If we didn't bailout, the result (in fact, both inputs too) is known to |
| 185 | // be a smi. |
| 186 | set_as_smi(accumulator0()); |
| 187 | set_as_smi(accumulator1()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 191 | void FastCodeGenerator::Generate(CompilationInfo* compilation_info) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 192 | ASSERT(info_ == NULL); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 193 | info_ = compilation_info; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 194 | Comment cmnt(masm_, "[ function compiled by fast code generator"); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 195 | |
| 196 | // Save the caller's frame pointer and set up our own. |
| 197 | Comment prologue_cmnt(masm(), ";; Prologue"); |
| 198 | __ push(rbp); |
| 199 | __ movq(rbp, rsp); |
| 200 | __ push(rsi); // Context. |
| 201 | __ push(rdi); // Closure. |
| 202 | // Note that we keep a live register reference to esi (context) at this |
| 203 | // point. |
| 204 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 205 | Label* bailout_to_beginning = info()->AddBailout(); |
| 206 | // Receiver (this) is allocated to a fixed register. |
| 207 | if (info()->has_this_properties()) { |
| 208 | Comment cmnt(masm(), ";; MapCheck(this)"); |
| 209 | if (FLAG_print_ir) { |
| 210 | PrintF("MapCheck(this)\n"); |
| 211 | } |
| 212 | ASSERT(info()->has_receiver() && info()->receiver()->IsHeapObject()); |
| 213 | Handle<HeapObject> object = Handle<HeapObject>::cast(info()->receiver()); |
| 214 | Handle<Map> map(object->map()); |
| 215 | EmitLoadReceiver(); |
| 216 | __ CheckMap(receiver_reg(), map, bailout_to_beginning, false); |
| 217 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 218 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 219 | // If there is a global variable access check if the global object is the |
| 220 | // same as at lazy-compilation time. |
| 221 | if (info()->has_globals()) { |
| 222 | Comment cmnt(masm(), ";; MapCheck(GLOBAL)"); |
| 223 | if (FLAG_print_ir) { |
| 224 | PrintF("MapCheck(GLOBAL)\n"); |
| 225 | } |
| 226 | ASSERT(info()->has_global_object()); |
| 227 | Handle<Map> map(info()->global_object()->map()); |
| 228 | __ movq(scratch0(), CodeGenerator::GlobalObject()); |
| 229 | __ CheckMap(scratch0(), map, bailout_to_beginning, true); |
| 230 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 231 | |
| 232 | VisitStatements(info()->function()->body()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 233 | |
| 234 | Comment return_cmnt(masm(), ";; Return(<undefined>)"); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 235 | if (FLAG_print_ir) { |
| 236 | PrintF("Return(<undefined>)\n"); |
| 237 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 238 | __ LoadRoot(rax, Heap::kUndefinedValueRootIndex); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 239 | __ movq(rsp, rbp); |
| 240 | __ pop(rbp); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 241 | __ ret((scope()->num_parameters() + 1) * kPointerSize); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | #undef __ |
| 246 | |
| 247 | |
| 248 | } } // namespace v8::internal |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 249 | |
| 250 | #endif // V8_TARGET_ARCH_X64 |