Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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_IA32) |
| 31 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 33 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "debug.h" |
| 35 | #include "runtime.h" |
| 36 | #include "serialize.h" |
| 37 | |
| 38 | namespace v8 { |
| 39 | namespace internal { |
| 40 | |
| 41 | // ------------------------------------------------------------------------- |
| 42 | // MacroAssembler implementation. |
| 43 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 44 | MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size) |
| 45 | : Assembler(arg_isolate, buffer, size), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | generating_stub_(false), |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 47 | allow_stub_calls_(true) { |
| 48 | if (isolate() != NULL) { |
| 49 | code_object_ = Handle<Object>(isolate()->heap()->undefined_value(), |
| 50 | isolate()); |
| 51 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 55 | void MacroAssembler::RecordWriteHelper(Register object, |
| 56 | Register addr, |
| 57 | Register scratch) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 58 | if (emit_debug_code()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 59 | // Check that the object is not in new space. |
| 60 | Label not_in_new_space; |
| 61 | InNewSpace(object, scratch, not_equal, ¬_in_new_space); |
| 62 | Abort("new-space object passed to RecordWriteHelper"); |
| 63 | bind(¬_in_new_space); |
| 64 | } |
| 65 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | // Compute the page start address from the heap object pointer, and reuse |
| 67 | // the 'object' register for it. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 68 | and_(object, ~Page::kPageAlignmentMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 70 | // Compute number of region covering addr. See Page::GetRegionNumberForAddress |
| 71 | // method for more details. |
| 72 | and_(addr, Page::kPageAlignmentMask); |
| 73 | shr(addr, Page::kRegionSizeLog2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 75 | // Set dirty mark for region. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 76 | // Bit tests with a memory operand should be avoided on Intel processors, |
| 77 | // as they usually have long latency and multiple uops. We load the bit base |
| 78 | // operand to a register at first and store it back after bit set. |
| 79 | mov(scratch, Operand(object, Page::kDirtyFlagOffset)); |
| 80 | bts(Operand(scratch), addr); |
| 81 | mov(Operand(object, Page::kDirtyFlagOffset), scratch); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg, |
| 86 | XMMRegister scratch_reg, |
| 87 | Register result_reg) { |
| 88 | Label done; |
| 89 | ExternalReference zero_ref = ExternalReference::address_of_zero(); |
| 90 | movdbl(scratch_reg, Operand::StaticVariable(zero_ref)); |
| 91 | Set(result_reg, Immediate(0)); |
| 92 | ucomisd(input_reg, scratch_reg); |
| 93 | j(below, &done, Label::kNear); |
| 94 | ExternalReference half_ref = ExternalReference::address_of_one_half(); |
| 95 | movdbl(scratch_reg, Operand::StaticVariable(half_ref)); |
| 96 | addsd(scratch_reg, input_reg); |
| 97 | cvttsd2si(result_reg, Operand(scratch_reg)); |
| 98 | test(result_reg, Immediate(0xFFFFFF00)); |
| 99 | j(zero, &done, Label::kNear); |
| 100 | Set(result_reg, Immediate(255)); |
| 101 | bind(&done); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void MacroAssembler::ClampUint8(Register reg) { |
| 106 | Label done; |
| 107 | test(reg, Immediate(0xFFFFFF00)); |
| 108 | j(zero, &done, Label::kNear); |
| 109 | setcc(negative, reg); // 1 if negative, 0 if positive. |
| 110 | dec_b(reg); // 0 if negative, 255 if positive. |
| 111 | bind(&done); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void MacroAssembler::InNewSpace(Register object, |
| 116 | Register scratch, |
| 117 | Condition cc, |
| 118 | Label* branch, |
| 119 | Label::Distance branch_near) { |
| 120 | ASSERT(cc == equal || cc == not_equal); |
| 121 | if (Serializer::enabled()) { |
| 122 | // Can't do arithmetic on external references if it might get serialized. |
| 123 | mov(scratch, Operand(object)); |
| 124 | // The mask isn't really an address. We load it as an external reference in |
| 125 | // case the size of the new space is different between the snapshot maker |
| 126 | // and the running system. |
| 127 | and_(Operand(scratch), |
| 128 | Immediate(ExternalReference::new_space_mask(isolate()))); |
| 129 | cmp(Operand(scratch), |
| 130 | Immediate(ExternalReference::new_space_start(isolate()))); |
| 131 | j(cc, branch, branch_near); |
| 132 | } else { |
| 133 | int32_t new_space_start = reinterpret_cast<int32_t>( |
| 134 | ExternalReference::new_space_start(isolate()).address()); |
| 135 | lea(scratch, Operand(object, -new_space_start)); |
| 136 | and_(scratch, isolate()->heap()->NewSpaceMask()); |
| 137 | j(cc, branch, branch_near); |
| 138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 142 | void MacroAssembler::RecordWrite(Register object, |
| 143 | int offset, |
| 144 | Register value, |
| 145 | Register scratch) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 146 | // First, check if a write barrier is even needed. The tests below |
| 147 | // catch stores of Smis and stores into young gen. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 148 | Label done; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | |
| 150 | // Skip barrier if writing a smi. |
| 151 | ASSERT_EQ(0, kSmiTag); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 152 | JumpIfSmi(value, &done, Label::kNear); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 154 | InNewSpace(object, value, equal, &done, Label::kNear); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 156 | // The offset is relative to a tagged or untagged HeapObject pointer, |
| 157 | // so either offset or offset + kHeapObjectTag must be a |
| 158 | // multiple of kPointerSize. |
| 159 | ASSERT(IsAligned(offset, kPointerSize) || |
| 160 | IsAligned(offset + kHeapObjectTag, kPointerSize)); |
| 161 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 162 | Register dst = scratch; |
| 163 | if (offset != 0) { |
| 164 | lea(dst, Operand(object, offset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | } else { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 166 | // Array access: calculate the destination address in the same manner as |
| 167 | // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset |
| 168 | // into an array of words. |
| 169 | ASSERT_EQ(1, kSmiTagSize); |
| 170 | ASSERT_EQ(0, kSmiTag); |
| 171 | lea(dst, Operand(object, dst, times_half_pointer_size, |
| 172 | FixedArray::kHeaderSize - kHeapObjectTag)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 174 | RecordWriteHelper(object, dst, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | |
| 176 | bind(&done); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 177 | |
| 178 | // Clobber all input registers when running with the debug-code flag |
| 179 | // turned on to provoke errors. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 180 | if (emit_debug_code()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 181 | mov(object, Immediate(BitCast<int32_t>(kZapValue))); |
| 182 | mov(value, Immediate(BitCast<int32_t>(kZapValue))); |
| 183 | mov(scratch, Immediate(BitCast<int32_t>(kZapValue))); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 184 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 188 | void MacroAssembler::RecordWrite(Register object, |
| 189 | Register address, |
| 190 | Register value) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 191 | // First, check if a write barrier is even needed. The tests below |
| 192 | // catch stores of Smis and stores into young gen. |
| 193 | Label done; |
| 194 | |
| 195 | // Skip barrier if writing a smi. |
| 196 | ASSERT_EQ(0, kSmiTag); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 197 | JumpIfSmi(value, &done, Label::kNear); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 198 | |
| 199 | InNewSpace(object, value, equal, &done); |
| 200 | |
| 201 | RecordWriteHelper(object, address, value); |
| 202 | |
| 203 | bind(&done); |
| 204 | |
| 205 | // Clobber all input registers when running with the debug-code flag |
| 206 | // turned on to provoke errors. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 207 | if (emit_debug_code()) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 208 | mov(object, Immediate(BitCast<int32_t>(kZapValue))); |
| 209 | mov(address, Immediate(BitCast<int32_t>(kZapValue))); |
| 210 | mov(value, Immediate(BitCast<int32_t>(kZapValue))); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 216 | void MacroAssembler::DebugBreak() { |
| 217 | Set(eax, Immediate(0)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 218 | mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate()))); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 219 | CEntryStub ces(1); |
| 220 | call(ces.GetCode(), RelocInfo::DEBUG_BREAK); |
| 221 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | #endif |
| 223 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 224 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | void MacroAssembler::Set(Register dst, const Immediate& x) { |
| 226 | if (x.is_zero()) { |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 227 | xor_(dst, Operand(dst)); // Shorter than mov. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | } else { |
| 229 | mov(dst, x); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void MacroAssembler::Set(const Operand& dst, const Immediate& x) { |
| 235 | mov(dst, x); |
| 236 | } |
| 237 | |
| 238 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 239 | bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) { |
| 240 | static const int kMaxImmediateBits = 17; |
| 241 | if (x.rmode_ != RelocInfo::NONE) return false; |
| 242 | return !is_intn(x.x_, kMaxImmediateBits); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void MacroAssembler::SafeSet(Register dst, const Immediate& x) { |
| 247 | if (IsUnsafeImmediate(x) && jit_cookie() != 0) { |
| 248 | Set(dst, Immediate(x.x_ ^ jit_cookie())); |
| 249 | xor_(dst, jit_cookie()); |
| 250 | } else { |
| 251 | Set(dst, x); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | void MacroAssembler::SafePush(const Immediate& x) { |
| 257 | if (IsUnsafeImmediate(x) && jit_cookie() != 0) { |
| 258 | push(Immediate(x.x_ ^ jit_cookie())); |
| 259 | xor_(Operand(esp, 0), Immediate(jit_cookie())); |
| 260 | } else { |
| 261 | push(x); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | void MacroAssembler::CmpObjectType(Register heap_object, |
| 267 | InstanceType type, |
| 268 | Register map) { |
| 269 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 270 | CmpInstanceType(map, type); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void MacroAssembler::CmpInstanceType(Register map, InstanceType type) { |
| 275 | cmpb(FieldOperand(map, Map::kInstanceTypeOffset), |
| 276 | static_cast<int8_t>(type)); |
| 277 | } |
| 278 | |
| 279 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 280 | void MacroAssembler::CheckFastElements(Register map, |
| 281 | Label* fail, |
| 282 | Label::Distance distance) { |
| 283 | STATIC_ASSERT(JSObject::FAST_ELEMENTS == 0); |
| 284 | cmpb(FieldOperand(map, Map::kBitField2Offset), |
| 285 | Map::kMaximumBitField2FastElementValue); |
| 286 | j(above, fail, distance); |
| 287 | } |
| 288 | |
| 289 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 290 | void MacroAssembler::CheckMap(Register obj, |
| 291 | Handle<Map> map, |
| 292 | Label* fail, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 293 | SmiCheckType smi_check_type) { |
| 294 | if (smi_check_type == DO_SMI_CHECK) { |
| 295 | JumpIfSmi(obj, fail); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 296 | } |
| 297 | cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map)); |
| 298 | j(not_equal, fail); |
| 299 | } |
| 300 | |
| 301 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 302 | void MacroAssembler::DispatchMap(Register obj, |
| 303 | Handle<Map> map, |
| 304 | Handle<Code> success, |
| 305 | SmiCheckType smi_check_type) { |
| 306 | Label fail; |
| 307 | if (smi_check_type == DO_SMI_CHECK) { |
| 308 | JumpIfSmi(obj, &fail); |
| 309 | } |
| 310 | cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map)); |
| 311 | j(equal, success); |
| 312 | |
| 313 | bind(&fail); |
| 314 | } |
| 315 | |
| 316 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 317 | Condition MacroAssembler::IsObjectStringType(Register heap_object, |
| 318 | Register map, |
| 319 | Register instance_type) { |
| 320 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 321 | movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset)); |
| 322 | ASSERT(kNotStringTag != 0); |
| 323 | test(instance_type, Immediate(kIsNotStringMask)); |
| 324 | return zero; |
| 325 | } |
| 326 | |
| 327 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 328 | void MacroAssembler::IsObjectJSObjectType(Register heap_object, |
| 329 | Register map, |
| 330 | Register scratch, |
| 331 | Label* fail) { |
| 332 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 333 | IsInstanceJSObjectType(map, scratch, fail); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void MacroAssembler::IsInstanceJSObjectType(Register map, |
| 338 | Register scratch, |
| 339 | Label* fail) { |
| 340 | movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 341 | sub(Operand(scratch), Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE)); |
| 342 | cmp(scratch, |
| 343 | LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 344 | j(above, fail); |
| 345 | } |
| 346 | |
| 347 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | void MacroAssembler::FCmp() { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 349 | if (CpuFeatures::IsSupported(CMOV)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 350 | fucomip(); |
| 351 | ffree(0); |
| 352 | fincstp(); |
| 353 | } else { |
| 354 | fucompp(); |
| 355 | push(eax); |
| 356 | fnstsw_ax(); |
| 357 | sahf(); |
| 358 | pop(eax); |
| 359 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 363 | void MacroAssembler::AbortIfNotNumber(Register object) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 364 | Label ok; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 365 | JumpIfSmi(object, &ok); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 366 | cmp(FieldOperand(object, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 367 | isolate()->factory()->heap_number_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 368 | Assert(equal, "Operand not a number"); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 369 | bind(&ok); |
| 370 | } |
| 371 | |
| 372 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 373 | void MacroAssembler::AbortIfNotSmi(Register object) { |
| 374 | test(object, Immediate(kSmiTagMask)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 375 | Assert(equal, "Operand is not a smi"); |
| 376 | } |
| 377 | |
| 378 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 379 | void MacroAssembler::AbortIfNotString(Register object) { |
| 380 | test(object, Immediate(kSmiTagMask)); |
| 381 | Assert(not_equal, "Operand is not a string"); |
| 382 | push(object); |
| 383 | mov(object, FieldOperand(object, HeapObject::kMapOffset)); |
| 384 | CmpInstanceType(object, FIRST_NONSTRING_TYPE); |
| 385 | pop(object); |
| 386 | Assert(below, "Operand is not a string"); |
| 387 | } |
| 388 | |
| 389 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 390 | void MacroAssembler::AbortIfSmi(Register object) { |
| 391 | test(object, Immediate(kSmiTagMask)); |
| 392 | Assert(not_equal, "Operand is a smi"); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | void MacroAssembler::EnterFrame(StackFrame::Type type) { |
| 397 | push(ebp); |
| 398 | mov(ebp, Operand(esp)); |
| 399 | push(esi); |
| 400 | push(Immediate(Smi::FromInt(type))); |
| 401 | push(Immediate(CodeObject())); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 402 | if (emit_debug_code()) { |
| 403 | cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | Check(not_equal, "code object not properly patched"); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
| 409 | void MacroAssembler::LeaveFrame(StackFrame::Type type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 410 | if (emit_debug_code()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 411 | cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset), |
| 412 | Immediate(Smi::FromInt(type))); |
| 413 | Check(equal, "stack frame types must match"); |
| 414 | } |
| 415 | leave(); |
| 416 | } |
| 417 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 418 | |
| 419 | void MacroAssembler::EnterExitFramePrologue() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | // Setup the frame structure on the stack. |
| 421 | ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize); |
| 422 | ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize); |
| 423 | ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize); |
| 424 | push(ebp); |
| 425 | mov(ebp, Operand(esp)); |
| 426 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 427 | // Reserve room for entry stack pointer and push the code object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 429 | push(Immediate(0)); // Saved entry sp, patched before call. |
| 430 | push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | |
| 432 | // Save the frame pointer and the context in top. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 433 | ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address, |
| 434 | isolate()); |
| 435 | ExternalReference context_address(Isolate::k_context_address, |
| 436 | isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | mov(Operand::StaticVariable(c_entry_fp_address), ebp); |
| 438 | mov(Operand::StaticVariable(context_address), esi); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 439 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 442 | void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) { |
| 443 | // Optionally save all XMM registers. |
| 444 | if (save_doubles) { |
| 445 | CpuFeatures::Scope scope(SSE2); |
| 446 | int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize; |
| 447 | sub(Operand(esp), Immediate(space)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 448 | const int offset = -2 * kPointerSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 449 | for (int i = 0; i < XMMRegister::kNumRegisters; i++) { |
| 450 | XMMRegister reg = XMMRegister::from_code(i); |
| 451 | movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg); |
| 452 | } |
| 453 | } else { |
| 454 | sub(Operand(esp), Immediate(argc * kPointerSize)); |
| 455 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 456 | |
| 457 | // Get the required frame alignment for the OS. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 458 | const int kFrameAlignment = OS::ActivationFrameAlignment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 459 | if (kFrameAlignment > 0) { |
| 460 | ASSERT(IsPowerOf2(kFrameAlignment)); |
| 461 | and_(esp, -kFrameAlignment); |
| 462 | } |
| 463 | |
| 464 | // Patch the saved entry sp. |
| 465 | mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp); |
| 466 | } |
| 467 | |
| 468 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 469 | void MacroAssembler::EnterExitFrame(bool save_doubles) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 470 | EnterExitFramePrologue(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 471 | |
| 472 | // Setup argc and argv in callee-saved registers. |
| 473 | int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize; |
| 474 | mov(edi, Operand(eax)); |
| 475 | lea(esi, Operand(ebp, eax, times_4, offset)); |
| 476 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 477 | // Reserve space for argc, argv and isolate. |
| 478 | EnterExitFrameEpilogue(3, save_doubles); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 482 | void MacroAssembler::EnterApiExitFrame(int argc) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 483 | EnterExitFramePrologue(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 484 | EnterExitFrameEpilogue(argc, false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 488 | void MacroAssembler::LeaveExitFrame(bool save_doubles) { |
| 489 | // Optionally restore all XMM registers. |
| 490 | if (save_doubles) { |
| 491 | CpuFeatures::Scope scope(SSE2); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 492 | const int offset = -2 * kPointerSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 493 | for (int i = 0; i < XMMRegister::kNumRegisters; i++) { |
| 494 | XMMRegister reg = XMMRegister::from_code(i); |
| 495 | movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize))); |
| 496 | } |
| 497 | } |
| 498 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 499 | // Get the return address from the stack and restore the frame pointer. |
| 500 | mov(ecx, Operand(ebp, 1 * kPointerSize)); |
| 501 | mov(ebp, Operand(ebp, 0 * kPointerSize)); |
| 502 | |
| 503 | // Pop the arguments and the receiver from the caller stack. |
| 504 | lea(esp, Operand(esi, 1 * kPointerSize)); |
| 505 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 506 | // Push the return address to get ready to return. |
| 507 | push(ecx); |
| 508 | |
| 509 | LeaveExitFrameEpilogue(); |
| 510 | } |
| 511 | |
| 512 | void MacroAssembler::LeaveExitFrameEpilogue() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | // Restore current context from top and clear it in debug mode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 514 | ExternalReference context_address(Isolate::k_context_address, isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 515 | mov(esi, Operand::StaticVariable(context_address)); |
| 516 | #ifdef DEBUG |
| 517 | mov(Operand::StaticVariable(context_address), Immediate(0)); |
| 518 | #endif |
| 519 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | // Clear the top frame. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 521 | ExternalReference c_entry_fp_address(Isolate::k_c_entry_fp_address, |
| 522 | isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 523 | mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0)); |
| 524 | } |
| 525 | |
| 526 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 527 | void MacroAssembler::LeaveApiExitFrame() { |
| 528 | mov(esp, Operand(ebp)); |
| 529 | pop(ebp); |
| 530 | |
| 531 | LeaveExitFrameEpilogue(); |
| 532 | } |
| 533 | |
| 534 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 535 | void MacroAssembler::PushTryHandler(CodeLocation try_location, |
| 536 | HandlerType type) { |
| 537 | // Adjust this code if not the case. |
| 538 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 539 | // The pc (return address) is already on TOS. |
| 540 | if (try_location == IN_JAVASCRIPT) { |
| 541 | if (type == TRY_CATCH_HANDLER) { |
| 542 | push(Immediate(StackHandler::TRY_CATCH)); |
| 543 | } else { |
| 544 | push(Immediate(StackHandler::TRY_FINALLY)); |
| 545 | } |
| 546 | push(ebp); |
| 547 | } else { |
| 548 | ASSERT(try_location == IN_JS_ENTRY); |
| 549 | // The frame pointer does not point to a JS frame so we save NULL |
| 550 | // for ebp. We expect the code throwing an exception to check ebp |
| 551 | // before dereferencing it to restore the context. |
| 552 | push(Immediate(StackHandler::ENTRY)); |
| 553 | push(Immediate(0)); // NULL frame pointer. |
| 554 | } |
| 555 | // Save the current handler as the next handler. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 556 | push(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address, |
| 557 | isolate()))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | // Link this handler as the new current one. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 559 | mov(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address, |
| 560 | isolate())), |
| 561 | esp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 565 | void MacroAssembler::PopTryHandler() { |
| 566 | ASSERT_EQ(0, StackHandlerConstants::kNextOffset); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 567 | pop(Operand::StaticVariable(ExternalReference(Isolate::k_handler_address, |
| 568 | isolate()))); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 569 | add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize)); |
| 570 | } |
| 571 | |
| 572 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 573 | void MacroAssembler::Throw(Register value) { |
| 574 | // Adjust this code if not the case. |
| 575 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 576 | |
| 577 | // eax must hold the exception. |
| 578 | if (!value.is(eax)) { |
| 579 | mov(eax, value); |
| 580 | } |
| 581 | |
| 582 | // Drop the sp to the top of the handler. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 583 | ExternalReference handler_address(Isolate::k_handler_address, |
| 584 | isolate()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 585 | mov(esp, Operand::StaticVariable(handler_address)); |
| 586 | |
| 587 | // Restore next handler and frame pointer, discard handler state. |
| 588 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 589 | pop(Operand::StaticVariable(handler_address)); |
| 590 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize); |
| 591 | pop(ebp); |
| 592 | pop(edx); // Remove state. |
| 593 | |
| 594 | // Before returning we restore the context from the frame pointer if |
| 595 | // not NULL. The frame pointer is NULL in the exception handler of |
| 596 | // a JS entry frame. |
| 597 | Set(esi, Immediate(0)); // Tentatively set context pointer to NULL. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 598 | Label skip; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 599 | cmp(ebp, 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 600 | j(equal, &skip, Label::kNear); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 601 | mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 602 | bind(&skip); |
| 603 | |
| 604 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 605 | ret(0); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | void MacroAssembler::ThrowUncatchable(UncatchableExceptionType type, |
| 610 | Register value) { |
| 611 | // Adjust this code if not the case. |
| 612 | STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
| 613 | |
| 614 | // eax must hold the exception. |
| 615 | if (!value.is(eax)) { |
| 616 | mov(eax, value); |
| 617 | } |
| 618 | |
| 619 | // Drop sp to the top stack handler. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 620 | ExternalReference handler_address(Isolate::k_handler_address, |
| 621 | isolate()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 622 | mov(esp, Operand::StaticVariable(handler_address)); |
| 623 | |
| 624 | // Unwind the handlers until the ENTRY handler is found. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 625 | Label loop, done; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 626 | bind(&loop); |
| 627 | // Load the type of the current stack handler. |
| 628 | const int kStateOffset = StackHandlerConstants::kStateOffset; |
| 629 | cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 630 | j(equal, &done, Label::kNear); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 631 | // Fetch the next handler in the list. |
| 632 | const int kNextOffset = StackHandlerConstants::kNextOffset; |
| 633 | mov(esp, Operand(esp, kNextOffset)); |
| 634 | jmp(&loop); |
| 635 | bind(&done); |
| 636 | |
| 637 | // Set the top handler address to next handler past the current ENTRY handler. |
| 638 | STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0); |
| 639 | pop(Operand::StaticVariable(handler_address)); |
| 640 | |
| 641 | if (type == OUT_OF_MEMORY) { |
| 642 | // Set external caught exception to false. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 643 | ExternalReference external_caught( |
| 644 | Isolate::k_external_caught_exception_address, |
| 645 | isolate()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 646 | mov(eax, false); |
| 647 | mov(Operand::StaticVariable(external_caught), eax); |
| 648 | |
| 649 | // Set pending exception and eax to out of memory exception. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 650 | ExternalReference pending_exception(Isolate::k_pending_exception_address, |
| 651 | isolate()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 652 | mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException())); |
| 653 | mov(Operand::StaticVariable(pending_exception), eax); |
| 654 | } |
| 655 | |
| 656 | // Clear the context pointer. |
| 657 | Set(esi, Immediate(0)); |
| 658 | |
| 659 | // Restore fp from handler and discard handler state. |
| 660 | STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize); |
| 661 | pop(ebp); |
| 662 | pop(edx); // State. |
| 663 | |
| 664 | STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize); |
| 665 | ret(0); |
| 666 | } |
| 667 | |
| 668 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg, |
| 670 | Register scratch, |
| 671 | Label* miss) { |
| 672 | Label same_contexts; |
| 673 | |
| 674 | ASSERT(!holder_reg.is(scratch)); |
| 675 | |
| 676 | // Load current lexical context from the stack frame. |
| 677 | mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 678 | |
| 679 | // When generating debug code, make sure the lexical context is set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 680 | if (emit_debug_code()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 681 | cmp(Operand(scratch), Immediate(0)); |
| 682 | Check(not_equal, "we should not have an empty lexical context"); |
| 683 | } |
| 684 | // Load the global context of the current context. |
| 685 | int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize; |
| 686 | mov(scratch, FieldOperand(scratch, offset)); |
| 687 | mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset)); |
| 688 | |
| 689 | // Check the context is a global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 690 | if (emit_debug_code()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 691 | push(scratch); |
| 692 | // Read the first word and compare to global_context_map. |
| 693 | mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 694 | cmp(scratch, isolate()->factory()->global_context_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 695 | Check(equal, "JSGlobalObject::global_context should be a global context."); |
| 696 | pop(scratch); |
| 697 | } |
| 698 | |
| 699 | // Check if both contexts are the same. |
| 700 | cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 701 | j(equal, &same_contexts); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 702 | |
| 703 | // Compare security tokens, save holder_reg on the stack so we can use it |
| 704 | // as a temporary register. |
| 705 | // |
| 706 | // TODO(119): avoid push(holder_reg)/pop(holder_reg) |
| 707 | push(holder_reg); |
| 708 | // Check that the security token in the calling global object is |
| 709 | // compatible with the security token in the receiving global |
| 710 | // object. |
| 711 | mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
| 712 | |
| 713 | // Check the context is a global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 714 | if (emit_debug_code()) { |
| 715 | cmp(holder_reg, isolate()->factory()->null_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 716 | Check(not_equal, "JSGlobalProxy::context() should not be null."); |
| 717 | |
| 718 | push(holder_reg); |
| 719 | // Read the first word and compare to global_context_map(), |
| 720 | mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 721 | cmp(holder_reg, isolate()->factory()->global_context_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 722 | Check(equal, "JSGlobalObject::global_context should be a global context."); |
| 723 | pop(holder_reg); |
| 724 | } |
| 725 | |
| 726 | int token_offset = Context::kHeaderSize + |
| 727 | Context::SECURITY_TOKEN_INDEX * kPointerSize; |
| 728 | mov(scratch, FieldOperand(scratch, token_offset)); |
| 729 | cmp(scratch, FieldOperand(holder_reg, token_offset)); |
| 730 | pop(holder_reg); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 731 | j(not_equal, miss); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 732 | |
| 733 | bind(&same_contexts); |
| 734 | } |
| 735 | |
| 736 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 737 | void MacroAssembler::LoadFromNumberDictionary(Label* miss, |
| 738 | Register elements, |
| 739 | Register key, |
| 740 | Register r0, |
| 741 | Register r1, |
| 742 | Register r2, |
| 743 | Register result) { |
| 744 | // Register use: |
| 745 | // |
| 746 | // elements - holds the slow-case elements of the receiver and is unchanged. |
| 747 | // |
| 748 | // key - holds the smi key on entry and is unchanged. |
| 749 | // |
| 750 | // Scratch registers: |
| 751 | // |
| 752 | // r0 - holds the untagged key on entry and holds the hash once computed. |
| 753 | // |
| 754 | // r1 - used to hold the capacity mask of the dictionary |
| 755 | // |
| 756 | // r2 - used for the index into the dictionary. |
| 757 | // |
| 758 | // result - holds the result on exit if the load succeeds and we fall through. |
| 759 | |
| 760 | Label done; |
| 761 | |
| 762 | // Compute the hash code from the untagged key. This must be kept in sync |
| 763 | // with ComputeIntegerHash in utils.h. |
| 764 | // |
| 765 | // hash = ~hash + (hash << 15); |
| 766 | mov(r1, r0); |
| 767 | not_(r0); |
| 768 | shl(r1, 15); |
| 769 | add(r0, Operand(r1)); |
| 770 | // hash = hash ^ (hash >> 12); |
| 771 | mov(r1, r0); |
| 772 | shr(r1, 12); |
| 773 | xor_(r0, Operand(r1)); |
| 774 | // hash = hash + (hash << 2); |
| 775 | lea(r0, Operand(r0, r0, times_4, 0)); |
| 776 | // hash = hash ^ (hash >> 4); |
| 777 | mov(r1, r0); |
| 778 | shr(r1, 4); |
| 779 | xor_(r0, Operand(r1)); |
| 780 | // hash = hash * 2057; |
| 781 | imul(r0, r0, 2057); |
| 782 | // hash = hash ^ (hash >> 16); |
| 783 | mov(r1, r0); |
| 784 | shr(r1, 16); |
| 785 | xor_(r0, Operand(r1)); |
| 786 | |
| 787 | // Compute capacity mask. |
| 788 | mov(r1, FieldOperand(elements, NumberDictionary::kCapacityOffset)); |
| 789 | shr(r1, kSmiTagSize); // convert smi to int |
| 790 | dec(r1); |
| 791 | |
| 792 | // Generate an unrolled loop that performs a few probes before giving up. |
| 793 | const int kProbes = 4; |
| 794 | for (int i = 0; i < kProbes; i++) { |
| 795 | // Use r2 for index calculations and keep the hash intact in r0. |
| 796 | mov(r2, r0); |
| 797 | // Compute the masked index: (hash + i + i * i) & mask. |
| 798 | if (i > 0) { |
| 799 | add(Operand(r2), Immediate(NumberDictionary::GetProbeOffset(i))); |
| 800 | } |
| 801 | and_(r2, Operand(r1)); |
| 802 | |
| 803 | // Scale the index by multiplying by the entry size. |
| 804 | ASSERT(NumberDictionary::kEntrySize == 3); |
| 805 | lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3 |
| 806 | |
| 807 | // Check if the key matches. |
| 808 | cmp(key, FieldOperand(elements, |
| 809 | r2, |
| 810 | times_pointer_size, |
| 811 | NumberDictionary::kElementsStartOffset)); |
| 812 | if (i != (kProbes - 1)) { |
| 813 | j(equal, &done); |
| 814 | } else { |
| 815 | j(not_equal, miss); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | bind(&done); |
| 820 | // Check that the value is a normal propety. |
| 821 | const int kDetailsOffset = |
| 822 | NumberDictionary::kElementsStartOffset + 2 * kPointerSize; |
| 823 | ASSERT_EQ(NORMAL, 0); |
| 824 | test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset), |
| 825 | Immediate(PropertyDetails::TypeField::mask() << kSmiTagSize)); |
| 826 | j(not_zero, miss); |
| 827 | |
| 828 | // Get the value at the masked, scaled index. |
| 829 | const int kValueOffset = |
| 830 | NumberDictionary::kElementsStartOffset + kPointerSize; |
| 831 | mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset)); |
| 832 | } |
| 833 | |
| 834 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 835 | void MacroAssembler::LoadAllocationTopHelper(Register result, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 836 | Register scratch, |
| 837 | AllocationFlags flags) { |
| 838 | ExternalReference new_space_allocation_top = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 839 | ExternalReference::new_space_allocation_top_address(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 840 | |
| 841 | // Just return if allocation top is already known. |
| 842 | if ((flags & RESULT_CONTAINS_TOP) != 0) { |
| 843 | // No use of scratch if allocation top is provided. |
| 844 | ASSERT(scratch.is(no_reg)); |
| 845 | #ifdef DEBUG |
| 846 | // Assert that result actually contains top on entry. |
| 847 | cmp(result, Operand::StaticVariable(new_space_allocation_top)); |
| 848 | Check(equal, "Unexpected allocation top"); |
| 849 | #endif |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | // Move address of new object to result. Use scratch register if available. |
| 854 | if (scratch.is(no_reg)) { |
| 855 | mov(result, Operand::StaticVariable(new_space_allocation_top)); |
| 856 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 857 | mov(Operand(scratch), Immediate(new_space_allocation_top)); |
| 858 | mov(result, Operand(scratch, 0)); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | |
| 863 | void MacroAssembler::UpdateAllocationTopHelper(Register result_end, |
| 864 | Register scratch) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 865 | if (emit_debug_code()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 866 | test(result_end, Immediate(kObjectAlignmentMask)); |
| 867 | Check(zero, "Unaligned allocation in new space"); |
| 868 | } |
| 869 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 870 | ExternalReference new_space_allocation_top = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 871 | ExternalReference::new_space_allocation_top_address(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 872 | |
| 873 | // Update new top. Use scratch if available. |
| 874 | if (scratch.is(no_reg)) { |
| 875 | mov(Operand::StaticVariable(new_space_allocation_top), result_end); |
| 876 | } else { |
| 877 | mov(Operand(scratch, 0), result_end); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | |
| 882 | void MacroAssembler::AllocateInNewSpace(int object_size, |
| 883 | Register result, |
| 884 | Register result_end, |
| 885 | Register scratch, |
| 886 | Label* gc_required, |
| 887 | AllocationFlags flags) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 888 | if (!FLAG_inline_new) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 889 | if (emit_debug_code()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 890 | // Trash the registers to simulate an allocation failure. |
| 891 | mov(result, Immediate(0x7091)); |
| 892 | if (result_end.is_valid()) { |
| 893 | mov(result_end, Immediate(0x7191)); |
| 894 | } |
| 895 | if (scratch.is_valid()) { |
| 896 | mov(scratch, Immediate(0x7291)); |
| 897 | } |
| 898 | } |
| 899 | jmp(gc_required); |
| 900 | return; |
| 901 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 902 | ASSERT(!result.is(result_end)); |
| 903 | |
| 904 | // Load address of new object into result. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 905 | LoadAllocationTopHelper(result, scratch, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 906 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 907 | Register top_reg = result_end.is_valid() ? result_end : result; |
| 908 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 909 | // Calculate new top and bail out if new space is exhausted. |
| 910 | ExternalReference new_space_allocation_limit = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 911 | ExternalReference::new_space_allocation_limit_address(isolate()); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 912 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 913 | if (!top_reg.is(result)) { |
| 914 | mov(top_reg, result); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 915 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 916 | add(Operand(top_reg), Immediate(object_size)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 917 | j(carry, gc_required); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 918 | cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 919 | j(above, gc_required); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 920 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 921 | // Update allocation top. |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 922 | UpdateAllocationTopHelper(top_reg, scratch); |
| 923 | |
| 924 | // Tag result if requested. |
| 925 | if (top_reg.is(result)) { |
| 926 | if ((flags & TAG_OBJECT) != 0) { |
| 927 | sub(Operand(result), Immediate(object_size - kHeapObjectTag)); |
| 928 | } else { |
| 929 | sub(Operand(result), Immediate(object_size)); |
| 930 | } |
| 931 | } else if ((flags & TAG_OBJECT) != 0) { |
| 932 | add(Operand(result), Immediate(kHeapObjectTag)); |
| 933 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | |
| 937 | void MacroAssembler::AllocateInNewSpace(int header_size, |
| 938 | ScaleFactor element_size, |
| 939 | Register element_count, |
| 940 | Register result, |
| 941 | Register result_end, |
| 942 | Register scratch, |
| 943 | Label* gc_required, |
| 944 | AllocationFlags flags) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 945 | if (!FLAG_inline_new) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 946 | if (emit_debug_code()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 947 | // Trash the registers to simulate an allocation failure. |
| 948 | mov(result, Immediate(0x7091)); |
| 949 | mov(result_end, Immediate(0x7191)); |
| 950 | if (scratch.is_valid()) { |
| 951 | mov(scratch, Immediate(0x7291)); |
| 952 | } |
| 953 | // Register element_count is not modified by the function. |
| 954 | } |
| 955 | jmp(gc_required); |
| 956 | return; |
| 957 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | ASSERT(!result.is(result_end)); |
| 959 | |
| 960 | // Load address of new object into result. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 961 | LoadAllocationTopHelper(result, scratch, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 962 | |
| 963 | // Calculate new top and bail out if new space is exhausted. |
| 964 | ExternalReference new_space_allocation_limit = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 965 | ExternalReference::new_space_allocation_limit_address(isolate()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 966 | |
| 967 | // We assume that element_count*element_size + header_size does not |
| 968 | // overflow. |
| 969 | lea(result_end, Operand(element_count, element_size, header_size)); |
| 970 | add(result_end, Operand(result)); |
| 971 | j(carry, gc_required); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 972 | cmp(result_end, Operand::StaticVariable(new_space_allocation_limit)); |
| 973 | j(above, gc_required); |
| 974 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | // Tag result if requested. |
| 976 | if ((flags & TAG_OBJECT) != 0) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 977 | lea(result, Operand(result, kHeapObjectTag)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 978 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 979 | |
| 980 | // Update allocation top. |
| 981 | UpdateAllocationTopHelper(result_end, scratch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | |
| 985 | void MacroAssembler::AllocateInNewSpace(Register object_size, |
| 986 | Register result, |
| 987 | Register result_end, |
| 988 | Register scratch, |
| 989 | Label* gc_required, |
| 990 | AllocationFlags flags) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 991 | if (!FLAG_inline_new) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 992 | if (emit_debug_code()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 993 | // Trash the registers to simulate an allocation failure. |
| 994 | mov(result, Immediate(0x7091)); |
| 995 | mov(result_end, Immediate(0x7191)); |
| 996 | if (scratch.is_valid()) { |
| 997 | mov(scratch, Immediate(0x7291)); |
| 998 | } |
| 999 | // object_size is left unchanged by this function. |
| 1000 | } |
| 1001 | jmp(gc_required); |
| 1002 | return; |
| 1003 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1004 | ASSERT(!result.is(result_end)); |
| 1005 | |
| 1006 | // Load address of new object into result. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1007 | LoadAllocationTopHelper(result, scratch, flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Calculate new top and bail out if new space is exhausted. |
| 1010 | ExternalReference new_space_allocation_limit = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1011 | ExternalReference::new_space_allocation_limit_address(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1012 | if (!object_size.is(result_end)) { |
| 1013 | mov(result_end, object_size); |
| 1014 | } |
| 1015 | add(result_end, Operand(result)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1016 | j(carry, gc_required); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1017 | cmp(result_end, Operand::StaticVariable(new_space_allocation_limit)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1018 | j(above, gc_required); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1019 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1020 | // Tag result if requested. |
| 1021 | if ((flags & TAG_OBJECT) != 0) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1022 | lea(result, Operand(result, kHeapObjectTag)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1023 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Update allocation top. |
| 1026 | UpdateAllocationTopHelper(result_end, scratch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | void MacroAssembler::UndoAllocationInNewSpace(Register object) { |
| 1031 | ExternalReference new_space_allocation_top = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1032 | ExternalReference::new_space_allocation_top_address(isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Make sure the object has no tag before resetting top. |
| 1035 | and_(Operand(object), Immediate(~kHeapObjectTagMask)); |
| 1036 | #ifdef DEBUG |
| 1037 | cmp(object, Operand::StaticVariable(new_space_allocation_top)); |
| 1038 | Check(below, "Undo allocation of non allocated memory"); |
| 1039 | #endif |
| 1040 | mov(Operand::StaticVariable(new_space_allocation_top), object); |
| 1041 | } |
| 1042 | |
| 1043 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1044 | void MacroAssembler::AllocateHeapNumber(Register result, |
| 1045 | Register scratch1, |
| 1046 | Register scratch2, |
| 1047 | Label* gc_required) { |
| 1048 | // Allocate heap number in new space. |
| 1049 | AllocateInNewSpace(HeapNumber::kSize, |
| 1050 | result, |
| 1051 | scratch1, |
| 1052 | scratch2, |
| 1053 | gc_required, |
| 1054 | TAG_OBJECT); |
| 1055 | |
| 1056 | // Set the map. |
| 1057 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1058 | Immediate(isolate()->factory()->heap_number_map())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1062 | void MacroAssembler::AllocateTwoByteString(Register result, |
| 1063 | Register length, |
| 1064 | Register scratch1, |
| 1065 | Register scratch2, |
| 1066 | Register scratch3, |
| 1067 | Label* gc_required) { |
| 1068 | // Calculate the number of bytes needed for the characters in the string while |
| 1069 | // observing object alignment. |
| 1070 | ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1071 | ASSERT(kShortSize == 2); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1072 | // scratch1 = length * 2 + kObjectAlignmentMask. |
| 1073 | lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1074 | and_(Operand(scratch1), Immediate(~kObjectAlignmentMask)); |
| 1075 | |
| 1076 | // Allocate two byte string in new space. |
| 1077 | AllocateInNewSpace(SeqTwoByteString::kHeaderSize, |
| 1078 | times_1, |
| 1079 | scratch1, |
| 1080 | result, |
| 1081 | scratch2, |
| 1082 | scratch3, |
| 1083 | gc_required, |
| 1084 | TAG_OBJECT); |
| 1085 | |
| 1086 | // Set the map, length and hash field. |
| 1087 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1088 | Immediate(isolate()->factory()->string_map())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1089 | mov(scratch1, length); |
| 1090 | SmiTag(scratch1); |
| 1091 | mov(FieldOperand(result, String::kLengthOffset), scratch1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1092 | mov(FieldOperand(result, String::kHashFieldOffset), |
| 1093 | Immediate(String::kEmptyHashField)); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | void MacroAssembler::AllocateAsciiString(Register result, |
| 1098 | Register length, |
| 1099 | Register scratch1, |
| 1100 | Register scratch2, |
| 1101 | Register scratch3, |
| 1102 | Label* gc_required) { |
| 1103 | // Calculate the number of bytes needed for the characters in the string while |
| 1104 | // observing object alignment. |
| 1105 | ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0); |
| 1106 | mov(scratch1, length); |
| 1107 | ASSERT(kCharSize == 1); |
| 1108 | add(Operand(scratch1), Immediate(kObjectAlignmentMask)); |
| 1109 | and_(Operand(scratch1), Immediate(~kObjectAlignmentMask)); |
| 1110 | |
| 1111 | // Allocate ascii string in new space. |
| 1112 | AllocateInNewSpace(SeqAsciiString::kHeaderSize, |
| 1113 | times_1, |
| 1114 | scratch1, |
| 1115 | result, |
| 1116 | scratch2, |
| 1117 | scratch3, |
| 1118 | gc_required, |
| 1119 | TAG_OBJECT); |
| 1120 | |
| 1121 | // Set the map, length and hash field. |
| 1122 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1123 | Immediate(isolate()->factory()->ascii_string_map())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1124 | mov(scratch1, length); |
| 1125 | SmiTag(scratch1); |
| 1126 | mov(FieldOperand(result, String::kLengthOffset), scratch1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1127 | mov(FieldOperand(result, String::kHashFieldOffset), |
| 1128 | Immediate(String::kEmptyHashField)); |
| 1129 | } |
| 1130 | |
| 1131 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1132 | void MacroAssembler::AllocateAsciiString(Register result, |
| 1133 | int length, |
| 1134 | Register scratch1, |
| 1135 | Register scratch2, |
| 1136 | Label* gc_required) { |
| 1137 | ASSERT(length > 0); |
| 1138 | |
| 1139 | // Allocate ascii string in new space. |
| 1140 | AllocateInNewSpace(SeqAsciiString::SizeFor(length), |
| 1141 | result, |
| 1142 | scratch1, |
| 1143 | scratch2, |
| 1144 | gc_required, |
| 1145 | TAG_OBJECT); |
| 1146 | |
| 1147 | // Set the map, length and hash field. |
| 1148 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1149 | Immediate(isolate()->factory()->ascii_string_map())); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1150 | mov(FieldOperand(result, String::kLengthOffset), |
| 1151 | Immediate(Smi::FromInt(length))); |
| 1152 | mov(FieldOperand(result, String::kHashFieldOffset), |
| 1153 | Immediate(String::kEmptyHashField)); |
| 1154 | } |
| 1155 | |
| 1156 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1157 | void MacroAssembler::AllocateConsString(Register result, |
| 1158 | Register scratch1, |
| 1159 | Register scratch2, |
| 1160 | Label* gc_required) { |
| 1161 | // Allocate heap number in new space. |
| 1162 | AllocateInNewSpace(ConsString::kSize, |
| 1163 | result, |
| 1164 | scratch1, |
| 1165 | scratch2, |
| 1166 | gc_required, |
| 1167 | TAG_OBJECT); |
| 1168 | |
| 1169 | // Set the map. The other fields are left uninitialized. |
| 1170 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1171 | Immediate(isolate()->factory()->cons_string_map())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | void MacroAssembler::AllocateAsciiConsString(Register result, |
| 1176 | Register scratch1, |
| 1177 | Register scratch2, |
| 1178 | Label* gc_required) { |
| 1179 | // Allocate heap number in new space. |
| 1180 | AllocateInNewSpace(ConsString::kSize, |
| 1181 | result, |
| 1182 | scratch1, |
| 1183 | scratch2, |
| 1184 | gc_required, |
| 1185 | TAG_OBJECT); |
| 1186 | |
| 1187 | // Set the map. The other fields are left uninitialized. |
| 1188 | mov(FieldOperand(result, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1189 | Immediate(isolate()->factory()->cons_ascii_string_map())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1192 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1193 | // Copy memory, byte-by-byte, from source to destination. Not optimized for |
| 1194 | // long or aligned copies. The contents of scratch and length are destroyed. |
| 1195 | // Source and destination are incremented by length. |
| 1196 | // Many variants of movsb, loop unrolling, word moves, and indexed operands |
| 1197 | // have been tried here already, and this is fastest. |
| 1198 | // A simpler loop is faster on small copies, but 30% slower on large ones. |
| 1199 | // The cld() instruction must have been emitted, to set the direction flag(), |
| 1200 | // before calling this function. |
| 1201 | void MacroAssembler::CopyBytes(Register source, |
| 1202 | Register destination, |
| 1203 | Register length, |
| 1204 | Register scratch) { |
| 1205 | Label loop, done, short_string, short_loop; |
| 1206 | // Experimentation shows that the short string loop is faster if length < 10. |
| 1207 | cmp(Operand(length), Immediate(10)); |
| 1208 | j(less_equal, &short_string); |
| 1209 | |
| 1210 | ASSERT(source.is(esi)); |
| 1211 | ASSERT(destination.is(edi)); |
| 1212 | ASSERT(length.is(ecx)); |
| 1213 | |
| 1214 | // Because source is 4-byte aligned in our uses of this function, |
| 1215 | // we keep source aligned for the rep_movs call by copying the odd bytes |
| 1216 | // at the end of the ranges. |
| 1217 | mov(scratch, Operand(source, length, times_1, -4)); |
| 1218 | mov(Operand(destination, length, times_1, -4), scratch); |
| 1219 | mov(scratch, ecx); |
| 1220 | shr(ecx, 2); |
| 1221 | rep_movs(); |
| 1222 | and_(Operand(scratch), Immediate(0x3)); |
| 1223 | add(destination, Operand(scratch)); |
| 1224 | jmp(&done); |
| 1225 | |
| 1226 | bind(&short_string); |
| 1227 | test(length, Operand(length)); |
| 1228 | j(zero, &done); |
| 1229 | |
| 1230 | bind(&short_loop); |
| 1231 | mov_b(scratch, Operand(source, 0)); |
| 1232 | mov_b(Operand(destination, 0), scratch); |
| 1233 | inc(source); |
| 1234 | inc(destination); |
| 1235 | dec(length); |
| 1236 | j(not_zero, &short_loop); |
| 1237 | |
| 1238 | bind(&done); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1241 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1242 | void MacroAssembler::NegativeZeroTest(Register result, |
| 1243 | Register op, |
| 1244 | Label* then_label) { |
| 1245 | Label ok; |
| 1246 | test(result, Operand(result)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1247 | j(not_zero, &ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1248 | test(op, Operand(op)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1249 | j(sign, then_label); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1250 | bind(&ok); |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | void MacroAssembler::NegativeZeroTest(Register result, |
| 1255 | Register op1, |
| 1256 | Register op2, |
| 1257 | Register scratch, |
| 1258 | Label* then_label) { |
| 1259 | Label ok; |
| 1260 | test(result, Operand(result)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1261 | j(not_zero, &ok); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1262 | mov(scratch, Operand(op1)); |
| 1263 | or_(scratch, Operand(op2)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1264 | j(sign, then_label); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1265 | bind(&ok); |
| 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | void MacroAssembler::TryGetFunctionPrototype(Register function, |
| 1270 | Register result, |
| 1271 | Register scratch, |
| 1272 | Label* miss) { |
| 1273 | // Check that the receiver isn't a smi. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1274 | JumpIfSmi(function, miss); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Check that the function really is a function. |
| 1277 | CmpObjectType(function, JS_FUNCTION_TYPE, result); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1278 | j(not_equal, miss); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1279 | |
| 1280 | // Make sure that the function has an instance prototype. |
| 1281 | Label non_instance; |
| 1282 | movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset)); |
| 1283 | test(scratch, Immediate(1 << Map::kHasNonInstancePrototype)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1284 | j(not_zero, &non_instance); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Get the prototype or initial map from the function. |
| 1287 | mov(result, |
| 1288 | FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
| 1289 | |
| 1290 | // If the prototype or initial map is the hole, don't return it and |
| 1291 | // simply miss the cache instead. This will allow us to allocate a |
| 1292 | // prototype object on-demand in the runtime system. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1293 | cmp(Operand(result), Immediate(isolate()->factory()->the_hole_value())); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1294 | j(equal, miss); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1295 | |
| 1296 | // If the function does not have an initial map, we're done. |
| 1297 | Label done; |
| 1298 | CmpObjectType(result, MAP_TYPE, scratch); |
| 1299 | j(not_equal, &done); |
| 1300 | |
| 1301 | // Get the prototype from the initial map. |
| 1302 | mov(result, FieldOperand(result, Map::kPrototypeOffset)); |
| 1303 | jmp(&done); |
| 1304 | |
| 1305 | // Non-instance prototype: Fetch prototype from constructor field |
| 1306 | // in initial map. |
| 1307 | bind(&non_instance); |
| 1308 | mov(result, FieldOperand(result, Map::kConstructorOffset)); |
| 1309 | |
| 1310 | // All done. |
| 1311 | bind(&done); |
| 1312 | } |
| 1313 | |
| 1314 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1315 | void MacroAssembler::CallStub(CodeStub* stub, unsigned ast_id) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1316 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1317 | call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1321 | MaybeObject* MacroAssembler::TryCallStub(CodeStub* stub) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1322 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1323 | Object* result; |
| 1324 | { MaybeObject* maybe_result = stub->TryGetCode(); |
| 1325 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1326 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1327 | call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1328 | return result; |
| 1329 | } |
| 1330 | |
| 1331 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1332 | void MacroAssembler::TailCallStub(CodeStub* stub) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1333 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1334 | jmp(stub->GetCode(), RelocInfo::CODE_TARGET); |
| 1335 | } |
| 1336 | |
| 1337 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1338 | MaybeObject* MacroAssembler::TryTailCallStub(CodeStub* stub) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1339 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1340 | Object* result; |
| 1341 | { MaybeObject* maybe_result = stub->TryGetCode(); |
| 1342 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1343 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1344 | jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1345 | return result; |
| 1346 | } |
| 1347 | |
| 1348 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1349 | void MacroAssembler::StubReturn(int argc) { |
| 1350 | ASSERT(argc >= 1 && generating_stub()); |
| 1351 | ret((argc - 1) * kPointerSize); |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | void MacroAssembler::IllegalOperation(int num_arguments) { |
| 1356 | if (num_arguments > 0) { |
| 1357 | add(Operand(esp), Immediate(num_arguments * kPointerSize)); |
| 1358 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1359 | mov(eax, Immediate(isolate()->factory()->undefined_value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1363 | void MacroAssembler::IndexFromHash(Register hash, Register index) { |
| 1364 | // The assert checks that the constants for the maximum number of digits |
| 1365 | // for an array index cached in the hash field and the number of bits |
| 1366 | // reserved for it does not conflict. |
| 1367 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 1368 | (1 << String::kArrayIndexValueBits)); |
| 1369 | // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in |
| 1370 | // the low kHashShift bits. |
| 1371 | and_(hash, String::kArrayIndexValueMask); |
| 1372 | STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0); |
| 1373 | if (String::kHashShift > kSmiTagSize) { |
| 1374 | shr(hash, String::kHashShift - kSmiTagSize); |
| 1375 | } |
| 1376 | if (!index.is(hash)) { |
| 1377 | mov(index, hash); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1382 | void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) { |
| 1383 | CallRuntime(Runtime::FunctionForId(id), num_arguments); |
| 1384 | } |
| 1385 | |
| 1386 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1387 | void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1388 | const Runtime::Function* function = Runtime::FunctionForId(id); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1389 | Set(eax, Immediate(function->nargs)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1390 | mov(ebx, Immediate(ExternalReference(function, isolate()))); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1391 | CEntryStub ces(1); |
| 1392 | ces.SaveDoubles(); |
| 1393 | CallStub(&ces); |
| 1394 | } |
| 1395 | |
| 1396 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1397 | MaybeObject* MacroAssembler::TryCallRuntime(Runtime::FunctionId id, |
| 1398 | int num_arguments) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1399 | return TryCallRuntime(Runtime::FunctionForId(id), num_arguments); |
| 1400 | } |
| 1401 | |
| 1402 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1403 | void MacroAssembler::CallRuntime(const Runtime::Function* f, |
| 1404 | int num_arguments) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1405 | // If the expected number of arguments of the runtime function is |
| 1406 | // constant, we check that the actual number of arguments match the |
| 1407 | // expectation. |
| 1408 | if (f->nargs >= 0 && f->nargs != num_arguments) { |
| 1409 | IllegalOperation(num_arguments); |
| 1410 | return; |
| 1411 | } |
| 1412 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1413 | // TODO(1236192): Most runtime routines don't need the number of |
| 1414 | // arguments passed in because it is constant. At some point we |
| 1415 | // should remove this need and make the runtime routine entry code |
| 1416 | // smarter. |
| 1417 | Set(eax, Immediate(num_arguments)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1418 | mov(ebx, Immediate(ExternalReference(f, isolate()))); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1419 | CEntryStub ces(1); |
| 1420 | CallStub(&ces); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1424 | MaybeObject* MacroAssembler::TryCallRuntime(const Runtime::Function* f, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1425 | int num_arguments) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1426 | if (f->nargs >= 0 && f->nargs != num_arguments) { |
| 1427 | IllegalOperation(num_arguments); |
| 1428 | // Since we did not call the stub, there was no allocation failure. |
| 1429 | // Return some non-failure object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1430 | return isolate()->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1433 | // TODO(1236192): Most runtime routines don't need the number of |
| 1434 | // arguments passed in because it is constant. At some point we |
| 1435 | // should remove this need and make the runtime routine entry code |
| 1436 | // smarter. |
| 1437 | Set(eax, Immediate(num_arguments)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1438 | mov(ebx, Immediate(ExternalReference(f, isolate()))); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1439 | CEntryStub ces(1); |
| 1440 | return TryCallStub(&ces); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 1444 | void MacroAssembler::CallExternalReference(ExternalReference ref, |
| 1445 | int num_arguments) { |
| 1446 | mov(eax, Immediate(num_arguments)); |
| 1447 | mov(ebx, Immediate(ref)); |
| 1448 | |
| 1449 | CEntryStub stub(1); |
| 1450 | CallStub(&stub); |
| 1451 | } |
| 1452 | |
| 1453 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1454 | void MacroAssembler::TailCallExternalReference(const ExternalReference& ext, |
| 1455 | int num_arguments, |
| 1456 | int result_size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1457 | // TODO(1236192): Most runtime routines don't need the number of |
| 1458 | // arguments passed in because it is constant. At some point we |
| 1459 | // should remove this need and make the runtime routine entry code |
| 1460 | // smarter. |
| 1461 | Set(eax, Immediate(num_arguments)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1462 | JumpToExternalReference(ext); |
| 1463 | } |
| 1464 | |
| 1465 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1466 | MaybeObject* MacroAssembler::TryTailCallExternalReference( |
| 1467 | const ExternalReference& ext, int num_arguments, int result_size) { |
| 1468 | // TODO(1236192): Most runtime routines don't need the number of |
| 1469 | // arguments passed in because it is constant. At some point we |
| 1470 | // should remove this need and make the runtime routine entry code |
| 1471 | // smarter. |
| 1472 | Set(eax, Immediate(num_arguments)); |
| 1473 | return TryJumpToExternalReference(ext); |
| 1474 | } |
| 1475 | |
| 1476 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1477 | void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid, |
| 1478 | int num_arguments, |
| 1479 | int result_size) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1480 | TailCallExternalReference(ExternalReference(fid, isolate()), |
| 1481 | num_arguments, |
| 1482 | result_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1486 | MaybeObject* MacroAssembler::TryTailCallRuntime(Runtime::FunctionId fid, |
| 1487 | int num_arguments, |
| 1488 | int result_size) { |
| 1489 | return TryTailCallExternalReference( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1490 | ExternalReference(fid, isolate()), num_arguments, result_size); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1494 | // If true, a Handle<T> returned by value from a function with cdecl calling |
| 1495 | // convention will be returned directly as a value of location_ field in a |
| 1496 | // register eax. |
| 1497 | // If false, it is returned as a pointer to a preallocated by caller memory |
| 1498 | // region. Pointer to this region should be passed to a function as an |
| 1499 | // implicit first argument. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1500 | #if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1501 | static const bool kReturnHandlesDirectly = true; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1502 | #else |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1503 | static const bool kReturnHandlesDirectly = false; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1504 | #endif |
| 1505 | |
| 1506 | |
| 1507 | Operand ApiParameterOperand(int index) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1508 | return Operand( |
| 1509 | esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1513 | void MacroAssembler::PrepareCallApiFunction(int argc) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1514 | if (kReturnHandlesDirectly) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1515 | EnterApiExitFrame(argc); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1516 | // When handles are returned directly we don't have to allocate extra |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1517 | // space for and pass an out parameter. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1518 | if (emit_debug_code()) { |
| 1519 | mov(esi, Immediate(BitCast<int32_t>(kZapValue))); |
| 1520 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1521 | } else { |
| 1522 | // We allocate two additional slots: return value and pointer to it. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1523 | EnterApiExitFrame(argc + 2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1524 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1525 | // The argument slots are filled as follows: |
| 1526 | // |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1527 | // n + 1: output slot |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1528 | // n: arg n |
| 1529 | // ... |
| 1530 | // 1: arg1 |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1531 | // 0: pointer to the output slot |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1532 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1533 | lea(esi, Operand(esp, (argc + 1) * kPointerSize)); |
| 1534 | mov(Operand(esp, 0 * kPointerSize), esi); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1535 | if (emit_debug_code()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1536 | mov(Operand(esi, 0), Immediate(0)); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | MaybeObject* MacroAssembler::TryCallApiFunctionAndReturn(ApiFunction* function, |
| 1543 | int stack_space) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1544 | ExternalReference next_address = |
| 1545 | ExternalReference::handle_scope_next_address(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1546 | ExternalReference limit_address = |
| 1547 | ExternalReference::handle_scope_limit_address(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1548 | ExternalReference level_address = |
| 1549 | ExternalReference::handle_scope_level_address(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1550 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1551 | // Allocate HandleScope in callee-save registers. |
| 1552 | mov(ebx, Operand::StaticVariable(next_address)); |
| 1553 | mov(edi, Operand::StaticVariable(limit_address)); |
| 1554 | add(Operand::StaticVariable(level_address), Immediate(1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1555 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1556 | // Call the api function! |
| 1557 | call(function->address(), RelocInfo::RUNTIME_ENTRY); |
| 1558 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1559 | if (!kReturnHandlesDirectly) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1560 | // PrepareCallApiFunction saved pointer to the output slot into |
| 1561 | // callee-save register esi. |
| 1562 | mov(eax, Operand(esi, 0)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1563 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1564 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1565 | Label empty_handle; |
| 1566 | Label prologue; |
| 1567 | Label promote_scheduled_exception; |
| 1568 | Label delete_allocated_handles; |
| 1569 | Label leave_exit_frame; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1570 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1571 | // Check if the result handle holds 0. |
| 1572 | test(eax, Operand(eax)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1573 | j(zero, &empty_handle); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1574 | // It was non-zero. Dereference to get the result value. |
| 1575 | mov(eax, Operand(eax, 0)); |
| 1576 | bind(&prologue); |
| 1577 | // No more valid handles (the result handle was the last one). Restore |
| 1578 | // previous handle scope. |
| 1579 | mov(Operand::StaticVariable(next_address), ebx); |
| 1580 | sub(Operand::StaticVariable(level_address), Immediate(1)); |
| 1581 | Assert(above_equal, "Invalid HandleScope level"); |
| 1582 | cmp(edi, Operand::StaticVariable(limit_address)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1583 | j(not_equal, &delete_allocated_handles); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1584 | bind(&leave_exit_frame); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1585 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1586 | // Check if the function scheduled an exception. |
| 1587 | ExternalReference scheduled_exception_address = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1588 | ExternalReference::scheduled_exception_address(isolate()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1589 | cmp(Operand::StaticVariable(scheduled_exception_address), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1590 | Immediate(isolate()->factory()->the_hole_value())); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1591 | j(not_equal, &promote_scheduled_exception); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1592 | LeaveApiExitFrame(); |
| 1593 | ret(stack_space * kPointerSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1594 | bind(&promote_scheduled_exception); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1595 | MaybeObject* result = |
| 1596 | TryTailCallRuntime(Runtime::kPromoteScheduledException, 0, 1); |
| 1597 | if (result->IsFailure()) { |
| 1598 | return result; |
| 1599 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1600 | bind(&empty_handle); |
| 1601 | // It was zero; the result is undefined. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1602 | mov(eax, isolate()->factory()->undefined_value()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1603 | jmp(&prologue); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1604 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1605 | // HandleScope limit has changed. Delete allocated extensions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1606 | ExternalReference delete_extensions = |
| 1607 | ExternalReference::delete_handle_scope_extensions(isolate()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1608 | bind(&delete_allocated_handles); |
| 1609 | mov(Operand::StaticVariable(limit_address), edi); |
| 1610 | mov(edi, eax); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1611 | mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address())); |
| 1612 | mov(eax, Immediate(delete_extensions)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1613 | call(Operand(eax)); |
| 1614 | mov(eax, edi); |
| 1615 | jmp(&leave_exit_frame); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1616 | |
| 1617 | return result; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1621 | void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1622 | // Set the entry point and jump to the C entry runtime stub. |
| 1623 | mov(ebx, Immediate(ext)); |
| 1624 | CEntryStub ces(1); |
| 1625 | jmp(ces.GetCode(), RelocInfo::CODE_TARGET); |
| 1626 | } |
| 1627 | |
| 1628 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1629 | MaybeObject* MacroAssembler::TryJumpToExternalReference( |
| 1630 | const ExternalReference& ext) { |
| 1631 | // Set the entry point and jump to the C entry runtime stub. |
| 1632 | mov(ebx, Immediate(ext)); |
| 1633 | CEntryStub ces(1); |
| 1634 | return TryTailCallStub(&ces); |
| 1635 | } |
| 1636 | |
| 1637 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1638 | void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) { |
| 1639 | // This macro takes the dst register to make the code more readable |
| 1640 | // at the call sites. However, the dst register has to be ecx to |
| 1641 | // follow the calling convention which requires the call type to be |
| 1642 | // in ecx. |
| 1643 | ASSERT(dst.is(ecx)); |
| 1644 | if (call_kind == CALL_AS_FUNCTION) { |
| 1645 | // Set to some non-zero smi by updating the least significant |
| 1646 | // byte. |
| 1647 | mov_b(Operand(dst), 1 << kSmiTagSize); |
| 1648 | } else { |
| 1649 | // Set to smi zero by clearing the register. |
| 1650 | xor_(dst, Operand(dst)); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1655 | void MacroAssembler::InvokePrologue(const ParameterCount& expected, |
| 1656 | const ParameterCount& actual, |
| 1657 | Handle<Code> code_constant, |
| 1658 | const Operand& code_operand, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1659 | Label* done, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1660 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1661 | Label::Distance done_near, |
| 1662 | const CallWrapper& call_wrapper, |
| 1663 | CallKind call_kind) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1664 | bool definitely_matches = false; |
| 1665 | Label invoke; |
| 1666 | if (expected.is_immediate()) { |
| 1667 | ASSERT(actual.is_immediate()); |
| 1668 | if (expected.immediate() == actual.immediate()) { |
| 1669 | definitely_matches = true; |
| 1670 | } else { |
| 1671 | mov(eax, actual.immediate()); |
| 1672 | const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel; |
| 1673 | if (expected.immediate() == sentinel) { |
| 1674 | // Don't worry about adapting arguments for builtins that |
| 1675 | // don't want that done. Skip adaption code by making it look |
| 1676 | // like we have a match between expected and actual number of |
| 1677 | // arguments. |
| 1678 | definitely_matches = true; |
| 1679 | } else { |
| 1680 | mov(ebx, expected.immediate()); |
| 1681 | } |
| 1682 | } |
| 1683 | } else { |
| 1684 | if (actual.is_immediate()) { |
| 1685 | // Expected is in register, actual is immediate. This is the |
| 1686 | // case when we invoke function values without going through the |
| 1687 | // IC mechanism. |
| 1688 | cmp(expected.reg(), actual.immediate()); |
| 1689 | j(equal, &invoke); |
| 1690 | ASSERT(expected.reg().is(ebx)); |
| 1691 | mov(eax, actual.immediate()); |
| 1692 | } else if (!expected.reg().is(actual.reg())) { |
| 1693 | // Both expected and actual are in (different) registers. This |
| 1694 | // is the case when we invoke functions using call and apply. |
| 1695 | cmp(expected.reg(), Operand(actual.reg())); |
| 1696 | j(equal, &invoke); |
| 1697 | ASSERT(actual.reg().is(eax)); |
| 1698 | ASSERT(expected.reg().is(ebx)); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | if (!definitely_matches) { |
| 1703 | Handle<Code> adaptor = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1704 | isolate()->builtins()->ArgumentsAdaptorTrampoline(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | if (!code_constant.is_null()) { |
| 1706 | mov(edx, Immediate(code_constant)); |
| 1707 | add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 1708 | } else if (!code_operand.is_reg(edx)) { |
| 1709 | mov(edx, code_operand); |
| 1710 | } |
| 1711 | |
| 1712 | if (flag == CALL_FUNCTION) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1713 | call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET)); |
| 1714 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1715 | call(adaptor, RelocInfo::CODE_TARGET); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1716 | call_wrapper.AfterCall(); |
| 1717 | jmp(done, done_near); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | } else { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1719 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1720 | jmp(adaptor, RelocInfo::CODE_TARGET); |
| 1721 | } |
| 1722 | bind(&invoke); |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | void MacroAssembler::InvokeCode(const Operand& code, |
| 1728 | const ParameterCount& expected, |
| 1729 | const ParameterCount& actual, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1730 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1731 | const CallWrapper& call_wrapper, |
| 1732 | CallKind call_kind) { |
| 1733 | Label done; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1734 | InvokePrologue(expected, actual, Handle<Code>::null(), code, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1735 | &done, flag, Label::kNear, call_wrapper, |
| 1736 | call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1737 | if (flag == CALL_FUNCTION) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1738 | call_wrapper.BeforeCall(CallSize(code)); |
| 1739 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1740 | call(code); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1741 | call_wrapper.AfterCall(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1742 | } else { |
| 1743 | ASSERT(flag == JUMP_FUNCTION); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1744 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1745 | jmp(code); |
| 1746 | } |
| 1747 | bind(&done); |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | void MacroAssembler::InvokeCode(Handle<Code> code, |
| 1752 | const ParameterCount& expected, |
| 1753 | const ParameterCount& actual, |
| 1754 | RelocInfo::Mode rmode, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1755 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1756 | const CallWrapper& call_wrapper, |
| 1757 | CallKind call_kind) { |
| 1758 | Label done; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | Operand dummy(eax); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1760 | InvokePrologue(expected, actual, code, dummy, &done, flag, Label::kNear, |
| 1761 | call_wrapper, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1762 | if (flag == CALL_FUNCTION) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1763 | call_wrapper.BeforeCall(CallSize(code, rmode)); |
| 1764 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1765 | call(code, rmode); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1766 | call_wrapper.AfterCall(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1767 | } else { |
| 1768 | ASSERT(flag == JUMP_FUNCTION); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1769 | SetCallKind(ecx, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | jmp(code, rmode); |
| 1771 | } |
| 1772 | bind(&done); |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | void MacroAssembler::InvokeFunction(Register fun, |
| 1777 | const ParameterCount& actual, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1778 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1779 | const CallWrapper& call_wrapper, |
| 1780 | CallKind call_kind) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1781 | ASSERT(fun.is(edi)); |
| 1782 | mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 1783 | mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
| 1784 | mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1785 | SmiUntag(ebx); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1786 | |
| 1787 | ParameterCount expected(ebx); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1788 | InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1789 | expected, actual, flag, call_wrapper, call_kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1793 | void MacroAssembler::InvokeFunction(JSFunction* function, |
| 1794 | const ParameterCount& actual, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1795 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1796 | const CallWrapper& call_wrapper, |
| 1797 | CallKind call_kind) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1798 | ASSERT(function->is_compiled()); |
| 1799 | // Get the function and setup the context. |
| 1800 | mov(edi, Immediate(Handle<JSFunction>(function))); |
| 1801 | mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1802 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1803 | ParameterCount expected(function->shared()->formal_parameter_count()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1804 | if (V8::UseCrankshaft()) { |
| 1805 | // TODO(kasperl): For now, we always call indirectly through the |
| 1806 | // code field in the function to allow recompilation to take effect |
| 1807 | // without changing any of the call sites. |
| 1808 | InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1809 | expected, actual, flag, call_wrapper, call_kind); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1810 | } else { |
| 1811 | Handle<Code> code(function->code()); |
| 1812 | InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1813 | flag, call_wrapper, call_kind); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1814 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1818 | void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, |
| 1819 | InvokeFlag flag, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1820 | const CallWrapper& call_wrapper) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1821 | // Calls are not allowed in some stubs. |
| 1822 | ASSERT(flag == JUMP_FUNCTION || allow_stub_calls()); |
| 1823 | |
| 1824 | // Rely on the assertion to check that the number of provided |
| 1825 | // arguments match the expected number of arguments. Fake a |
| 1826 | // parameter count to avoid emitting code to do the check. |
| 1827 | ParameterCount expected(0); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1828 | GetBuiltinFunction(edi, id); |
| 1829 | InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1830 | expected, expected, flag, call_wrapper, CALL_AS_METHOD); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1833 | void MacroAssembler::GetBuiltinFunction(Register target, |
| 1834 | Builtins::JavaScript id) { |
| 1835 | // Load the JavaScript builtin function from the builtins object. |
| 1836 | mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 1837 | mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset)); |
| 1838 | mov(target, FieldOperand(target, |
| 1839 | JSBuiltinsObject::OffsetOfFunctionWithId(id))); |
| 1840 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1841 | |
| 1842 | void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1843 | ASSERT(!target.is(edi)); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1844 | // Load the JavaScript builtin function from the builtins object. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1845 | GetBuiltinFunction(edi, id); |
| 1846 | // Load the code entry point from the function into the target register. |
| 1847 | mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1851 | void MacroAssembler::LoadContext(Register dst, int context_chain_length) { |
| 1852 | if (context_chain_length > 0) { |
| 1853 | // Move up the chain of contexts to the context containing the slot. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1854 | mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1855 | for (int i = 1; i < context_chain_length; i++) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1856 | mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX))); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1857 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1858 | } else { |
| 1859 | // Slot is in the current function context. Move it into the |
| 1860 | // destination register in case we store into it (the write barrier |
| 1861 | // cannot be allowed to destroy the context in esi). |
| 1862 | mov(dst, esi); |
| 1863 | } |
| 1864 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1865 | // We should not have found a with context by walking the context chain |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1866 | // (i.e., the static scope chain and runtime context chain do not agree). |
| 1867 | // A variable occurring in such a scope should have slot type LOOKUP and |
| 1868 | // not CONTEXT. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1869 | if (emit_debug_code()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1870 | cmp(FieldOperand(dst, HeapObject::kMapOffset), |
| 1871 | isolate()->factory()->with_context_map()); |
| 1872 | Check(not_equal, "Variable resolved to with context."); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1877 | void MacroAssembler::LoadGlobalFunction(int index, Register function) { |
| 1878 | // Load the global or builtins object from the current context. |
| 1879 | mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 1880 | // Load the global context from the global or builtins object. |
| 1881 | mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset)); |
| 1882 | // Load the function from the global context. |
| 1883 | mov(function, Operand(function, Context::SlotOffset(index))); |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | void MacroAssembler::LoadGlobalFunctionInitialMap(Register function, |
| 1888 | Register map) { |
| 1889 | // Load the initial map. The global functions all have initial maps. |
| 1890 | mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1891 | if (emit_debug_code()) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1892 | Label ok, fail; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1893 | CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1894 | jmp(&ok); |
| 1895 | bind(&fail); |
| 1896 | Abort("Global functions must have initial map"); |
| 1897 | bind(&ok); |
| 1898 | } |
| 1899 | } |
| 1900 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1901 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1902 | // Store the value in register src in the safepoint register stack |
| 1903 | // slot for register dst. |
| 1904 | void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) { |
| 1905 | mov(SafepointRegisterSlot(dst), src); |
| 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) { |
| 1910 | mov(SafepointRegisterSlot(dst), src); |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) { |
| 1915 | mov(dst, SafepointRegisterSlot(src)); |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | Operand MacroAssembler::SafepointRegisterSlot(Register reg) { |
| 1920 | return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize); |
| 1921 | } |
| 1922 | |
| 1923 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1924 | int MacroAssembler::SafepointRegisterStackIndex(int reg_code) { |
| 1925 | // The registers are pushed starting with the lowest encoding, |
| 1926 | // which means that lowest encodings are furthest away from |
| 1927 | // the stack pointer. |
| 1928 | ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters); |
| 1929 | return kNumSafepointRegisters - reg_code - 1; |
| 1930 | } |
| 1931 | |
| 1932 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1933 | void MacroAssembler::Ret() { |
| 1934 | ret(0); |
| 1935 | } |
| 1936 | |
| 1937 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1938 | void MacroAssembler::Ret(int bytes_dropped, Register scratch) { |
| 1939 | if (is_uint16(bytes_dropped)) { |
| 1940 | ret(bytes_dropped); |
| 1941 | } else { |
| 1942 | pop(scratch); |
| 1943 | add(Operand(esp), Immediate(bytes_dropped)); |
| 1944 | push(scratch); |
| 1945 | ret(0); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1952 | void MacroAssembler::Drop(int stack_elements) { |
| 1953 | if (stack_elements > 0) { |
| 1954 | add(Operand(esp), Immediate(stack_elements * kPointerSize)); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1959 | void MacroAssembler::Move(Register dst, Register src) { |
| 1960 | if (!dst.is(src)) { |
| 1961 | mov(dst, src); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1966 | void MacroAssembler::Move(Register dst, Handle<Object> value) { |
| 1967 | mov(dst, value); |
| 1968 | } |
| 1969 | |
| 1970 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1971 | void MacroAssembler::SetCounter(StatsCounter* counter, int value) { |
| 1972 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1973 | mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value)); |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) { |
| 1979 | ASSERT(value > 0); |
| 1980 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1981 | Operand operand = Operand::StaticVariable(ExternalReference(counter)); |
| 1982 | if (value == 1) { |
| 1983 | inc(operand); |
| 1984 | } else { |
| 1985 | add(operand, Immediate(value)); |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | |
| 1991 | void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) { |
| 1992 | ASSERT(value > 0); |
| 1993 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1994 | Operand operand = Operand::StaticVariable(ExternalReference(counter)); |
| 1995 | if (value == 1) { |
| 1996 | dec(operand); |
| 1997 | } else { |
| 1998 | sub(operand, Immediate(value)); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2004 | void MacroAssembler::IncrementCounter(Condition cc, |
| 2005 | StatsCounter* counter, |
| 2006 | int value) { |
| 2007 | ASSERT(value > 0); |
| 2008 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 2009 | Label skip; |
| 2010 | j(NegateCondition(cc), &skip); |
| 2011 | pushfd(); |
| 2012 | IncrementCounter(counter, value); |
| 2013 | popfd(); |
| 2014 | bind(&skip); |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | |
| 2019 | void MacroAssembler::DecrementCounter(Condition cc, |
| 2020 | StatsCounter* counter, |
| 2021 | int value) { |
| 2022 | ASSERT(value > 0); |
| 2023 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 2024 | Label skip; |
| 2025 | j(NegateCondition(cc), &skip); |
| 2026 | pushfd(); |
| 2027 | DecrementCounter(counter, value); |
| 2028 | popfd(); |
| 2029 | bind(&skip); |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2034 | void MacroAssembler::Assert(Condition cc, const char* msg) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2035 | if (emit_debug_code()) Check(cc, msg); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2039 | void MacroAssembler::AssertFastElements(Register elements) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2040 | if (emit_debug_code()) { |
| 2041 | Factory* factory = isolate()->factory(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2042 | Label ok; |
| 2043 | cmp(FieldOperand(elements, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2044 | Immediate(factory->fixed_array_map())); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2045 | j(equal, &ok); |
| 2046 | cmp(FieldOperand(elements, HeapObject::kMapOffset), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2047 | Immediate(factory->fixed_double_array_map())); |
| 2048 | j(equal, &ok); |
| 2049 | cmp(FieldOperand(elements, HeapObject::kMapOffset), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2050 | Immediate(factory->fixed_cow_array_map())); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2051 | j(equal, &ok); |
| 2052 | Abort("JSObject with fast elements map has slow elements"); |
| 2053 | bind(&ok); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2058 | void MacroAssembler::Check(Condition cc, const char* msg) { |
| 2059 | Label L; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2060 | j(cc, &L); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2061 | Abort(msg); |
| 2062 | // will not return here |
| 2063 | bind(&L); |
| 2064 | } |
| 2065 | |
| 2066 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2067 | void MacroAssembler::CheckStackAlignment() { |
| 2068 | int frame_alignment = OS::ActivationFrameAlignment(); |
| 2069 | int frame_alignment_mask = frame_alignment - 1; |
| 2070 | if (frame_alignment > kPointerSize) { |
| 2071 | ASSERT(IsPowerOf2(frame_alignment)); |
| 2072 | Label alignment_as_expected; |
| 2073 | test(esp, Immediate(frame_alignment_mask)); |
| 2074 | j(zero, &alignment_as_expected); |
| 2075 | // Abort if stack is not aligned. |
| 2076 | int3(); |
| 2077 | bind(&alignment_as_expected); |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2082 | void MacroAssembler::Abort(const char* msg) { |
| 2083 | // We want to pass the msg string like a smi to avoid GC |
| 2084 | // problems, however msg is not guaranteed to be aligned |
| 2085 | // properly. Instead, we pass an aligned pointer that is |
| 2086 | // a proper v8 smi, but also pass the alignment difference |
| 2087 | // from the real pointer as a smi. |
| 2088 | intptr_t p1 = reinterpret_cast<intptr_t>(msg); |
| 2089 | intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag; |
| 2090 | ASSERT(reinterpret_cast<Object*>(p0)->IsSmi()); |
| 2091 | #ifdef DEBUG |
| 2092 | if (msg != NULL) { |
| 2093 | RecordComment("Abort message: "); |
| 2094 | RecordComment(msg); |
| 2095 | } |
| 2096 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2097 | // Disable stub call restrictions to always allow calls to abort. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2098 | AllowStubCallsScope allow_scope(this, true); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2099 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2100 | push(eax); |
| 2101 | push(Immediate(p0)); |
| 2102 | push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)))); |
| 2103 | CallRuntime(Runtime::kAbort, 2); |
| 2104 | // will not return here |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2105 | int3(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2109 | void MacroAssembler::LoadInstanceDescriptors(Register map, |
| 2110 | Register descriptors) { |
| 2111 | mov(descriptors, |
| 2112 | FieldOperand(map, Map::kInstanceDescriptorsOrBitField3Offset)); |
| 2113 | Label not_smi; |
| 2114 | JumpIfNotSmi(descriptors, ¬_smi); |
| 2115 | mov(descriptors, isolate()->factory()->empty_descriptor_array()); |
| 2116 | bind(¬_smi); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2120 | void MacroAssembler::LoadPowerOf2(XMMRegister dst, |
| 2121 | Register scratch, |
| 2122 | int power) { |
| 2123 | ASSERT(is_uintn(power + HeapNumber::kExponentBias, |
| 2124 | HeapNumber::kExponentBits)); |
| 2125 | mov(scratch, Immediate(power + HeapNumber::kExponentBias)); |
| 2126 | movd(dst, Operand(scratch)); |
| 2127 | psllq(dst, HeapNumber::kMantissaBits); |
| 2128 | } |
| 2129 | |
| 2130 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2131 | void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii( |
| 2132 | Register instance_type, |
| 2133 | Register scratch, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2134 | Label* failure) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2135 | if (!scratch.is(instance_type)) { |
| 2136 | mov(scratch, instance_type); |
| 2137 | } |
| 2138 | and_(scratch, |
| 2139 | kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask); |
| 2140 | cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag); |
| 2141 | j(not_equal, failure); |
| 2142 | } |
| 2143 | |
| 2144 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2145 | void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1, |
| 2146 | Register object2, |
| 2147 | Register scratch1, |
| 2148 | Register scratch2, |
| 2149 | Label* failure) { |
| 2150 | // Check that both objects are not smis. |
| 2151 | ASSERT_EQ(0, kSmiTag); |
| 2152 | mov(scratch1, Operand(object1)); |
| 2153 | and_(scratch1, Operand(object2)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 2154 | JumpIfSmi(scratch1, failure); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2155 | |
| 2156 | // Load instance type for both strings. |
| 2157 | mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset)); |
| 2158 | mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset)); |
| 2159 | movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset)); |
| 2160 | movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset)); |
| 2161 | |
| 2162 | // Check that both are flat ascii strings. |
| 2163 | const int kFlatAsciiStringMask = |
| 2164 | kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask; |
| 2165 | const int kFlatAsciiStringTag = ASCII_STRING_TYPE; |
| 2166 | // Interleave bits from both instance types and compare them in one check. |
| 2167 | ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3)); |
| 2168 | and_(scratch1, kFlatAsciiStringMask); |
| 2169 | and_(scratch2, kFlatAsciiStringMask); |
| 2170 | lea(scratch1, Operand(scratch1, scratch2, times_8, 0)); |
| 2171 | cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3)); |
| 2172 | j(not_equal, failure); |
| 2173 | } |
| 2174 | |
| 2175 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2176 | void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2177 | int frame_alignment = OS::ActivationFrameAlignment(); |
| 2178 | if (frame_alignment != 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2179 | // Make stack end at alignment and make room for num_arguments words |
| 2180 | // and the original value of esp. |
| 2181 | mov(scratch, esp); |
| 2182 | sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2183 | ASSERT(IsPowerOf2(frame_alignment)); |
| 2184 | and_(esp, -frame_alignment); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2185 | mov(Operand(esp, num_arguments * kPointerSize), scratch); |
| 2186 | } else { |
| 2187 | sub(Operand(esp), Immediate(num_arguments * kPointerSize)); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | |
| 2192 | void MacroAssembler::CallCFunction(ExternalReference function, |
| 2193 | int num_arguments) { |
| 2194 | // Trashing eax is ok as it will be the return value. |
| 2195 | mov(Operand(eax), Immediate(function)); |
| 2196 | CallCFunction(eax, num_arguments); |
| 2197 | } |
| 2198 | |
| 2199 | |
| 2200 | void MacroAssembler::CallCFunction(Register function, |
| 2201 | int num_arguments) { |
| 2202 | // Check stack alignment. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2203 | if (emit_debug_code()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2204 | CheckStackAlignment(); |
| 2205 | } |
| 2206 | |
| 2207 | call(Operand(function)); |
| 2208 | if (OS::ActivationFrameAlignment() != 0) { |
| 2209 | mov(esp, Operand(esp, num_arguments * kPointerSize)); |
| 2210 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2211 | add(Operand(esp), Immediate(num_arguments * kPointerSize)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2216 | CodePatcher::CodePatcher(byte* address, int size) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2217 | : address_(address), |
| 2218 | size_(size), |
| 2219 | masm_(Isolate::Current(), address, size + Assembler::kGap) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2220 | // Create a new macro assembler pointing to the address of the code to patch. |
| 2221 | // The size is adjusted with kGap on order for the assembler to generate size |
| 2222 | // bytes of instructions without failing with buffer size constraints. |
| 2223 | ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap); |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | CodePatcher::~CodePatcher() { |
| 2228 | // Indicate that code has changed. |
| 2229 | CPU::FlushICache(address_, size_); |
| 2230 | |
| 2231 | // Check that the code was patched as expected. |
| 2232 | ASSERT(masm_.pc_ == address_ + size_); |
| 2233 | ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap); |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | } } // namespace v8::internal |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2238 | |
| 2239 | #endif // V8_TARGET_ARCH_IA32 |