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 defined(V8_TARGET_ARCH_IA32)
32 #include "bootstrapper.h"
36 #include "serialize.h"
41 // -------------------------------------------------------------------------
42 // MacroAssembler implementation.
44 MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
45 : Assembler(arg_isolate, buffer, size),
46 generating_stub_(false),
47 allow_stub_calls_(true),
49 if (isolate() != NULL) {
50 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
56 void MacroAssembler::InNewSpace(
61 Label::Distance condition_met_distance) {
62 ASSERT(cc == equal || cc == not_equal);
63 if (scratch.is(object)) {
64 and_(scratch, Immediate(~Page::kPageAlignmentMask));
66 mov(scratch, Immediate(~Page::kPageAlignmentMask));
67 and_(scratch, object);
69 // Check that we can use a test_b.
70 ASSERT(MemoryChunk::IN_FROM_SPACE < 8);
71 ASSERT(MemoryChunk::IN_TO_SPACE < 8);
72 int mask = (1 << MemoryChunk::IN_FROM_SPACE)
73 | (1 << MemoryChunk::IN_TO_SPACE);
74 // If non-zero, the page belongs to new-space.
75 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
76 static_cast<uint8_t>(mask));
77 j(cc, condition_met, condition_met_distance);
81 void MacroAssembler::RememberedSetHelper(
82 Register object, // Only used for debug checks.
85 SaveFPRegsMode save_fp,
86 MacroAssembler::RememberedSetFinalAction and_then) {
88 if (FLAG_debug_code) {
90 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
94 // Load store buffer top.
95 ExternalReference store_buffer =
96 ExternalReference::store_buffer_top(isolate());
97 mov(scratch, Operand::StaticVariable(store_buffer));
98 // Store pointer to buffer.
99 mov(Operand(scratch, 0), addr);
100 // Increment buffer top.
101 add(scratch, Immediate(kPointerSize));
102 // Write back new top of buffer.
103 mov(Operand::StaticVariable(store_buffer), scratch);
104 // Call stub on end of buffer.
105 // Check for end of buffer.
106 test(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
107 if (and_then == kReturnAtEnd) {
108 Label buffer_overflowed;
109 j(not_equal, &buffer_overflowed, Label::kNear);
111 bind(&buffer_overflowed);
113 ASSERT(and_then == kFallThroughAtEnd);
114 j(equal, &done, Label::kNear);
116 StoreBufferOverflowStub store_buffer_overflow =
117 StoreBufferOverflowStub(save_fp);
118 CallStub(&store_buffer_overflow);
119 if (and_then == kReturnAtEnd) {
122 ASSERT(and_then == kFallThroughAtEnd);
128 void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
129 XMMRegister scratch_reg,
130 Register result_reg) {
132 ExternalReference zero_ref = ExternalReference::address_of_zero();
133 movdbl(scratch_reg, Operand::StaticVariable(zero_ref));
134 Set(result_reg, Immediate(0));
135 ucomisd(input_reg, scratch_reg);
136 j(below, &done, Label::kNear);
137 ExternalReference half_ref = ExternalReference::address_of_one_half();
138 movdbl(scratch_reg, Operand::StaticVariable(half_ref));
139 addsd(scratch_reg, input_reg);
140 cvttsd2si(result_reg, Operand(scratch_reg));
141 test(result_reg, Immediate(0xFFFFFF00));
142 j(zero, &done, Label::kNear);
143 Set(result_reg, Immediate(255));
148 void MacroAssembler::ClampUint8(Register reg) {
150 test(reg, Immediate(0xFFFFFF00));
151 j(zero, &done, Label::kNear);
152 setcc(negative, reg); // 1 if negative, 0 if positive.
153 dec_b(reg); // 0 if negative, 255 if positive.
158 void MacroAssembler::RecordWriteArray(Register object,
161 SaveFPRegsMode save_fp,
162 RememberedSetAction remembered_set_action,
163 SmiCheck smi_check) {
164 // First, check if a write barrier is even needed. The tests below
165 // catch stores of Smis.
168 // Skip barrier if writing a smi.
169 if (smi_check == INLINE_SMI_CHECK) {
170 ASSERT_EQ(0, kSmiTag);
171 test(value, Immediate(kSmiTagMask));
175 // Array access: calculate the destination address in the same manner as
176 // KeyedStoreIC::GenerateGeneric. Multiply a smi by 2 to get an offset
177 // into an array of words.
178 Register dst = index;
179 lea(dst, Operand(object, index, times_half_pointer_size,
180 FixedArray::kHeaderSize - kHeapObjectTag));
183 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
187 // Clobber clobbered input registers when running with the debug-code flag
188 // turned on to provoke errors.
189 if (emit_debug_code()) {
190 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
191 mov(index, Immediate(BitCast<int32_t>(kZapValue)));
196 void MacroAssembler::RecordWriteField(
201 SaveFPRegsMode save_fp,
202 RememberedSetAction remembered_set_action,
203 SmiCheck smi_check) {
204 // First, check if a write barrier is even needed. The tests below
205 // catch stores of Smis.
208 // Skip barrier if writing a smi.
209 if (smi_check == INLINE_SMI_CHECK) {
210 JumpIfSmi(value, &done, Label::kNear);
213 // Although the object register is tagged, the offset is relative to the start
214 // of the object, so so offset must be a multiple of kPointerSize.
215 ASSERT(IsAligned(offset, kPointerSize));
217 lea(dst, FieldOperand(object, offset));
218 if (emit_debug_code()) {
220 test_b(dst, (1 << kPointerSizeLog2) - 1);
221 j(zero, &ok, Label::kNear);
227 object, dst, value, save_fp, remembered_set_action, OMIT_SMI_CHECK);
231 // Clobber clobbered input registers when running with the debug-code flag
232 // turned on to provoke errors.
233 if (emit_debug_code()) {
234 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
235 mov(dst, Immediate(BitCast<int32_t>(kZapValue)));
240 void MacroAssembler::RecordWriteForMap(
245 SaveFPRegsMode save_fp) {
248 Register address = scratch1;
249 Register value = scratch2;
250 if (emit_debug_code()) {
252 lea(address, FieldOperand(object, HeapObject::kMapOffset));
253 test_b(address, (1 << kPointerSizeLog2) - 1);
254 j(zero, &ok, Label::kNear);
259 ASSERT(!object.is(value));
260 ASSERT(!object.is(address));
261 ASSERT(!value.is(address));
262 if (emit_debug_code()) {
266 if (!FLAG_incremental_marking) {
270 // A single check of the map's pages interesting flag suffices, since it is
271 // only set during incremental collection, and then it's also guaranteed that
272 // the from object's page's interesting flag is also set. This optimization
273 // relies on the fact that maps can never be in new space.
274 ASSERT(!isolate()->heap()->InNewSpace(*map));
275 CheckPageFlagForMap(map,
276 MemoryChunk::kPointersToHereAreInterestingMask,
281 // Delay the initialization of |address| and |value| for the stub until it's
282 // known that the will be needed. Up until this point their values are not
283 // needed since they are embedded in the operands of instructions that need
285 lea(address, FieldOperand(object, HeapObject::kMapOffset));
286 mov(value, Immediate(map));
287 RecordWriteStub stub(object, value, address, OMIT_REMEMBERED_SET, save_fp);
292 // Clobber clobbered input registers when running with the debug-code flag
293 // turned on to provoke errors.
294 if (emit_debug_code()) {
295 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
296 mov(scratch1, Immediate(BitCast<int32_t>(kZapValue)));
297 mov(scratch2, Immediate(BitCast<int32_t>(kZapValue)));
302 void MacroAssembler::RecordWrite(Register object,
305 SaveFPRegsMode fp_mode,
306 RememberedSetAction remembered_set_action,
307 SmiCheck smi_check) {
308 ASSERT(!object.is(value));
309 ASSERT(!object.is(address));
310 ASSERT(!value.is(address));
311 if (emit_debug_code()) {
315 if (remembered_set_action == OMIT_REMEMBERED_SET &&
316 !FLAG_incremental_marking) {
320 if (FLAG_debug_code) {
322 cmp(value, Operand(address, 0));
323 j(equal, &ok, Label::kNear);
328 // First, check if a write barrier is even needed. The tests below
329 // catch stores of Smis and stores into young gen.
332 if (smi_check == INLINE_SMI_CHECK) {
333 // Skip barrier if writing a smi.
334 JumpIfSmi(value, &done, Label::kNear);
338 value, // Used as scratch.
339 MemoryChunk::kPointersToHereAreInterestingMask,
343 CheckPageFlag(object,
344 value, // Used as scratch.
345 MemoryChunk::kPointersFromHereAreInterestingMask,
350 RecordWriteStub stub(object, value, address, remembered_set_action, fp_mode);
355 // Clobber clobbered registers when running with the debug-code flag
356 // turned on to provoke errors.
357 if (emit_debug_code()) {
358 mov(address, Immediate(BitCast<int32_t>(kZapValue)));
359 mov(value, Immediate(BitCast<int32_t>(kZapValue)));
364 #ifdef ENABLE_DEBUGGER_SUPPORT
365 void MacroAssembler::DebugBreak() {
366 Set(eax, Immediate(0));
367 mov(ebx, Immediate(ExternalReference(Runtime::kDebugBreak, isolate())));
369 call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
374 void MacroAssembler::Set(Register dst, const Immediate& x) {
376 xor_(dst, dst); // Shorter than mov.
383 void MacroAssembler::Set(const Operand& dst, const Immediate& x) {
388 bool MacroAssembler::IsUnsafeImmediate(const Immediate& x) {
389 static const int kMaxImmediateBits = 17;
390 if (x.rmode_ != RelocInfo::NONE) return false;
391 return !is_intn(x.x_, kMaxImmediateBits);
395 void MacroAssembler::SafeSet(Register dst, const Immediate& x) {
396 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
397 Set(dst, Immediate(x.x_ ^ jit_cookie()));
398 xor_(dst, jit_cookie());
405 void MacroAssembler::SafePush(const Immediate& x) {
406 if (IsUnsafeImmediate(x) && jit_cookie() != 0) {
407 push(Immediate(x.x_ ^ jit_cookie()));
408 xor_(Operand(esp, 0), Immediate(jit_cookie()));
415 void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
416 // see ROOT_ACCESSOR macro in factory.h
417 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
422 void MacroAssembler::CompareRoot(const Operand& with,
423 Heap::RootListIndex index) {
424 // see ROOT_ACCESSOR macro in factory.h
425 Handle<Object> value(&isolate()->heap()->roots_array_start()[index]);
430 void MacroAssembler::CmpObjectType(Register heap_object,
433 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
434 CmpInstanceType(map, type);
438 void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
439 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
440 static_cast<int8_t>(type));
444 void MacroAssembler::CheckFastElements(Register map,
446 Label::Distance distance) {
447 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
448 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
449 STATIC_ASSERT(FAST_ELEMENTS == 2);
450 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
451 cmpb(FieldOperand(map, Map::kBitField2Offset),
452 Map::kMaximumBitField2FastHoleyElementValue);
453 j(above, fail, distance);
457 void MacroAssembler::CheckFastObjectElements(Register map,
459 Label::Distance distance) {
460 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
461 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
462 STATIC_ASSERT(FAST_ELEMENTS == 2);
463 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
464 cmpb(FieldOperand(map, Map::kBitField2Offset),
465 Map::kMaximumBitField2FastHoleySmiElementValue);
466 j(below_equal, fail, distance);
467 cmpb(FieldOperand(map, Map::kBitField2Offset),
468 Map::kMaximumBitField2FastHoleyElementValue);
469 j(above, fail, distance);
473 void MacroAssembler::CheckFastSmiElements(Register map,
475 Label::Distance distance) {
476 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
477 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
478 cmpb(FieldOperand(map, Map::kBitField2Offset),
479 Map::kMaximumBitField2FastHoleySmiElementValue);
480 j(above, fail, distance);
484 void MacroAssembler::StoreNumberToDoubleElements(
485 Register maybe_number,
489 XMMRegister scratch2,
491 bool specialize_for_processor) {
492 Label smi_value, done, maybe_nan, not_nan, is_nan, have_double_value;
493 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
495 CheckMap(maybe_number,
496 isolate()->factory()->heap_number_map(),
500 // Double value, canonicalize NaN.
501 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
502 cmp(FieldOperand(maybe_number, offset),
503 Immediate(kNaNOrInfinityLowerBoundUpper32));
504 j(greater_equal, &maybe_nan, Label::kNear);
507 ExternalReference canonical_nan_reference =
508 ExternalReference::address_of_canonical_non_hole_nan();
509 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
510 CpuFeatures::Scope use_sse2(SSE2);
511 movdbl(scratch2, FieldOperand(maybe_number, HeapNumber::kValueOffset));
512 bind(&have_double_value);
513 movdbl(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize),
516 fld_d(FieldOperand(maybe_number, HeapNumber::kValueOffset));
517 bind(&have_double_value);
518 fstp_d(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize));
523 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
524 // it's an Infinity, and the non-NaN code path applies.
525 j(greater, &is_nan, Label::kNear);
526 cmp(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
529 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
530 CpuFeatures::Scope use_sse2(SSE2);
531 movdbl(scratch2, Operand::StaticVariable(canonical_nan_reference));
533 fld_d(Operand::StaticVariable(canonical_nan_reference));
535 jmp(&have_double_value, Label::kNear);
538 // Value is a smi. Convert to a double and store.
539 // Preserve original value.
540 mov(scratch1, maybe_number);
542 if (CpuFeatures::IsSupported(SSE2) && specialize_for_processor) {
543 CpuFeatures::Scope fscope(SSE2);
544 cvtsi2sd(scratch2, scratch1);
545 movdbl(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize),
549 fild_s(Operand(esp, 0));
551 fstp_d(FieldOperand(elements, key, times_4, FixedDoubleArray::kHeaderSize));
557 void MacroAssembler::CompareMap(Register obj,
559 Label* early_success,
560 CompareMapMode mode) {
561 cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
562 if (mode == ALLOW_ELEMENT_TRANSITION_MAPS) {
563 ElementsKind kind = map->elements_kind();
564 if (IsFastElementsKind(kind)) {
565 bool packed = IsFastPackedElementsKind(kind);
566 Map* current_map = *map;
567 while (CanTransitionToMoreGeneralFastElementsKind(kind, packed)) {
568 kind = GetNextMoreGeneralFastElementsKind(kind, packed);
569 current_map = current_map->LookupElementsTransitionMap(kind);
570 if (!current_map) break;
571 j(equal, early_success, Label::kNear);
572 cmp(FieldOperand(obj, HeapObject::kMapOffset),
573 Handle<Map>(current_map));
580 void MacroAssembler::CheckMap(Register obj,
583 SmiCheckType smi_check_type,
584 CompareMapMode mode) {
585 if (smi_check_type == DO_SMI_CHECK) {
586 JumpIfSmi(obj, fail);
590 CompareMap(obj, map, &success, mode);
596 void MacroAssembler::DispatchMap(Register obj,
598 Handle<Code> success,
599 SmiCheckType smi_check_type) {
601 if (smi_check_type == DO_SMI_CHECK) {
602 JumpIfSmi(obj, &fail);
604 cmp(FieldOperand(obj, HeapObject::kMapOffset), Immediate(map));
611 Condition MacroAssembler::IsObjectStringType(Register heap_object,
613 Register instance_type) {
614 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
615 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
616 STATIC_ASSERT(kNotStringTag != 0);
617 test(instance_type, Immediate(kIsNotStringMask));
622 void MacroAssembler::IsObjectJSObjectType(Register heap_object,
626 mov(map, FieldOperand(heap_object, HeapObject::kMapOffset));
627 IsInstanceJSObjectType(map, scratch, fail);
631 void MacroAssembler::IsInstanceJSObjectType(Register map,
634 movzx_b(scratch, FieldOperand(map, Map::kInstanceTypeOffset));
635 sub(scratch, Immediate(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
637 LAST_NONCALLABLE_SPEC_OBJECT_TYPE - FIRST_NONCALLABLE_SPEC_OBJECT_TYPE);
642 void MacroAssembler::FCmp() {
643 if (CpuFeatures::IsSupported(CMOV)) {
656 void MacroAssembler::AbortIfNotNumber(Register object) {
658 JumpIfSmi(object, &ok);
659 cmp(FieldOperand(object, HeapObject::kMapOffset),
660 isolate()->factory()->heap_number_map());
661 Assert(equal, "Operand not a number");
666 void MacroAssembler::AbortIfNotSmi(Register object) {
667 test(object, Immediate(kSmiTagMask));
668 Assert(equal, "Operand is not a smi");
672 void MacroAssembler::AbortIfNotString(Register object) {
673 test(object, Immediate(kSmiTagMask));
674 Assert(not_equal, "Operand is not a string");
676 mov(object, FieldOperand(object, HeapObject::kMapOffset));
677 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
679 Assert(below, "Operand is not a string");
683 void MacroAssembler::AbortIfSmi(Register object) {
684 test(object, Immediate(kSmiTagMask));
685 Assert(not_equal, "Operand is a smi");
689 void MacroAssembler::EnterFrame(StackFrame::Type type) {
693 push(Immediate(Smi::FromInt(type)));
694 push(Immediate(CodeObject()));
695 if (emit_debug_code()) {
696 cmp(Operand(esp, 0), Immediate(isolate()->factory()->undefined_value()));
697 Check(not_equal, "code object not properly patched");
702 void MacroAssembler::LeaveFrame(StackFrame::Type type) {
703 if (emit_debug_code()) {
704 cmp(Operand(ebp, StandardFrameConstants::kMarkerOffset),
705 Immediate(Smi::FromInt(type)));
706 Check(equal, "stack frame types must match");
712 void MacroAssembler::EnterExitFramePrologue() {
713 // Set up the frame structure on the stack.
714 ASSERT(ExitFrameConstants::kCallerSPDisplacement == +2 * kPointerSize);
715 ASSERT(ExitFrameConstants::kCallerPCOffset == +1 * kPointerSize);
716 ASSERT(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
720 // Reserve room for entry stack pointer and push the code object.
721 ASSERT(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
722 push(Immediate(0)); // Saved entry sp, patched before call.
723 push(Immediate(CodeObject())); // Accessed from ExitFrame::code_slot.
725 // Save the frame pointer and the context in top.
726 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
728 ExternalReference context_address(Isolate::kContextAddress,
730 mov(Operand::StaticVariable(c_entry_fp_address), ebp);
731 mov(Operand::StaticVariable(context_address), esi);
735 void MacroAssembler::EnterExitFrameEpilogue(int argc, bool save_doubles) {
736 // Optionally save all XMM registers.
738 CpuFeatures::Scope scope(SSE2);
739 int space = XMMRegister::kNumRegisters * kDoubleSize + argc * kPointerSize;
740 sub(esp, Immediate(space));
741 const int offset = -2 * kPointerSize;
742 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
743 XMMRegister reg = XMMRegister::from_code(i);
744 movdbl(Operand(ebp, offset - ((i + 1) * kDoubleSize)), reg);
747 sub(esp, Immediate(argc * kPointerSize));
750 // Get the required frame alignment for the OS.
751 const int kFrameAlignment = OS::ActivationFrameAlignment();
752 if (kFrameAlignment > 0) {
753 ASSERT(IsPowerOf2(kFrameAlignment));
754 and_(esp, -kFrameAlignment);
757 // Patch the saved entry sp.
758 mov(Operand(ebp, ExitFrameConstants::kSPOffset), esp);
762 void MacroAssembler::EnterExitFrame(bool save_doubles) {
763 EnterExitFramePrologue();
765 // Set up argc and argv in callee-saved registers.
766 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
768 lea(esi, Operand(ebp, eax, times_4, offset));
770 // Reserve space for argc, argv and isolate.
771 EnterExitFrameEpilogue(3, save_doubles);
775 void MacroAssembler::EnterApiExitFrame(int argc) {
776 EnterExitFramePrologue();
777 EnterExitFrameEpilogue(argc, false);
781 void MacroAssembler::LeaveExitFrame(bool save_doubles) {
782 // Optionally restore all XMM registers.
784 CpuFeatures::Scope scope(SSE2);
785 const int offset = -2 * kPointerSize;
786 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
787 XMMRegister reg = XMMRegister::from_code(i);
788 movdbl(reg, Operand(ebp, offset - ((i + 1) * kDoubleSize)));
792 // Get the return address from the stack and restore the frame pointer.
793 mov(ecx, Operand(ebp, 1 * kPointerSize));
794 mov(ebp, Operand(ebp, 0 * kPointerSize));
796 // Pop the arguments and the receiver from the caller stack.
797 lea(esp, Operand(esi, 1 * kPointerSize));
799 // Push the return address to get ready to return.
802 LeaveExitFrameEpilogue();
805 void MacroAssembler::LeaveExitFrameEpilogue() {
806 // Restore current context from top and clear it in debug mode.
807 ExternalReference context_address(Isolate::kContextAddress, isolate());
808 mov(esi, Operand::StaticVariable(context_address));
810 mov(Operand::StaticVariable(context_address), Immediate(0));
813 // Clear the top frame.
814 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
816 mov(Operand::StaticVariable(c_entry_fp_address), Immediate(0));
820 void MacroAssembler::LeaveApiExitFrame() {
824 LeaveExitFrameEpilogue();
828 void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
830 // Adjust this code if not the case.
831 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
832 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
833 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
834 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
835 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
836 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
838 // We will build up the handler from the bottom by pushing on the stack.
839 // First push the frame pointer and context.
840 if (kind == StackHandler::JS_ENTRY) {
841 // The frame pointer does not point to a JS frame so we save NULL for
842 // ebp. We expect the code throwing an exception to check ebp before
843 // dereferencing it to restore the context.
844 push(Immediate(0)); // NULL frame pointer.
845 push(Immediate(Smi::FromInt(0))); // No context.
850 // Push the state and the code object.
852 StackHandler::IndexField::encode(handler_index) |
853 StackHandler::KindField::encode(kind);
854 push(Immediate(state));
857 // Link the current handler as the next handler.
858 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
859 push(Operand::StaticVariable(handler_address));
860 // Set this new handler as the current one.
861 mov(Operand::StaticVariable(handler_address), esp);
865 void MacroAssembler::PopTryHandler() {
866 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
867 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
868 pop(Operand::StaticVariable(handler_address));
869 add(esp, Immediate(StackHandlerConstants::kSize - kPointerSize));
873 void MacroAssembler::JumpToHandlerEntry() {
874 // Compute the handler entry address and jump to it. The handler table is
875 // a fixed array of (smi-tagged) code offsets.
876 // eax = exception, edi = code object, edx = state.
877 mov(ebx, FieldOperand(edi, Code::kHandlerTableOffset));
878 shr(edx, StackHandler::kKindWidth);
879 mov(edx, FieldOperand(ebx, edx, times_4, FixedArray::kHeaderSize));
881 lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
886 void MacroAssembler::Throw(Register value) {
887 // Adjust this code if not the case.
888 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
889 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
890 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
891 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
892 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
893 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
895 // The exception is expected in eax.
896 if (!value.is(eax)) {
899 // Drop the stack pointer to the top of the top handler.
900 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
901 mov(esp, Operand::StaticVariable(handler_address));
902 // Restore the next handler.
903 pop(Operand::StaticVariable(handler_address));
905 // Remove the code object and state, compute the handler address in edi.
906 pop(edi); // Code object.
907 pop(edx); // Index and state.
909 // Restore the context and frame pointer.
910 pop(esi); // Context.
911 pop(ebp); // Frame pointer.
913 // If the handler is a JS frame, restore the context to the frame.
914 // (kind == ENTRY) == (ebp == 0) == (esi == 0), so we could test either
918 j(zero, &skip, Label::kNear);
919 mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
922 JumpToHandlerEntry();
926 void MacroAssembler::ThrowUncatchable(Register value) {
927 // Adjust this code if not the case.
928 STATIC_ASSERT(StackHandlerConstants::kSize == 5 * kPointerSize);
929 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
930 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
931 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
932 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
933 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
935 // The exception is expected in eax.
936 if (!value.is(eax)) {
939 // Drop the stack pointer to the top of the top stack handler.
940 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
941 mov(esp, Operand::StaticVariable(handler_address));
943 // Unwind the handlers until the top ENTRY handler is found.
944 Label fetch_next, check_kind;
945 jmp(&check_kind, Label::kNear);
947 mov(esp, Operand(esp, StackHandlerConstants::kNextOffset));
950 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
951 test(Operand(esp, StackHandlerConstants::kStateOffset),
952 Immediate(StackHandler::KindField::kMask));
953 j(not_zero, &fetch_next);
955 // Set the top handler address to next handler past the top ENTRY handler.
956 pop(Operand::StaticVariable(handler_address));
958 // Remove the code object and state, compute the handler address in edi.
959 pop(edi); // Code object.
960 pop(edx); // Index and state.
962 // Clear the context pointer and frame pointer (0 was saved in the handler).
966 JumpToHandlerEntry();
970 void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
975 ASSERT(!holder_reg.is(scratch));
977 // Load current lexical context from the stack frame.
978 mov(scratch, Operand(ebp, StandardFrameConstants::kContextOffset));
980 // When generating debug code, make sure the lexical context is set.
981 if (emit_debug_code()) {
982 cmp(scratch, Immediate(0));
983 Check(not_equal, "we should not have an empty lexical context");
985 // Load the global context of the current context.
986 int offset = Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
987 mov(scratch, FieldOperand(scratch, offset));
988 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
990 // Check the context is a global context.
991 if (emit_debug_code()) {
993 // Read the first word and compare to global_context_map.
994 mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
995 cmp(scratch, isolate()->factory()->global_context_map());
996 Check(equal, "JSGlobalObject::global_context should be a global context.");
1000 // Check if both contexts are the same.
1001 cmp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
1002 j(equal, &same_contexts);
1004 // Compare security tokens, save holder_reg on the stack so we can use it
1005 // as a temporary register.
1007 // TODO(119): avoid push(holder_reg)/pop(holder_reg)
1009 // Check that the security token in the calling global object is
1010 // compatible with the security token in the receiving global
1012 mov(holder_reg, FieldOperand(holder_reg, JSGlobalProxy::kContextOffset));
1014 // Check the context is a global context.
1015 if (emit_debug_code()) {
1016 cmp(holder_reg, isolate()->factory()->null_value());
1017 Check(not_equal, "JSGlobalProxy::context() should not be null.");
1020 // Read the first word and compare to global_context_map(),
1021 mov(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
1022 cmp(holder_reg, isolate()->factory()->global_context_map());
1023 Check(equal, "JSGlobalObject::global_context should be a global context.");
1027 int token_offset = Context::kHeaderSize +
1028 Context::SECURITY_TOKEN_INDEX * kPointerSize;
1029 mov(scratch, FieldOperand(scratch, token_offset));
1030 cmp(scratch, FieldOperand(holder_reg, token_offset));
1034 bind(&same_contexts);
1038 // Compute the hash code from the untagged key. This must be kept in sync
1039 // with ComputeIntegerHash in utils.h.
1041 // Note: r0 will contain hash code
1042 void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
1043 // Xor original key with a seed.
1044 if (Serializer::enabled()) {
1045 ExternalReference roots_array_start =
1046 ExternalReference::roots_array_start(isolate());
1047 mov(scratch, Immediate(Heap::kHashSeedRootIndex));
1049 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
1053 int32_t seed = isolate()->heap()->HashSeed();
1054 xor_(r0, Immediate(seed));
1057 // hash = ~hash + (hash << 15);
1062 // hash = hash ^ (hash >> 12);
1066 // hash = hash + (hash << 2);
1067 lea(r0, Operand(r0, r0, times_4, 0));
1068 // hash = hash ^ (hash >> 4);
1072 // hash = hash * 2057;
1074 // hash = hash ^ (hash >> 16);
1082 void MacroAssembler::LoadFromNumberDictionary(Label* miss,
1091 // elements - holds the slow-case elements of the receiver and is unchanged.
1093 // key - holds the smi key on entry and is unchanged.
1095 // Scratch registers:
1097 // r0 - holds the untagged key on entry and holds the hash once computed.
1099 // r1 - used to hold the capacity mask of the dictionary
1101 // r2 - used for the index into the dictionary.
1103 // result - holds the result on exit if the load succeeds and we fall through.
1107 GetNumberHash(r0, r1);
1109 // Compute capacity mask.
1110 mov(r1, FieldOperand(elements, SeededNumberDictionary::kCapacityOffset));
1111 shr(r1, kSmiTagSize); // convert smi to int
1114 // Generate an unrolled loop that performs a few probes before giving up.
1115 const int kProbes = 4;
1116 for (int i = 0; i < kProbes; i++) {
1117 // Use r2 for index calculations and keep the hash intact in r0.
1119 // Compute the masked index: (hash + i + i * i) & mask.
1121 add(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
1125 // Scale the index by multiplying by the entry size.
1126 ASSERT(SeededNumberDictionary::kEntrySize == 3);
1127 lea(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
1129 // Check if the key matches.
1130 cmp(key, FieldOperand(elements,
1133 SeededNumberDictionary::kElementsStartOffset));
1134 if (i != (kProbes - 1)) {
1142 // Check that the value is a normal propety.
1143 const int kDetailsOffset =
1144 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
1145 ASSERT_EQ(NORMAL, 0);
1146 test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
1147 Immediate(PropertyDetails::TypeField::kMask << kSmiTagSize));
1150 // Get the value at the masked, scaled index.
1151 const int kValueOffset =
1152 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
1153 mov(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
1157 void MacroAssembler::LoadAllocationTopHelper(Register result,
1159 AllocationFlags flags) {
1160 ExternalReference new_space_allocation_top =
1161 ExternalReference::new_space_allocation_top_address(isolate());
1163 // Just return if allocation top is already known.
1164 if ((flags & RESULT_CONTAINS_TOP) != 0) {
1165 // No use of scratch if allocation top is provided.
1166 ASSERT(scratch.is(no_reg));
1168 // Assert that result actually contains top on entry.
1169 cmp(result, Operand::StaticVariable(new_space_allocation_top));
1170 Check(equal, "Unexpected allocation top");
1175 // Move address of new object to result. Use scratch register if available.
1176 if (scratch.is(no_reg)) {
1177 mov(result, Operand::StaticVariable(new_space_allocation_top));
1179 mov(scratch, Immediate(new_space_allocation_top));
1180 mov(result, Operand(scratch, 0));
1185 void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
1187 if (emit_debug_code()) {
1188 test(result_end, Immediate(kObjectAlignmentMask));
1189 Check(zero, "Unaligned allocation in new space");
1192 ExternalReference new_space_allocation_top =
1193 ExternalReference::new_space_allocation_top_address(isolate());
1195 // Update new top. Use scratch if available.
1196 if (scratch.is(no_reg)) {
1197 mov(Operand::StaticVariable(new_space_allocation_top), result_end);
1199 mov(Operand(scratch, 0), result_end);
1204 void MacroAssembler::AllocateInNewSpace(int object_size,
1206 Register result_end,
1209 AllocationFlags flags) {
1210 if (!FLAG_inline_new) {
1211 if (emit_debug_code()) {
1212 // Trash the registers to simulate an allocation failure.
1213 mov(result, Immediate(0x7091));
1214 if (result_end.is_valid()) {
1215 mov(result_end, Immediate(0x7191));
1217 if (scratch.is_valid()) {
1218 mov(scratch, Immediate(0x7291));
1224 ASSERT(!result.is(result_end));
1226 // Load address of new object into result.
1227 LoadAllocationTopHelper(result, scratch, flags);
1229 Register top_reg = result_end.is_valid() ? result_end : result;
1231 // Calculate new top and bail out if new space is exhausted.
1232 ExternalReference new_space_allocation_limit =
1233 ExternalReference::new_space_allocation_limit_address(isolate());
1235 if (!top_reg.is(result)) {
1236 mov(top_reg, result);
1238 add(top_reg, Immediate(object_size));
1239 j(carry, gc_required);
1240 cmp(top_reg, Operand::StaticVariable(new_space_allocation_limit));
1241 j(above, gc_required);
1243 // Update allocation top.
1244 UpdateAllocationTopHelper(top_reg, scratch);
1246 // Tag result if requested.
1247 if (top_reg.is(result)) {
1248 if ((flags & TAG_OBJECT) != 0) {
1249 sub(result, Immediate(object_size - kHeapObjectTag));
1251 sub(result, Immediate(object_size));
1253 } else if ((flags & TAG_OBJECT) != 0) {
1254 add(result, Immediate(kHeapObjectTag));
1259 void MacroAssembler::AllocateInNewSpace(int header_size,
1260 ScaleFactor element_size,
1261 Register element_count,
1263 Register result_end,
1266 AllocationFlags flags) {
1267 if (!FLAG_inline_new) {
1268 if (emit_debug_code()) {
1269 // Trash the registers to simulate an allocation failure.
1270 mov(result, Immediate(0x7091));
1271 mov(result_end, Immediate(0x7191));
1272 if (scratch.is_valid()) {
1273 mov(scratch, Immediate(0x7291));
1275 // Register element_count is not modified by the function.
1280 ASSERT(!result.is(result_end));
1282 // Load address of new object into result.
1283 LoadAllocationTopHelper(result, scratch, flags);
1285 // Calculate new top and bail out if new space is exhausted.
1286 ExternalReference new_space_allocation_limit =
1287 ExternalReference::new_space_allocation_limit_address(isolate());
1289 // We assume that element_count*element_size + header_size does not
1291 lea(result_end, Operand(element_count, element_size, header_size));
1292 add(result_end, result);
1293 j(carry, gc_required);
1294 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
1295 j(above, gc_required);
1297 // Tag result if requested.
1298 if ((flags & TAG_OBJECT) != 0) {
1299 lea(result, Operand(result, kHeapObjectTag));
1302 // Update allocation top.
1303 UpdateAllocationTopHelper(result_end, scratch);
1307 void MacroAssembler::AllocateInNewSpace(Register object_size,
1309 Register result_end,
1312 AllocationFlags flags) {
1313 if (!FLAG_inline_new) {
1314 if (emit_debug_code()) {
1315 // Trash the registers to simulate an allocation failure.
1316 mov(result, Immediate(0x7091));
1317 mov(result_end, Immediate(0x7191));
1318 if (scratch.is_valid()) {
1319 mov(scratch, Immediate(0x7291));
1321 // object_size is left unchanged by this function.
1326 ASSERT(!result.is(result_end));
1328 // Load address of new object into result.
1329 LoadAllocationTopHelper(result, scratch, flags);
1331 // Calculate new top and bail out if new space is exhausted.
1332 ExternalReference new_space_allocation_limit =
1333 ExternalReference::new_space_allocation_limit_address(isolate());
1334 if (!object_size.is(result_end)) {
1335 mov(result_end, object_size);
1337 add(result_end, result);
1338 j(carry, gc_required);
1339 cmp(result_end, Operand::StaticVariable(new_space_allocation_limit));
1340 j(above, gc_required);
1342 // Tag result if requested.
1343 if ((flags & TAG_OBJECT) != 0) {
1344 lea(result, Operand(result, kHeapObjectTag));
1347 // Update allocation top.
1348 UpdateAllocationTopHelper(result_end, scratch);
1352 void MacroAssembler::UndoAllocationInNewSpace(Register object) {
1353 ExternalReference new_space_allocation_top =
1354 ExternalReference::new_space_allocation_top_address(isolate());
1356 // Make sure the object has no tag before resetting top.
1357 and_(object, Immediate(~kHeapObjectTagMask));
1359 cmp(object, Operand::StaticVariable(new_space_allocation_top));
1360 Check(below, "Undo allocation of non allocated memory");
1362 mov(Operand::StaticVariable(new_space_allocation_top), object);
1366 void MacroAssembler::AllocateHeapNumber(Register result,
1369 Label* gc_required) {
1370 // Allocate heap number in new space.
1371 AllocateInNewSpace(HeapNumber::kSize,
1379 mov(FieldOperand(result, HeapObject::kMapOffset),
1380 Immediate(isolate()->factory()->heap_number_map()));
1384 void MacroAssembler::AllocateTwoByteString(Register result,
1389 Label* gc_required) {
1390 // Calculate the number of bytes needed for the characters in the string while
1391 // observing object alignment.
1392 ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
1393 ASSERT(kShortSize == 2);
1394 // scratch1 = length * 2 + kObjectAlignmentMask.
1395 lea(scratch1, Operand(length, length, times_1, kObjectAlignmentMask));
1396 and_(scratch1, Immediate(~kObjectAlignmentMask));
1398 // Allocate two byte string in new space.
1399 AllocateInNewSpace(SeqTwoByteString::kHeaderSize,
1408 // Set the map, length and hash field.
1409 mov(FieldOperand(result, HeapObject::kMapOffset),
1410 Immediate(isolate()->factory()->string_map()));
1411 mov(scratch1, length);
1413 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1414 mov(FieldOperand(result, String::kHashFieldOffset),
1415 Immediate(String::kEmptyHashField));
1419 void MacroAssembler::AllocateAsciiString(Register result,
1424 Label* gc_required) {
1425 // Calculate the number of bytes needed for the characters in the string while
1426 // observing object alignment.
1427 ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
1428 mov(scratch1, length);
1429 ASSERT(kCharSize == 1);
1430 add(scratch1, Immediate(kObjectAlignmentMask));
1431 and_(scratch1, Immediate(~kObjectAlignmentMask));
1433 // Allocate ASCII string in new space.
1434 AllocateInNewSpace(SeqAsciiString::kHeaderSize,
1443 // Set the map, length and hash field.
1444 mov(FieldOperand(result, HeapObject::kMapOffset),
1445 Immediate(isolate()->factory()->ascii_string_map()));
1446 mov(scratch1, length);
1448 mov(FieldOperand(result, String::kLengthOffset), scratch1);
1449 mov(FieldOperand(result, String::kHashFieldOffset),
1450 Immediate(String::kEmptyHashField));
1454 void MacroAssembler::AllocateAsciiString(Register result,
1458 Label* gc_required) {
1461 // Allocate ASCII string in new space.
1462 AllocateInNewSpace(SeqAsciiString::SizeFor(length),
1469 // Set the map, length and hash field.
1470 mov(FieldOperand(result, HeapObject::kMapOffset),
1471 Immediate(isolate()->factory()->ascii_string_map()));
1472 mov(FieldOperand(result, String::kLengthOffset),
1473 Immediate(Smi::FromInt(length)));
1474 mov(FieldOperand(result, String::kHashFieldOffset),
1475 Immediate(String::kEmptyHashField));
1479 void MacroAssembler::AllocateTwoByteConsString(Register result,
1482 Label* gc_required) {
1483 // Allocate heap number in new space.
1484 AllocateInNewSpace(ConsString::kSize,
1491 // Set the map. The other fields are left uninitialized.
1492 mov(FieldOperand(result, HeapObject::kMapOffset),
1493 Immediate(isolate()->factory()->cons_string_map()));
1497 void MacroAssembler::AllocateAsciiConsString(Register result,
1500 Label* gc_required) {
1501 // Allocate heap number in new space.
1502 AllocateInNewSpace(ConsString::kSize,
1509 // Set the map. The other fields are left uninitialized.
1510 mov(FieldOperand(result, HeapObject::kMapOffset),
1511 Immediate(isolate()->factory()->cons_ascii_string_map()));
1515 void MacroAssembler::AllocateTwoByteSlicedString(Register result,
1518 Label* gc_required) {
1519 // Allocate heap number in new space.
1520 AllocateInNewSpace(SlicedString::kSize,
1527 // Set the map. The other fields are left uninitialized.
1528 mov(FieldOperand(result, HeapObject::kMapOffset),
1529 Immediate(isolate()->factory()->sliced_string_map()));
1533 void MacroAssembler::AllocateAsciiSlicedString(Register result,
1536 Label* gc_required) {
1537 // Allocate heap number in new space.
1538 AllocateInNewSpace(SlicedString::kSize,
1545 // Set the map. The other fields are left uninitialized.
1546 mov(FieldOperand(result, HeapObject::kMapOffset),
1547 Immediate(isolate()->factory()->sliced_ascii_string_map()));
1551 // Copy memory, byte-by-byte, from source to destination. Not optimized for
1552 // long or aligned copies. The contents of scratch and length are destroyed.
1553 // Source and destination are incremented by length.
1554 // Many variants of movsb, loop unrolling, word moves, and indexed operands
1555 // have been tried here already, and this is fastest.
1556 // A simpler loop is faster on small copies, but 30% slower on large ones.
1557 // The cld() instruction must have been emitted, to set the direction flag(),
1558 // before calling this function.
1559 void MacroAssembler::CopyBytes(Register source,
1560 Register destination,
1563 Label loop, done, short_string, short_loop;
1564 // Experimentation shows that the short string loop is faster if length < 10.
1565 cmp(length, Immediate(10));
1566 j(less_equal, &short_string);
1568 ASSERT(source.is(esi));
1569 ASSERT(destination.is(edi));
1570 ASSERT(length.is(ecx));
1572 // Because source is 4-byte aligned in our uses of this function,
1573 // we keep source aligned for the rep_movs call by copying the odd bytes
1574 // at the end of the ranges.
1575 mov(scratch, Operand(source, length, times_1, -4));
1576 mov(Operand(destination, length, times_1, -4), scratch);
1580 and_(scratch, Immediate(0x3));
1581 add(destination, scratch);
1584 bind(&short_string);
1585 test(length, length);
1589 mov_b(scratch, Operand(source, 0));
1590 mov_b(Operand(destination, 0), scratch);
1594 j(not_zero, &short_loop);
1600 void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
1601 Register end_offset,
1606 mov(Operand(start_offset, 0), filler);
1607 add(start_offset, Immediate(kPointerSize));
1609 cmp(start_offset, end_offset);
1614 void MacroAssembler::BooleanBitTest(Register object,
1617 bit_index += kSmiTagSize + kSmiShiftSize;
1618 ASSERT(IsPowerOf2(kBitsPerByte));
1619 int byte_index = bit_index / kBitsPerByte;
1620 int byte_bit_index = bit_index & (kBitsPerByte - 1);
1621 test_b(FieldOperand(object, field_offset + byte_index),
1622 static_cast<byte>(1 << byte_bit_index));
1627 void MacroAssembler::NegativeZeroTest(Register result,
1629 Label* then_label) {
1631 test(result, result);
1634 j(sign, then_label);
1639 void MacroAssembler::NegativeZeroTest(Register result,
1643 Label* then_label) {
1645 test(result, result);
1649 j(sign, then_label);
1654 void MacroAssembler::TryGetFunctionPrototype(Register function,
1658 bool miss_on_bound_function) {
1659 // Check that the receiver isn't a smi.
1660 JumpIfSmi(function, miss);
1662 // Check that the function really is a function.
1663 CmpObjectType(function, JS_FUNCTION_TYPE, result);
1666 if (miss_on_bound_function) {
1667 // If a bound function, go to miss label.
1669 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
1670 BooleanBitTest(scratch, SharedFunctionInfo::kCompilerHintsOffset,
1671 SharedFunctionInfo::kBoundFunction);
1675 // Make sure that the function has an instance prototype.
1677 movzx_b(scratch, FieldOperand(result, Map::kBitFieldOffset));
1678 test(scratch, Immediate(1 << Map::kHasNonInstancePrototype));
1679 j(not_zero, &non_instance);
1681 // Get the prototype or initial map from the function.
1683 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1685 // If the prototype or initial map is the hole, don't return it and
1686 // simply miss the cache instead. This will allow us to allocate a
1687 // prototype object on-demand in the runtime system.
1688 cmp(result, Immediate(isolate()->factory()->the_hole_value()));
1691 // If the function does not have an initial map, we're done.
1693 CmpObjectType(result, MAP_TYPE, scratch);
1694 j(not_equal, &done);
1696 // Get the prototype from the initial map.
1697 mov(result, FieldOperand(result, Map::kPrototypeOffset));
1700 // Non-instance prototype: Fetch prototype from constructor field
1702 bind(&non_instance);
1703 mov(result, FieldOperand(result, Map::kConstructorOffset));
1710 void MacroAssembler::CallStub(CodeStub* stub, unsigned ast_id) {
1711 ASSERT(AllowThisStubCall(stub)); // Calls are not allowed in some stubs.
1712 call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
1716 void MacroAssembler::TailCallStub(CodeStub* stub) {
1717 ASSERT(allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe());
1718 jmp(stub->GetCode(), RelocInfo::CODE_TARGET);
1722 void MacroAssembler::StubReturn(int argc) {
1723 ASSERT(argc >= 1 && generating_stub());
1724 ret((argc - 1) * kPointerSize);
1728 bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
1729 if (!has_frame_ && stub->SometimesSetsUpAFrame()) return false;
1730 return allow_stub_calls_ || stub->CompilingCallsToThisStubIsGCSafe();
1734 void MacroAssembler::IllegalOperation(int num_arguments) {
1735 if (num_arguments > 0) {
1736 add(esp, Immediate(num_arguments * kPointerSize));
1738 mov(eax, Immediate(isolate()->factory()->undefined_value()));
1742 void MacroAssembler::IndexFromHash(Register hash, Register index) {
1743 // The assert checks that the constants for the maximum number of digits
1744 // for an array index cached in the hash field and the number of bits
1745 // reserved for it does not conflict.
1746 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
1747 (1 << String::kArrayIndexValueBits));
1748 // We want the smi-tagged index in key. kArrayIndexValueMask has zeros in
1749 // the low kHashShift bits.
1750 and_(hash, String::kArrayIndexValueMask);
1751 STATIC_ASSERT(String::kHashShift >= kSmiTagSize && kSmiTag == 0);
1752 if (String::kHashShift > kSmiTagSize) {
1753 shr(hash, String::kHashShift - kSmiTagSize);
1755 if (!index.is(hash)) {
1761 void MacroAssembler::CallRuntime(Runtime::FunctionId id, int num_arguments) {
1762 CallRuntime(Runtime::FunctionForId(id), num_arguments);
1766 void MacroAssembler::CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1767 const Runtime::Function* function = Runtime::FunctionForId(id);
1768 Set(eax, Immediate(function->nargs));
1769 mov(ebx, Immediate(ExternalReference(function, isolate())));
1770 CEntryStub ces(1, kSaveFPRegs);
1775 void MacroAssembler::CallRuntime(const Runtime::Function* f,
1776 int num_arguments) {
1777 // If the expected number of arguments of the runtime function is
1778 // constant, we check that the actual number of arguments match the
1780 if (f->nargs >= 0 && f->nargs != num_arguments) {
1781 IllegalOperation(num_arguments);
1785 // TODO(1236192): Most runtime routines don't need the number of
1786 // arguments passed in because it is constant. At some point we
1787 // should remove this need and make the runtime routine entry code
1789 Set(eax, Immediate(num_arguments));
1790 mov(ebx, Immediate(ExternalReference(f, isolate())));
1796 void MacroAssembler::CallExternalReference(ExternalReference ref,
1797 int num_arguments) {
1798 mov(eax, Immediate(num_arguments));
1799 mov(ebx, Immediate(ref));
1806 void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
1809 // TODO(1236192): Most runtime routines don't need the number of
1810 // arguments passed in because it is constant. At some point we
1811 // should remove this need and make the runtime routine entry code
1813 Set(eax, Immediate(num_arguments));
1814 JumpToExternalReference(ext);
1818 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
1821 TailCallExternalReference(ExternalReference(fid, isolate()),
1827 // If true, a Handle<T> returned by value from a function with cdecl calling
1828 // convention will be returned directly as a value of location_ field in a
1830 // If false, it is returned as a pointer to a preallocated by caller memory
1831 // region. Pointer to this region should be passed to a function as an
1832 // implicit first argument.
1833 #if defined(USING_BSD_ABI) || defined(__MINGW32__) || defined(__CYGWIN__)
1834 static const bool kReturnHandlesDirectly = true;
1836 static const bool kReturnHandlesDirectly = false;
1840 Operand ApiParameterOperand(int index) {
1842 esp, (index + (kReturnHandlesDirectly ? 0 : 1)) * kPointerSize);
1846 void MacroAssembler::PrepareCallApiFunction(int argc) {
1847 if (kReturnHandlesDirectly) {
1848 EnterApiExitFrame(argc);
1849 // When handles are returned directly we don't have to allocate extra
1850 // space for and pass an out parameter.
1851 if (emit_debug_code()) {
1852 mov(esi, Immediate(BitCast<int32_t>(kZapValue)));
1855 // We allocate two additional slots: return value and pointer to it.
1856 EnterApiExitFrame(argc + 2);
1858 // The argument slots are filled as follows:
1860 // n + 1: output slot
1864 // 0: pointer to the output slot
1866 lea(esi, Operand(esp, (argc + 1) * kPointerSize));
1867 mov(Operand(esp, 0 * kPointerSize), esi);
1868 if (emit_debug_code()) {
1869 mov(Operand(esi, 0), Immediate(0));
1875 void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
1877 ExternalReference next_address =
1878 ExternalReference::handle_scope_next_address();
1879 ExternalReference limit_address =
1880 ExternalReference::handle_scope_limit_address();
1881 ExternalReference level_address =
1882 ExternalReference::handle_scope_level_address();
1884 // Allocate HandleScope in callee-save registers.
1885 mov(ebx, Operand::StaticVariable(next_address));
1886 mov(edi, Operand::StaticVariable(limit_address));
1887 add(Operand::StaticVariable(level_address), Immediate(1));
1889 // Call the api function.
1890 call(function_address, RelocInfo::RUNTIME_ENTRY);
1892 if (!kReturnHandlesDirectly) {
1893 // PrepareCallApiFunction saved pointer to the output slot into
1894 // callee-save register esi.
1895 mov(eax, Operand(esi, 0));
1900 Label promote_scheduled_exception;
1901 Label delete_allocated_handles;
1902 Label leave_exit_frame;
1904 // Check if the result handle holds 0.
1906 j(zero, &empty_handle);
1907 // It was non-zero. Dereference to get the result value.
1908 mov(eax, Operand(eax, 0));
1910 // No more valid handles (the result handle was the last one). Restore
1911 // previous handle scope.
1912 mov(Operand::StaticVariable(next_address), ebx);
1913 sub(Operand::StaticVariable(level_address), Immediate(1));
1914 Assert(above_equal, "Invalid HandleScope level");
1915 cmp(edi, Operand::StaticVariable(limit_address));
1916 j(not_equal, &delete_allocated_handles);
1917 bind(&leave_exit_frame);
1919 // Check if the function scheduled an exception.
1920 ExternalReference scheduled_exception_address =
1921 ExternalReference::scheduled_exception_address(isolate());
1922 cmp(Operand::StaticVariable(scheduled_exception_address),
1923 Immediate(isolate()->factory()->the_hole_value()));
1924 j(not_equal, &promote_scheduled_exception);
1925 LeaveApiExitFrame();
1926 ret(stack_space * kPointerSize);
1927 bind(&promote_scheduled_exception);
1928 TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
1930 bind(&empty_handle);
1931 // It was zero; the result is undefined.
1932 mov(eax, isolate()->factory()->undefined_value());
1935 // HandleScope limit has changed. Delete allocated extensions.
1936 ExternalReference delete_extensions =
1937 ExternalReference::delete_handle_scope_extensions(isolate());
1938 bind(&delete_allocated_handles);
1939 mov(Operand::StaticVariable(limit_address), edi);
1941 mov(Operand(esp, 0), Immediate(ExternalReference::isolate_address()));
1942 mov(eax, Immediate(delete_extensions));
1945 jmp(&leave_exit_frame);
1949 void MacroAssembler::JumpToExternalReference(const ExternalReference& ext) {
1950 // Set the entry point and jump to the C entry runtime stub.
1951 mov(ebx, Immediate(ext));
1953 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
1957 void MacroAssembler::SetCallKind(Register dst, CallKind call_kind) {
1958 // This macro takes the dst register to make the code more readable
1959 // at the call sites. However, the dst register has to be ecx to
1960 // follow the calling convention which requires the call type to be
1962 ASSERT(dst.is(ecx));
1963 if (call_kind == CALL_AS_FUNCTION) {
1964 // Set to some non-zero smi by updating the least significant
1966 mov_b(dst, 1 << kSmiTagSize);
1968 // Set to smi zero by clearing the register.
1974 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
1975 const ParameterCount& actual,
1976 Handle<Code> code_constant,
1977 const Operand& code_operand,
1979 bool* definitely_mismatches,
1981 Label::Distance done_near,
1982 const CallWrapper& call_wrapper,
1983 CallKind call_kind) {
1984 bool definitely_matches = false;
1985 *definitely_mismatches = false;
1987 if (expected.is_immediate()) {
1988 ASSERT(actual.is_immediate());
1989 if (expected.immediate() == actual.immediate()) {
1990 definitely_matches = true;
1992 mov(eax, actual.immediate());
1993 const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
1994 if (expected.immediate() == sentinel) {
1995 // Don't worry about adapting arguments for builtins that
1996 // don't want that done. Skip adaption code by making it look
1997 // like we have a match between expected and actual number of
1999 definitely_matches = true;
2001 *definitely_mismatches = true;
2002 mov(ebx, expected.immediate());
2006 if (actual.is_immediate()) {
2007 // Expected is in register, actual is immediate. This is the
2008 // case when we invoke function values without going through the
2010 cmp(expected.reg(), actual.immediate());
2012 ASSERT(expected.reg().is(ebx));
2013 mov(eax, actual.immediate());
2014 } else if (!expected.reg().is(actual.reg())) {
2015 // Both expected and actual are in (different) registers. This
2016 // is the case when we invoke functions using call and apply.
2017 cmp(expected.reg(), actual.reg());
2019 ASSERT(actual.reg().is(eax));
2020 ASSERT(expected.reg().is(ebx));
2024 if (!definitely_matches) {
2025 Handle<Code> adaptor =
2026 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2027 if (!code_constant.is_null()) {
2028 mov(edx, Immediate(code_constant));
2029 add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
2030 } else if (!code_operand.is_reg(edx)) {
2031 mov(edx, code_operand);
2034 if (flag == CALL_FUNCTION) {
2035 call_wrapper.BeforeCall(CallSize(adaptor, RelocInfo::CODE_TARGET));
2036 SetCallKind(ecx, call_kind);
2037 call(adaptor, RelocInfo::CODE_TARGET);
2038 call_wrapper.AfterCall();
2039 if (!*definitely_mismatches) {
2040 jmp(done, done_near);
2043 SetCallKind(ecx, call_kind);
2044 jmp(adaptor, RelocInfo::CODE_TARGET);
2051 void MacroAssembler::InvokeCode(const Operand& code,
2052 const ParameterCount& expected,
2053 const ParameterCount& actual,
2055 const CallWrapper& call_wrapper,
2056 CallKind call_kind) {
2057 // You can't call a function without a valid frame.
2058 ASSERT(flag == JUMP_FUNCTION || has_frame());
2061 bool definitely_mismatches = false;
2062 InvokePrologue(expected, actual, Handle<Code>::null(), code,
2063 &done, &definitely_mismatches, flag, Label::kNear,
2064 call_wrapper, call_kind);
2065 if (!definitely_mismatches) {
2066 if (flag == CALL_FUNCTION) {
2067 call_wrapper.BeforeCall(CallSize(code));
2068 SetCallKind(ecx, call_kind);
2070 call_wrapper.AfterCall();
2072 ASSERT(flag == JUMP_FUNCTION);
2073 SetCallKind(ecx, call_kind);
2081 void MacroAssembler::InvokeCode(Handle<Code> code,
2082 const ParameterCount& expected,
2083 const ParameterCount& actual,
2084 RelocInfo::Mode rmode,
2086 const CallWrapper& call_wrapper,
2087 CallKind call_kind) {
2088 // You can't call a function without a valid frame.
2089 ASSERT(flag == JUMP_FUNCTION || has_frame());
2092 Operand dummy(eax, 0);
2093 bool definitely_mismatches = false;
2094 InvokePrologue(expected, actual, code, dummy, &done, &definitely_mismatches,
2095 flag, Label::kNear, call_wrapper, call_kind);
2096 if (!definitely_mismatches) {
2097 if (flag == CALL_FUNCTION) {
2098 call_wrapper.BeforeCall(CallSize(code, rmode));
2099 SetCallKind(ecx, call_kind);
2101 call_wrapper.AfterCall();
2103 ASSERT(flag == JUMP_FUNCTION);
2104 SetCallKind(ecx, call_kind);
2112 void MacroAssembler::InvokeFunction(Register fun,
2113 const ParameterCount& actual,
2115 const CallWrapper& call_wrapper,
2116 CallKind call_kind) {
2117 // You can't call a function without a valid frame.
2118 ASSERT(flag == JUMP_FUNCTION || has_frame());
2120 ASSERT(fun.is(edi));
2121 mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2122 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2123 mov(ebx, FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
2126 ParameterCount expected(ebx);
2127 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2128 expected, actual, flag, call_wrapper, call_kind);
2132 void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
2133 const ParameterCount& actual,
2135 const CallWrapper& call_wrapper,
2136 CallKind call_kind) {
2137 // You can't call a function without a valid frame.
2138 ASSERT(flag == JUMP_FUNCTION || has_frame());
2140 // Get the function and setup the context.
2141 LoadHeapObject(edi, function);
2142 mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
2144 ParameterCount expected(function->shared()->formal_parameter_count());
2145 // We call indirectly through the code field in the function to
2146 // allow recompilation to take effect without changing any of the
2148 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2149 expected, actual, flag, call_wrapper, call_kind);
2153 void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
2155 const CallWrapper& call_wrapper) {
2156 // You can't call a builtin without a valid frame.
2157 ASSERT(flag == JUMP_FUNCTION || has_frame());
2159 // Rely on the assertion to check that the number of provided
2160 // arguments match the expected number of arguments. Fake a
2161 // parameter count to avoid emitting code to do the check.
2162 ParameterCount expected(0);
2163 GetBuiltinFunction(edi, id);
2164 InvokeCode(FieldOperand(edi, JSFunction::kCodeEntryOffset),
2165 expected, expected, flag, call_wrapper, CALL_AS_METHOD);
2169 void MacroAssembler::GetBuiltinFunction(Register target,
2170 Builtins::JavaScript id) {
2171 // Load the JavaScript builtin function from the builtins object.
2172 mov(target, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2173 mov(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
2174 mov(target, FieldOperand(target,
2175 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
2179 void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
2180 ASSERT(!target.is(edi));
2181 // Load the JavaScript builtin function from the builtins object.
2182 GetBuiltinFunction(edi, id);
2183 // Load the code entry point from the function into the target register.
2184 mov(target, FieldOperand(edi, JSFunction::kCodeEntryOffset));
2188 void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2189 if (context_chain_length > 0) {
2190 // Move up the chain of contexts to the context containing the slot.
2191 mov(dst, Operand(esi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2192 for (int i = 1; i < context_chain_length; i++) {
2193 mov(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2196 // Slot is in the current function context. Move it into the
2197 // destination register in case we store into it (the write barrier
2198 // cannot be allowed to destroy the context in esi).
2202 // We should not have found a with context by walking the context chain
2203 // (i.e., the static scope chain and runtime context chain do not agree).
2204 // A variable occurring in such a scope should have slot type LOOKUP and
2206 if (emit_debug_code()) {
2207 cmp(FieldOperand(dst, HeapObject::kMapOffset),
2208 isolate()->factory()->with_context_map());
2209 Check(not_equal, "Variable resolved to with context.");
2214 void MacroAssembler::LoadTransitionedArrayMapConditional(
2215 ElementsKind expected_kind,
2216 ElementsKind transitioned_kind,
2217 Register map_in_out,
2219 Label* no_map_match) {
2220 // Load the global or builtins object from the current context.
2221 mov(scratch, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2222 mov(scratch, FieldOperand(scratch, GlobalObject::kGlobalContextOffset));
2224 // Check that the function's map is the same as the expected cached map.
2225 mov(scratch, Operand(scratch,
2226 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
2228 size_t offset = expected_kind * kPointerSize +
2229 FixedArrayBase::kHeaderSize;
2230 cmp(map_in_out, FieldOperand(scratch, offset));
2231 j(not_equal, no_map_match);
2233 // Use the transitioned cached map.
2234 offset = transitioned_kind * kPointerSize +
2235 FixedArrayBase::kHeaderSize;
2236 mov(map_in_out, FieldOperand(scratch, offset));
2240 void MacroAssembler::LoadInitialArrayMap(
2241 Register function_in, Register scratch,
2242 Register map_out, bool can_have_holes) {
2243 ASSERT(!function_in.is(map_out));
2245 mov(map_out, FieldOperand(function_in,
2246 JSFunction::kPrototypeOrInitialMapOffset));
2247 if (!FLAG_smi_only_arrays) {
2248 ElementsKind kind = can_have_holes ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS;
2249 LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
2254 } else if (can_have_holes) {
2255 LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
2256 FAST_HOLEY_SMI_ELEMENTS,
2265 void MacroAssembler::LoadGlobalFunction(int index, Register function) {
2266 // Load the global or builtins object from the current context.
2267 mov(function, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2268 // Load the global context from the global or builtins object.
2269 mov(function, FieldOperand(function, GlobalObject::kGlobalContextOffset));
2270 // Load the function from the global context.
2271 mov(function, Operand(function, Context::SlotOffset(index)));
2275 void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
2277 // Load the initial map. The global functions all have initial maps.
2278 mov(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2279 if (emit_debug_code()) {
2281 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
2284 Abort("Global functions must have initial map");
2290 // Store the value in register src in the safepoint register stack
2291 // slot for register dst.
2292 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
2293 mov(SafepointRegisterSlot(dst), src);
2297 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Immediate src) {
2298 mov(SafepointRegisterSlot(dst), src);
2302 void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
2303 mov(dst, SafepointRegisterSlot(src));
2307 Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
2308 return Operand(esp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
2312 int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
2313 // The registers are pushed starting with the lowest encoding,
2314 // which means that lowest encodings are furthest away from
2315 // the stack pointer.
2316 ASSERT(reg_code >= 0 && reg_code < kNumSafepointRegisters);
2317 return kNumSafepointRegisters - reg_code - 1;
2321 void MacroAssembler::LoadHeapObject(Register result,
2322 Handle<HeapObject> object) {
2323 if (isolate()->heap()->InNewSpace(*object)) {
2324 Handle<JSGlobalPropertyCell> cell =
2325 isolate()->factory()->NewJSGlobalPropertyCell(object);
2326 mov(result, Operand::Cell(cell));
2328 mov(result, object);
2333 void MacroAssembler::PushHeapObject(Handle<HeapObject> object) {
2334 if (isolate()->heap()->InNewSpace(*object)) {
2335 Handle<JSGlobalPropertyCell> cell =
2336 isolate()->factory()->NewJSGlobalPropertyCell(object);
2337 push(Operand::Cell(cell));
2344 void MacroAssembler::Ret() {
2349 void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
2350 if (is_uint16(bytes_dropped)) {
2354 add(esp, Immediate(bytes_dropped));
2361 void MacroAssembler::Drop(int stack_elements) {
2362 if (stack_elements > 0) {
2363 add(esp, Immediate(stack_elements * kPointerSize));
2368 void MacroAssembler::Move(Register dst, Register src) {
2375 void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
2376 if (FLAG_native_code_counters && counter->Enabled()) {
2377 mov(Operand::StaticVariable(ExternalReference(counter)), Immediate(value));
2382 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
2384 if (FLAG_native_code_counters && counter->Enabled()) {
2385 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2389 add(operand, Immediate(value));
2395 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
2397 if (FLAG_native_code_counters && counter->Enabled()) {
2398 Operand operand = Operand::StaticVariable(ExternalReference(counter));
2402 sub(operand, Immediate(value));
2408 void MacroAssembler::IncrementCounter(Condition cc,
2409 StatsCounter* counter,
2412 if (FLAG_native_code_counters && counter->Enabled()) {
2414 j(NegateCondition(cc), &skip);
2416 IncrementCounter(counter, value);
2423 void MacroAssembler::DecrementCounter(Condition cc,
2424 StatsCounter* counter,
2427 if (FLAG_native_code_counters && counter->Enabled()) {
2429 j(NegateCondition(cc), &skip);
2431 DecrementCounter(counter, value);
2438 void MacroAssembler::Assert(Condition cc, const char* msg) {
2439 if (emit_debug_code()) Check(cc, msg);
2443 void MacroAssembler::AssertFastElements(Register elements) {
2444 if (emit_debug_code()) {
2445 Factory* factory = isolate()->factory();
2447 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2448 Immediate(factory->fixed_array_map()));
2450 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2451 Immediate(factory->fixed_double_array_map()));
2453 cmp(FieldOperand(elements, HeapObject::kMapOffset),
2454 Immediate(factory->fixed_cow_array_map()));
2456 Abort("JSObject with fast elements map has slow elements");
2462 void MacroAssembler::Check(Condition cc, const char* msg) {
2466 // will not return here
2471 void MacroAssembler::CheckStackAlignment() {
2472 int frame_alignment = OS::ActivationFrameAlignment();
2473 int frame_alignment_mask = frame_alignment - 1;
2474 if (frame_alignment > kPointerSize) {
2475 ASSERT(IsPowerOf2(frame_alignment));
2476 Label alignment_as_expected;
2477 test(esp, Immediate(frame_alignment_mask));
2478 j(zero, &alignment_as_expected);
2479 // Abort if stack is not aligned.
2481 bind(&alignment_as_expected);
2486 void MacroAssembler::Abort(const char* msg) {
2487 // We want to pass the msg string like a smi to avoid GC
2488 // problems, however msg is not guaranteed to be aligned
2489 // properly. Instead, we pass an aligned pointer that is
2490 // a proper v8 smi, but also pass the alignment difference
2491 // from the real pointer as a smi.
2492 intptr_t p1 = reinterpret_cast<intptr_t>(msg);
2493 intptr_t p0 = (p1 & ~kSmiTagMask) + kSmiTag;
2494 ASSERT(reinterpret_cast<Object*>(p0)->IsSmi());
2497 RecordComment("Abort message: ");
2503 push(Immediate(p0));
2504 push(Immediate(reinterpret_cast<intptr_t>(Smi::FromInt(p1 - p0))));
2505 // Disable stub call restrictions to always allow calls to abort.
2507 // We don't actually want to generate a pile of code for this, so just
2508 // claim there is a stack frame, without generating one.
2509 FrameScope scope(this, StackFrame::NONE);
2510 CallRuntime(Runtime::kAbort, 2);
2512 CallRuntime(Runtime::kAbort, 2);
2514 // will not return here
2519 void MacroAssembler::LoadInstanceDescriptors(Register map,
2520 Register descriptors) {
2522 FieldOperand(map, Map::kInstanceDescriptorsOrBitField3Offset));
2524 JumpIfNotSmi(descriptors, ¬_smi);
2525 mov(descriptors, isolate()->factory()->empty_descriptor_array());
2530 void MacroAssembler::LoadPowerOf2(XMMRegister dst,
2533 ASSERT(is_uintn(power + HeapNumber::kExponentBias,
2534 HeapNumber::kExponentBits));
2535 mov(scratch, Immediate(power + HeapNumber::kExponentBias));
2537 psllq(dst, HeapNumber::kMantissaBits);
2541 void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
2542 Register instance_type,
2545 if (!scratch.is(instance_type)) {
2546 mov(scratch, instance_type);
2549 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2550 cmp(scratch, kStringTag | kSeqStringTag | kAsciiStringTag);
2551 j(not_equal, failure);
2555 void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(Register object1,
2560 // Check that both objects are not smis.
2561 STATIC_ASSERT(kSmiTag == 0);
2562 mov(scratch1, object1);
2563 and_(scratch1, object2);
2564 JumpIfSmi(scratch1, failure);
2566 // Load instance type for both strings.
2567 mov(scratch1, FieldOperand(object1, HeapObject::kMapOffset));
2568 mov(scratch2, FieldOperand(object2, HeapObject::kMapOffset));
2569 movzx_b(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2570 movzx_b(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2572 // Check that both are flat ASCII strings.
2573 const int kFlatAsciiStringMask =
2574 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2575 const int kFlatAsciiStringTag = ASCII_STRING_TYPE;
2576 // Interleave bits from both instance types and compare them in one check.
2577 ASSERT_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2578 and_(scratch1, kFlatAsciiStringMask);
2579 and_(scratch2, kFlatAsciiStringMask);
2580 lea(scratch1, Operand(scratch1, scratch2, times_8, 0));
2581 cmp(scratch1, kFlatAsciiStringTag | (kFlatAsciiStringTag << 3));
2582 j(not_equal, failure);
2586 void MacroAssembler::PrepareCallCFunction(int num_arguments, Register scratch) {
2587 int frame_alignment = OS::ActivationFrameAlignment();
2588 if (frame_alignment != 0) {
2589 // Make stack end at alignment and make room for num_arguments words
2590 // and the original value of esp.
2592 sub(esp, Immediate((num_arguments + 1) * kPointerSize));
2593 ASSERT(IsPowerOf2(frame_alignment));
2594 and_(esp, -frame_alignment);
2595 mov(Operand(esp, num_arguments * kPointerSize), scratch);
2597 sub(esp, Immediate(num_arguments * kPointerSize));
2602 void MacroAssembler::CallCFunction(ExternalReference function,
2603 int num_arguments) {
2604 // Trashing eax is ok as it will be the return value.
2605 mov(eax, Immediate(function));
2606 CallCFunction(eax, num_arguments);
2610 void MacroAssembler::CallCFunction(Register function,
2611 int num_arguments) {
2612 ASSERT(has_frame());
2613 // Check stack alignment.
2614 if (emit_debug_code()) {
2615 CheckStackAlignment();
2619 if (OS::ActivationFrameAlignment() != 0) {
2620 mov(esp, Operand(esp, num_arguments * kPointerSize));
2622 add(esp, Immediate(num_arguments * kPointerSize));
2627 bool AreAliased(Register r1, Register r2, Register r3, Register r4) {
2628 if (r1.is(r2)) return true;
2629 if (r1.is(r3)) return true;
2630 if (r1.is(r4)) return true;
2631 if (r2.is(r3)) return true;
2632 if (r2.is(r4)) return true;
2633 if (r3.is(r4)) return true;
2638 CodePatcher::CodePatcher(byte* address, int size)
2639 : address_(address),
2641 masm_(NULL, address, size + Assembler::kGap) {
2642 // Create a new macro assembler pointing to the address of the code to patch.
2643 // The size is adjusted with kGap on order for the assembler to generate size
2644 // bytes of instructions without failing with buffer size constraints.
2645 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2649 CodePatcher::~CodePatcher() {
2650 // Indicate that code has changed.
2651 CPU::FlushICache(address_, size_);
2653 // Check that the code was patched as expected.
2654 ASSERT(masm_.pc_ == address_ + size_);
2655 ASSERT(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
2659 void MacroAssembler::CheckPageFlag(
2664 Label* condition_met,
2665 Label::Distance condition_met_distance) {
2666 ASSERT(cc == zero || cc == not_zero);
2667 if (scratch.is(object)) {
2668 and_(scratch, Immediate(~Page::kPageAlignmentMask));
2670 mov(scratch, Immediate(~Page::kPageAlignmentMask));
2671 and_(scratch, object);
2673 if (mask < (1 << kBitsPerByte)) {
2674 test_b(Operand(scratch, MemoryChunk::kFlagsOffset),
2675 static_cast<uint8_t>(mask));
2677 test(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
2679 j(cc, condition_met, condition_met_distance);
2683 void MacroAssembler::CheckPageFlagForMap(
2687 Label* condition_met,
2688 Label::Distance condition_met_distance) {
2689 ASSERT(cc == zero || cc == not_zero);
2690 Page* page = Page::FromAddress(map->address());
2691 ExternalReference reference(ExternalReference::page_flags(page));
2692 // The inlined static address check of the page's flags relies
2693 // on maps never being compacted.
2694 ASSERT(!isolate()->heap()->mark_compact_collector()->
2695 IsOnEvacuationCandidate(*map));
2696 if (mask < (1 << kBitsPerByte)) {
2697 test_b(Operand::StaticVariable(reference), static_cast<uint8_t>(mask));
2699 test(Operand::StaticVariable(reference), Immediate(mask));
2701 j(cc, condition_met, condition_met_distance);
2705 void MacroAssembler::JumpIfBlack(Register object,
2709 Label::Distance on_black_near) {
2710 HasColor(object, scratch0, scratch1,
2711 on_black, on_black_near,
2712 1, 0); // kBlackBitPattern.
2713 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
2717 void MacroAssembler::HasColor(Register object,
2718 Register bitmap_scratch,
2719 Register mask_scratch,
2721 Label::Distance has_color_distance,
2724 ASSERT(!AreAliased(object, bitmap_scratch, mask_scratch, ecx));
2726 GetMarkBits(object, bitmap_scratch, mask_scratch);
2728 Label other_color, word_boundary;
2729 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2730 j(first_bit == 1 ? zero : not_zero, &other_color, Label::kNear);
2731 add(mask_scratch, mask_scratch); // Shift left 1 by adding.
2732 j(zero, &word_boundary, Label::kNear);
2733 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2734 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
2735 jmp(&other_color, Label::kNear);
2737 bind(&word_boundary);
2738 test_b(Operand(bitmap_scratch, MemoryChunk::kHeaderSize + kPointerSize), 1);
2740 j(second_bit == 1 ? not_zero : zero, has_color, has_color_distance);
2745 void MacroAssembler::GetMarkBits(Register addr_reg,
2746 Register bitmap_reg,
2747 Register mask_reg) {
2748 ASSERT(!AreAliased(addr_reg, mask_reg, bitmap_reg, ecx));
2749 mov(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
2750 and_(bitmap_reg, addr_reg);
2753 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
2756 (Page::kPageAlignmentMask >> shift) & ~(Bitmap::kBytesPerCell - 1));
2758 add(bitmap_reg, ecx);
2760 shr(ecx, kPointerSizeLog2);
2761 and_(ecx, (1 << Bitmap::kBitsPerCellLog2) - 1);
2762 mov(mask_reg, Immediate(1));
2767 void MacroAssembler::EnsureNotWhite(
2769 Register bitmap_scratch,
2770 Register mask_scratch,
2771 Label* value_is_white_and_not_data,
2772 Label::Distance distance) {
2773 ASSERT(!AreAliased(value, bitmap_scratch, mask_scratch, ecx));
2774 GetMarkBits(value, bitmap_scratch, mask_scratch);
2776 // If the value is black or grey we don't need to do anything.
2777 ASSERT(strcmp(Marking::kWhiteBitPattern, "00") == 0);
2778 ASSERT(strcmp(Marking::kBlackBitPattern, "10") == 0);
2779 ASSERT(strcmp(Marking::kGreyBitPattern, "11") == 0);
2780 ASSERT(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
2784 // Since both black and grey have a 1 in the first position and white does
2785 // not have a 1 there we only need to check one bit.
2786 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2787 j(not_zero, &done, Label::kNear);
2789 if (FLAG_debug_code) {
2790 // Check for impossible bit pattern.
2793 // shl. May overflow making the check conservative.
2794 add(mask_scratch, mask_scratch);
2795 test(mask_scratch, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
2796 j(zero, &ok, Label::kNear);
2802 // Value is white. We check whether it is data that doesn't need scanning.
2803 // Currently only checks for HeapNumber and non-cons strings.
2804 Register map = ecx; // Holds map while checking type.
2805 Register length = ecx; // Holds length of object after checking type.
2806 Label not_heap_number;
2807 Label is_data_object;
2809 // Check for heap-number
2810 mov(map, FieldOperand(value, HeapObject::kMapOffset));
2811 cmp(map, FACTORY->heap_number_map());
2812 j(not_equal, ¬_heap_number, Label::kNear);
2813 mov(length, Immediate(HeapNumber::kSize));
2814 jmp(&is_data_object, Label::kNear);
2816 bind(¬_heap_number);
2817 // Check for strings.
2818 ASSERT(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
2819 ASSERT(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
2820 // If it's a string and it's not a cons string then it's an object containing
2822 Register instance_type = ecx;
2823 movzx_b(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
2824 test_b(instance_type, kIsIndirectStringMask | kIsNotStringMask);
2825 j(not_zero, value_is_white_and_not_data);
2826 // It's a non-indirect (non-cons and non-slice) string.
2827 // If it's external, the length is just ExternalString::kSize.
2828 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
2830 // External strings are the only ones with the kExternalStringTag bit
2832 ASSERT_EQ(0, kSeqStringTag & kExternalStringTag);
2833 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
2834 test_b(instance_type, kExternalStringTag);
2835 j(zero, ¬_external, Label::kNear);
2836 mov(length, Immediate(ExternalString::kSize));
2837 jmp(&is_data_object, Label::kNear);
2839 bind(¬_external);
2840 // Sequential string, either ASCII or UC16.
2841 ASSERT(kAsciiStringTag == 0x04);
2842 and_(length, Immediate(kStringEncodingMask));
2843 xor_(length, Immediate(kStringEncodingMask));
2844 add(length, Immediate(0x04));
2845 // Value now either 4 (if ASCII) or 8 (if UC16), i.e., char-size shifted
2846 // by 2. If we multiply the string length as smi by this, it still
2847 // won't overflow a 32-bit value.
2848 ASSERT_EQ(SeqAsciiString::kMaxSize, SeqTwoByteString::kMaxSize);
2849 ASSERT(SeqAsciiString::kMaxSize <=
2850 static_cast<int>(0xffffffffu >> (2 + kSmiTagSize)));
2851 imul(length, FieldOperand(value, String::kLengthOffset));
2852 shr(length, 2 + kSmiTagSize + kSmiShiftSize);
2853 add(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
2854 and_(length, Immediate(~kObjectAlignmentMask));
2856 bind(&is_data_object);
2857 // Value is a data object, and it is white. Mark it black. Since we know
2858 // that the object is white we can make it black by flipping one bit.
2859 or_(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
2861 and_(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
2862 add(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset),
2864 if (FLAG_debug_code) {
2865 mov(length, Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
2866 cmp(length, Operand(bitmap_scratch, MemoryChunk::kSizeOffset));
2867 Check(less_equal, "Live Bytes Count overflow chunk size");
2874 void MacroAssembler::CheckEnumCache(Label* call_runtime) {
2879 // Check that there are no elements. Register ecx contains the
2880 // current JS object we've reached through the prototype chain.
2881 cmp(FieldOperand(ecx, JSObject::kElementsOffset),
2882 isolate()->factory()->empty_fixed_array());
2883 j(not_equal, call_runtime);
2885 // Check that instance descriptors are not empty so that we can
2886 // check for an enum cache. Leave the map in ebx for the subsequent
2888 mov(ebx, FieldOperand(ecx, HeapObject::kMapOffset));
2889 mov(edx, FieldOperand(ebx, Map::kInstanceDescriptorsOrBitField3Offset));
2890 JumpIfSmi(edx, call_runtime);
2892 // Check that there is an enum cache in the non-empty instance
2893 // descriptors (edx). This is the case if the next enumeration
2894 // index field does not contain a smi.
2895 mov(edx, FieldOperand(edx, DescriptorArray::kEnumerationIndexOffset));
2896 JumpIfSmi(edx, call_runtime);
2898 // For all objects but the receiver, check that the cache is empty.
2899 Label check_prototype;
2901 j(equal, &check_prototype, Label::kNear);
2902 mov(edx, FieldOperand(edx, DescriptorArray::kEnumCacheBridgeCacheOffset));
2903 cmp(edx, isolate()->factory()->empty_fixed_array());
2904 j(not_equal, call_runtime);
2906 // Load the prototype from the map and loop if non-null.
2907 bind(&check_prototype);
2908 mov(ecx, FieldOperand(ebx, Map::kPrototypeOffset));
2909 cmp(ecx, isolate()->factory()->null_value());
2910 j(not_equal, &next);
2913 } } // namespace v8::internal
2915 #endif // V8_TARGET_ARCH_IA32