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