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