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