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