1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
30 #if V8_TARGET_ARCH_IA32
32 #include "bootstrapper.h"
34 #include "cpu-profiler.h"
37 #include "serialize.h"
42 // -------------------------------------------------------------------------
43 // MacroAssembler implementation.
45 MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
46 : Assembler(arg_isolate, buffer, size),
47 generating_stub_(false),
48 allow_stub_calls_(true),
50 if (isolate() != NULL) {
51 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
57 void MacroAssembler::InNewSpace(
62 Label::Distance condition_met_distance) {
63 ASSERT(cc == equal || cc == not_equal);
64 if (scratch.is(object)) {
65 and_(scratch, Immediate(~Page::kPageAlignmentMask));
67 mov(scratch, Immediate(~Page::kPageAlignmentMask));
68 and_(scratch, object);
70 // Check that we can use a test_b.
71 ASSERT(MemoryChunk::IN_FROM_SPACE < 8);
72 ASSERT(MemoryChunk::IN_TO_SPACE < 8);
73 int mask = (1 << MemoryChunk::IN_FROM_SPACE)
74 | (1 << MemoryChunk::IN_TO_SPACE);
75 // If non-zero, the page belongs to new-space.
76 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
77 static_cast<uint8_t>(mask));
78 j(cc, condition_met, condition_met_distance);
82 void MacroAssembler::RememberedSetHelper(
83 Register object, // Only used for debug checks.
86 SaveFPRegsMode save_fp,
87 MacroAssembler::RememberedSetFinalAction and_then) {
89 if (emit_debug_code()) {
91 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
95 // Load store buffer top.
96 ExternalReference store_buffer =
97 ExternalReference::store_buffer_top(isolate());
98 mov(scratch, Operand::StaticVariable(store_buffer));
99 // Store pointer to buffer.
100 mov(Operand(scratch, 0), addr);
101 // Increment buffer top.
102 add(scratch, Immediate(kPointerSize));
103 // Write back new top of buffer.
104 mov(Operand::StaticVariable(store_buffer), scratch);
105 // Call stub on end of buffer.
106 // Check for end of buffer.
107 test(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
108 if (and_then == kReturnAtEnd) {
109 Label buffer_overflowed;
110 j(not_equal, &buffer_overflowed, Label::kNear);
112 bind(&buffer_overflowed);
114 ASSERT(and_then == kFallThroughAtEnd);
115 j(equal, &done, Label::kNear);
117 StoreBufferOverflowStub store_buffer_overflow =
118 StoreBufferOverflowStub(save_fp);
119 CallStub(&store_buffer_overflow);
120 if (and_then == kReturnAtEnd) {
123 ASSERT(and_then == kFallThroughAtEnd);
129 void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
130 XMMRegister scratch_reg,
131 Register result_reg) {
134 pxor(scratch_reg, scratch_reg);
135 cvtsd2si(result_reg, input_reg);
136 test(result_reg, Immediate(0xFFFFFF00));
137 j(zero, &done, Label::kNear);
138 cmp(result_reg, Immediate(0x80000000));
139 j(equal, &conv_failure, Label::kNear);
140 mov(result_reg, Immediate(0));
141 setcc(above, result_reg);
142 sub(result_reg, Immediate(1));
143 and_(result_reg, Immediate(255));
144 jmp(&done, Label::kNear);
146 Set(result_reg, Immediate(0));
147 ucomisd(input_reg, scratch_reg);
148 j(below, &done, Label::kNear);
149 Set(result_reg, Immediate(255));
154 void MacroAssembler::ClampUint8(Register reg) {
156 test(reg, Immediate(0xFFFFFF00));
157 j(zero, &done, Label::kNear);
158 setcc(negative, reg); // 1 if negative, 0 if positive.
159 dec_b(reg); // 0 if negative, 255 if positive.
164 static double kUint32Bias =
165 static_cast<double>(static_cast<uint32_t>(0xFFFFFFFF)) + 1;
168 void MacroAssembler::LoadUint32(XMMRegister dst,
170 XMMRegister scratch) {
172 cmp(src, Immediate(0));
174 Operand(reinterpret_cast<int32_t>(&kUint32Bias), RelocInfo::NONE32));
176 j(not_sign, &done, Label::kNear);
182 void MacroAssembler::RecordWriteArray(Register object,
185 SaveFPRegsMode save_fp,
186 RememberedSetAction remembered_set_action,
187 SmiCheck smi_check) {
188 // First, check if a write barrier is even needed. The tests below
189 // catch stores of Smis.
192 // Skip barrier if writing a smi.
193 if (smi_check == INLINE_SMI_CHECK) {
194 ASSERT_EQ(0, kSmiTag);
195 test(value, Immediate(kSmiTagMask));
199 // Array access: calculate the destination address in the same manner as
200 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
201 // into an array of words.
202 Register dst = index;
203 lea(dst, Operand(object, index, times_half_pointer_size,
204 FixedArray::kHeaderSize - kHeapObjectTag));
207 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
211 // Clobber clobbered input registers when running with the debug-code flag
212 // turned on to provoke errors.
213 if (emit_debug_code()) {
214 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
215 mov(index, Immediate(BitCast<int32_t>(kZapValue)));
220 void MacroAssembler::RecordWriteField(
225 SaveFPRegsMode save_fp,
226 RememberedSetAction remembered_set_action,
227 SmiCheck smi_check) {
228 // First, check if a write barrier is even needed. The tests below
229 // catch stores of Smis.
232 // Skip barrier if writing a smi.
233 if (smi_check == INLINE_SMI_CHECK) {
234 JumpIfSmi(value, &done, Label::kNear);
237 // Although the object register is tagged, the offset is relative to the start
238 // of the object, so so offset must be a multiple of kPointerSize.
239 ASSERT(IsAligned(offset, kPointerSize));
241 lea(dst, FieldOperand(object, offset));
242 if (emit_debug_code()) {
244 test_b(dst, (1 << kPointerSizeLog2) - 1);
245 j(zero, &ok, Label::kNear);
251 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
255 // Clobber clobbered input registers when running with the debug-code flag
256 // turned on to provoke errors.
257 if (emit_debug_code()) {
258 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
259 mov(dst, Immediate(BitCast<int32_t>(kZapValue)));
264 void MacroAssembler::RecordWriteForMap(
269 SaveFPRegsMode save_fp) {
272 Register address = scratch1;
273 Register value = scratch2;
274 if (emit_debug_code()) {
276 lea(address, FieldOperand(object, HeapObject::kMapOffset));
277 test_b(address, (1 << kPointerSizeLog2) - 1);
278 j(zero, &ok, Label::kNear);
283 ASSERT(!object.is(value));
284 ASSERT(!object.is(address));
285 ASSERT(!value.is(address));
286 AssertNotSmi(object);
288 if (!FLAG_incremental_marking) {
292 // A single check of the map's pages interesting flag suffices, since it is
293 // only set during incremental collection, and then it's also guaranteed that
294 // the from object's page's interesting flag is also set. This optimization
295 // relies on the fact that maps can never be in new space.
296 ASSERT(!isolate()->heap()->InNewSpace(*map));
297 CheckPageFlagForMap(map,
298 MemoryChunk::kPointersToHereAreInterestingMask,
303 // Delay the initialization of |address| and |value| for the stub until it's
304 // known that the will be needed. Up until this point their values are not
305 // needed since they are embedded in the operands of instructions that need
307 lea(address, FieldOperand(object, HeapObject::kMapOffset));
308 mov(value, Immediate(map));
309 RecordWriteStub stub(object, value, address, OMIT_REMEMBERED_SET, save_fp);
314 // Clobber clobbered input registers when running with the debug-code flag
315 // turned on to provoke errors.
316 if (emit_debug_code()) {
317 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
318 mov(scratch1, Immediate(BitCast<int32_t>(kZapValue)));
319 mov(scratch2, Immediate(BitCast<int32_t>(kZapValue)));
324 void MacroAssembler::RecordWrite(Register object,
327 SaveFPRegsMode fp_mode,
328 RememberedSetAction remembered_set_action,
329 SmiCheck smi_check) {
330 ASSERT(!object.is(value));
331 ASSERT(!object.is(address));
332 ASSERT(!value.is(address));
333 AssertNotSmi(object);
335 if (remembered_set_action == OMIT_REMEMBERED_SET &&
336 !FLAG_incremental_marking) {
340 if (emit_debug_code()) {
342 cmp(value, Operand(address, 0));
343 j(equal, &ok, Label::kNear);
348 // First, check if a write barrier is even needed. The tests below
349 // catch stores of Smis and stores into young gen.
352 if (smi_check == INLINE_SMI_CHECK) {
353 // Skip barrier if writing a smi.
354 JumpIfSmi(value, &done, Label::kNear);
358 value, // Used as scratch.
359 MemoryChunk::kPointersToHereAreInterestingMask,
363 CheckPageFlag(object,
364 value, // Used as scratch.
365 MemoryChunk::kPointersFromHereAreInterestingMask,
370 RecordWriteStub stub(object, value, address, remembered_set_action, fp_mode);
375 // Clobber clobbered registers when running with the debug-code flag
376 // turned on to provoke errors.
377 if (emit_debug_code()) {
378 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
379 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
384 #ifdef ENABLE_DEBUGGER_SUPPORT
385 void MacroAssembler::DebugBreak() {
386 Set(eax, Immediate(0));
387 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
389 call(ces.GetCode(isolate()), RelocInfo::DEBUG_BREAK);
394 void MacroAssembler::Set(Register dst, const Immediate& x) {
396 xor_(dst, dst); // Shorter than mov.
403 void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
408 bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
409 static const int kMaxImmediateBits = 17;
410 if (!RelocInfo::IsNone(x.rmode_)) return false;
411 return !is_intn(x.x_, kMaxImmediateBits);
415 void MacroAssembler::SafeSet(Register dst, const Immediate& x) {
416 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
417 Set(dst, Immediate(x.x_ ^ jit_cookie()));
418 xor_(dst, jit_cookie());
425 void MacroAssembler::SafePush(const Immediate& x) {
426 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
427 push(Immediate(x.x_ ^ jit_cookie()));
428 xor_(Operand(esp, 0), Immediate(jit_cookie()));
435 void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
436 // see ROOT_ACCESSOR macro in factory.h
437 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
442 void MacroAssembler::CompareRoot(const Operand& with,
443 Heap::RootListIndex index) {
444 // see ROOT_ACCESSOR macro in factory.h
445 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
450 void MacroAssembler::CmpObjectType(Register heap_object,
453 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
454 CmpInstanceType(map, type);
458 void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
459 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
460 static_cast<int8_t>(type));
464 void MacroAssembler::CheckFastElements(Register map,
466 Label::Distance distance) {
467 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
468 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
469 STATIC_ASSERT(FAST_ELEMENTS == 2);
470 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
471 cmpb(FieldOperand(map, Map::kBitField2Offset),
472 Map::kMaximumBitField2FastHoleyElementValue);
473 j(above, fail, distance);
477 void MacroAssembler::CheckFastObjectElements(Register map,
479 Label::Distance distance) {
480 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
481 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
482 STATIC_ASSERT(FAST_ELEMENTS == 2);
483 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
484 cmpb(FieldOperand(map, Map::kBitField2Offset),
485 Map::kMaximumBitField2FastHoleySmiElementValue);
486 j(below_equal, fail, distance);
487 cmpb(FieldOperand(map, Map::kBitField2Offset),
488 Map::kMaximumBitField2FastHoleyElementValue);
489 j(above, fail, distance);
493 void MacroAssembler::CheckFastSmiElements(Register map,
495 Label::Distance distance) {
496 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
497 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
498 cmpb(FieldOperand(map, Map::kBitField2Offset),
499 Map::kMaximumBitField2FastHoleySmiElementValue);
500 j(above, fail, distance);
504 void MacroAssembler::StoreNumberToDoubleElements(
505 Register maybe_number,
509 XMMRegister scratch2,
511 bool specialize_for_processor,
512 int elements_offset) {
513 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
514 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
516 CheckMap(maybe_number,
517 isolate()->factory()->heap_number_map(),
521 // Double value, canonicalize NaN.
522 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
523 cmp(FieldOperand(maybe_number, offset),
524 Immediate(kNaNOrInfinityLowerBoundUpper32));
525 j(greater_equal, &maybe_nan, Label::kNear);
528 ExternalReference canonical_nan_reference =
529 ExternalReference::address_of_canonical_non_hole_nan();
530 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
531 CpuFeatureScope use_sse2(this, SSE2);
532 movdbl(scratch2, FieldOperand(maybe_number, HeapNumber::kValueOffset));
533 bind(&have_double_value);
534 movdbl(FieldOperand(elements, key, times_4,
535 FixedDoubleArray::kHeaderSize - elements_offset),
538 fld_d(FieldOperand(maybe_number, HeapNumber::kValueOffset));
539 bind(&have_double_value);
540 fstp_d(FieldOperand(elements, key, times_4,
541 FixedDoubleArray::kHeaderSize - elements_offset));
546 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
547 // it's an Infinity, and the non-NaN code path applies.
548 j(greater, &is_nan, Label::kNear);
549 cmp(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
552 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
553 CpuFeatureScope use_sse2(this, SSE2);
554 movdbl(scratch2, Operand::StaticVariable(canonical_nan_reference));
556 fld_d(Operand::StaticVariable(canonical_nan_reference));
558 jmp(&have_double_value, Label::kNear);
561 // Value is a smi. Convert to a double and store.
562 // Preserve original value.
563 mov(scratch1, maybe_number);
565 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
566 CpuFeatureScope fscope(this, SSE2);
567 cvtsi2sd(scratch2, scratch1);
568 movdbl(FieldOperand(elements, key, times_4,
569 FixedDoubleArray::kHeaderSize - elements_offset),
573 fild_s(Operand(esp, 0));
575 fstp_d(FieldOperand(elements, key, times_4,
576 FixedDoubleArray::kHeaderSize - elements_offset));
582 void MacroAssembler::CompareMap(Register obj,
584 Label* early_success) {
585 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
589 void MacroAssembler::CheckMap(Register obj,
592 SmiCheckType smi_check_type) {
593 if (smi_check_type == DO_SMI_CHECK) {
594 JumpIfSmi(obj, fail);
598 CompareMap(obj, map, &success);
604 void MacroAssembler::DispatchMap(Register obj,
607 Handle<Code> success,
608 SmiCheckType smi_check_type) {
610 if (smi_check_type == DO_SMI_CHECK) {
611 JumpIfSmi(obj, &fail);
613 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
620 Condition MacroAssembler::IsObjectStringType(Register heap_object,
622 Register instance_type) {
623 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
624 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
625 STATIC_ASSERT(kNotStringTag != 0);
626 test(instance_type, Immediate(kIsNotStringMask));
631 Condition MacroAssembler::IsObjectNameType(Register heap_object,
633 Register instance_type) {
634 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
635 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
636 cmpb(instance_type, static_cast<uint8_t>(LAST_NAME_TYPE));
641 void MacroAssembler::IsObjectJSObjectType(Register heap_object,
645 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
646 IsInstanceJSObjectType(map, scratch, fail);
650 void MacroAssembler::IsInstanceJSObjectType(Register map,
653 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
654 sub(scratch, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
656 LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
661 void MacroAssembler::FCmp() {
662 if (CpuFeatures::IsSupported(CMOV)) {
675 void MacroAssembler::AssertNumber(Register object) {
676 if (emit_debug_code()) {
678 JumpIfSmi(object, &ok);
679 cmp(FieldOperand(object, HeapObject::kMapOffset),
680 isolate()->factory()->heap_number_map());
681 Check(equal, kOperandNotANumber);
687 void MacroAssembler::AssertSmi(Register object) {
688 if (emit_debug_code()) {
689 test(object, Immediate(kSmiTagMask));
690 Check(equal, kOperandIsNotASmi);
695 void MacroAssembler::AssertString(Register object) {
696 if (emit_debug_code()) {
697 test(object, Immediate(kSmiTagMask));
698 Check(not_equal, kOperandIsASmiAndNotAString);
700 mov(object, FieldOperand(object, HeapObject::kMapOffset));
701 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
703 Check(below, kOperandIsNotAString);
708 void MacroAssembler::AssertName(Register object) {
709 if (emit_debug_code()) {
710 test(object, Immediate(kSmiTagMask));
711 Check(not_equal, kOperandIsASmiAndNotAName);
713 mov(object, FieldOperand(object, HeapObject::kMapOffset));
714 CmpInstanceType(object, LAST_NAME_TYPE);
716 Check(below_equal, kOperandIsNotAName);
721 void MacroAssembler::AssertNotSmi(Register object) {
722 if (emit_debug_code()) {
723 test(object, Immediate(kSmiTagMask));
724 Check(not_equal, kOperandIsASmi);
729 void MacroAssembler::EnterFrame(StackFrame::Type type) {
733 push(Immediate(Smi::FromInt(type)));
734 push(Immediate(CodeObject()));
735 if (emit_debug_code()) {
736 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
737 Check(not_equal, kCodeObjectNotProperlyPatched);
742 void MacroAssembler::LeaveFrame(StackFrame::Type type) {
743 if (emit_debug_code()) {
744 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
745 Immediate(Smi::FromInt(type)));
746 Check(equal, kStackFrameTypesMustMatch);
752 void MacroAssembler::EnterExitFramePrologue() {
753 // Set up the frame structure on the stack.
754 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
755 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
756 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
760 // Reserve room for entry stack pointer and push the code object.
761 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
762 push(Immediate(0)); // Saved entry sp, patched before call.
763 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
765 // Save the frame pointer and the context in top.
766 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
768 ExternalReference context_address(Isolate::kContextAddress,
770 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
771 mov(Operand::StaticVariable(context_address), esi);
775 void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
776 // Optionally save all XMM registers.
778 CpuFeatureScope scope(this, SSE2);
779 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
780 sub(esp, Immediate(space));
781 const int offset = -2 * kPointerSize;
782 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
783 XMMRegister reg = XMMRegister::from_code(i);
784 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
787 sub(esp, Immediate(argc * kPointerSize));
790 // Get the required frame alignment for the OS.
791 const int kFrameAlignment = OS::ActivationFrameAlignment();
792 if (kFrameAlignment > 0) {
793 ASSERT(IsPowerOf2(kFrameAlignment));
794 and_(esp, -kFrameAlignment);
797 // Patch the saved entry sp.
798 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
802 void MacroAssembler::EnterExitFrame(bool save_doubles) {
803 EnterExitFramePrologue();
805 // Set up argc and argv in callee-saved registers.
806 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
808 lea(esi, Operand(ebp, eax, times_4, offset));
810 // Reserve space for argc, argv and isolate.
811 EnterExitFrameEpilogue(3, save_doubles);
815 void MacroAssembler::EnterApiExitFrame(int argc) {
816 EnterExitFramePrologue();
817 EnterExitFrameEpilogue(argc, false);
821 void MacroAssembler::LeaveExitFrame(bool save_doubles) {
822 // Optionally restore all XMM registers.
824 CpuFeatureScope scope(this, SSE2);
825 const int offset = -2 * kPointerSize;
826 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
827 XMMRegister reg = XMMRegister::from_code(i);
828 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
832 // Get the return address from the stack and restore the frame pointer.
833 mov(ecx, Operand(ebp, 1 * kPointerSize));
834 mov(ebp, Operand(ebp, 0 * kPointerSize));
836 // Pop the arguments and the receiver from the caller stack.
837 lea(esp, Operand(esi, 1 * kPointerSize));
839 // Push the return address to get ready to return.
842 LeaveExitFrameEpilogue();
846 void MacroAssembler::LeaveExitFrameEpilogue() {
847 // Restore current context from top and clear it in debug mode.
848 ExternalReference context_address(Isolate::kContextAddress, isolate());
849 mov(esi, Operand::StaticVariable(context_address));
851 mov(Operand::StaticVariable(context_address), Immediate(0));
854 // Clear the top frame.
855 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
857 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
861 void MacroAssembler::LeaveApiExitFrame() {
865 LeaveExitFrameEpilogue();
869 void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
871 // Adjust this code if not the case.
872 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
873 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
874 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
875 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
876 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
877 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
879 // We will build up the handler from the bottom by pushing on the stack.
880 // First push the frame pointer and context.
881 if (kind == StackHandler::JS_ENTRY) {
882 // The frame pointer does not point to a JS frame so we save NULL for
883 // ebp. We expect the code throwing an exception to check ebp before
884 // dereferencing it to restore the context.
885 push(Immediate(0)); // NULL frame pointer.
886 push(Immediate(Smi::FromInt(0))); // No context.
891 // Push the state and the code object.
893 StackHandler::IndexField::encode(handler_index) |
894 StackHandler::KindField::encode(kind);
895 push(Immediate(state));
898 // Link the current handler as the next handler.
899 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
900 push(Operand::StaticVariable(handler_address));
901 // Set this new handler as the current one.
902 mov(Operand::StaticVariable(handler_address), esp);
906 void MacroAssembler::PopTryHandler() {
907 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
908 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
909 pop(Operand::StaticVariable(handler_address));
910 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
914 void MacroAssembler::JumpToHandlerEntry() {
915 // Compute the handler entry address and jump to it. The handler table is
916 // a fixed array of (smi-tagged) code offsets.
917 // eax = exception, edi = code object, edx = state.
918 mov(ebx, FieldOperand(edi, Code::kHandlerTableOffset));
919 shr(edx, StackHandler::kKindWidth);
920 mov(edx, FieldOperand(ebx, edx, times_4, FixedArray::kHeaderSize));
922 lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
927 void MacroAssembler::Throw(Register value) {
928 // Adjust this code if not the case.
929 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
930 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
931 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
932 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
933 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
934 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
936 // The exception is expected in eax.
937 if (!value.is(eax)) {
940 // Drop the stack pointer to the top of the top handler.
941 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
942 mov(esp, Operand::StaticVariable(handler_address));
943 // Restore the next handler.
944 pop(Operand::StaticVariable(handler_address));
946 // Remove the code object and state, compute the handler address in edi.
947 pop(edi); // Code object.
948 pop(edx); // Index and state.
950 // Restore the context and frame pointer.
951 pop(esi); // Context.
952 pop(ebp); // Frame pointer.
954 // If the handler is a JS frame, restore the context to the frame.
955 // (kind == ENTRY) == (ebp == 0) == (esi == 0), so we could test either
959 j(zero, &skip, Label::kNear);
960 mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
963 JumpToHandlerEntry();
967 void MacroAssembler::ThrowUncatchable(Register value) {
968 // Adjust this code if not the case.
969 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
970 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
971 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
972 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
973 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
974 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
976 // The exception is expected in eax.
977 if (!value.is(eax)) {
980 // Drop the stack pointer to the top of the top stack handler.
981 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
982 mov(esp, Operand::StaticVariable(handler_address));
984 // Unwind the handlers until the top ENTRY handler is found.
985 Label fetch_next, check_kind;
986 jmp(&check_kind, Label::kNear);
988 mov(esp, Operand(esp, StackHandlerConstants::kNextOffset));
991 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
992 test(Operand(esp, StackHandlerConstants::kStateOffset),
993 Immediate(StackHandler::KindField::kMask));
994 j(not_zero, &fetch_next);
996 // Set the top handler address to next handler past the top ENTRY handler.
997 pop(Operand::StaticVariable(handler_address));
999 // Remove the code object and state, compute the handler address in edi.
1000 pop(edi); // Code object.
1001 pop(edx); // Index and state.
1003 // Clear the context pointer and frame pointer (0 was saved in the handler).
1007 JumpToHandlerEntry();
1011 void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
1015 Label same_contexts;
1017 ASSERT(!holder_reg.is(scratch1));
1018 ASSERT(!holder_reg.is(scratch2));
1019 ASSERT(!scratch1.is(scratch2));
1021 // Load current lexical context from the stack frame.
1022 mov(scratch1, Operand(ebp, StandardFrameConstants::kContextOffset));
1024 // When generating debug code, make sure the lexical context is set.
1025 if (emit_debug_code()) {
1026 cmp(scratch1, Immediate(0));
1027 Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
1029 // Load the native context of the current context.
1031 Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
1032 mov(scratch1, FieldOperand(scratch1, offset));
1033 mov(scratch1, FieldOperand(scratch1, GlobalObject::kNativeContextOffset));
1035 // Check the context is a native context.
1036 if (emit_debug_code()) {
1037 // Read the first word and compare to native_context_map.
1038 cmp(FieldOperand(scratch1, HeapObject::kMapOffset),
1039 isolate()->factory()->native_context_map());
1040 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
1043 // Check if both contexts are the same.
1044 cmp(scratch1, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
1045 j(equal, &same_contexts);
1047 // Compare security tokens, save holder_reg on the stack so we can use it
1048 // as a temporary register.
1050 // Check that the security token in the calling global object is
1051 // compatible with the security token in the receiving global
1054 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
1056 // Check the context is a native context.
1057 if (emit_debug_code()) {
1058 cmp(scratch2, isolate()->factory()->null_value());
1059 Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
1061 // Read the first word and compare to native_context_map(),
1062 cmp(FieldOperand(scratch2, HeapObject::kMapOffset),
1063 isolate()->factory()->native_context_map());
1064 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
1067 int token_offset = Context::kHeaderSize +
1068 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1069 mov(scratch1, FieldOperand(scratch1, token_offset));
1070 cmp(scratch1, FieldOperand(scratch2, token_offset));
1073 bind(&same_contexts);
1077 // Compute the hash code from the untagged key. This must be kept in sync
1078 // with ComputeIntegerHash in utils.h.
1080 // Note: r0 will contain hash code
1081 void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1082 // Xor original key with a seed.
1083 if (Serializer::enabled()) {
1084 ExternalReference roots_array_start =
1085 ExternalReference::roots_array_start(isolate());
1086 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
1088 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
1092 int32_t seed = isolate()->heap()->HashSeed();
1093 xor_(r0, Immediate(seed));
1096 // hash = ~hash + (hash << 15);
1101 // hash = hash ^ (hash >> 12);
1105 // hash = hash + (hash << 2);
1106 lea(r0, Operand(r0, r0, times_4, 0));
1107 // hash = hash ^ (hash >> 4);
1111 // hash = hash * 2057;
1113 // hash = hash ^ (hash >> 16);
1121 void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1130 // elements - holds the slow-case elements of the receiver and is unchanged.
1132 // key - holds the smi key on entry and is unchanged.
1134 // Scratch registers:
1136 // r0 - holds the untagged key on entry and holds the hash once computed.
1138 // r1 - used to hold the capacity mask of the dictionary
1140 // r2 - used for the index into the dictionary.
1142 // result - holds the result on exit if the load succeeds and we fall through.
1146 GetNumberHash(r0, r1);
1148 // Compute capacity mask.
1149 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
1150 shr(r1, kSmiTagSize); // convert smi to int
1153 // Generate an unrolled loop that performs a few probes before giving up.
1154 const int kProbes = 4;
1155 for (int i = 0; i < kProbes; i++) {
1156 // Use r2 for index calculations and keep the hash intact in r0.
1158 // Compute the masked index: (hash + i + i * i) & mask.
1160 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
1164 // Scale the index by multiplying by the entry size.
1165 ASSERT(SeededNumberDictionary::kEntrySize == 3);
1166 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1168 // Check if the key matches.
1169 cmp(key, FieldOperand(elements,
1172 SeededNumberDictionary::kElementsStartOffset));
1173 if (i != (kProbes - 1)) {
1181 // Check that the value is a normal propety.
1182 const int kDetailsOffset =
1183 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
1184 ASSERT_EQ(NORMAL, 0);
1185 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
1186 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
1189 // Get the value at the masked, scaled index.
1190 const int kValueOffset =
1191 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
1192 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1196 void MacroAssembler::LoadAllocationTopHelper(Register result,
1198 AllocationFlags flags) {
1199 ExternalReference allocation_top =
1200 AllocationUtils::GetAllocationTopReference(isolate(), flags);
1202 // Just return if allocation top is already known.
1203 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1204 // No use of scratch if allocation top is provided.
1205 ASSERT(scratch.is(no_reg));
1207 // Assert that result actually contains top on entry.
1208 cmp(result, Operand::StaticVariable(allocation_top));
1209 Check(equal, kUnexpectedAllocationTop);
1214 // Move address of new object to result. Use scratch register if available.
1215 if (scratch.is(no_reg)) {
1216 mov(result, Operand::StaticVariable(allocation_top));
1218 mov(scratch, Immediate(allocation_top));
1219 mov(result, Operand(scratch, 0));
1224 void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
1226 AllocationFlags flags) {
1227 if (emit_debug_code()) {
1228 test(result_end, Immediate(kObjectAlignmentMask));
1229 Check(zero, kUnalignedAllocationInNewSpace);
1232 ExternalReference allocation_top =
1233 AllocationUtils::GetAllocationTopReference(isolate(), flags);
1235 // Update new top. Use scratch if available.
1236 if (scratch.is(no_reg)) {
1237 mov(Operand::StaticVariable(allocation_top), result_end);
1239 mov(Operand(scratch, 0), result_end);
1244 void MacroAssembler::Allocate(int object_size,
1246 Register result_end,
1249 AllocationFlags flags) {
1250 ASSERT((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1251 ASSERT(object_size <= Page::kMaxNonCodeHeapObjectSize);
1252 if (!FLAG_inline_new) {
1253 if (emit_debug_code()) {
1254 // Trash the registers to simulate an allocation failure.
1255 mov(result, Immediate(0x7091));
1256 if (result_end.is_valid()) {
1257 mov(result_end, Immediate(0x7191));
1259 if (scratch.is_valid()) {
1260 mov(scratch, Immediate(0x7291));
1266 ASSERT(!result.is(result_end));
1268 // Load address of new object into result.
1269 LoadAllocationTopHelper(result, scratch, flags);
1271 // Align the next allocation. Storing the filler map without checking top is
1272 // always safe because the limit of the heap is always aligned.
1273 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1274 ASSERT((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1275 ASSERT(kPointerAlignment * 2 == kDoubleAlignment);
1277 test(result, Immediate(kDoubleAlignmentMask));
1278 j(zero, &aligned, Label::kNear);
1279 mov(Operand(result, 0),
1280 Immediate(isolate()->factory()->one_pointer_filler_map()));
1281 add(result, Immediate(kDoubleSize / 2));
1285 Register top_reg = result_end.is_valid() ? result_end : result;
1287 // Calculate new top and bail out if space is exhausted.
1288 ExternalReference allocation_limit =
1289 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1291 if (!top_reg.is(result)) {
1292 mov(top_reg, result);
1294 add(top_reg, Immediate(object_size));
1295 j(carry, gc_required);
1296 cmp(top_reg, Operand::StaticVariable(allocation_limit));
1297 j(above, gc_required);
1299 // Update allocation top.
1300 UpdateAllocationTopHelper(top_reg, scratch, flags);
1302 // Tag result if requested.
1303 bool tag_result = (flags & TAG_OBJECT) != 0;
1304 if (top_reg.is(result)) {
1306 sub(result, Immediate(object_size - kHeapObjectTag));
1308 sub(result, Immediate(object_size));
1310 } else if (tag_result) {
1311 ASSERT(kHeapObjectTag == 1);
1317 void MacroAssembler::Allocate(int header_size,
1318 ScaleFactor element_size,
1319 Register element_count,
1320 RegisterValueType element_count_type,
1322 Register result_end,
1325 AllocationFlags flags) {
1326 ASSERT((flags & SIZE_IN_WORDS) == 0);
1327 if (!FLAG_inline_new) {
1328 if (emit_debug_code()) {
1329 // Trash the registers to simulate an allocation failure.
1330 mov(result, Immediate(0x7091));
1331 mov(result_end, Immediate(0x7191));
1332 if (scratch.is_valid()) {
1333 mov(scratch, Immediate(0x7291));
1335 // Register element_count is not modified by the function.
1340 ASSERT(!result.is(result_end));
1342 // Load address of new object into result.
1343 LoadAllocationTopHelper(result, scratch, flags);
1345 // Align the next allocation. Storing the filler map without checking top is
1346 // always safe because the limit of the heap is always aligned.
1347 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1348 ASSERT((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1349 ASSERT(kPointerAlignment * 2 == kDoubleAlignment);
1351 test(result, Immediate(kDoubleAlignmentMask));
1352 j(zero, &aligned, Label::kNear);
1353 mov(Operand(result, 0),
1354 Immediate(isolate()->factory()->one_pointer_filler_map()));
1355 add(result, Immediate(kDoubleSize / 2));
1359 // Calculate new top and bail out if space is exhausted.
1360 ExternalReference allocation_limit =
1361 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1363 // We assume that element_count*element_size + header_size does not
1365 if (element_count_type == REGISTER_VALUE_IS_SMI) {
1366 STATIC_ASSERT(static_cast<ScaleFactor>(times_2 - 1) == times_1);
1367 STATIC_ASSERT(static_cast<ScaleFactor>(times_4 - 1) == times_2);
1368 STATIC_ASSERT(static_cast<ScaleFactor>(times_8 - 1) == times_4);
1369 ASSERT(element_size >= times_2);
1370 ASSERT(kSmiTagSize == 1);
1371 element_size = static_cast<ScaleFactor>(element_size - 1);
1373 ASSERT(element_count_type == REGISTER_VALUE_IS_INT32);
1375 lea(result_end, Operand(element_count, element_size, header_size));
1376 add(result_end, result);
1377 j(carry, gc_required);
1378 cmp(result_end, Operand::StaticVariable(allocation_limit));
1379 j(above, gc_required);
1381 if ((flags & TAG_OBJECT) != 0) {
1382 ASSERT(kHeapObjectTag == 1);
1386 // Update allocation top.
1387 UpdateAllocationTopHelper(result_end, scratch, flags);
1391 void MacroAssembler::Allocate(Register object_size,
1393 Register result_end,
1396 AllocationFlags flags) {
1397 ASSERT((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
1398 if (!FLAG_inline_new) {
1399 if (emit_debug_code()) {
1400 // Trash the registers to simulate an allocation failure.
1401 mov(result, Immediate(0x7091));
1402 mov(result_end, Immediate(0x7191));
1403 if (scratch.is_valid()) {
1404 mov(scratch, Immediate(0x7291));
1406 // object_size is left unchanged by this function.
1411 ASSERT(!result.is(result_end));
1413 // Load address of new object into result.
1414 LoadAllocationTopHelper(result, scratch, flags);
1416 // Align the next allocation. Storing the filler map without checking top is
1417 // always safe because the limit of the heap is always aligned.
1418 if ((flags & DOUBLE_ALIGNMENT) != 0) {
1419 ASSERT((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
1420 ASSERT(kPointerAlignment * 2 == kDoubleAlignment);
1422 test(result, Immediate(kDoubleAlignmentMask));
1423 j(zero, &aligned, Label::kNear);
1424 mov(Operand(result, 0),
1425 Immediate(isolate()->factory()->one_pointer_filler_map()));
1426 add(result, Immediate(kDoubleSize / 2));
1430 // Calculate new top and bail out if space is exhausted.
1431 ExternalReference allocation_limit =
1432 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
1434 if (!object_size.is(result_end)) {
1435 mov(result_end, object_size);
1437 add(result_end, result);
1438 j(carry, gc_required);
1439 cmp(result_end, Operand::StaticVariable(allocation_limit));
1440 j(above, gc_required);
1442 // Tag result if requested.
1443 if ((flags & TAG_OBJECT) != 0) {
1444 ASSERT(kHeapObjectTag == 1);
1448 // Update allocation top.
1449 UpdateAllocationTopHelper(result_end, scratch, flags);
1453 void MacroAssembler::UndoAllocationInNewSpace(Register object) {
1454 ExternalReference new_space_allocation_top =
1455 ExternalReference::new_space_allocation_top_address(isolate());
1457 // Make sure the object has no tag before resetting top.
1458 and_(object, Immediate(~kHeapObjectTagMask));
1460 cmp(object, Operand::StaticVariable(new_space_allocation_top));
1461 Check(below, kUndoAllocationOfNonAllocatedMemory);
1463 mov(Operand::StaticVariable(new_space_allocation_top), object);
1467 void MacroAssembler::AllocateHeapNumber(Register result,
1470 Label* gc_required) {
1471 // Allocate heap number in new space.
1472 Allocate(HeapNumber::kSize, result, scratch1, scratch2, gc_required,
1476 mov(FieldOperand(result, HeapObject::kMapOffset),
1477 Immediate(isolate()->factory()->heap_number_map()));
1481 void MacroAssembler::AllocateTwoByteString(Register result,
1486 Label* gc_required) {
1487 // Calculate the number of bytes needed for the characters in the string while
1488 // observing object alignment.
1489 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1490 ASSERT(kShortSize == 2);
1491 // scratch1 = length * 2 + kObjectAlignmentMask.
1492 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
1493 and_(scratch1, Immediate(~kObjectAlignmentMask));
1495 // Allocate two byte string in new space.
1496 Allocate(SeqTwoByteString::kHeaderSize,
1499 REGISTER_VALUE_IS_INT32,
1506 // Set the map, length and hash field.
1507 mov(FieldOperand(result, HeapObject::kMapOffset),
1508 Immediate(isolate()->factory()->string_map()));
1509 mov(scratch1, length);
1511 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1512 mov(FieldOperand(result, String::kHashFieldOffset),
1513 Immediate(String::kEmptyHashField));
1517 void MacroAssembler::AllocateAsciiString(Register result,
1522 Label* gc_required) {
1523 // Calculate the number of bytes needed for the characters in the string while
1524 // observing object alignment.
1525 ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1526 mov(scratch1, length);
1527 ASSERT(kCharSize == 1);
1528 add(scratch1, Immediate(kObjectAlignmentMask));
1529 and_(scratch1, Immediate(~kObjectAlignmentMask));
1531 // Allocate ASCII string in new space.
1532 Allocate(SeqOneByteString::kHeaderSize,
1535 REGISTER_VALUE_IS_INT32,
1542 // Set the map, length and hash field.
1543 mov(FieldOperand(result, HeapObject::kMapOffset),
1544 Immediate(isolate()->factory()->ascii_string_map()));
1545 mov(scratch1, length);
1547 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1548 mov(FieldOperand(result, String::kHashFieldOffset),
1549 Immediate(String::kEmptyHashField));
1553 void MacroAssembler::AllocateAsciiString(Register result,
1557 Label* gc_required) {
1560 // Allocate ASCII string in new space.
1561 Allocate(SeqOneByteString::SizeFor(length), result, scratch1, scratch2,
1562 gc_required, TAG_OBJECT);
1564 // Set the map, length and hash field.
1565 mov(FieldOperand(result, HeapObject::kMapOffset),
1566 Immediate(isolate()->factory()->ascii_string_map()));
1567 mov(FieldOperand(result, String::kLengthOffset),
1568 Immediate(Smi::FromInt(length)));
1569 mov(FieldOperand(result, String::kHashFieldOffset),
1570 Immediate(String::kEmptyHashField));
1574 void MacroAssembler::AllocateTwoByteConsString(Register result,
1577 Label* gc_required) {
1578 // Allocate heap number in new space.
1579 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
1582 // Set the map. The other fields are left uninitialized.
1583 mov(FieldOperand(result, HeapObject::kMapOffset),
1584 Immediate(isolate()->factory()->cons_string_map()));
1588 void MacroAssembler::AllocateAsciiConsString(Register result,
1591 Label* gc_required) {
1592 Label allocate_new_space, install_map;
1593 AllocationFlags flags = TAG_OBJECT;
1595 ExternalReference high_promotion_mode = ExternalReference::
1596 new_space_high_promotion_mode_active_address(isolate());
1598 test(Operand::StaticVariable(high_promotion_mode), Immediate(1));
1599 j(zero, &allocate_new_space);
1601 Allocate(ConsString::kSize,
1606 static_cast<AllocationFlags>(flags | PRETENURE_OLD_POINTER_SPACE));
1609 bind(&allocate_new_space);
1610 Allocate(ConsString::kSize,
1618 // Set the map. The other fields are left uninitialized.
1619 mov(FieldOperand(result, HeapObject::kMapOffset),
1620 Immediate(isolate()->factory()->cons_ascii_string_map()));
1624 void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1627 Label* gc_required) {
1628 // Allocate heap number in new space.
1629 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1632 // Set the map. The other fields are left uninitialized.
1633 mov(FieldOperand(result, HeapObject::kMapOffset),
1634 Immediate(isolate()->factory()->sliced_string_map()));
1638 void MacroAssembler::AllocateAsciiSlicedString(Register result,
1641 Label* gc_required) {
1642 // Allocate heap number in new space.
1643 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
1646 // Set the map. The other fields are left uninitialized.
1647 mov(FieldOperand(result, HeapObject::kMapOffset),
1648 Immediate(isolate()->factory()->sliced_ascii_string_map()));
1652 // Copy memory, byte-by-byte, from source to destination. Not optimized for
1653 // long or aligned copies. The contents of scratch and length are destroyed.
1654 // Source and destination are incremented by length.
1655 // Many variants of movsb, loop unrolling, word moves, and indexed operands
1656 // have been tried here already, and this is fastest.
1657 // A simpler loop is faster on small copies, but 30% slower on large ones.
1658 // The cld() instruction must have been emitted, to set the direction flag(),
1659 // before calling this function.
1660 void MacroAssembler::CopyBytes(Register source,
1661 Register destination,
1664 Label loop, done, short_string, short_loop;
1665 // Experimentation shows that the short string loop is faster if length < 10.
1666 cmp(length, Immediate(10));
1667 j(less_equal, &short_string);
1669 ASSERT(source.is(esi));
1670 ASSERT(destination.is(edi));
1671 ASSERT(length.is(ecx));
1673 // Because source is 4-byte aligned in our uses of this function,
1674 // we keep source aligned for the rep_movs call by copying the odd bytes
1675 // at the end of the ranges.
1676 mov(scratch, Operand(source, length, times_1, -4));
1677 mov(Operand(destination, length, times_1, -4), scratch);
1681 and_(scratch, Immediate(0x3));
1682 add(destination, scratch);
1685 bind(&short_string);
1686 test(length, length);
1690 mov_b(scratch, Operand(source, 0));
1691 mov_b(Operand(destination, 0), scratch);
1695 j(not_zero, &short_loop);
1701 void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
1702 Register end_offset,
1707 mov(Operand(start_offset, 0), filler);
1708 add(start_offset, Immediate(kPointerSize));
1710 cmp(start_offset, end_offset);
1715 void MacroAssembler::BooleanBitTest(Register object,
1718 bit_index += kSmiTagSize + kSmiShiftSize;
1719 ASSERT(IsPowerOf2(kBitsPerByte));
1720 int byte_index = bit_index / kBitsPerByte;
1721 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1722 test_b(FieldOperand(object, field_offset + byte_index),
1723 static_cast<byte>(1 << byte_bit_index));
1728 void MacroAssembler::NegativeZeroTest(Register result,
1730 Label* then_label) {
1732 test(result, result);
1735 j(sign, then_label);
1740 void MacroAssembler::NegativeZeroTest(Register result,
1744 Label* then_label) {
1746 test(result, result);
1750 j(sign, then_label);
1755 void MacroAssembler::TryGetFunctionPrototype(Register function,
1759 bool miss_on_bound_function) {
1760 // Check that the receiver isn't a smi.
1761 JumpIfSmi(function, miss);
1763 // Check that the function really is a function.
1764 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1767 if (miss_on_bound_function) {
1768 // If a bound function, go to miss label.
1770 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1771 BooleanBitTest(scratch, SharedFunctionInfo::kCompilerHintsOffset,
1772 SharedFunctionInfo::kBoundFunction);
1776 // Make sure that the function has an instance prototype.
1778 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1779 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1780 j(not_zero, &non_instance);
1782 // Get the prototype or initial map from the function.
1784 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1786 // If the prototype or initial map is the hole, don't return it and
1787 // simply miss the cache instead. This will allow us to allocate a
1788 // prototype object on-demand in the runtime system.
1789 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
1792 // If the function does not have an initial map, we're done.
1794 CmpObjectType(result, MAP_TYPE, scratch);
1795 j(not_equal, &done);
1797 // Get the prototype from the initial map.
1798 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1801 // Non-instance prototype: Fetch prototype from constructor field
1803 bind(&non_instance);
1804 mov(result, FieldOperand(result, Map::kConstructorOffset));
1811 void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
1812 ASSERT(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
1813 call(stub->GetCode(isolate()), RelocInfo::CODE_TARGET, ast_id);
1817 void MacroAssembler::TailCallStub(CodeStub* stub) {
1818 ASSERT(allow_stub_calls_ ||
1819 stub->CompilingCallsToThisStubIsGCSafe(isolate()));
1820 jmp(stub->GetCode(isolate()), RelocInfo::CODE_TARGET);
1824 void MacroAssembler::StubReturn(int argc) {
1825 ASSERT(argc >= 1 && generating_stub());
1826 ret((argc - 1) * kPointerSize);
1830 bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
1831 if (!has_frame_ && stub->SometimesSetsUpAFrame()) return false;
1832 return allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe(isolate());
1836 void MacroAssembler::IllegalOperation(int num_arguments) {
1837 if (num_arguments > 0) {
1838 add(esp, Immediate(num_arguments * kPointerSize));
1840 mov(eax, Immediate(isolate()->factory()->undefined_value()));
1844 void MacroAssembler::IndexFromHash(Register hash, Register index) {
1845 // The assert checks that the constants for the maximum number of digits
1846 // for an array index cached in the hash field and the number of bits
1847 // reserved for it does not conflict.
1848 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1849 (1 << String::kArrayIndexValueBits));
1850 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1851 // the low kHashShift bits.
1852 and_(hash, String::kArrayIndexValueMask);
1853 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1854 if (String::kHashShift > kSmiTagSize) {
1855 shr(hash, String::kHashShift - kSmiTagSize);
1857 if (!index.is(hash)) {
1863 void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1864 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1868 void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1869 const Runtime::Function* function = Runtime::FunctionForId(id);
1870 Set(eax, Immediate(function->nargs));
1871 mov(ebx, Immediate(ExternalReference(function, isolate())));
1872 CEntryStub ces(1, CpuFeatures::IsSupported(SSE2) ? kSaveFPRegs
1878 void MacroAssembler::CallRuntime(const Runtime::Function* f,
1879 int num_arguments) {
1880 // If the expected number of arguments of the runtime function is
1881 // constant, we check that the actual number of arguments match the
1883 if (f->nargs >= 0 && f->nargs != num_arguments) {
1884 IllegalOperation(num_arguments);
1888 // TODO(1236192): Most runtime routines don't need the number of
1889 // arguments passed in because it is constant. At some point we
1890 // should remove this need and make the runtime routine entry code
1892 Set(eax, Immediate(num_arguments));
1893 mov(ebx, Immediate(ExternalReference(f, isolate())));
1899 void MacroAssembler::CallExternalReference(ExternalReference ref,
1900 int num_arguments) {
1901 mov(eax, Immediate(num_arguments));
1902 mov(ebx, Immediate(ref));
1909 void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1912 // TODO(1236192): Most runtime routines don't need the number of
1913 // arguments passed in because it is constant. At some point we
1914 // should remove this need and make the runtime routine entry code
1916 Set(eax, Immediate(num_arguments));
1917 JumpToExternalReference(ext);
1921 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1924 TailCallExternalReference(ExternalReference(fid, isolate()),
1930 // If true, a Handle<T> returned by value from a function with cdecl calling
1931 // convention will be returned directly as a value of location_ field in a
1933 // If false, it is returned as a pointer to a preallocated by caller memory
1934 // region. Pointer to this region should be passed to a function as an
1935 // implicit first argument.
1936 #if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
1937 static const bool kReturnHandlesDirectly = true;
1939 static const bool kReturnHandlesDirectly = false;
1943 Operand ApiParameterOperand(int index, bool returns_handle) {
1944 int offset = (index +(kReturnHandlesDirectly || !returns_handle ? 0 : 1));
1945 return Operand(esp, offset * kPointerSize);
1949 void MacroAssembler::PrepareCallApiFunction(int argc, bool returns_handle) {
1950 if (kReturnHandlesDirectly || !returns_handle) {
1951 EnterApiExitFrame(argc);
1952 // When handles are returned directly we don't have to allocate extra
1953 // space for and pass an out parameter.
1954 if (emit_debug_code()) {
1955 mov(esi, Immediate(BitCast<int32_t>(kZapValue)));
1958 // We allocate two additional slots: return value and pointer to it.
1959 EnterApiExitFrame(argc + 2);
1961 // The argument slots are filled as follows:
1963 // n + 1: output slot
1967 // 0: pointer to the output slot
1969 lea(esi, Operand(esp, (argc + 1) * kPointerSize));
1970 mov(Operand(esp, 0 * kPointerSize), esi);
1971 if (emit_debug_code()) {
1972 mov(Operand(esi, 0), Immediate(0));
1978 void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
1979 Address thunk_address,
1980 Operand thunk_last_arg,
1982 bool returns_handle,
1983 int return_value_offset) {
1984 ExternalReference next_address =
1985 ExternalReference::handle_scope_next_address(isolate());
1986 ExternalReference limit_address =
1987 ExternalReference::handle_scope_limit_address(isolate());
1988 ExternalReference level_address =
1989 ExternalReference::handle_scope_level_address(isolate());
1991 // Allocate HandleScope in callee-save registers.
1992 mov(ebx, Operand::StaticVariable(next_address));
1993 mov(edi, Operand::StaticVariable(limit_address));
1994 add(Operand::StaticVariable(level_address), Immediate(1));
1996 if (FLAG_log_timer_events) {
1997 FrameScope frame(this, StackFrame::MANUAL);
1998 PushSafepointRegisters();
1999 PrepareCallCFunction(1, eax);
2000 mov(Operand(esp, 0),
2001 Immediate(ExternalReference::isolate_address(isolate())));
2002 CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
2003 PopSafepointRegisters();
2007 Label profiler_disabled;
2008 Label end_profiler_check;
2009 bool* is_profiling_flag =
2010 isolate()->cpu_profiler()->is_profiling_address();
2011 STATIC_ASSERT(sizeof(*is_profiling_flag) == 1);
2012 mov(eax, Immediate(reinterpret_cast<Address>(is_profiling_flag)));
2013 cmpb(Operand(eax, 0), 0);
2014 j(zero, &profiler_disabled);
2016 // Additional parameter is the address of the actual getter function.
2017 mov(thunk_last_arg, Immediate(function_address));
2018 // Call the api function.
2019 call(thunk_address, RelocInfo::RUNTIME_ENTRY);
2020 jmp(&end_profiler_check);
2022 bind(&profiler_disabled);
2023 // Call the api function.
2024 call(function_address, RelocInfo::RUNTIME_ENTRY);
2025 bind(&end_profiler_check);
2027 if (FLAG_log_timer_events) {
2028 FrameScope frame(this, StackFrame::MANUAL);
2029 PushSafepointRegisters();
2030 PrepareCallCFunction(1, eax);
2031 mov(Operand(esp, 0),
2032 Immediate(ExternalReference::isolate_address(isolate())));
2033 CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
2034 PopSafepointRegisters();
2038 if (returns_handle) {
2039 if (!kReturnHandlesDirectly) {
2040 // PrepareCallApiFunction saved pointer to the output slot into
2041 // callee-save register esi.
2042 mov(eax, Operand(esi, 0));
2045 // Check if the result handle holds 0.
2047 j(zero, &empty_handle);
2048 // It was non-zero. Dereference to get the result value.
2049 mov(eax, Operand(eax, 0));
2051 bind(&empty_handle);
2053 // Load the value from ReturnValue
2054 mov(eax, Operand(ebp, return_value_offset * kPointerSize));
2056 Label promote_scheduled_exception;
2057 Label delete_allocated_handles;
2058 Label leave_exit_frame;
2061 // No more valid handles (the result handle was the last one). Restore
2062 // previous handle scope.
2063 mov(Operand::StaticVariable(next_address), ebx);
2064 sub(Operand::StaticVariable(level_address), Immediate(1));
2065 Assert(above_equal, kInvalidHandleScopeLevel);
2066 cmp(edi, Operand::StaticVariable(limit_address));
2067 j(not_equal, &delete_allocated_handles);
2068 bind(&leave_exit_frame);
2070 // Check if the function scheduled an exception.
2071 ExternalReference scheduled_exception_address =
2072 ExternalReference::scheduled_exception_address(isolate());
2073 cmp(Operand::StaticVariable(scheduled_exception_address),
2074 Immediate(isolate()->factory()->the_hole_value()));
2075 j(not_equal, &promote_scheduled_exception);
2077 #if ENABLE_EXTRA_CHECKS
2078 // Check if the function returned a valid JavaScript value.
2080 Register return_value = eax;
2083 JumpIfSmi(return_value, &ok, Label::kNear);
2084 mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
2086 CmpInstanceType(map, FIRST_NONSTRING_TYPE);
2087 j(below, &ok, Label::kNear);
2089 CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
2090 j(above_equal, &ok, Label::kNear);
2092 cmp(map, isolate()->factory()->heap_number_map());
2093 j(equal, &ok, Label::kNear);
2095 cmp(return_value, isolate()->factory()->undefined_value());
2096 j(equal, &ok, Label::kNear);
2098 cmp(return_value, isolate()->factory()->true_value());
2099 j(equal, &ok, Label::kNear);
2101 cmp(return_value, isolate()->factory()->false_value());
2102 j(equal, &ok, Label::kNear);
2104 cmp(return_value, isolate()->factory()->null_value());
2105 j(equal, &ok, Label::kNear);
2107 Abort(kAPICallReturnedInvalidObject);
2112 LeaveApiExitFrame();
2113 ret(stack_space * kPointerSize);
2115 bind(&promote_scheduled_exception);
2116 TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
2118 // HandleScope limit has changed. Delete allocated extensions.
2119 ExternalReference delete_extensions =
2120 ExternalReference::delete_handle_scope_extensions(isolate());
2121 bind(&delete_allocated_handles);
2122 mov(Operand::StaticVariable(limit_address), edi);
2124 mov(Operand(esp, 0),
2125 Immediate(ExternalReference::isolate_address(isolate())));
2126 mov(eax, Immediate(delete_extensions));
2129 jmp(&leave_exit_frame);
2133 void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
2134 // Set the entry point and jump to the C entry runtime stub.
2135 mov(ebx, Immediate(ext));
2137 jmp(ces.GetCode(isolate()), RelocInfo::CODE_TARGET);
2141 void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
2142 // This macro takes the dst register to make the code more readable
2143 // at the call sites. However, the dst register has to be ecx to
2144 // follow the calling convention which requires the call type to be
2146 ASSERT(dst.is(ecx));
2147 if (call_kind == CALL_AS_FUNCTION) {
2148 // Set to some non-zero smi by updating the least significant
2150 mov_b(dst, 1 << kSmiTagSize);
2152 // Set to smi zero by clearing the register.
2158 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2159 const ParameterCount& actual,
2160 Handle<Code> code_constant,
2161 const Operand& code_operand,
2163 bool* definitely_mismatches,
2165 Label::Distance done_near,
2166 const CallWrapper& call_wrapper,
2167 CallKind call_kind) {
2168 bool definitely_matches = false;
2169 *definitely_mismatches = false;
2171 if (expected.is_immediate()) {
2172 ASSERT(actual.is_immediate());
2173 if (expected.immediate() == actual.immediate()) {
2174 definitely_matches = true;
2176 mov(eax, actual.immediate());
2177 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
2178 if (expected.immediate() == sentinel) {
2179 // Don't worry about adapting arguments for builtins that
2180 // don't want that done. Skip adaption code by making it look
2181 // like we have a match between expected and actual number of
2183 definitely_matches = true;
2185 *definitely_mismatches = true;
2186 mov(ebx, expected.immediate());
2190 if (actual.is_immediate()) {
2191 // Expected is in register, actual is immediate. This is the
2192 // case when we invoke function values without going through the
2194 cmp(expected.reg(), actual.immediate());
2196 ASSERT(expected.reg().is(ebx));
2197 mov(eax, actual.immediate());
2198 } else if (!expected.reg().is(actual.reg())) {
2199 // Both expected and actual are in (different) registers. This
2200 // is the case when we invoke functions using call and apply.
2201 cmp(expected.reg(), actual.reg());
2203 ASSERT(actual.reg().is(eax));
2204 ASSERT(expected.reg().is(ebx));
2208 if (!definitely_matches) {
2209 Handle<Code> adaptor =
2210 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2211 if (!code_constant.is_null()) {
2212 mov(edx, Immediate(code_constant));
2213 add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
2214 } else if (!code_operand.is_reg(edx)) {
2215 mov(edx, code_operand);
2218 if (flag == CALL_FUNCTION) {
2219 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
2220 SetCallKind(ecx, call_kind);
2221 call(adaptor, RelocInfo::CODE_TARGET);
2222 call_wrapper.AfterCall();
2223 if (!*definitely_mismatches) {
2224 jmp(done, done_near);
2227 SetCallKind(ecx, call_kind);
2228 jmp(adaptor, RelocInfo::CODE_TARGET);
2235 void MacroAssembler::InvokeCode(const Operand& code,
2236 const ParameterCount& expected,
2237 const ParameterCount& actual,
2239 const CallWrapper& call_wrapper,
2240 CallKind call_kind) {
2241 // You can't call a function without a valid frame.
2242 ASSERT(flag == JUMP_FUNCTION || has_frame());
2245 bool definitely_mismatches = false;
2246 InvokePrologue(expected, actual, Handle<Code>::null(), code,
2247 &done, &definitely_mismatches, flag, Label::kNear,
2248 call_wrapper, call_kind);
2249 if (!definitely_mismatches) {
2250 if (flag == CALL_FUNCTION) {
2251 call_wrapper.BeforeCall(CallSize(code));
2252 SetCallKind(ecx, call_kind);
2254 call_wrapper.AfterCall();
2256 ASSERT(flag == JUMP_FUNCTION);
2257 SetCallKind(ecx, call_kind);
2265 void MacroAssembler::InvokeCode(Handle<Code> code,
2266 const ParameterCount& expected,
2267 const ParameterCount& actual,
2268 RelocInfo::Mode rmode,
2270 const CallWrapper& call_wrapper,
2271 CallKind call_kind) {
2272 // You can't call a function without a valid frame.
2273 ASSERT(flag == JUMP_FUNCTION || has_frame());
2276 Operand dummy(eax, 0);
2277 bool definitely_mismatches = false;
2278 InvokePrologue(expected, actual, code, dummy, &done, &definitely_mismatches,
2279 flag, Label::kNear, call_wrapper, call_kind);
2280 if (!definitely_mismatches) {
2281 if (flag == CALL_FUNCTION) {
2282 call_wrapper.BeforeCall(CallSize(code, rmode));
2283 SetCallKind(ecx, call_kind);
2285 call_wrapper.AfterCall();
2287 ASSERT(flag == JUMP_FUNCTION);
2288 SetCallKind(ecx, call_kind);
2296 void MacroAssembler::InvokeFunction(Register fun,
2297 const ParameterCount& actual,
2299 const CallWrapper& call_wrapper,
2300 CallKind call_kind) {
2301 // You can't call a function without a valid frame.
2302 ASSERT(flag == JUMP_FUNCTION || has_frame());
2304 ASSERT(fun.is(edi));
2305 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2306 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2307 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
2310 ParameterCount expected(ebx);
2311 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2312 expected, actual, flag, call_wrapper, call_kind);
2316 void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
2317 const ParameterCount& expected,
2318 const ParameterCount& actual,
2320 const CallWrapper& call_wrapper,
2321 CallKind call_kind) {
2322 // You can't call a function without a valid frame.
2323 ASSERT(flag == JUMP_FUNCTION || has_frame());
2325 // Get the function and setup the context.
2326 LoadHeapObject(edi, function);
2327 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2329 // We call indirectly through the code field in the function to
2330 // allow recompilation to take effect without changing any of the
2332 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2333 expected, actual, flag, call_wrapper, call_kind);
2337 void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
2339 const CallWrapper& call_wrapper) {
2340 // You can't call a builtin without a valid frame.
2341 ASSERT(flag == JUMP_FUNCTION || has_frame());
2343 // Rely on the assertion to check that the number of provided
2344 // arguments match the expected number of arguments. Fake a
2345 // parameter count to avoid emitting code to do the check.
2346 ParameterCount expected(0);
2347 GetBuiltinFunction(edi, id);
2348 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2349 expected, expected, flag, call_wrapper, CALL_AS_METHOD);
2353 void MacroAssembler::GetBuiltinFunction(Register target,
2354 Builtins::JavaScript id) {
2355 // Load the JavaScript builtin function from the builtins object.
2356 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2357 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
2358 mov(target, FieldOperand(target,
2359 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2363 void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2364 ASSERT(!target.is(edi));
2365 // Load the JavaScript builtin function from the builtins object.
2366 GetBuiltinFunction(edi, id);
2367 // Load the code entry point from the function into the target register.
2368 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
2372 void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2373 if (context_chain_length > 0) {
2374 // Move up the chain of contexts to the context containing the slot.
2375 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2376 for (int i = 1; i < context_chain_length; i++) {
2377 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2380 // Slot is in the current function context. Move it into the
2381 // destination register in case we store into it (the write barrier
2382 // cannot be allowed to destroy the context in esi).
2386 // We should not have found a with context by walking the context chain
2387 // (i.e., the static scope chain and runtime context chain do not agree).
2388 // A variable occurring in such a scope should have slot type LOOKUP and
2390 if (emit_debug_code()) {
2391 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2392 isolate()->factory()->with_context_map());
2393 Check(not_equal, kVariableResolvedToWithContext);
2398 void MacroAssembler::LoadTransitionedArrayMapConditional(
2399 ElementsKind expected_kind,
2400 ElementsKind transitioned_kind,
2401 Register map_in_out,
2403 Label* no_map_match) {
2404 // Load the global or builtins object from the current context.
2405 mov(scratch, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2406 mov(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
2408 // Check that the function's map is the same as the expected cached map.
2409 mov(scratch, Operand(scratch,
2410 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
2412 size_t offset = expected_kind * kPointerSize +
2413 FixedArrayBase::kHeaderSize;
2414 cmp(map_in_out, FieldOperand(scratch, offset));
2415 j(not_equal, no_map_match);
2417 // Use the transitioned cached map.
2418 offset = transitioned_kind * kPointerSize +
2419 FixedArrayBase::kHeaderSize;
2420 mov(map_in_out, FieldOperand(scratch, offset));
2424 void MacroAssembler::LoadInitialArrayMap(
2425 Register function_in, Register scratch,
2426 Register map_out, bool can_have_holes) {
2427 ASSERT(!function_in.is(map_out));
2429 mov(map_out, FieldOperand(function_in,
2430 JSFunction::kPrototypeOrInitialMapOffset));
2431 if (!FLAG_smi_only_arrays) {
2432 ElementsKind kind = can_have_holes ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS;
2433 LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
2438 } else if (can_have_holes) {
2439 LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
2440 FAST_HOLEY_SMI_ELEMENTS,
2449 void MacroAssembler::LoadGlobalContext(Register global_context) {
2450 // Load the global or builtins object from the current context.
2452 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2453 // Load the native context from the global or builtins object.
2455 FieldOperand(global_context, GlobalObject::kNativeContextOffset));
2459 void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2460 // Load the global or builtins object from the current context.
2462 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
2463 // Load the native context from the global or builtins object.
2465 FieldOperand(function, GlobalObject::kNativeContextOffset));
2466 // Load the function from the native context.
2467 mov(function, Operand(function, Context::SlotOffset(index)));
2471 void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2473 // Load the initial map. The global functions all have initial maps.
2474 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2475 if (emit_debug_code()) {
2477 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
2480 Abort(kGlobalFunctionsMustHaveInitialMap);
2486 // Store the value in register src in the safepoint register stack
2487 // slot for register dst.
2488 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2489 mov(SafepointRegisterSlot(dst), src);
2493 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2494 mov(SafepointRegisterSlot(dst), src);
2498 void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2499 mov(dst, SafepointRegisterSlot(src));
2503 Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2504 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2508 int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2509 // The registers are pushed starting with the lowest encoding,
2510 // which means that lowest encodings are furthest away from
2511 // the stack pointer.
2512 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
2513 return kNumSafepointRegisters - reg_code - 1;
2517 void MacroAssembler::LoadHeapObject(Register result,
2518 Handle<HeapObject> object) {
2519 AllowDeferredHandleDereference embedding_raw_address;
2520 if (isolate()->heap()->InNewSpace(*object)) {
2521 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2522 mov(result, Operand::ForCell(cell));
2524 mov(result, object);
2529 void MacroAssembler::CmpHeapObject(Register reg, Handle<HeapObject> object) {
2530 AllowDeferredHandleDereference using_raw_address;
2531 if (isolate()->heap()->InNewSpace(*object)) {
2532 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2533 cmp(reg, Operand::ForCell(cell));
2540 void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2541 AllowDeferredHandleDereference using_raw_address;
2542 if (isolate()->heap()->InNewSpace(*object)) {
2543 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2544 push(Operand::ForCell(cell));
2551 void MacroAssembler::Ret() {
2556 void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2557 if (is_uint16(bytes_dropped)) {
2561 add(esp, Immediate(bytes_dropped));
2568 void MacroAssembler::VerifyX87StackDepth(uint32_t depth) {
2569 // Make sure the floating point stack is either empty or has depth items.
2572 // The top-of-stack (tos) is 7 if there is one item pushed.
2573 int tos = (8 - depth) % 8;
2574 const int kTopMask = 0x3800;
2578 and_(eax, kTopMask);
2580 cmp(eax, Immediate(tos));
2581 Check(equal, kUnexpectedFPUStackDepthAfterInstruction);
2587 void MacroAssembler::Drop(int stack_elements) {
2588 if (stack_elements > 0) {
2589 add(esp, Immediate(stack_elements * kPointerSize));
2594 void MacroAssembler::Move(Register dst, Register src) {
2601 void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2602 if (FLAG_native_code_counters && counter->Enabled()) {
2603 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2608 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
2610 if (FLAG_native_code_counters && counter->Enabled()) {
2611 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2615 add(operand, Immediate(value));
2621 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
2623 if (FLAG_native_code_counters && counter->Enabled()) {
2624 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2628 sub(operand, Immediate(value));
2634 void MacroAssembler::IncrementCounter(Condition cc,
2635 StatsCounter* counter,
2638 if (FLAG_native_code_counters && counter->Enabled()) {
2640 j(NegateCondition(cc), &skip);
2642 IncrementCounter(counter, value);
2649 void MacroAssembler::DecrementCounter(Condition cc,
2650 StatsCounter* counter,
2653 if (FLAG_native_code_counters && counter->Enabled()) {
2655 j(NegateCondition(cc), &skip);
2657 DecrementCounter(counter, value);
2664 void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
2665 if (emit_debug_code()) Check(cc, reason);
2669 void MacroAssembler::AssertFastElements(Register elements) {
2670 if (emit_debug_code()) {
2671 Factory* factory = isolate()->factory();
2673 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2674 Immediate(factory->fixed_array_map()));
2676 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2677 Immediate(factory->fixed_double_array_map()));
2679 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2680 Immediate(factory->fixed_cow_array_map()));
2682 Abort(kJSObjectWithFastElementsMapHasSlowElements);
2688 void MacroAssembler::Check(Condition cc, BailoutReason reason) {
2692 // will not return here
2697 void MacroAssembler::CheckStackAlignment() {
2698 int frame_alignment = OS::ActivationFrameAlignment();
2699 int frame_alignment_mask = frame_alignment - 1;
2700 if (frame_alignment > kPointerSize) {
2701 ASSERT(IsPowerOf2(frame_alignment));
2702 Label alignment_as_expected;
2703 test(esp, Immediate(frame_alignment_mask));
2704 j(zero, &alignment_as_expected);
2705 // Abort if stack is not aligned.
2707 bind(&alignment_as_expected);
2712 void MacroAssembler::Abort(BailoutReason reason) {
2713 // We want to pass the msg string like a smi to avoid GC
2714 // problems, however msg is not guaranteed to be aligned
2715 // properly. Instead, we pass an aligned pointer that is
2716 // a proper v8 smi, but also pass the alignment difference
2717 // from the real pointer as a smi.
2718 const char* msg = GetBailoutReason(reason);
2719 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2720 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2721 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2724 RecordComment("Abort message: ");
2730 push(Immediate(p0));
2731 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
2732 // Disable stub call restrictions to always allow calls to abort.
2734 // We don't actually want to generate a pile of code for this, so just
2735 // claim there is a stack frame, without generating one.
2736 FrameScope scope(this, StackFrame::NONE);
2737 CallRuntime(Runtime::kAbort, 2);
2739 CallRuntime(Runtime::kAbort, 2);
2741 // will not return here
2746 void MacroAssembler::LoadInstanceDescriptors(Register map,
2747 Register descriptors) {
2748 mov(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
2752 void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
2753 mov(dst, FieldOperand(map, Map::kBitField3Offset));
2754 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
2758 void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2761 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
2762 HeapNumber::kExponentBits));
2763 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
2765 psllq(dst, HeapNumber::kMantissaBits);
2769 void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
2770 Register instance_type,
2773 if (!scratch.is(instance_type)) {
2774 mov(scratch, instance_type);
2777 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2778 cmp(scratch, kStringTag | kSeqStringTag | kOneByteStringTag);
2779 j(not_equal, failure);
2783 void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
2788 // Check that both objects are not smis.
2789 STATIC_ASSERT(kSmiTag == 0);
2790 mov(scratch1, object1);
2791 and_(scratch1, object2);
2792 JumpIfSmi(scratch1, failure);
2794 // Load instance type for both strings.
2795 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2796 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2797 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2798 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2800 // Check that both are flat ASCII strings.
2801 const int kFlatAsciiStringMask =
2802 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2803 const int kFlatAsciiStringTag =
2804 kStringTag | kOneByteStringTag | kSeqStringTag;
2805 // Interleave bits from both instance types and compare them in one check.
2806 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2807 and_(scratch1, kFlatAsciiStringMask);
2808 and_(scratch2, kFlatAsciiStringMask);
2809 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2810 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
2811 j(not_equal, failure);
2815 void MacroAssembler::JumpIfNotUniqueName(Operand operand,
2816 Label* not_unique_name,
2817 Label::Distance distance) {
2818 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2820 test(operand, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2822 cmpb(operand, static_cast<uint8_t>(SYMBOL_TYPE));
2823 j(not_equal, not_unique_name, distance);
2829 void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2830 int frame_alignment = OS::ActivationFrameAlignment();
2831 if (frame_alignment != 0) {
2832 // Make stack end at alignment and make room for num_arguments words
2833 // and the original value of esp.
2835 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
2836 ASSERT(IsPowerOf2(frame_alignment));
2837 and_(esp, -frame_alignment);
2838 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2840 sub(esp, Immediate(num_arguments * kPointerSize));
2845 void MacroAssembler::CallCFunction(ExternalReference function,
2846 int num_arguments) {
2847 // Trashing eax is ok as it will be the return value.
2848 mov(eax, Immediate(function));
2849 CallCFunction(eax, num_arguments);
2853 void MacroAssembler::CallCFunction(Register function,
2854 int num_arguments) {
2855 ASSERT(has_frame());
2856 // Check stack alignment.
2857 if (emit_debug_code()) {
2858 CheckStackAlignment();
2862 if (OS::ActivationFrameAlignment() != 0) {
2863 mov(esp, Operand(esp, num_arguments * kPointerSize));
2865 add(esp, Immediate(num_arguments * kPointerSize));
2870 bool AreAliased(Register r1, Register r2, Register r3, Register r4) {
2871 if (r1.is(r2)) return true;
2872 if (r1.is(r3)) return true;
2873 if (r1.is(r4)) return true;
2874 if (r2.is(r3)) return true;
2875 if (r2.is(r4)) return true;
2876 if (r3.is(r4)) return true;
2881 CodePatcher::CodePatcher(byte* address, int size)
2882 : address_(address),
2884 masm_(NULL, address, size + Assembler::kGap) {
2885 // Create a new macro assembler pointing to the address of the code to patch.
2886 // The size is adjusted with kGap on order for the assembler to generate size
2887 // bytes of instructions without failing with buffer size constraints.
2888 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2892 CodePatcher::~CodePatcher() {
2893 // Indicate that code has changed.
2894 CPU::FlushICache(address_, size_);
2896 // Check that the code was patched as expected.
2897 ASSERT(masm_.pc_ == address_ + size_);
2898 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2902 void MacroAssembler::CheckPageFlag(
2907 Label* condition_met,
2908 Label::Distance condition_met_distance) {
2909 ASSERT(cc == zero || cc == not_zero);
2910 if (scratch.is(object)) {
2911 and_(scratch, Immediate(~Page::kPageAlignmentMask));
2913 mov(scratch, Immediate(~Page::kPageAlignmentMask));
2914 and_(scratch, object);
2916 if (mask < (1 << kBitsPerByte)) {
2917 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
2918 static_cast<uint8_t>(mask));
2920 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
2922 j(cc, condition_met, condition_met_distance);
2926 void MacroAssembler::CheckPageFlagForMap(
2930 Label* condition_met,
2931 Label::Distance condition_met_distance) {
2932 ASSERT(cc == zero || cc == not_zero);
2933 Page* page = Page::FromAddress(map->address());
2934 ExternalReference reference(ExternalReference::page_flags(page));
2935 // The inlined static address check of the page's flags relies
2936 // on maps never being compacted.
2937 ASSERT(!isolate()->heap()->mark_compact_collector()->
2938 IsOnEvacuationCandidate(*map));
2939 if (mask < (1 << kBitsPerByte)) {
2940 test_b(Operand::StaticVariable(reference), static_cast<uint8_t>(mask));
2942 test(Operand::StaticVariable(reference), Immediate(mask));
2944 j(cc, condition_met, condition_met_distance);
2948 void MacroAssembler::CheckMapDeprecated(Handle<Map> map,
2950 Label* if_deprecated) {
2951 if (map->CanBeDeprecated()) {
2953 mov(scratch, FieldOperand(scratch, Map::kBitField3Offset));
2954 and_(scratch, Immediate(Smi::FromInt(Map::Deprecated::kMask)));
2955 j(not_zero, if_deprecated);
2960 void MacroAssembler::JumpIfBlack(Register object,
2964 Label::Distance on_black_near) {
2965 HasColor(object, scratch0, scratch1,
2966 on_black, on_black_near,
2967 1, 0); // kBlackBitPattern.
2968 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
2972 void MacroAssembler::HasColor(Register object,
2973 Register bitmap_scratch,
2974 Register mask_scratch,
2976 Label::Distance has_color_distance,
2979 ASSERT(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
2981 GetMarkBits(object, bitmap_scratch, mask_scratch);
2983 Label other_color, word_boundary;
2984 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2985 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
2986 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
2987 j(zero, &word_boundary, Label::kNear);
2988 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2989 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
2990 jmp(&other_color, Label::kNear);
2992 bind(&word_boundary);
2993 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize), 1);
2995 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
3000 void MacroAssembler::GetMarkBits(Register addr_reg,
3001 Register bitmap_reg,
3002 Register mask_reg) {
3003 ASSERT(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
3004 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
3005 and_(bitmap_reg, addr_reg);
3008 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
3011 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
3013 add(bitmap_reg, ecx);
3015 shr(ecx, kPointerSizeLog2);
3016 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
3017 mov(mask_reg, Immediate(1));
3022 void MacroAssembler::EnsureNotWhite(
3024 Register bitmap_scratch,
3025 Register mask_scratch,
3026 Label* value_is_white_and_not_data,
3027 Label::Distance distance) {
3028 ASSERT(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
3029 GetMarkBits(value, bitmap_scratch, mask_scratch);
3031 // If the value is black or grey we don't need to do anything.
3032 ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0);
3033 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
3034 ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0);
3035 ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
3039 // Since both black and grey have a 1 in the first position and white does
3040 // not have a 1 there we only need to check one bit.
3041 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3042 j(not_zero, &done, Label::kNear);
3044 if (emit_debug_code()) {
3045 // Check for impossible bit pattern.
3048 // shl. May overflow making the check conservative.
3049 add(mask_scratch, mask_scratch);
3050 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
3051 j(zero, &ok, Label::kNear);
3057 // Value is white. We check whether it is data that doesn't need scanning.
3058 // Currently only checks for HeapNumber and non-cons strings.
3059 Register map = ecx; // Holds map while checking type.
3060 Register length = ecx; // Holds length of object after checking type.
3061 Label not_heap_number;
3062 Label is_data_object;
3064 // Check for heap-number
3065 mov(map, FieldOperand(value, HeapObject::kMapOffset));
3066 cmp(map, isolate()->factory()->heap_number_map());
3067 j(not_equal, ¬_heap_number, Label::kNear);
3068 mov(length, Immediate(HeapNumber::kSize));
3069 jmp(&is_data_object, Label::kNear);
3071 bind(¬_heap_number);
3072 // Check for strings.
3073 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
3074 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
3075 // If it's a string and it's not a cons string then it's an object containing
3077 Register instance_type = ecx;
3078 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3079 test_b(instance_type, kIsIndirectStringMask | kIsNotStringMask);
3080 j(not_zero, value_is_white_and_not_data);
3081 // It's a non-indirect (non-cons and non-slice) string.
3082 // If it's external, the length is just ExternalString::kSize.
3083 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
3085 // External strings are the only ones with the kExternalStringTag bit
3087 ASSERT_EQ(0, kSeqStringTag & kExternalStringTag);
3088 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
3089 test_b(instance_type, kExternalStringTag);
3090 j(zero, ¬_external, Label::kNear);
3091 mov(length, Immediate(ExternalString::kSize));
3092 jmp(&is_data_object, Label::kNear);
3094 bind(¬_external);
3095 // Sequential string, either ASCII or UC16.
3096 ASSERT(kOneByteStringTag == 0x04);
3097 and_(length, Immediate(kStringEncodingMask));
3098 xor_(length, Immediate(kStringEncodingMask));
3099 add(length, Immediate(0x04));
3100 // Value now either 4 (if ASCII) or 8 (if UC16), i.e., char-size shifted
3101 // by 2. If we multiply the string length as smi by this, it still
3102 // won't overflow a 32-bit value.
3103 ASSERT_EQ(SeqOneByteString::kMaxSize, SeqTwoByteString::kMaxSize);
3104 ASSERT(SeqOneByteString::kMaxSize <=
3105 static_cast<int>(0xffffffffu >> (2 + kSmiTagSize)));
3106 imul(length, FieldOperand(value, String::kLengthOffset));
3107 shr(length, 2 + kSmiTagSize + kSmiShiftSize);
3108 add(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
3109 and_(length, Immediate(~kObjectAlignmentMask));
3111 bind(&is_data_object);
3112 // Value is a data object, and it is white. Mark it black. Since we know
3113 // that the object is white we can make it black by flipping one bit.
3114 or_(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
3116 and_(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
3117 add(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset),
3119 if (emit_debug_code()) {
3120 mov(length, Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
3121 cmp(length, Operand(bitmap_scratch, MemoryChunk::kSizeOffset));
3122 Check(less_equal, kLiveBytesCountOverflowChunkSize);
3129 void MacroAssembler::EnumLength(Register dst, Register map) {
3130 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3131 mov(dst, FieldOperand(map, Map::kBitField3Offset));
3132 and_(dst, Immediate(Smi::FromInt(Map::EnumLengthBits::kMask)));
3136 void MacroAssembler::CheckEnumCache(Label* call_runtime) {
3140 // Check if the enum length field is properly initialized, indicating that
3141 // there is an enum cache.
3142 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
3144 EnumLength(edx, ebx);
3145 cmp(edx, Immediate(Smi::FromInt(Map::kInvalidEnumCache)));
3146 j(equal, call_runtime);
3151 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
3153 // For all objects but the receiver, check that the cache is empty.
3154 EnumLength(edx, ebx);
3155 cmp(edx, Immediate(Smi::FromInt(0)));
3156 j(not_equal, call_runtime);
3160 // Check that there are no elements. Register rcx contains the current JS
3161 // object we've reached through the prototype chain.
3162 mov(ecx, FieldOperand(ecx, JSObject::kElementsOffset));
3163 cmp(ecx, isolate()->factory()->empty_fixed_array());
3164 j(not_equal, call_runtime);
3166 mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
3167 cmp(ecx, isolate()->factory()->null_value());
3168 j(not_equal, &next);
3172 void MacroAssembler::TestJSArrayForAllocationMemento(
3173 Register receiver_reg,
3174 Register scratch_reg) {
3175 Label no_memento_available;
3177 ExternalReference new_space_start =
3178 ExternalReference::new_space_start(isolate());
3179 ExternalReference new_space_allocation_top =
3180 ExternalReference::new_space_allocation_top_address(isolate());
3182 lea(scratch_reg, Operand(receiver_reg,
3183 JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
3184 cmp(scratch_reg, Immediate(new_space_start));
3185 j(less, &no_memento_available);
3186 cmp(scratch_reg, Operand::StaticVariable(new_space_allocation_top));
3187 j(greater, &no_memento_available);
3188 cmp(MemOperand(scratch_reg, -AllocationMemento::kSize),
3189 Immediate(Handle<Map>(isolate()->heap()->allocation_memento_map())));
3190 bind(&no_memento_available);
3194 } } // namespace v8::internal
3196 #endif // V8_TARGET_ARCH_IA32