ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 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" |
| 33 | #include "codegen-inl.h" |
| 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | MacroAssembler::MacroAssembler(void* buffer, int size) |
| 45 | : Assembler(buffer, size), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 46 | generating_stub_(false), |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 47 | allow_stub_calls_(true), |
| 48 | code_object_(Heap::undefined_value()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 52 | void MacroAssembler::RecordWriteHelper(Register object, |
| 53 | Register addr, |
| 54 | Register scratch) { |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 55 | if (FLAG_debug_code) { |
| 56 | // Check that the object is not in new space. |
| 57 | Label not_in_new_space; |
| 58 | InNewSpace(object, scratch, not_equal, ¬_in_new_space); |
| 59 | Abort("new-space object passed to RecordWriteHelper"); |
| 60 | bind(¬_in_new_space); |
| 61 | } |
| 62 | |
christian.plesner.hansen@gmail.com | 5a6af92 | 2009-08-12 14:20:51 +0000 | [diff] [blame] | 63 | // Compute the page start address from the heap object pointer, and reuse |
| 64 | // the 'object' register for it. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 65 | and_(object, ~Page::kPageAlignmentMask); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 67 | // Compute number of region covering addr. See Page::GetRegionNumberForAddress |
| 68 | // method for more details. |
| 69 | and_(addr, Page::kPageAlignmentMask); |
| 70 | shr(addr, Page::kRegionSizeLog2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 72 | // Set dirty mark for region. |
| 73 | bts(Operand(object, Page::kDirtyFlagOffset), addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 77 | void MacroAssembler::InNewSpace(Register object, |
| 78 | Register scratch, |
| 79 | Condition cc, |
| 80 | Label* branch) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 81 | ASSERT(cc == equal || cc == not_equal); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 82 | if (Serializer::enabled()) { |
| 83 | // Can't do arithmetic on external references if it might get serialized. |
| 84 | mov(scratch, Operand(object)); |
| 85 | // The mask isn't really an address. We load it as an external reference in |
| 86 | // case the size of the new space is different between the snapshot maker |
| 87 | // and the running system. |
| 88 | and_(Operand(scratch), Immediate(ExternalReference::new_space_mask())); |
| 89 | cmp(Operand(scratch), Immediate(ExternalReference::new_space_start())); |
| 90 | j(cc, branch); |
| 91 | } else { |
| 92 | int32_t new_space_start = reinterpret_cast<int32_t>( |
| 93 | ExternalReference::new_space_start().address()); |
| 94 | lea(scratch, Operand(object, -new_space_start)); |
| 95 | and_(scratch, Heap::NewSpaceMask()); |
| 96 | j(cc, branch); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 101 | void MacroAssembler::RecordWrite(Register object, |
| 102 | int offset, |
| 103 | Register value, |
| 104 | Register scratch) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 105 | // The compiled code assumes that record write doesn't change the |
| 106 | // context register, so we check that none of the clobbered |
| 107 | // registers are esi. |
| 108 | ASSERT(!object.is(esi) && !value.is(esi) && !scratch.is(esi)); |
| 109 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 110 | // First, check if a write barrier is even needed. The tests below |
| 111 | // catch stores of Smis and stores into young gen. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | Label done; |
| 113 | |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 114 | // Skip barrier if writing a smi. |
| 115 | ASSERT_EQ(0, kSmiTag); |
| 116 | test(value, Immediate(kSmiTagMask)); |
| 117 | j(zero, &done); |
| 118 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 119 | InNewSpace(object, value, equal, &done); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | |
whesse@chromium.org | e88a9ed | 2010-04-15 15:07:46 +0000 | [diff] [blame] | 121 | // The offset is relative to a tagged or untagged HeapObject pointer, |
| 122 | // so either offset or offset + kHeapObjectTag must be a |
| 123 | // multiple of kPointerSize. |
| 124 | ASSERT(IsAligned(offset, kPointerSize) || |
| 125 | IsAligned(offset + kHeapObjectTag, kPointerSize)); |
| 126 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 127 | Register dst = scratch; |
| 128 | if (offset != 0) { |
| 129 | lea(dst, Operand(object, offset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 131 | // Array access: calculate the destination address in the same manner as |
| 132 | // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset |
| 133 | // into an array of words. |
| 134 | ASSERT_EQ(1, kSmiTagSize); |
| 135 | ASSERT_EQ(0, kSmiTag); |
| 136 | lea(dst, Operand(object, dst, times_half_pointer_size, |
| 137 | FixedArray::kHeaderSize - kHeapObjectTag)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 139 | RecordWriteHelper(object, dst, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | |
| 141 | bind(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 142 | |
| 143 | // Clobber all input registers when running with the debug-code flag |
| 144 | // turned on to provoke errors. |
| 145 | if (FLAG_debug_code) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 146 | mov(object, Immediate(BitCast<int32_t>(kZapValue))); |
| 147 | mov(value, Immediate(BitCast<int32_t>(kZapValue))); |
| 148 | mov(scratch, Immediate(BitCast<int32_t>(kZapValue))); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 149 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 153 | void MacroAssembler::RecordWrite(Register object, |
| 154 | Register address, |
| 155 | Register value) { |
| 156 | // The compiled code assumes that record write doesn't change the |
| 157 | // context register, so we check that none of the clobbered |
| 158 | // registers are esi. |
| 159 | ASSERT(!object.is(esi) && !value.is(esi) && !address.is(esi)); |
| 160 | |
| 161 | // First, check if a write barrier is even needed. The tests below |
| 162 | // catch stores of Smis and stores into young gen. |
| 163 | Label done; |
| 164 | |
| 165 | // Skip barrier if writing a smi. |
| 166 | ASSERT_EQ(0, kSmiTag); |
| 167 | test(value, Immediate(kSmiTagMask)); |
| 168 | j(zero, &done); |
| 169 | |
| 170 | InNewSpace(object, value, equal, &done); |
| 171 | |
| 172 | RecordWriteHelper(object, address, value); |
| 173 | |
| 174 | bind(&done); |
| 175 | |
| 176 | // Clobber all input registers when running with the debug-code flag |
| 177 | // turned on to provoke errors. |
| 178 | if (FLAG_debug_code) { |
| 179 | mov(object, Immediate(BitCast<int32_t>(kZapValue))); |
| 180 | mov(address, Immediate(BitCast<int32_t>(kZapValue))); |
| 181 | mov(value, Immediate(BitCast<int32_t>(kZapValue))); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 186 | void MacroAssembler::StackLimitCheck(Label* on_stack_overflow) { |
| 187 | cmp(esp, |
| 188 | Operand::StaticVariable(ExternalReference::address_of_stack_limit())); |
| 189 | j(below, on_stack_overflow); |
| 190 | } |
| 191 | |
| 192 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 193 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | void MacroAssembler::SaveRegistersToMemory(RegList regs) { |
| 195 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 196 | // Copy the content of registers to memory location. |
| 197 | for (int i = 0; i < kNumJSCallerSaved; i++) { |
| 198 | int r = JSCallerSavedCode(i); |
| 199 | if ((regs & (1 << r)) != 0) { |
| 200 | Register reg = { r }; |
| 201 | ExternalReference reg_addr = |
| 202 | ExternalReference(Debug_Address::Register(i)); |
| 203 | mov(Operand::StaticVariable(reg_addr), reg); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void MacroAssembler::RestoreRegistersFromMemory(RegList regs) { |
| 210 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 211 | // Copy the content of memory location to registers. |
| 212 | for (int i = kNumJSCallerSaved; --i >= 0;) { |
| 213 | int r = JSCallerSavedCode(i); |
| 214 | if ((regs & (1 << r)) != 0) { |
| 215 | Register reg = { r }; |
| 216 | ExternalReference reg_addr = |
| 217 | ExternalReference(Debug_Address::Register(i)); |
| 218 | mov(reg, Operand::StaticVariable(reg_addr)); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void MacroAssembler::PushRegistersFromMemory(RegList regs) { |
| 225 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 226 | // Push the content of the memory location to the stack. |
| 227 | for (int i = 0; i < kNumJSCallerSaved; i++) { |
| 228 | int r = JSCallerSavedCode(i); |
| 229 | if ((regs & (1 << r)) != 0) { |
| 230 | ExternalReference reg_addr = |
| 231 | ExternalReference(Debug_Address::Register(i)); |
| 232 | push(Operand::StaticVariable(reg_addr)); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void MacroAssembler::PopRegistersToMemory(RegList regs) { |
| 239 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 240 | // Pop the content from the stack to the memory location. |
| 241 | for (int i = kNumJSCallerSaved; --i >= 0;) { |
| 242 | int r = JSCallerSavedCode(i); |
| 243 | if ((regs & (1 << r)) != 0) { |
| 244 | ExternalReference reg_addr = |
| 245 | ExternalReference(Debug_Address::Register(i)); |
| 246 | pop(Operand::StaticVariable(reg_addr)); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void MacroAssembler::CopyRegistersFromStackToMemory(Register base, |
| 253 | Register scratch, |
| 254 | RegList regs) { |
| 255 | ASSERT((regs & ~kJSCallerSaved) == 0); |
| 256 | // Copy the content of the stack to the memory location and adjust base. |
| 257 | for (int i = kNumJSCallerSaved; --i >= 0;) { |
| 258 | int r = JSCallerSavedCode(i); |
| 259 | if ((regs & (1 << r)) != 0) { |
| 260 | mov(scratch, Operand(base, 0)); |
| 261 | ExternalReference reg_addr = |
| 262 | ExternalReference(Debug_Address::Register(i)); |
| 263 | mov(Operand::StaticVariable(reg_addr), scratch); |
| 264 | lea(base, Operand(base, kPointerSize)); |
| 265 | } |
| 266 | } |
| 267 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 268 | |
| 269 | void MacroAssembler::DebugBreak() { |
| 270 | Set(eax, Immediate(0)); |
| 271 | mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak))); |
| 272 | CEntryStub ces(1); |
| 273 | call(ces.GetCode(), RelocInfo::DEBUG_BREAK); |
| 274 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 275 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | |
| 277 | void MacroAssembler::Set(Register dst, const Immediate& x) { |
| 278 | if (x.is_zero()) { |
| 279 | xor_(dst, Operand(dst)); // shorter than mov |
| 280 | } else { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 281 | mov(dst, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | void MacroAssembler::Set(const Operand& dst, const Immediate& x) { |
| 287 | mov(dst, x); |
| 288 | } |
| 289 | |
| 290 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 291 | void MacroAssembler::CmpObjectType(Register heap_object, |
| 292 | InstanceType type, |
| 293 | Register map) { |
| 294 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 295 | CmpInstanceType(map, type); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | void MacroAssembler::CmpInstanceType(Register map, InstanceType type) { |
| 300 | cmpb(FieldOperand(map, Map::kInstanceTypeOffset), |
| 301 | static_cast<int8_t>(type)); |
| 302 | } |
| 303 | |
| 304 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 305 | void MacroAssembler::CheckMap(Register obj, |
| 306 | Handle<Map> map, |
| 307 | Label* fail, |
| 308 | bool is_heap_object) { |
| 309 | if (!is_heap_object) { |
| 310 | test(obj, Immediate(kSmiTagMask)); |
| 311 | j(zero, fail); |
| 312 | } |
| 313 | cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map)); |
| 314 | j(not_equal, fail); |
| 315 | } |
| 316 | |
| 317 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 318 | Condition MacroAssembler::IsObjectStringType(Register heap_object, |
| 319 | Register map, |
| 320 | Register instance_type) { |
| 321 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 322 | movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset)); |
| 323 | ASSERT(kNotStringTag != 0); |
| 324 | test(instance_type, Immediate(kIsNotStringMask)); |
| 325 | return zero; |
| 326 | } |
| 327 | |
| 328 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 329 | void MacroAssembler::IsObjectJSObjectType(Register heap_object, |
| 330 | Register map, |
| 331 | Register scratch, |
| 332 | Label* fail) { |
| 333 | mov(map, FieldOperand(heap_object, HeapObject::kMapOffset)); |
| 334 | IsInstanceJSObjectType(map, scratch, fail); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | void MacroAssembler::IsInstanceJSObjectType(Register map, |
| 339 | Register scratch, |
| 340 | Label* fail) { |
| 341 | movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset)); |
| 342 | sub(Operand(scratch), Immediate(FIRST_JS_OBJECT_TYPE)); |
| 343 | cmp(scratch, LAST_JS_OBJECT_TYPE - FIRST_JS_OBJECT_TYPE); |
| 344 | j(above, fail); |
| 345 | } |
| 346 | |
| 347 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | void MacroAssembler::FCmp() { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 349 | if (CpuFeatures::IsSupported(CMOV)) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 350 | fucomip(); |
| 351 | ffree(0); |
| 352 | fincstp(); |
| 353 | } else { |
| 354 | fucompp(); |
| 355 | push(eax); |
| 356 | fnstsw_ax(); |
| 357 | sahf(); |
| 358 | pop(eax); |
| 359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 363 | void MacroAssembler::AbortIfNotNumber(Register object) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 364 | Label ok; |
| 365 | test(object, Immediate(kSmiTagMask)); |
| 366 | j(zero, &ok); |
| 367 | cmp(FieldOperand(object, HeapObject::kMapOffset), |
| 368 | Factory::heap_number_map()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 369 | Assert(equal, "Operand not a number"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 370 | bind(&ok); |
| 371 | } |
| 372 | |
| 373 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 374 | void MacroAssembler::AbortIfNotSmi(Register object) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 375 | test(object, Immediate(kSmiTagMask)); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 376 | Assert(equal, "Operand not a smi"); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 380 | void MacroAssembler::EnterFrame(StackFrame::Type type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | push(ebp); |
| 382 | mov(ebp, Operand(esp)); |
| 383 | push(esi); |
| 384 | push(Immediate(Smi::FromInt(type))); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 385 | push(Immediate(CodeObject())); |
| 386 | if (FLAG_debug_code) { |
| 387 | cmp(Operand(esp, 0), Immediate(Factory::undefined_value())); |
| 388 | Check(not_equal, "code object not properly patched"); |
| 389 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 393 | void MacroAssembler::LeaveFrame(StackFrame::Type type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | if (FLAG_debug_code) { |
| 395 | cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset), |
| 396 | Immediate(Smi::FromInt(type))); |
| 397 | Check(equal, "stack frame types must match"); |
| 398 | } |
| 399 | leave(); |
| 400 | } |
| 401 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 402 | void MacroAssembler::EnterExitFramePrologue(ExitFrame::Mode mode) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 403 | // Setup the frame structure on the stack. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 404 | ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 405 | ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize); |
| 406 | ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize); |
| 407 | push(ebp); |
| 408 | mov(ebp, Operand(esp)); |
| 409 | |
| 410 | // Reserve room for entry stack pointer and push the debug marker. |
| 411 | ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 412 | push(Immediate(0)); // Saved entry sp, patched before call. |
| 413 | push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 414 | |
| 415 | // Save the frame pointer and the context in top. |
| 416 | ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address); |
| 417 | ExternalReference context_address(Top::k_context_address); |
| 418 | mov(Operand::StaticVariable(c_entry_fp_address), ebp); |
| 419 | mov(Operand::StaticVariable(context_address), esi); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 420 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 421 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 422 | void MacroAssembler::EnterExitFrameEpilogue(ExitFrame::Mode mode, int argc) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 423 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 424 | // Save the state of all registers to the stack from the memory |
| 425 | // location. This is needed to allow nested break points. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 426 | if (mode == ExitFrame::MODE_DEBUG) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 427 | // TODO(1243899): This should be symmetric to |
| 428 | // CopyRegistersFromStackToMemory() but it isn't! esp is assumed |
| 429 | // correct here, but computed for the other call. Very error |
| 430 | // prone! FIX THIS. Actually there are deeper problems with |
| 431 | // register saving than this asymmetry (see the bug report |
| 432 | // associated with this issue). |
| 433 | PushRegistersFromMemory(kJSCallerSaved); |
| 434 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 435 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 436 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 437 | // Reserve space for arguments. |
| 438 | sub(Operand(esp), Immediate(argc * kPointerSize)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 439 | |
| 440 | // Get the required frame alignment for the OS. |
| 441 | static const int kFrameAlignment = OS::ActivationFrameAlignment(); |
| 442 | if (kFrameAlignment > 0) { |
| 443 | ASSERT(IsPowerOf2(kFrameAlignment)); |
| 444 | and_(esp, -kFrameAlignment); |
| 445 | } |
| 446 | |
| 447 | // Patch the saved entry sp. |
| 448 | mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp); |
| 449 | } |
| 450 | |
| 451 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 452 | void MacroAssembler::EnterExitFrame(ExitFrame::Mode mode) { |
| 453 | EnterExitFramePrologue(mode); |
| 454 | |
| 455 | // Setup argc and argv in callee-saved registers. |
| 456 | int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize; |
| 457 | mov(edi, Operand(eax)); |
| 458 | lea(esi, Operand(ebp, eax, times_4, offset)); |
| 459 | |
| 460 | EnterExitFrameEpilogue(mode, 2); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void MacroAssembler::EnterApiExitFrame(ExitFrame::Mode mode, |
| 465 | int stack_space, |
| 466 | int argc) { |
| 467 | EnterExitFramePrologue(mode); |
| 468 | |
| 469 | int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize; |
| 470 | lea(esi, Operand(ebp, (stack_space * kPointerSize) + offset)); |
| 471 | |
| 472 | EnterExitFrameEpilogue(mode, argc); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | void MacroAssembler::LeaveExitFrame(ExitFrame::Mode mode) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 477 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 478 | // Restore the memory copy of the registers by digging them out from |
| 479 | // the stack. This is needed to allow nested break points. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 480 | if (mode == ExitFrame::MODE_DEBUG) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 481 | // It's okay to clobber register ebx below because we don't need |
| 482 | // the function pointer after this. |
| 483 | const int kCallerSavedSize = kNumJSCallerSaved * kPointerSize; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 484 | int kOffset = ExitFrameConstants::kCodeOffset - kCallerSavedSize; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 485 | lea(ebx, Operand(ebp, kOffset)); |
| 486 | CopyRegistersFromStackToMemory(ebx, ecx, kJSCallerSaved); |
| 487 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 488 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 489 | |
| 490 | // Get the return address from the stack and restore the frame pointer. |
| 491 | mov(ecx, Operand(ebp, 1 * kPointerSize)); |
| 492 | mov(ebp, Operand(ebp, 0 * kPointerSize)); |
| 493 | |
| 494 | // Pop the arguments and the receiver from the caller stack. |
| 495 | lea(esp, Operand(esi, 1 * kPointerSize)); |
| 496 | |
| 497 | // Restore current context from top and clear it in debug mode. |
| 498 | ExternalReference context_address(Top::k_context_address); |
| 499 | mov(esi, Operand::StaticVariable(context_address)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 500 | #ifdef DEBUG |
| 501 | mov(Operand::StaticVariable(context_address), Immediate(0)); |
| 502 | #endif |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 503 | |
| 504 | // Push the return address to get ready to return. |
| 505 | push(ecx); |
| 506 | |
| 507 | // Clear the top frame. |
| 508 | ExternalReference c_entry_fp_address(Top::k_c_entry_fp_address); |
| 509 | mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0)); |
| 510 | } |
| 511 | |
| 512 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | void MacroAssembler::PushTryHandler(CodeLocation try_location, |
| 514 | HandlerType type) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 515 | // Adjust this code if not the case. |
| 516 | ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 517 | // The pc (return address) is already on TOS. |
| 518 | if (try_location == IN_JAVASCRIPT) { |
| 519 | if (type == TRY_CATCH_HANDLER) { |
| 520 | push(Immediate(StackHandler::TRY_CATCH)); |
| 521 | } else { |
| 522 | push(Immediate(StackHandler::TRY_FINALLY)); |
| 523 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | push(ebp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | } else { |
| 526 | ASSERT(try_location == IN_JS_ENTRY); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 527 | // The frame pointer does not point to a JS frame so we save NULL |
| 528 | // for ebp. We expect the code throwing an exception to check ebp |
| 529 | // before dereferencing it to restore the context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | push(Immediate(StackHandler::ENTRY)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 531 | push(Immediate(0)); // NULL frame pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 533 | // Save the current handler as the next handler. |
| 534 | push(Operand::StaticVariable(ExternalReference(Top::k_handler_address))); |
| 535 | // Link this handler as the new current one. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | mov(Operand::StaticVariable(ExternalReference(Top::k_handler_address)), esp); |
| 537 | } |
| 538 | |
| 539 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 540 | void MacroAssembler::PopTryHandler() { |
| 541 | ASSERT_EQ(0, StackHandlerConstants::kNextOffset); |
| 542 | pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address))); |
| 543 | add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize)); |
| 544 | } |
| 545 | |
| 546 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 547 | void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 548 | Register scratch, |
| 549 | Label* miss) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 550 | Label same_contexts; |
| 551 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | ASSERT(!holder_reg.is(scratch)); |
| 553 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 554 | // Load current lexical context from the stack frame. |
| 555 | mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 556 | |
| 557 | // When generating debug code, make sure the lexical context is set. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | if (FLAG_debug_code) { |
| 559 | cmp(Operand(scratch), Immediate(0)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 560 | Check(not_equal, "we should not have an empty lexical context"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 561 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 562 | // Load the global context of the current context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize; |
| 564 | mov(scratch, FieldOperand(scratch, offset)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 565 | mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset)); |
| 566 | |
| 567 | // Check the context is a global context. |
| 568 | if (FLAG_debug_code) { |
| 569 | push(scratch); |
| 570 | // Read the first word and compare to global_context_map. |
| 571 | mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset)); |
| 572 | cmp(scratch, Factory::global_context_map()); |
| 573 | Check(equal, "JSGlobalObject::global_context should be a global context."); |
| 574 | pop(scratch); |
| 575 | } |
| 576 | |
| 577 | // Check if both contexts are the same. |
| 578 | cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
| 579 | j(equal, &same_contexts, taken); |
| 580 | |
| 581 | // Compare security tokens, save holder_reg on the stack so we can use it |
| 582 | // as a temporary register. |
| 583 | // |
| 584 | // TODO(119): avoid push(holder_reg)/pop(holder_reg) |
| 585 | push(holder_reg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 586 | // Check that the security token in the calling global object is |
| 587 | // compatible with the security token in the receiving global |
| 588 | // object. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 589 | mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset)); |
| 590 | |
| 591 | // Check the context is a global context. |
| 592 | if (FLAG_debug_code) { |
| 593 | cmp(holder_reg, Factory::null_value()); |
| 594 | Check(not_equal, "JSGlobalProxy::context() should not be null."); |
| 595 | |
| 596 | push(holder_reg); |
| 597 | // Read the first word and compare to global_context_map(), |
| 598 | mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset)); |
| 599 | cmp(holder_reg, Factory::global_context_map()); |
| 600 | Check(equal, "JSGlobalObject::global_context should be a global context."); |
| 601 | pop(holder_reg); |
| 602 | } |
| 603 | |
| 604 | int token_offset = Context::kHeaderSize + |
| 605 | Context::SECURITY_TOKEN_INDEX * kPointerSize; |
| 606 | mov(scratch, FieldOperand(scratch, token_offset)); |
| 607 | cmp(scratch, FieldOperand(holder_reg, token_offset)); |
| 608 | pop(holder_reg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | j(not_equal, miss, not_taken); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 610 | |
| 611 | bind(&same_contexts); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 615 | void MacroAssembler::LoadAllocationTopHelper(Register result, |
| 616 | Register result_end, |
| 617 | Register scratch, |
| 618 | AllocationFlags flags) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 619 | ExternalReference new_space_allocation_top = |
| 620 | ExternalReference::new_space_allocation_top_address(); |
| 621 | |
| 622 | // Just return if allocation top is already known. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 623 | if ((flags & RESULT_CONTAINS_TOP) != 0) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 624 | // No use of scratch if allocation top is provided. |
| 625 | ASSERT(scratch.is(no_reg)); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 626 | #ifdef DEBUG |
| 627 | // Assert that result actually contains top on entry. |
| 628 | cmp(result, Operand::StaticVariable(new_space_allocation_top)); |
| 629 | Check(equal, "Unexpected allocation top"); |
| 630 | #endif |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 631 | return; |
| 632 | } |
| 633 | |
| 634 | // Move address of new object to result. Use scratch register if available. |
| 635 | if (scratch.is(no_reg)) { |
| 636 | mov(result, Operand::StaticVariable(new_space_allocation_top)); |
| 637 | } else { |
| 638 | ASSERT(!scratch.is(result_end)); |
| 639 | mov(Operand(scratch), Immediate(new_space_allocation_top)); |
| 640 | mov(result, Operand(scratch, 0)); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | |
| 645 | void MacroAssembler::UpdateAllocationTopHelper(Register result_end, |
| 646 | Register scratch) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 647 | if (FLAG_debug_code) { |
| 648 | test(result_end, Immediate(kObjectAlignmentMask)); |
| 649 | Check(zero, "Unaligned allocation in new space"); |
| 650 | } |
| 651 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 652 | ExternalReference new_space_allocation_top = |
| 653 | ExternalReference::new_space_allocation_top_address(); |
| 654 | |
| 655 | // Update new top. Use scratch if available. |
| 656 | if (scratch.is(no_reg)) { |
| 657 | mov(Operand::StaticVariable(new_space_allocation_top), result_end); |
| 658 | } else { |
| 659 | mov(Operand(scratch, 0), result_end); |
| 660 | } |
| 661 | } |
| 662 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 663 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 664 | void MacroAssembler::AllocateInNewSpace(int object_size, |
| 665 | Register result, |
| 666 | Register result_end, |
| 667 | Register scratch, |
| 668 | Label* gc_required, |
| 669 | AllocationFlags flags) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 670 | ASSERT(!result.is(result_end)); |
| 671 | |
| 672 | // Load address of new object into result. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 673 | LoadAllocationTopHelper(result, result_end, scratch, flags); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 674 | |
| 675 | // Calculate new top and bail out if new space is exhausted. |
| 676 | ExternalReference new_space_allocation_limit = |
| 677 | ExternalReference::new_space_allocation_limit_address(); |
| 678 | lea(result_end, Operand(result, object_size)); |
| 679 | cmp(result_end, Operand::StaticVariable(new_space_allocation_limit)); |
| 680 | j(above, gc_required, not_taken); |
| 681 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 682 | // Tag result if requested. |
| 683 | if ((flags & TAG_OBJECT) != 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 684 | lea(result, Operand(result, kHeapObjectTag)); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 685 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 686 | |
| 687 | // Update allocation top. |
| 688 | UpdateAllocationTopHelper(result_end, scratch); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 692 | void MacroAssembler::AllocateInNewSpace(int header_size, |
| 693 | ScaleFactor element_size, |
| 694 | Register element_count, |
| 695 | Register result, |
| 696 | Register result_end, |
| 697 | Register scratch, |
| 698 | Label* gc_required, |
| 699 | AllocationFlags flags) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 700 | ASSERT(!result.is(result_end)); |
| 701 | |
| 702 | // Load address of new object into result. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 703 | LoadAllocationTopHelper(result, result_end, scratch, flags); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 704 | |
| 705 | // Calculate new top and bail out if new space is exhausted. |
| 706 | ExternalReference new_space_allocation_limit = |
| 707 | ExternalReference::new_space_allocation_limit_address(); |
| 708 | lea(result_end, Operand(result, element_count, element_size, header_size)); |
| 709 | cmp(result_end, Operand::StaticVariable(new_space_allocation_limit)); |
| 710 | j(above, gc_required); |
| 711 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 712 | // Tag result if requested. |
| 713 | if ((flags & TAG_OBJECT) != 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 714 | lea(result, Operand(result, kHeapObjectTag)); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 715 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 716 | |
| 717 | // Update allocation top. |
| 718 | UpdateAllocationTopHelper(result_end, scratch); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 722 | void MacroAssembler::AllocateInNewSpace(Register object_size, |
| 723 | Register result, |
| 724 | Register result_end, |
| 725 | Register scratch, |
| 726 | Label* gc_required, |
| 727 | AllocationFlags flags) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 728 | ASSERT(!result.is(result_end)); |
| 729 | |
| 730 | // Load address of new object into result. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 731 | LoadAllocationTopHelper(result, result_end, scratch, flags); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 732 | |
| 733 | // Calculate new top and bail out if new space is exhausted. |
| 734 | ExternalReference new_space_allocation_limit = |
| 735 | ExternalReference::new_space_allocation_limit_address(); |
| 736 | if (!object_size.is(result_end)) { |
| 737 | mov(result_end, object_size); |
| 738 | } |
| 739 | add(result_end, Operand(result)); |
| 740 | cmp(result_end, Operand::StaticVariable(new_space_allocation_limit)); |
| 741 | j(above, gc_required, not_taken); |
| 742 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 743 | // Tag result if requested. |
| 744 | if ((flags & TAG_OBJECT) != 0) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 745 | lea(result, Operand(result, kHeapObjectTag)); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 746 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 747 | |
| 748 | // Update allocation top. |
| 749 | UpdateAllocationTopHelper(result_end, scratch); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | |
| 753 | void MacroAssembler::UndoAllocationInNewSpace(Register object) { |
| 754 | ExternalReference new_space_allocation_top = |
| 755 | ExternalReference::new_space_allocation_top_address(); |
| 756 | |
| 757 | // Make sure the object has no tag before resetting top. |
| 758 | and_(Operand(object), Immediate(~kHeapObjectTagMask)); |
| 759 | #ifdef DEBUG |
| 760 | cmp(object, Operand::StaticVariable(new_space_allocation_top)); |
| 761 | Check(below, "Undo allocation of non allocated memory"); |
| 762 | #endif |
| 763 | mov(Operand::StaticVariable(new_space_allocation_top), object); |
| 764 | } |
| 765 | |
| 766 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 767 | void MacroAssembler::AllocateHeapNumber(Register result, |
| 768 | Register scratch1, |
| 769 | Register scratch2, |
| 770 | Label* gc_required) { |
| 771 | // Allocate heap number in new space. |
| 772 | AllocateInNewSpace(HeapNumber::kSize, |
| 773 | result, |
| 774 | scratch1, |
| 775 | scratch2, |
| 776 | gc_required, |
| 777 | TAG_OBJECT); |
| 778 | |
| 779 | // Set the map. |
| 780 | mov(FieldOperand(result, HeapObject::kMapOffset), |
| 781 | Immediate(Factory::heap_number_map())); |
| 782 | } |
| 783 | |
| 784 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 785 | void MacroAssembler::AllocateTwoByteString(Register result, |
| 786 | Register length, |
| 787 | Register scratch1, |
| 788 | Register scratch2, |
| 789 | Register scratch3, |
| 790 | Label* gc_required) { |
| 791 | // Calculate the number of bytes needed for the characters in the string while |
| 792 | // observing object alignment. |
| 793 | ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 794 | ASSERT(kShortSize == 2); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 795 | // scratch1 = length * 2 + kObjectAlignmentMask. |
| 796 | lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 797 | and_(Operand(scratch1), Immediate(~kObjectAlignmentMask)); |
| 798 | |
| 799 | // Allocate two byte string in new space. |
| 800 | AllocateInNewSpace(SeqTwoByteString::kHeaderSize, |
| 801 | times_1, |
| 802 | scratch1, |
| 803 | result, |
| 804 | scratch2, |
| 805 | scratch3, |
| 806 | gc_required, |
| 807 | TAG_OBJECT); |
| 808 | |
| 809 | // Set the map, length and hash field. |
| 810 | mov(FieldOperand(result, HeapObject::kMapOffset), |
| 811 | Immediate(Factory::string_map())); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 812 | mov(scratch1, length); |
| 813 | SmiTag(scratch1); |
| 814 | mov(FieldOperand(result, String::kLengthOffset), scratch1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 815 | mov(FieldOperand(result, String::kHashFieldOffset), |
| 816 | Immediate(String::kEmptyHashField)); |
| 817 | } |
| 818 | |
| 819 | |
| 820 | void MacroAssembler::AllocateAsciiString(Register result, |
| 821 | Register length, |
| 822 | Register scratch1, |
| 823 | Register scratch2, |
| 824 | Register scratch3, |
| 825 | Label* gc_required) { |
| 826 | // Calculate the number of bytes needed for the characters in the string while |
| 827 | // observing object alignment. |
| 828 | ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0); |
| 829 | mov(scratch1, length); |
| 830 | ASSERT(kCharSize == 1); |
| 831 | add(Operand(scratch1), Immediate(kObjectAlignmentMask)); |
| 832 | and_(Operand(scratch1), Immediate(~kObjectAlignmentMask)); |
| 833 | |
| 834 | // Allocate ascii string in new space. |
| 835 | AllocateInNewSpace(SeqAsciiString::kHeaderSize, |
| 836 | times_1, |
| 837 | scratch1, |
| 838 | result, |
| 839 | scratch2, |
| 840 | scratch3, |
| 841 | gc_required, |
| 842 | TAG_OBJECT); |
| 843 | |
| 844 | // Set the map, length and hash field. |
| 845 | mov(FieldOperand(result, HeapObject::kMapOffset), |
| 846 | Immediate(Factory::ascii_string_map())); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 847 | mov(scratch1, length); |
| 848 | SmiTag(scratch1); |
| 849 | mov(FieldOperand(result, String::kLengthOffset), scratch1); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 850 | mov(FieldOperand(result, String::kHashFieldOffset), |
| 851 | Immediate(String::kEmptyHashField)); |
| 852 | } |
| 853 | |
| 854 | |
| 855 | void MacroAssembler::AllocateConsString(Register result, |
| 856 | Register scratch1, |
| 857 | Register scratch2, |
| 858 | Label* gc_required) { |
| 859 | // Allocate heap number in new space. |
| 860 | AllocateInNewSpace(ConsString::kSize, |
| 861 | result, |
| 862 | scratch1, |
| 863 | scratch2, |
| 864 | gc_required, |
| 865 | TAG_OBJECT); |
| 866 | |
| 867 | // Set the map. The other fields are left uninitialized. |
| 868 | mov(FieldOperand(result, HeapObject::kMapOffset), |
| 869 | Immediate(Factory::cons_string_map())); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | void MacroAssembler::AllocateAsciiConsString(Register result, |
| 874 | Register scratch1, |
| 875 | Register scratch2, |
| 876 | Label* gc_required) { |
| 877 | // Allocate heap number in new space. |
| 878 | AllocateInNewSpace(ConsString::kSize, |
| 879 | result, |
| 880 | scratch1, |
| 881 | scratch2, |
| 882 | gc_required, |
| 883 | TAG_OBJECT); |
| 884 | |
| 885 | // Set the map. The other fields are left uninitialized. |
| 886 | mov(FieldOperand(result, HeapObject::kMapOffset), |
| 887 | Immediate(Factory::cons_ascii_string_map())); |
| 888 | } |
| 889 | |
| 890 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 891 | void MacroAssembler::NegativeZeroTest(CodeGenerator* cgen, |
| 892 | Register result, |
| 893 | Register op, |
| 894 | JumpTarget* then_target) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 895 | JumpTarget ok; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 896 | test(result, Operand(result)); |
| 897 | ok.Branch(not_zero, taken); |
| 898 | test(op, Operand(op)); |
| 899 | then_target->Branch(sign, not_taken); |
| 900 | ok.Bind(); |
| 901 | } |
| 902 | |
| 903 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | void MacroAssembler::NegativeZeroTest(Register result, |
| 905 | Register op, |
| 906 | Label* then_label) { |
| 907 | Label ok; |
| 908 | test(result, Operand(result)); |
| 909 | j(not_zero, &ok, taken); |
| 910 | test(op, Operand(op)); |
| 911 | j(sign, then_label, not_taken); |
| 912 | bind(&ok); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | void MacroAssembler::NegativeZeroTest(Register result, |
| 917 | Register op1, |
| 918 | Register op2, |
| 919 | Register scratch, |
| 920 | Label* then_label) { |
| 921 | Label ok; |
| 922 | test(result, Operand(result)); |
| 923 | j(not_zero, &ok, taken); |
| 924 | mov(scratch, Operand(op1)); |
| 925 | or_(scratch, Operand(op2)); |
| 926 | j(sign, then_label, not_taken); |
| 927 | bind(&ok); |
| 928 | } |
| 929 | |
| 930 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 931 | void MacroAssembler::TryGetFunctionPrototype(Register function, |
| 932 | Register result, |
| 933 | Register scratch, |
| 934 | Label* miss) { |
| 935 | // Check that the receiver isn't a smi. |
| 936 | test(function, Immediate(kSmiTagMask)); |
| 937 | j(zero, miss, not_taken); |
| 938 | |
| 939 | // Check that the function really is a function. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 940 | CmpObjectType(function, JS_FUNCTION_TYPE, result); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 941 | j(not_equal, miss, not_taken); |
| 942 | |
| 943 | // Make sure that the function has an instance prototype. |
| 944 | Label non_instance; |
| 945 | movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset)); |
| 946 | test(scratch, Immediate(1 << Map::kHasNonInstancePrototype)); |
| 947 | j(not_zero, &non_instance, not_taken); |
| 948 | |
| 949 | // Get the prototype or initial map from the function. |
| 950 | mov(result, |
| 951 | FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset)); |
| 952 | |
| 953 | // If the prototype or initial map is the hole, don't return it and |
| 954 | // simply miss the cache instead. This will allow us to allocate a |
| 955 | // prototype object on-demand in the runtime system. |
| 956 | cmp(Operand(result), Immediate(Factory::the_hole_value())); |
| 957 | j(equal, miss, not_taken); |
| 958 | |
| 959 | // If the function does not have an initial map, we're done. |
| 960 | Label done; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 961 | CmpObjectType(result, MAP_TYPE, scratch); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 962 | j(not_equal, &done); |
| 963 | |
| 964 | // Get the prototype from the initial map. |
| 965 | mov(result, FieldOperand(result, Map::kPrototypeOffset)); |
| 966 | jmp(&done); |
| 967 | |
| 968 | // Non-instance prototype: Fetch prototype from constructor field |
| 969 | // in initial map. |
| 970 | bind(&non_instance); |
| 971 | mov(result, FieldOperand(result, Map::kConstructorOffset)); |
| 972 | |
| 973 | // All done. |
| 974 | bind(&done); |
| 975 | } |
| 976 | |
| 977 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 978 | void MacroAssembler::CallStub(CodeStub* stub) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 979 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 980 | call(stub->GetCode(), RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 984 | Object* MacroAssembler::TryCallStub(CodeStub* stub) { |
| 985 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
| 986 | Object* result = stub->TryGetCode(); |
| 987 | if (!result->IsFailure()) { |
| 988 | call(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET); |
| 989 | } |
| 990 | return result; |
| 991 | } |
| 992 | |
| 993 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 994 | void MacroAssembler::TailCallStub(CodeStub* stub) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 995 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 996 | jmp(stub->GetCode(), RelocInfo::CODE_TARGET); |
| 997 | } |
| 998 | |
| 999 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1000 | Object* MacroAssembler::TryTailCallStub(CodeStub* stub) { |
| 1001 | ASSERT(allow_stub_calls()); // Calls are not allowed in some stubs. |
| 1002 | Object* result = stub->TryGetCode(); |
| 1003 | if (!result->IsFailure()) { |
| 1004 | jmp(Handle<Code>(Code::cast(result)), RelocInfo::CODE_TARGET); |
| 1005 | } |
| 1006 | return result; |
| 1007 | } |
| 1008 | |
| 1009 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | void MacroAssembler::StubReturn(int argc) { |
| 1011 | ASSERT(argc >= 1 && generating_stub()); |
| 1012 | ret((argc - 1) * kPointerSize); |
| 1013 | } |
| 1014 | |
| 1015 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1016 | void MacroAssembler::IllegalOperation(int num_arguments) { |
| 1017 | if (num_arguments > 0) { |
| 1018 | add(Operand(esp), Immediate(num_arguments * kPointerSize)); |
| 1019 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1020 | mov(eax, Immediate(Factory::undefined_value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) { |
| 1025 | CallRuntime(Runtime::FunctionForId(id), num_arguments); |
| 1026 | } |
| 1027 | |
| 1028 | |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1029 | Object* MacroAssembler::TryCallRuntime(Runtime::FunctionId id, |
| 1030 | int num_arguments) { |
| 1031 | return TryCallRuntime(Runtime::FunctionForId(id), num_arguments); |
| 1032 | } |
| 1033 | |
| 1034 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1035 | void MacroAssembler::CallRuntime(Runtime::Function* f, int num_arguments) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1036 | // If the expected number of arguments of the runtime function is |
| 1037 | // constant, we check that the actual number of arguments match the |
| 1038 | // expectation. |
| 1039 | if (f->nargs >= 0 && f->nargs != num_arguments) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1040 | IllegalOperation(num_arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1044 | // TODO(1236192): Most runtime routines don't need the number of |
| 1045 | // arguments passed in because it is constant. At some point we |
| 1046 | // should remove this need and make the runtime routine entry code |
| 1047 | // smarter. |
| 1048 | Set(eax, Immediate(num_arguments)); |
| 1049 | mov(ebx, Immediate(ExternalReference(f))); |
| 1050 | CEntryStub ces(1); |
| 1051 | CallStub(&ces); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1055 | Object* MacroAssembler::TryCallRuntime(Runtime::Function* f, |
| 1056 | int num_arguments) { |
| 1057 | if (f->nargs >= 0 && f->nargs != num_arguments) { |
| 1058 | IllegalOperation(num_arguments); |
| 1059 | // Since we did not call the stub, there was no allocation failure. |
| 1060 | // Return some non-failure object. |
| 1061 | return Heap::undefined_value(); |
| 1062 | } |
| 1063 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1064 | // TODO(1236192): Most runtime routines don't need the number of |
| 1065 | // arguments passed in because it is constant. At some point we |
| 1066 | // should remove this need and make the runtime routine entry code |
| 1067 | // smarter. |
| 1068 | Set(eax, Immediate(num_arguments)); |
| 1069 | mov(ebx, Immediate(ExternalReference(f))); |
| 1070 | CEntryStub ces(1); |
| 1071 | return TryCallStub(&ces); |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame^] | 1075 | void MacroAssembler::CallExternalReference(ExternalReference ref, |
| 1076 | int num_arguments) { |
| 1077 | mov(eax, Immediate(num_arguments)); |
| 1078 | mov(ebx, Immediate(ref)); |
| 1079 | |
| 1080 | CEntryStub stub(1); |
| 1081 | CallStub(&stub); |
| 1082 | } |
| 1083 | |
| 1084 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1085 | void MacroAssembler::TailCallExternalReference(const ExternalReference& ext, |
| 1086 | int num_arguments, |
| 1087 | int result_size) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 1088 | // TODO(1236192): Most runtime routines don't need the number of |
| 1089 | // arguments passed in because it is constant. At some point we |
| 1090 | // should remove this need and make the runtime routine entry code |
| 1091 | // smarter. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1092 | Set(eax, Immediate(num_arguments)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1093 | JumpToExternalReference(ext); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid, |
| 1098 | int num_arguments, |
| 1099 | int result_size) { |
| 1100 | TailCallExternalReference(ExternalReference(fid), num_arguments, result_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1104 | void MacroAssembler::PushHandleScope(Register scratch) { |
| 1105 | // Push the number of extensions, smi-tagged so the gc will ignore it. |
| 1106 | ExternalReference extensions_address = |
| 1107 | ExternalReference::handle_scope_extensions_address(); |
| 1108 | mov(scratch, Operand::StaticVariable(extensions_address)); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame^] | 1109 | SmiTag(scratch); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1110 | push(scratch); |
| 1111 | mov(Operand::StaticVariable(extensions_address), Immediate(0)); |
| 1112 | // Push next and limit pointers which will be wordsize aligned and |
| 1113 | // hence automatically smi tagged. |
| 1114 | ExternalReference next_address = |
| 1115 | ExternalReference::handle_scope_next_address(); |
| 1116 | push(Operand::StaticVariable(next_address)); |
| 1117 | ExternalReference limit_address = |
| 1118 | ExternalReference::handle_scope_limit_address(); |
| 1119 | push(Operand::StaticVariable(limit_address)); |
| 1120 | } |
| 1121 | |
| 1122 | |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1123 | Object* MacroAssembler::PopHandleScopeHelper(Register saved, |
| 1124 | Register scratch, |
| 1125 | bool gc_allowed) { |
| 1126 | Object* result = NULL; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1127 | ExternalReference extensions_address = |
| 1128 | ExternalReference::handle_scope_extensions_address(); |
| 1129 | Label write_back; |
| 1130 | mov(scratch, Operand::StaticVariable(extensions_address)); |
| 1131 | cmp(Operand(scratch), Immediate(0)); |
| 1132 | j(equal, &write_back); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame^] | 1133 | push(saved); |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1134 | if (gc_allowed) { |
| 1135 | CallRuntime(Runtime::kDeleteHandleScopeExtensions, 0); |
| 1136 | } else { |
| 1137 | result = TryCallRuntime(Runtime::kDeleteHandleScopeExtensions, 0); |
| 1138 | if (result->IsFailure()) return result; |
| 1139 | } |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame^] | 1140 | pop(saved); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1141 | |
| 1142 | bind(&write_back); |
| 1143 | ExternalReference limit_address = |
| 1144 | ExternalReference::handle_scope_limit_address(); |
| 1145 | pop(Operand::StaticVariable(limit_address)); |
| 1146 | ExternalReference next_address = |
| 1147 | ExternalReference::handle_scope_next_address(); |
| 1148 | pop(Operand::StaticVariable(next_address)); |
| 1149 | pop(scratch); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame^] | 1150 | SmiUntag(scratch); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1151 | mov(Operand::StaticVariable(extensions_address), scratch); |
kmillikin@chromium.org | 2d5475f | 2009-12-20 18:15:52 +0000 | [diff] [blame] | 1152 | |
| 1153 | return result; |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void MacroAssembler::PopHandleScope(Register saved, Register scratch) { |
| 1158 | PopHandleScopeHelper(saved, scratch, true); |
| 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | Object* MacroAssembler::TryPopHandleScope(Register saved, Register scratch) { |
| 1163 | return PopHandleScopeHelper(saved, scratch, false); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1167 | void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | // 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] | 1169 | mov(ebx, Immediate(ext)); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1170 | CEntryStub ces(1); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1171 | jmp(ces.GetCode(), RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | void MacroAssembler::InvokePrologue(const ParameterCount& expected, |
| 1176 | const ParameterCount& actual, |
| 1177 | Handle<Code> code_constant, |
| 1178 | const Operand& code_operand, |
| 1179 | Label* done, |
| 1180 | InvokeFlag flag) { |
| 1181 | bool definitely_matches = false; |
| 1182 | Label invoke; |
| 1183 | if (expected.is_immediate()) { |
| 1184 | ASSERT(actual.is_immediate()); |
| 1185 | if (expected.immediate() == actual.immediate()) { |
| 1186 | definitely_matches = true; |
| 1187 | } else { |
| 1188 | mov(eax, actual.immediate()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1189 | const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel; |
| 1190 | if (expected.immediate() == sentinel) { |
| 1191 | // Don't worry about adapting arguments for builtins that |
| 1192 | // don't want that done. Skip adaption code by making it look |
| 1193 | // like we have a match between expected and actual number of |
| 1194 | // arguments. |
| 1195 | definitely_matches = true; |
| 1196 | } else { |
| 1197 | mov(ebx, expected.immediate()); |
| 1198 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } else { |
| 1201 | if (actual.is_immediate()) { |
| 1202 | // Expected is in register, actual is immediate. This is the |
| 1203 | // case when we invoke function values without going through the |
| 1204 | // IC mechanism. |
| 1205 | cmp(expected.reg(), actual.immediate()); |
| 1206 | j(equal, &invoke); |
| 1207 | ASSERT(expected.reg().is(ebx)); |
| 1208 | mov(eax, actual.immediate()); |
| 1209 | } else if (!expected.reg().is(actual.reg())) { |
| 1210 | // Both expected and actual are in (different) registers. This |
| 1211 | // is the case when we invoke functions using call and apply. |
| 1212 | cmp(expected.reg(), Operand(actual.reg())); |
| 1213 | j(equal, &invoke); |
| 1214 | ASSERT(actual.reg().is(eax)); |
| 1215 | ASSERT(expected.reg().is(ebx)); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (!definitely_matches) { |
| 1220 | Handle<Code> adaptor = |
| 1221 | Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)); |
| 1222 | if (!code_constant.is_null()) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1223 | mov(edx, Immediate(code_constant)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1224 | add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 1225 | } else if (!code_operand.is_reg(edx)) { |
| 1226 | mov(edx, code_operand); |
| 1227 | } |
| 1228 | |
| 1229 | if (flag == CALL_FUNCTION) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1230 | call(adaptor, RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | jmp(done); |
| 1232 | } else { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1233 | jmp(adaptor, RelocInfo::CODE_TARGET); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | } |
| 1235 | bind(&invoke); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | void MacroAssembler::InvokeCode(const Operand& code, |
| 1241 | const ParameterCount& expected, |
| 1242 | const ParameterCount& actual, |
| 1243 | InvokeFlag flag) { |
| 1244 | Label done; |
| 1245 | InvokePrologue(expected, actual, Handle<Code>::null(), code, &done, flag); |
| 1246 | if (flag == CALL_FUNCTION) { |
| 1247 | call(code); |
| 1248 | } else { |
| 1249 | ASSERT(flag == JUMP_FUNCTION); |
| 1250 | jmp(code); |
| 1251 | } |
| 1252 | bind(&done); |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | void MacroAssembler::InvokeCode(Handle<Code> code, |
| 1257 | const ParameterCount& expected, |
| 1258 | const ParameterCount& actual, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1259 | RelocInfo::Mode rmode, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1260 | InvokeFlag flag) { |
| 1261 | Label done; |
| 1262 | Operand dummy(eax); |
| 1263 | InvokePrologue(expected, actual, code, dummy, &done, flag); |
| 1264 | if (flag == CALL_FUNCTION) { |
| 1265 | call(code, rmode); |
| 1266 | } else { |
| 1267 | ASSERT(flag == JUMP_FUNCTION); |
| 1268 | jmp(code, rmode); |
| 1269 | } |
| 1270 | bind(&done); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | void MacroAssembler::InvokeFunction(Register fun, |
| 1275 | const ParameterCount& actual, |
| 1276 | InvokeFlag flag) { |
| 1277 | ASSERT(fun.is(edi)); |
| 1278 | mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 1279 | mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
| 1280 | mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1281 | SmiUntag(ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1282 | mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset)); |
| 1283 | lea(edx, FieldOperand(edx, Code::kHeaderSize)); |
| 1284 | |
| 1285 | ParameterCount expected(ebx); |
| 1286 | InvokeCode(Operand(edx), expected, actual, flag); |
| 1287 | } |
| 1288 | |
| 1289 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1290 | void MacroAssembler::InvokeFunction(JSFunction* function, |
| 1291 | const ParameterCount& actual, |
| 1292 | InvokeFlag flag) { |
| 1293 | ASSERT(function->is_compiled()); |
| 1294 | // Get the function and setup the context. |
| 1295 | mov(edi, Immediate(Handle<JSFunction>(function))); |
| 1296 | mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1298 | // Invoke the cached code. |
| 1299 | Handle<Code> code(function->code()); |
| 1300 | ParameterCount expected(function->shared()->formal_parameter_count()); |
| 1301 | InvokeCode(code, expected, actual, RelocInfo::CODE_TARGET, flag); |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id, InvokeFlag flag) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1306 | // Calls are not allowed in some stubs. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1307 | ASSERT(flag == JUMP_FUNCTION || allow_stub_calls()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | |
| 1309 | // Rely on the assertion to check that the number of provided |
| 1310 | // arguments match the expected number of arguments. Fake a |
| 1311 | // parameter count to avoid emitting code to do the check. |
| 1312 | ParameterCount expected(0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1313 | GetBuiltinEntry(edx, id); |
| 1314 | InvokeCode(Operand(edx), expected, expected, flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1319 | ASSERT(!target.is(edi)); |
| 1320 | |
| 1321 | // Load the builtins object into target register. |
| 1322 | mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX))); |
| 1323 | mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset)); |
| 1324 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1325 | // Load the JavaScript builtin function from the builtins object. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1326 | mov(edi, FieldOperand(target, JSBuiltinsObject::OffsetOfFunctionWithId(id))); |
| 1327 | |
| 1328 | // Load the code entry point from the builtins object. |
| 1329 | mov(target, FieldOperand(target, JSBuiltinsObject::OffsetOfCodeWithId(id))); |
| 1330 | if (FLAG_debug_code) { |
| 1331 | // Make sure the code objects in the builtins object and in the |
| 1332 | // builtin function are the same. |
| 1333 | push(target); |
| 1334 | mov(target, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 1335 | mov(target, FieldOperand(target, SharedFunctionInfo::kCodeOffset)); |
| 1336 | cmp(target, Operand(esp, 0)); |
| 1337 | Assert(equal, "Builtin code object changed"); |
| 1338 | pop(target); |
| 1339 | } |
| 1340 | lea(target, FieldOperand(target, Code::kHeaderSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1344 | void MacroAssembler::LoadContext(Register dst, int context_chain_length) { |
| 1345 | if (context_chain_length > 0) { |
| 1346 | // Move up the chain of contexts to the context containing the slot. |
| 1347 | mov(dst, Operand(esi, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 1348 | // Load the function context (which is the incoming, outer context). |
| 1349 | mov(dst, FieldOperand(dst, JSFunction::kContextOffset)); |
| 1350 | for (int i = 1; i < context_chain_length; i++) { |
| 1351 | mov(dst, Operand(dst, Context::SlotOffset(Context::CLOSURE_INDEX))); |
| 1352 | mov(dst, FieldOperand(dst, JSFunction::kContextOffset)); |
| 1353 | } |
| 1354 | // The context may be an intermediate context, not a function context. |
| 1355 | mov(dst, Operand(dst, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 1356 | } else { // Slot is in the current function context. |
| 1357 | // The context may be an intermediate context, not a function context. |
| 1358 | mov(dst, Operand(esi, Context::SlotOffset(Context::FCONTEXT_INDEX))); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1364 | void MacroAssembler::Ret() { |
| 1365 | ret(0); |
| 1366 | } |
| 1367 | |
| 1368 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1369 | void MacroAssembler::Drop(int stack_elements) { |
| 1370 | if (stack_elements > 0) { |
| 1371 | add(Operand(esp), Immediate(stack_elements * kPointerSize)); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | void MacroAssembler::Move(Register dst, Handle<Object> value) { |
| 1377 | mov(dst, value); |
| 1378 | } |
| 1379 | |
| 1380 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1381 | void MacroAssembler::SetCounter(StatsCounter* counter, int value) { |
| 1382 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1383 | mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value)); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) { |
| 1389 | ASSERT(value > 0); |
| 1390 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1391 | Operand operand = Operand::StaticVariable(ExternalReference(counter)); |
| 1392 | if (value == 1) { |
| 1393 | inc(operand); |
| 1394 | } else { |
| 1395 | add(operand, Immediate(value)); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) { |
| 1402 | ASSERT(value > 0); |
| 1403 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1404 | Operand operand = Operand::StaticVariable(ExternalReference(counter)); |
| 1405 | if (value == 1) { |
| 1406 | dec(operand); |
| 1407 | } else { |
| 1408 | sub(operand, Immediate(value)); |
| 1409 | } |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1414 | void MacroAssembler::IncrementCounter(Condition cc, |
| 1415 | StatsCounter* counter, |
| 1416 | int value) { |
| 1417 | ASSERT(value > 0); |
| 1418 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1419 | Label skip; |
| 1420 | j(NegateCondition(cc), &skip); |
| 1421 | pushfd(); |
| 1422 | IncrementCounter(counter, value); |
| 1423 | popfd(); |
| 1424 | bind(&skip); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | void MacroAssembler::DecrementCounter(Condition cc, |
| 1430 | StatsCounter* counter, |
| 1431 | int value) { |
| 1432 | ASSERT(value > 0); |
| 1433 | if (FLAG_native_code_counters && counter->Enabled()) { |
| 1434 | Label skip; |
| 1435 | j(NegateCondition(cc), &skip); |
| 1436 | pushfd(); |
| 1437 | DecrementCounter(counter, value); |
| 1438 | popfd(); |
| 1439 | bind(&skip); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1444 | void MacroAssembler::Assert(Condition cc, const char* msg) { |
| 1445 | if (FLAG_debug_code) Check(cc, msg); |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | void MacroAssembler::Check(Condition cc, const char* msg) { |
| 1450 | Label L; |
| 1451 | j(cc, &L, taken); |
| 1452 | Abort(msg); |
| 1453 | // will not return here |
| 1454 | bind(&L); |
| 1455 | } |
| 1456 | |
| 1457 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1458 | void MacroAssembler::CheckStackAlignment() { |
| 1459 | int frame_alignment = OS::ActivationFrameAlignment(); |
| 1460 | int frame_alignment_mask = frame_alignment - 1; |
| 1461 | if (frame_alignment > kPointerSize) { |
| 1462 | ASSERT(IsPowerOf2(frame_alignment)); |
| 1463 | Label alignment_as_expected; |
| 1464 | test(esp, Immediate(frame_alignment_mask)); |
| 1465 | j(zero, &alignment_as_expected); |
| 1466 | // Abort if stack is not aligned. |
| 1467 | int3(); |
| 1468 | bind(&alignment_as_expected); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | void MacroAssembler::Abort(const char* msg) { |
| 1474 | // We want to pass the msg string like a smi to avoid GC |
| 1475 | // problems, however msg is not guaranteed to be aligned |
| 1476 | // properly. Instead, we pass an aligned pointer that is |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1477 | // a proper v8 smi, but also pass the alignment difference |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1478 | // from the real pointer as a smi. |
| 1479 | intptr_t p1 = reinterpret_cast<intptr_t>(msg); |
| 1480 | intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag; |
| 1481 | ASSERT(reinterpret_cast<Object*>(p0)->IsSmi()); |
| 1482 | #ifdef DEBUG |
| 1483 | if (msg != NULL) { |
| 1484 | RecordComment("Abort message: "); |
| 1485 | RecordComment(msg); |
| 1486 | } |
| 1487 | #endif |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1488 | // Disable stub call restrictions to always allow calls to abort. |
| 1489 | set_allow_stub_calls(true); |
| 1490 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1491 | push(eax); |
| 1492 | push(Immediate(p0)); |
| 1493 | push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0)))); |
| 1494 | CallRuntime(Runtime::kAbort, 2); |
| 1495 | // will not return here |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1496 | int3(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1500 | void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii( |
| 1501 | Register instance_type, |
| 1502 | Register scratch, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1503 | Label* failure) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1504 | if (!scratch.is(instance_type)) { |
| 1505 | mov(scratch, instance_type); |
| 1506 | } |
| 1507 | and_(scratch, |
| 1508 | kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask); |
| 1509 | cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag); |
| 1510 | j(not_equal, failure); |
| 1511 | } |
| 1512 | |
| 1513 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1514 | void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1, |
| 1515 | Register object2, |
| 1516 | Register scratch1, |
| 1517 | Register scratch2, |
| 1518 | Label* failure) { |
| 1519 | // Check that both objects are not smis. |
| 1520 | ASSERT_EQ(0, kSmiTag); |
| 1521 | mov(scratch1, Operand(object1)); |
| 1522 | and_(scratch1, Operand(object2)); |
| 1523 | test(scratch1, Immediate(kSmiTagMask)); |
| 1524 | j(zero, failure); |
| 1525 | |
| 1526 | // Load instance type for both strings. |
| 1527 | mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset)); |
| 1528 | mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset)); |
| 1529 | movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset)); |
| 1530 | movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset)); |
| 1531 | |
| 1532 | // Check that both are flat ascii strings. |
| 1533 | const int kFlatAsciiStringMask = |
| 1534 | kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask; |
| 1535 | const int kFlatAsciiStringTag = ASCII_STRING_TYPE; |
| 1536 | // Interleave bits from both instance types and compare them in one check. |
| 1537 | ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3)); |
| 1538 | and_(scratch1, kFlatAsciiStringMask); |
| 1539 | and_(scratch2, kFlatAsciiStringMask); |
| 1540 | lea(scratch1, Operand(scratch1, scratch2, times_8, 0)); |
| 1541 | cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3)); |
| 1542 | j(not_equal, failure); |
| 1543 | } |
| 1544 | |
| 1545 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1546 | void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) { |
| 1547 | int frameAlignment = OS::ActivationFrameAlignment(); |
| 1548 | if (frameAlignment != 0) { |
| 1549 | // Make stack end at alignment and make room for num_arguments words |
| 1550 | // and the original value of esp. |
| 1551 | mov(scratch, esp); |
| 1552 | sub(Operand(esp), Immediate((num_arguments + 1) * kPointerSize)); |
| 1553 | ASSERT(IsPowerOf2(frameAlignment)); |
| 1554 | and_(esp, -frameAlignment); |
| 1555 | mov(Operand(esp, num_arguments * kPointerSize), scratch); |
| 1556 | } else { |
| 1557 | sub(Operand(esp), Immediate(num_arguments * kPointerSize)); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | void MacroAssembler::CallCFunction(ExternalReference function, |
| 1563 | int num_arguments) { |
| 1564 | // Trashing eax is ok as it will be the return value. |
| 1565 | mov(Operand(eax), Immediate(function)); |
| 1566 | CallCFunction(eax, num_arguments); |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | void MacroAssembler::CallCFunction(Register function, |
| 1571 | int num_arguments) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1572 | // Check stack alignment. |
| 1573 | if (FLAG_debug_code) { |
| 1574 | CheckStackAlignment(); |
| 1575 | } |
| 1576 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1577 | call(Operand(function)); |
| 1578 | if (OS::ActivationFrameAlignment() != 0) { |
| 1579 | mov(esp, Operand(esp, num_arguments * kPointerSize)); |
| 1580 | } else { |
| 1581 | add(Operand(esp), Immediate(num_arguments * sizeof(int32_t))); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1586 | CodePatcher::CodePatcher(byte* address, int size) |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1587 | : address_(address), size_(size), masm_(address, size + Assembler::kGap) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1588 | // 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] | 1589 | // The size is adjusted with kGap on order for the assembler to generate size |
| 1590 | // bytes of instructions without failing with buffer size constraints. |
| 1591 | ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | CodePatcher::~CodePatcher() { |
| 1596 | // Indicate that code has changed. |
| 1597 | CPU::FlushICache(address_, size_); |
| 1598 | |
| 1599 | // Check that the code was patched as expected. |
| 1600 | ASSERT(masm_.pc_ == address_ + size_); |
| 1601 | ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap); |
| 1602 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1603 | |
| 1604 | |
| 1605 | } } // namespace v8::internal |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1606 | |
| 1607 | #endif // V8_TARGET_ARCH_IA32 |