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