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