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