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