1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "src/bootstrapper.h"
10 #include "src/codegen.h"
11 #include "src/cpu-profiler.h"
12 #include "src/debug.h"
13 #include "src/heap/heap.h"
14 #include "src/isolate-inl.h"
15 #include "src/serialize.h"
16 #include "src/x64/assembler-x64.h"
17 #include "src/x64/macro-assembler-x64.h"
22 MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
23 : Assembler(arg_isolate, buffer, size),
24 generating_stub_(false),
26 root_array_available_(true) {
27 if (isolate() != NULL) {
28 code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
34 static const int64_t kInvalidRootRegisterDelta = -1;
37 int64_t MacroAssembler::RootRegisterDelta(ExternalReference other) {
38 if (predictable_code_size() &&
39 (other.address() < reinterpret_cast<Address>(isolate()) ||
40 other.address() >= reinterpret_cast<Address>(isolate() + 1))) {
41 return kInvalidRootRegisterDelta;
43 Address roots_register_value = kRootRegisterBias +
44 reinterpret_cast<Address>(isolate()->heap()->roots_array_start());
46 int64_t delta = kInvalidRootRegisterDelta; // Bogus initialization.
47 if (kPointerSize == kInt64Size) {
48 delta = other.address() - roots_register_value;
50 // For x32, zero extend the address to 64-bit and calculate the delta.
51 uint64_t o = static_cast<uint32_t>(
52 reinterpret_cast<intptr_t>(other.address()));
53 uint64_t r = static_cast<uint32_t>(
54 reinterpret_cast<intptr_t>(roots_register_value));
61 Operand MacroAssembler::ExternalOperand(ExternalReference target,
63 if (root_array_available_ && !serializer_enabled()) {
64 int64_t delta = RootRegisterDelta(target);
65 if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
66 return Operand(kRootRegister, static_cast<int32_t>(delta));
69 Move(scratch, target);
70 return Operand(scratch, 0);
74 void MacroAssembler::Load(Register destination, ExternalReference source) {
75 if (root_array_available_ && !serializer_enabled()) {
76 int64_t delta = RootRegisterDelta(source);
77 if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
78 movp(destination, Operand(kRootRegister, static_cast<int32_t>(delta)));
83 if (destination.is(rax)) {
86 Move(kScratchRegister, source);
87 movp(destination, Operand(kScratchRegister, 0));
92 void MacroAssembler::Store(ExternalReference destination, Register source) {
93 if (root_array_available_ && !serializer_enabled()) {
94 int64_t delta = RootRegisterDelta(destination);
95 if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
96 movp(Operand(kRootRegister, static_cast<int32_t>(delta)), source);
101 if (source.is(rax)) {
102 store_rax(destination);
104 Move(kScratchRegister, destination);
105 movp(Operand(kScratchRegister, 0), source);
110 void MacroAssembler::LoadAddress(Register destination,
111 ExternalReference source) {
112 if (root_array_available_ && !serializer_enabled()) {
113 int64_t delta = RootRegisterDelta(source);
114 if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
115 leap(destination, Operand(kRootRegister, static_cast<int32_t>(delta)));
120 Move(destination, source);
124 int MacroAssembler::LoadAddressSize(ExternalReference source) {
125 if (root_array_available_ && !serializer_enabled()) {
126 // This calculation depends on the internals of LoadAddress.
127 // It's correctness is ensured by the asserts in the Call
128 // instruction below.
129 int64_t delta = RootRegisterDelta(source);
130 if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
131 // Operand is leap(scratch, Operand(kRootRegister, delta));
132 // Opcodes : REX.W 8D ModRM Disp8/Disp32 - 4 or 7.
134 if (!is_int8(static_cast<int32_t>(delta))) {
135 size += 3; // Need full four-byte displacement in lea.
140 // Size of movp(destination, src);
141 return Assembler::kMoveAddressIntoScratchRegisterInstructionLength;
145 void MacroAssembler::PushAddress(ExternalReference source) {
146 int64_t address = reinterpret_cast<int64_t>(source.address());
147 if (is_int32(address) && !serializer_enabled()) {
148 if (emit_debug_code()) {
149 Move(kScratchRegister, kZapValue, Assembler::RelocInfoNone());
151 Push(Immediate(static_cast<int32_t>(address)));
154 LoadAddress(kScratchRegister, source);
155 Push(kScratchRegister);
159 void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
160 DCHECK(root_array_available_);
161 movp(destination, Operand(kRootRegister,
162 (index << kPointerSizeLog2) - kRootRegisterBias));
166 void MacroAssembler::LoadRootIndexed(Register destination,
167 Register variable_offset,
169 DCHECK(root_array_available_);
171 Operand(kRootRegister,
172 variable_offset, times_pointer_size,
173 (fixed_offset << kPointerSizeLog2) - kRootRegisterBias));
177 void MacroAssembler::StoreRoot(Register source, Heap::RootListIndex index) {
178 DCHECK(root_array_available_);
179 movp(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias),
184 void MacroAssembler::PushRoot(Heap::RootListIndex index) {
185 DCHECK(root_array_available_);
186 Push(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias));
190 void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
191 DCHECK(root_array_available_);
192 cmpp(with, Operand(kRootRegister,
193 (index << kPointerSizeLog2) - kRootRegisterBias));
197 void MacroAssembler::CompareRoot(const Operand& with,
198 Heap::RootListIndex index) {
199 DCHECK(root_array_available_);
200 DCHECK(!with.AddressUsesRegister(kScratchRegister));
201 LoadRoot(kScratchRegister, index);
202 cmpp(with, kScratchRegister);
206 void MacroAssembler::RememberedSetHelper(Register object, // For debug tests.
209 SaveFPRegsMode save_fp,
210 RememberedSetFinalAction and_then) {
211 if (emit_debug_code()) {
213 JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
217 // Load store buffer top.
218 LoadRoot(scratch, Heap::kStoreBufferTopRootIndex);
219 // Store pointer to buffer.
220 movp(Operand(scratch, 0), addr);
221 // Increment buffer top.
222 addp(scratch, Immediate(kPointerSize));
223 // Write back new top of buffer.
224 StoreRoot(scratch, Heap::kStoreBufferTopRootIndex);
225 // Call stub on end of buffer.
227 // Check for end of buffer.
228 testp(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
229 if (and_then == kReturnAtEnd) {
230 Label buffer_overflowed;
231 j(not_equal, &buffer_overflowed, Label::kNear);
233 bind(&buffer_overflowed);
235 DCHECK(and_then == kFallThroughAtEnd);
236 j(equal, &done, Label::kNear);
238 StoreBufferOverflowStub store_buffer_overflow =
239 StoreBufferOverflowStub(isolate(), save_fp);
240 CallStub(&store_buffer_overflow);
241 if (and_then == kReturnAtEnd) {
244 DCHECK(and_then == kFallThroughAtEnd);
250 void MacroAssembler::InNewSpace(Register object,
254 Label::Distance distance) {
255 if (serializer_enabled()) {
256 // Can't do arithmetic on external references if it might get serialized.
257 // The mask isn't really an address. We load it as an external reference in
258 // case the size of the new space is different between the snapshot maker
259 // and the running system.
260 if (scratch.is(object)) {
261 Move(kScratchRegister, ExternalReference::new_space_mask(isolate()));
262 andp(scratch, kScratchRegister);
264 Move(scratch, ExternalReference::new_space_mask(isolate()));
265 andp(scratch, object);
267 Move(kScratchRegister, ExternalReference::new_space_start(isolate()));
268 cmpp(scratch, kScratchRegister);
269 j(cc, branch, distance);
271 DCHECK(kPointerSize == kInt64Size
272 ? is_int32(static_cast<int64_t>(isolate()->heap()->NewSpaceMask()))
273 : kPointerSize == kInt32Size);
274 intptr_t new_space_start =
275 reinterpret_cast<intptr_t>(isolate()->heap()->NewSpaceStart());
276 Move(kScratchRegister, reinterpret_cast<Address>(-new_space_start),
277 Assembler::RelocInfoNone());
278 if (scratch.is(object)) {
279 addp(scratch, kScratchRegister);
281 leap(scratch, Operand(object, kScratchRegister, times_1, 0));
284 Immediate(static_cast<int32_t>(isolate()->heap()->NewSpaceMask())));
285 j(cc, branch, distance);
290 void MacroAssembler::RecordWriteField(
295 SaveFPRegsMode save_fp,
296 RememberedSetAction remembered_set_action,
298 PointersToHereCheck pointers_to_here_check_for_value) {
299 // First, check if a write barrier is even needed. The tests below
300 // catch stores of Smis.
303 // Skip barrier if writing a smi.
304 if (smi_check == INLINE_SMI_CHECK) {
305 JumpIfSmi(value, &done);
308 // Although the object register is tagged, the offset is relative to the start
309 // of the object, so so offset must be a multiple of kPointerSize.
310 DCHECK(IsAligned(offset, kPointerSize));
312 leap(dst, FieldOperand(object, offset));
313 if (emit_debug_code()) {
315 testb(dst, Immediate((1 << kPointerSizeLog2) - 1));
316 j(zero, &ok, Label::kNear);
321 RecordWrite(object, dst, value, save_fp, remembered_set_action,
322 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
326 // Clobber clobbered input registers when running with the debug-code flag
327 // turned on to provoke errors.
328 if (emit_debug_code()) {
329 Move(value, kZapValue, Assembler::RelocInfoNone());
330 Move(dst, kZapValue, Assembler::RelocInfoNone());
335 void MacroAssembler::RecordWriteArray(
339 SaveFPRegsMode save_fp,
340 RememberedSetAction remembered_set_action,
342 PointersToHereCheck pointers_to_here_check_for_value) {
343 // First, check if a write barrier is even needed. The tests below
344 // catch stores of Smis.
347 // Skip barrier if writing a smi.
348 if (smi_check == INLINE_SMI_CHECK) {
349 JumpIfSmi(value, &done);
352 // Array access: calculate the destination address. Index is not a smi.
353 Register dst = index;
354 leap(dst, Operand(object, index, times_pointer_size,
355 FixedArray::kHeaderSize - kHeapObjectTag));
357 RecordWrite(object, dst, value, save_fp, remembered_set_action,
358 OMIT_SMI_CHECK, pointers_to_here_check_for_value);
362 // Clobber clobbered input registers when running with the debug-code flag
363 // turned on to provoke errors.
364 if (emit_debug_code()) {
365 Move(value, kZapValue, Assembler::RelocInfoNone());
366 Move(index, kZapValue, Assembler::RelocInfoNone());
371 void MacroAssembler::RecordWriteForMap(Register object,
374 SaveFPRegsMode fp_mode) {
375 DCHECK(!object.is(kScratchRegister));
376 DCHECK(!object.is(map));
377 DCHECK(!object.is(dst));
378 DCHECK(!map.is(dst));
379 AssertNotSmi(object);
381 if (emit_debug_code()) {
383 if (map.is(kScratchRegister)) pushq(map);
384 CompareMap(map, isolate()->factory()->meta_map());
385 if (map.is(kScratchRegister)) popq(map);
386 j(equal, &ok, Label::kNear);
391 if (!FLAG_incremental_marking) {
395 if (emit_debug_code()) {
397 if (map.is(kScratchRegister)) pushq(map);
398 cmpp(map, FieldOperand(object, HeapObject::kMapOffset));
399 if (map.is(kScratchRegister)) popq(map);
400 j(equal, &ok, Label::kNear);
405 // Compute the address.
406 leap(dst, FieldOperand(object, HeapObject::kMapOffset));
408 // First, check if a write barrier is even needed. The tests below
409 // catch stores of smis and stores into the young generation.
412 // A single check of the map's pages interesting flag suffices, since it is
413 // only set during incremental collection, and then it's also guaranteed that
414 // the from object's page's interesting flag is also set. This optimization
415 // relies on the fact that maps can never be in new space.
417 map, // Used as scratch.
418 MemoryChunk::kPointersToHereAreInterestingMask,
423 RecordWriteStub stub(isolate(), object, map, dst, OMIT_REMEMBERED_SET,
429 // Count number of write barriers in generated code.
430 isolate()->counters()->write_barriers_static()->Increment();
431 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
433 // Clobber clobbered registers when running with the debug-code flag
434 // turned on to provoke errors.
435 if (emit_debug_code()) {
436 Move(dst, kZapValue, Assembler::RelocInfoNone());
437 Move(map, kZapValue, Assembler::RelocInfoNone());
442 void MacroAssembler::RecordWrite(
446 SaveFPRegsMode fp_mode,
447 RememberedSetAction remembered_set_action,
449 PointersToHereCheck pointers_to_here_check_for_value) {
450 DCHECK(!object.is(value));
451 DCHECK(!object.is(address));
452 DCHECK(!value.is(address));
453 AssertNotSmi(object);
455 if (remembered_set_action == OMIT_REMEMBERED_SET &&
456 !FLAG_incremental_marking) {
460 if (emit_debug_code()) {
462 cmpp(value, Operand(address, 0));
463 j(equal, &ok, Label::kNear);
468 // First, check if a write barrier is even needed. The tests below
469 // catch stores of smis and stores into the young generation.
472 if (smi_check == INLINE_SMI_CHECK) {
473 // Skip barrier if writing a smi.
474 JumpIfSmi(value, &done);
477 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
479 value, // Used as scratch.
480 MemoryChunk::kPointersToHereAreInterestingMask,
486 CheckPageFlag(object,
487 value, // Used as scratch.
488 MemoryChunk::kPointersFromHereAreInterestingMask,
493 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
499 // Count number of write barriers in generated code.
500 isolate()->counters()->write_barriers_static()->Increment();
501 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
503 // Clobber clobbered registers when running with the debug-code flag
504 // turned on to provoke errors.
505 if (emit_debug_code()) {
506 Move(address, kZapValue, Assembler::RelocInfoNone());
507 Move(value, kZapValue, Assembler::RelocInfoNone());
512 void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
513 if (emit_debug_code()) Check(cc, reason);
517 void MacroAssembler::AssertFastElements(Register elements) {
518 if (emit_debug_code()) {
520 CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
521 Heap::kFixedArrayMapRootIndex);
522 j(equal, &ok, Label::kNear);
523 CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
524 Heap::kFixedDoubleArrayMapRootIndex);
525 j(equal, &ok, Label::kNear);
526 CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
527 Heap::kFixedCOWArrayMapRootIndex);
528 j(equal, &ok, Label::kNear);
529 Abort(kJSObjectWithFastElementsMapHasSlowElements);
535 void MacroAssembler::Check(Condition cc, BailoutReason reason) {
537 j(cc, &L, Label::kNear);
539 // Control will not return here.
544 void MacroAssembler::CheckStackAlignment() {
545 int frame_alignment = base::OS::ActivationFrameAlignment();
546 int frame_alignment_mask = frame_alignment - 1;
547 if (frame_alignment > kPointerSize) {
548 DCHECK(IsPowerOf2(frame_alignment));
549 Label alignment_as_expected;
550 testp(rsp, Immediate(frame_alignment_mask));
551 j(zero, &alignment_as_expected, Label::kNear);
552 // Abort if stack is not aligned.
554 bind(&alignment_as_expected);
559 void MacroAssembler::NegativeZeroTest(Register result,
563 testl(result, result);
564 j(not_zero, &ok, Label::kNear);
571 void MacroAssembler::Abort(BailoutReason reason) {
573 const char* msg = GetBailoutReason(reason);
575 RecordComment("Abort message: ");
579 if (FLAG_trap_on_abort) {
585 Move(kScratchRegister, Smi::FromInt(static_cast<int>(reason)),
586 Assembler::RelocInfoNone());
587 Push(kScratchRegister);
590 // We don't actually want to generate a pile of code for this, so just
591 // claim there is a stack frame, without generating one.
592 FrameScope scope(this, StackFrame::NONE);
593 CallRuntime(Runtime::kAbort, 1);
595 CallRuntime(Runtime::kAbort, 1);
597 // Control will not return here.
602 void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
603 DCHECK(AllowThisStubCall(stub)); // Calls are not allowed in some stubs
604 Call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
608 void MacroAssembler::TailCallStub(CodeStub* stub) {
609 Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
613 void MacroAssembler::StubReturn(int argc) {
614 DCHECK(argc >= 1 && generating_stub());
615 ret((argc - 1) * kPointerSize);
619 bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
620 return has_frame_ || !stub->SometimesSetsUpAFrame();
624 void MacroAssembler::IndexFromHash(Register hash, Register index) {
625 // The assert checks that the constants for the maximum number of digits
626 // for an array index cached in the hash field and the number of bits
627 // reserved for it does not conflict.
628 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
629 (1 << String::kArrayIndexValueBits));
630 if (!hash.is(index)) {
633 DecodeFieldToSmi<String::ArrayIndexValueBits>(index);
637 void MacroAssembler::CallRuntime(const Runtime::Function* f,
639 SaveFPRegsMode save_doubles) {
640 // If the expected number of arguments of the runtime function is
641 // constant, we check that the actual number of arguments match the
643 CHECK(f->nargs < 0 || f->nargs == num_arguments);
645 // TODO(1236192): Most runtime routines don't need the number of
646 // arguments passed in because it is constant. At some point we
647 // should remove this need and make the runtime routine entry code
649 Set(rax, num_arguments);
650 LoadAddress(rbx, ExternalReference(f, isolate()));
651 CEntryStub ces(isolate(), f->result_size, save_doubles);
656 void MacroAssembler::CallExternalReference(const ExternalReference& ext,
658 Set(rax, num_arguments);
659 LoadAddress(rbx, ext);
661 CEntryStub stub(isolate(), 1);
666 void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
669 // ----------- S t a t e -------------
670 // -- rsp[0] : return address
671 // -- rsp[8] : argument num_arguments - 1
673 // -- rsp[8 * num_arguments] : argument 0 (receiver)
674 // -----------------------------------
676 // TODO(1236192): Most runtime routines don't need the number of
677 // arguments passed in because it is constant. At some point we
678 // should remove this need and make the runtime routine entry code
680 Set(rax, num_arguments);
681 JumpToExternalReference(ext, result_size);
685 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
688 TailCallExternalReference(ExternalReference(fid, isolate()),
694 static int Offset(ExternalReference ref0, ExternalReference ref1) {
695 int64_t offset = (ref0.address() - ref1.address());
696 // Check that fits into int.
697 DCHECK(static_cast<int>(offset) == offset);
698 return static_cast<int>(offset);
702 void MacroAssembler::PrepareCallApiFunction(int arg_stack_space) {
703 EnterApiExitFrame(arg_stack_space);
707 void MacroAssembler::CallApiFunctionAndReturn(
708 Register function_address,
709 ExternalReference thunk_ref,
710 Register thunk_last_arg,
712 Operand return_value_operand,
713 Operand* context_restore_operand) {
715 Label promote_scheduled_exception;
716 Label exception_handled;
717 Label delete_allocated_handles;
718 Label leave_exit_frame;
721 Factory* factory = isolate()->factory();
722 ExternalReference next_address =
723 ExternalReference::handle_scope_next_address(isolate());
724 const int kNextOffset = 0;
725 const int kLimitOffset = Offset(
726 ExternalReference::handle_scope_limit_address(isolate()),
728 const int kLevelOffset = Offset(
729 ExternalReference::handle_scope_level_address(isolate()),
731 ExternalReference scheduled_exception_address =
732 ExternalReference::scheduled_exception_address(isolate());
734 DCHECK(rdx.is(function_address) || r8.is(function_address));
735 // Allocate HandleScope in callee-save registers.
736 Register prev_next_address_reg = r14;
737 Register prev_limit_reg = rbx;
738 Register base_reg = r15;
739 Move(base_reg, next_address);
740 movp(prev_next_address_reg, Operand(base_reg, kNextOffset));
741 movp(prev_limit_reg, Operand(base_reg, kLimitOffset));
742 addl(Operand(base_reg, kLevelOffset), Immediate(1));
744 if (FLAG_log_timer_events) {
745 FrameScope frame(this, StackFrame::MANUAL);
746 PushSafepointRegisters();
747 PrepareCallCFunction(1);
748 LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
749 CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
750 PopSafepointRegisters();
754 Label profiler_disabled;
755 Label end_profiler_check;
756 Move(rax, ExternalReference::is_profiling_address(isolate()));
757 cmpb(Operand(rax, 0), Immediate(0));
758 j(zero, &profiler_disabled);
760 // Third parameter is the address of the actual getter function.
761 Move(thunk_last_arg, function_address);
762 Move(rax, thunk_ref);
763 jmp(&end_profiler_check);
765 bind(&profiler_disabled);
766 // Call the api function!
767 Move(rax, function_address);
769 bind(&end_profiler_check);
771 // Call the api function!
774 if (FLAG_log_timer_events) {
775 FrameScope frame(this, StackFrame::MANUAL);
776 PushSafepointRegisters();
777 PrepareCallCFunction(1);
778 LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
779 CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
780 PopSafepointRegisters();
783 // Load the value from ReturnValue
784 movp(rax, return_value_operand);
787 // No more valid handles (the result handle was the last one). Restore
788 // previous handle scope.
789 subl(Operand(base_reg, kLevelOffset), Immediate(1));
790 movp(Operand(base_reg, kNextOffset), prev_next_address_reg);
791 cmpp(prev_limit_reg, Operand(base_reg, kLimitOffset));
792 j(not_equal, &delete_allocated_handles);
793 bind(&leave_exit_frame);
795 // Check if the function scheduled an exception.
796 Move(rsi, scheduled_exception_address);
797 Cmp(Operand(rsi, 0), factory->the_hole_value());
798 j(not_equal, &promote_scheduled_exception);
799 bind(&exception_handled);
801 #if ENABLE_EXTRA_CHECKS
802 // Check if the function returned a valid JavaScript value.
804 Register return_value = rax;
807 JumpIfSmi(return_value, &ok, Label::kNear);
808 movp(map, FieldOperand(return_value, HeapObject::kMapOffset));
810 CmpInstanceType(map, FIRST_NONSTRING_TYPE);
811 j(below, &ok, Label::kNear);
813 CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
814 j(above_equal, &ok, Label::kNear);
816 CompareRoot(map, Heap::kHeapNumberMapRootIndex);
817 j(equal, &ok, Label::kNear);
819 CompareRoot(return_value, Heap::kUndefinedValueRootIndex);
820 j(equal, &ok, Label::kNear);
822 CompareRoot(return_value, Heap::kTrueValueRootIndex);
823 j(equal, &ok, Label::kNear);
825 CompareRoot(return_value, Heap::kFalseValueRootIndex);
826 j(equal, &ok, Label::kNear);
828 CompareRoot(return_value, Heap::kNullValueRootIndex);
829 j(equal, &ok, Label::kNear);
831 Abort(kAPICallReturnedInvalidObject);
836 bool restore_context = context_restore_operand != NULL;
837 if (restore_context) {
838 movp(rsi, *context_restore_operand);
840 LeaveApiExitFrame(!restore_context);
841 ret(stack_space * kPointerSize);
843 bind(&promote_scheduled_exception);
845 FrameScope frame(this, StackFrame::INTERNAL);
846 CallRuntime(Runtime::kPromoteScheduledException, 0);
848 jmp(&exception_handled);
850 // HandleScope limit has changed. Delete allocated extensions.
851 bind(&delete_allocated_handles);
852 movp(Operand(base_reg, kLimitOffset), prev_limit_reg);
853 movp(prev_limit_reg, rax);
854 LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
856 ExternalReference::delete_handle_scope_extensions(isolate()));
858 movp(rax, prev_limit_reg);
859 jmp(&leave_exit_frame);
863 void MacroAssembler::JumpToExternalReference(const ExternalReference& ext,
865 // Set the entry point and jump to the C entry runtime stub.
866 LoadAddress(rbx, ext);
867 CEntryStub ces(isolate(), result_size);
868 jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
872 void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
874 const CallWrapper& call_wrapper) {
875 // You can't call a builtin without a valid frame.
876 DCHECK(flag == JUMP_FUNCTION || has_frame());
878 // Rely on the assertion to check that the number of provided
879 // arguments match the expected number of arguments. Fake a
880 // parameter count to avoid emitting code to do the check.
881 ParameterCount expected(0);
882 GetBuiltinEntry(rdx, id);
883 InvokeCode(rdx, expected, expected, flag, call_wrapper);
887 void MacroAssembler::GetBuiltinFunction(Register target,
888 Builtins::JavaScript id) {
889 // Load the builtins object into target register.
890 movp(target, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
891 movp(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
892 movp(target, FieldOperand(target,
893 JSBuiltinsObject::OffsetOfFunctionWithId(id)));
897 void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
898 DCHECK(!target.is(rdi));
899 // Load the JavaScript builtin function from the builtins object.
900 GetBuiltinFunction(rdi, id);
901 movp(target, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
905 #define REG(Name) { kRegister_ ## Name ## _Code }
907 static const Register saved_regs[] = {
908 REG(rax), REG(rcx), REG(rdx), REG(rbx), REG(rbp), REG(rsi), REG(rdi), REG(r8),
909 REG(r9), REG(r10), REG(r11)
914 static const int kNumberOfSavedRegs = sizeof(saved_regs) / sizeof(Register);
917 void MacroAssembler::PushCallerSaved(SaveFPRegsMode fp_mode,
920 Register exclusion3) {
921 // We don't allow a GC during a store buffer overflow so there is no need to
922 // store the registers in any particular way, but we do have to store and
924 for (int i = 0; i < kNumberOfSavedRegs; i++) {
925 Register reg = saved_regs[i];
926 if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
930 // R12 to r15 are callee save on all platforms.
931 if (fp_mode == kSaveFPRegs) {
932 subp(rsp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
933 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
934 XMMRegister reg = XMMRegister::from_code(i);
935 movsd(Operand(rsp, i * kDoubleSize), reg);
941 void MacroAssembler::PopCallerSaved(SaveFPRegsMode fp_mode,
944 Register exclusion3) {
945 if (fp_mode == kSaveFPRegs) {
946 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
947 XMMRegister reg = XMMRegister::from_code(i);
948 movsd(reg, Operand(rsp, i * kDoubleSize));
950 addp(rsp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
952 for (int i = kNumberOfSavedRegs - 1; i >= 0; i--) {
953 Register reg = saved_regs[i];
954 if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
961 void MacroAssembler::Cvtlsi2sd(XMMRegister dst, Register src) {
967 void MacroAssembler::Cvtlsi2sd(XMMRegister dst, const Operand& src) {
973 void MacroAssembler::Load(Register dst, const Operand& src, Representation r) {
974 DCHECK(!r.IsDouble());
975 if (r.IsInteger8()) {
977 } else if (r.IsUInteger8()) {
979 } else if (r.IsInteger16()) {
981 } else if (r.IsUInteger16()) {
983 } else if (r.IsInteger32()) {
991 void MacroAssembler::Store(const Operand& dst, Register src, Representation r) {
992 DCHECK(!r.IsDouble());
993 if (r.IsInteger8() || r.IsUInteger8()) {
995 } else if (r.IsInteger16() || r.IsUInteger16()) {
997 } else if (r.IsInteger32()) {
1000 if (r.IsHeapObject()) {
1002 } else if (r.IsSmi()) {
1010 void MacroAssembler::Set(Register dst, int64_t x) {
1013 } else if (is_uint32(x)) {
1014 movl(dst, Immediate(static_cast<uint32_t>(x)));
1015 } else if (is_int32(x)) {
1016 movq(dst, Immediate(static_cast<int32_t>(x)));
1023 void MacroAssembler::Set(const Operand& dst, intptr_t x) {
1024 if (kPointerSize == kInt64Size) {
1026 movp(dst, Immediate(static_cast<int32_t>(x)));
1028 Set(kScratchRegister, x);
1029 movp(dst, kScratchRegister);
1032 movp(dst, Immediate(static_cast<int32_t>(x)));
1037 // ----------------------------------------------------------------------------
1038 // Smi tagging, untagging and tag detection.
1040 bool MacroAssembler::IsUnsafeInt(const int32_t x) {
1041 static const int kMaxBits = 17;
1042 return !is_intn(x, kMaxBits);
1046 void MacroAssembler::SafeMove(Register dst, Smi* src) {
1047 DCHECK(!dst.is(kScratchRegister));
1048 if (IsUnsafeInt(src->value()) && jit_cookie() != 0) {
1049 if (SmiValuesAre32Bits()) {
1050 // JIT cookie can be converted to Smi.
1051 Move(dst, Smi::FromInt(src->value() ^ jit_cookie()));
1052 Move(kScratchRegister, Smi::FromInt(jit_cookie()));
1053 xorp(dst, kScratchRegister);
1055 DCHECK(SmiValuesAre31Bits());
1056 int32_t value = static_cast<int32_t>(reinterpret_cast<intptr_t>(src));
1057 movp(dst, Immediate(value ^ jit_cookie()));
1058 xorp(dst, Immediate(jit_cookie()));
1066 void MacroAssembler::SafePush(Smi* src) {
1067 if (IsUnsafeInt(src->value()) && jit_cookie() != 0) {
1068 if (SmiValuesAre32Bits()) {
1069 // JIT cookie can be converted to Smi.
1070 Push(Smi::FromInt(src->value() ^ jit_cookie()));
1071 Move(kScratchRegister, Smi::FromInt(jit_cookie()));
1072 xorp(Operand(rsp, 0), kScratchRegister);
1074 DCHECK(SmiValuesAre31Bits());
1075 int32_t value = static_cast<int32_t>(reinterpret_cast<intptr_t>(src));
1076 Push(Immediate(value ^ jit_cookie()));
1077 xorp(Operand(rsp, 0), Immediate(jit_cookie()));
1085 Register MacroAssembler::GetSmiConstant(Smi* source) {
1086 int value = source->value();
1088 xorl(kScratchRegister, kScratchRegister);
1089 return kScratchRegister;
1092 return kSmiConstantRegister;
1094 LoadSmiConstant(kScratchRegister, source);
1095 return kScratchRegister;
1099 void MacroAssembler::LoadSmiConstant(Register dst, Smi* source) {
1100 if (emit_debug_code()) {
1101 Move(dst, Smi::FromInt(kSmiConstantRegisterValue),
1102 Assembler::RelocInfoNone());
1103 cmpp(dst, kSmiConstantRegister);
1104 Assert(equal, kUninitializedKSmiConstantRegister);
1106 int value = source->value();
1111 bool negative = value < 0;
1112 unsigned int uvalue = negative ? -value : value;
1117 Operand(kSmiConstantRegister, kSmiConstantRegister, times_8, 0));
1121 leap(dst, Operand(dst, kSmiConstantRegister, times_8, 0));
1125 leap(dst, Operand(dst, kSmiConstantRegister, times_4, 0));
1129 Operand(kSmiConstantRegister, kSmiConstantRegister, times_4, 0));
1133 Operand(kSmiConstantRegister, kSmiConstantRegister, times_2, 0));
1137 Operand(kSmiConstantRegister, kSmiConstantRegister, times_1, 0));
1140 movp(dst, kSmiConstantRegister);
1146 Move(dst, source, Assembler::RelocInfoNone());
1155 void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
1156 STATIC_ASSERT(kSmiTag == 0);
1160 shlp(dst, Immediate(kSmiShift));
1164 void MacroAssembler::Integer32ToSmiField(const Operand& dst, Register src) {
1165 if (emit_debug_code()) {
1166 testb(dst, Immediate(0x01));
1168 j(zero, &ok, Label::kNear);
1169 Abort(kInteger32ToSmiFieldWritingToNonSmiLocation);
1173 if (SmiValuesAre32Bits()) {
1174 DCHECK(kSmiShift % kBitsPerByte == 0);
1175 movl(Operand(dst, kSmiShift / kBitsPerByte), src);
1177 DCHECK(SmiValuesAre31Bits());
1178 Integer32ToSmi(kScratchRegister, src);
1179 movp(dst, kScratchRegister);
1184 void MacroAssembler::Integer64PlusConstantToSmi(Register dst,
1188 addl(dst, Immediate(constant));
1190 leal(dst, Operand(src, constant));
1192 shlp(dst, Immediate(kSmiShift));
1196 void MacroAssembler::SmiToInteger32(Register dst, Register src) {
1197 STATIC_ASSERT(kSmiTag == 0);
1202 if (SmiValuesAre32Bits()) {
1203 shrp(dst, Immediate(kSmiShift));
1205 DCHECK(SmiValuesAre31Bits());
1206 sarl(dst, Immediate(kSmiShift));
1211 void MacroAssembler::SmiToInteger32(Register dst, const Operand& src) {
1212 if (SmiValuesAre32Bits()) {
1213 movl(dst, Operand(src, kSmiShift / kBitsPerByte));
1215 DCHECK(SmiValuesAre31Bits());
1217 sarl(dst, Immediate(kSmiShift));
1222 void MacroAssembler::SmiToInteger64(Register dst, Register src) {
1223 STATIC_ASSERT(kSmiTag == 0);
1227 sarp(dst, Immediate(kSmiShift));
1228 if (kPointerSize == kInt32Size) {
1229 // Sign extend to 64-bit.
1235 void MacroAssembler::SmiToInteger64(Register dst, const Operand& src) {
1236 if (SmiValuesAre32Bits()) {
1237 movsxlq(dst, Operand(src, kSmiShift / kBitsPerByte));
1239 DCHECK(SmiValuesAre31Bits());
1241 SmiToInteger64(dst, dst);
1246 void MacroAssembler::SmiTest(Register src) {
1252 void MacroAssembler::SmiCompare(Register smi1, Register smi2) {
1259 void MacroAssembler::SmiCompare(Register dst, Smi* src) {
1265 void MacroAssembler::Cmp(Register dst, Smi* src) {
1266 DCHECK(!dst.is(kScratchRegister));
1267 if (src->value() == 0) {
1270 Register constant_reg = GetSmiConstant(src);
1271 cmpp(dst, constant_reg);
1276 void MacroAssembler::SmiCompare(Register dst, const Operand& src) {
1283 void MacroAssembler::SmiCompare(const Operand& dst, Register src) {
1290 void MacroAssembler::SmiCompare(const Operand& dst, Smi* src) {
1292 if (SmiValuesAre32Bits()) {
1293 cmpl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(src->value()));
1295 DCHECK(SmiValuesAre31Bits());
1296 cmpl(dst, Immediate(src));
1301 void MacroAssembler::Cmp(const Operand& dst, Smi* src) {
1302 // The Operand cannot use the smi register.
1303 Register smi_reg = GetSmiConstant(src);
1304 DCHECK(!dst.AddressUsesRegister(smi_reg));
1309 void MacroAssembler::SmiCompareInteger32(const Operand& dst, Register src) {
1310 if (SmiValuesAre32Bits()) {
1311 cmpl(Operand(dst, kSmiShift / kBitsPerByte), src);
1313 DCHECK(SmiValuesAre31Bits());
1314 SmiToInteger32(kScratchRegister, dst);
1315 cmpl(kScratchRegister, src);
1320 void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
1326 SmiToInteger64(dst, src);
1332 if (power < kSmiShift) {
1333 sarp(dst, Immediate(kSmiShift - power));
1334 } else if (power > kSmiShift) {
1335 shlp(dst, Immediate(power - kSmiShift));
1340 void MacroAssembler::PositiveSmiDivPowerOfTwoToInteger32(Register dst,
1343 DCHECK((0 <= power) && (power < 32));
1345 shrp(dst, Immediate(power + kSmiShift));
1347 UNIMPLEMENTED(); // Not used.
1352 void MacroAssembler::SmiOrIfSmis(Register dst, Register src1, Register src2,
1354 Label::Distance near_jump) {
1355 if (dst.is(src1) || dst.is(src2)) {
1356 DCHECK(!src1.is(kScratchRegister));
1357 DCHECK(!src2.is(kScratchRegister));
1358 movp(kScratchRegister, src1);
1359 orp(kScratchRegister, src2);
1360 JumpIfNotSmi(kScratchRegister, on_not_smis, near_jump);
1361 movp(dst, kScratchRegister);
1365 JumpIfNotSmi(dst, on_not_smis, near_jump);
1370 Condition MacroAssembler::CheckSmi(Register src) {
1371 STATIC_ASSERT(kSmiTag == 0);
1372 testb(src, Immediate(kSmiTagMask));
1377 Condition MacroAssembler::CheckSmi(const Operand& src) {
1378 STATIC_ASSERT(kSmiTag == 0);
1379 testb(src, Immediate(kSmiTagMask));
1384 Condition MacroAssembler::CheckNonNegativeSmi(Register src) {
1385 STATIC_ASSERT(kSmiTag == 0);
1386 // Test that both bits of the mask 0x8000000000000001 are zero.
1387 movp(kScratchRegister, src);
1388 rolp(kScratchRegister, Immediate(1));
1389 testb(kScratchRegister, Immediate(3));
1394 Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
1395 if (first.is(second)) {
1396 return CheckSmi(first);
1398 STATIC_ASSERT(kSmiTag == 0 && kHeapObjectTag == 1 && kHeapObjectTagMask == 3);
1399 if (SmiValuesAre32Bits()) {
1400 leal(kScratchRegister, Operand(first, second, times_1, 0));
1401 testb(kScratchRegister, Immediate(0x03));
1403 DCHECK(SmiValuesAre31Bits());
1404 movl(kScratchRegister, first);
1405 orl(kScratchRegister, second);
1406 testb(kScratchRegister, Immediate(kSmiTagMask));
1412 Condition MacroAssembler::CheckBothNonNegativeSmi(Register first,
1414 if (first.is(second)) {
1415 return CheckNonNegativeSmi(first);
1417 movp(kScratchRegister, first);
1418 orp(kScratchRegister, second);
1419 rolp(kScratchRegister, Immediate(1));
1420 testl(kScratchRegister, Immediate(3));
1425 Condition MacroAssembler::CheckEitherSmi(Register first,
1428 if (first.is(second)) {
1429 return CheckSmi(first);
1431 if (scratch.is(second)) {
1432 andl(scratch, first);
1434 if (!scratch.is(first)) {
1435 movl(scratch, first);
1437 andl(scratch, second);
1439 testb(scratch, Immediate(kSmiTagMask));
1444 Condition MacroAssembler::CheckIsMinSmi(Register src) {
1445 DCHECK(!src.is(kScratchRegister));
1446 // If we overflow by subtracting one, it's the minimal smi value.
1447 cmpp(src, kSmiConstantRegister);
1452 Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
1453 if (SmiValuesAre32Bits()) {
1454 // A 32-bit integer value can always be converted to a smi.
1457 DCHECK(SmiValuesAre31Bits());
1458 cmpl(src, Immediate(0xc0000000));
1464 Condition MacroAssembler::CheckUInteger32ValidSmiValue(Register src) {
1465 if (SmiValuesAre32Bits()) {
1466 // An unsigned 32-bit integer value is valid as long as the high bit
1471 DCHECK(SmiValuesAre31Bits());
1472 testl(src, Immediate(0xc0000000));
1478 void MacroAssembler::CheckSmiToIndicator(Register dst, Register src) {
1480 andl(dst, Immediate(kSmiTagMask));
1482 movl(dst, Immediate(kSmiTagMask));
1488 void MacroAssembler::CheckSmiToIndicator(Register dst, const Operand& src) {
1489 if (!(src.AddressUsesRegister(dst))) {
1490 movl(dst, Immediate(kSmiTagMask));
1494 andl(dst, Immediate(kSmiTagMask));
1499 void MacroAssembler::JumpIfValidSmiValue(Register src,
1501 Label::Distance near_jump) {
1502 Condition is_valid = CheckInteger32ValidSmiValue(src);
1503 j(is_valid, on_valid, near_jump);
1507 void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1509 Label::Distance near_jump) {
1510 Condition is_valid = CheckInteger32ValidSmiValue(src);
1511 j(NegateCondition(is_valid), on_invalid, near_jump);
1515 void MacroAssembler::JumpIfUIntValidSmiValue(Register src,
1517 Label::Distance near_jump) {
1518 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1519 j(is_valid, on_valid, near_jump);
1523 void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1525 Label::Distance near_jump) {
1526 Condition is_valid = CheckUInteger32ValidSmiValue(src);
1527 j(NegateCondition(is_valid), on_invalid, near_jump);
1531 void MacroAssembler::JumpIfSmi(Register src,
1533 Label::Distance near_jump) {
1534 Condition smi = CheckSmi(src);
1535 j(smi, on_smi, near_jump);
1539 void MacroAssembler::JumpIfNotSmi(Register src,
1541 Label::Distance near_jump) {
1542 Condition smi = CheckSmi(src);
1543 j(NegateCondition(smi), on_not_smi, near_jump);
1547 void MacroAssembler::JumpUnlessNonNegativeSmi(
1548 Register src, Label* on_not_smi_or_negative,
1549 Label::Distance near_jump) {
1550 Condition non_negative_smi = CheckNonNegativeSmi(src);
1551 j(NegateCondition(non_negative_smi), on_not_smi_or_negative, near_jump);
1555 void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1558 Label::Distance near_jump) {
1559 SmiCompare(src, constant);
1560 j(equal, on_equals, near_jump);
1564 void MacroAssembler::JumpIfNotBothSmi(Register src1,
1566 Label* on_not_both_smi,
1567 Label::Distance near_jump) {
1568 Condition both_smi = CheckBothSmi(src1, src2);
1569 j(NegateCondition(both_smi), on_not_both_smi, near_jump);
1573 void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1575 Label* on_not_both_smi,
1576 Label::Distance near_jump) {
1577 Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
1578 j(NegateCondition(both_smi), on_not_both_smi, near_jump);
1582 void MacroAssembler::SmiAddConstant(Register dst, Register src, Smi* constant) {
1583 if (constant->value() == 0) {
1588 } else if (dst.is(src)) {
1589 DCHECK(!dst.is(kScratchRegister));
1590 switch (constant->value()) {
1592 addp(dst, kSmiConstantRegister);
1595 leap(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1598 leap(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1601 leap(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1604 Register constant_reg = GetSmiConstant(constant);
1605 addp(dst, constant_reg);
1609 switch (constant->value()) {
1611 leap(dst, Operand(src, kSmiConstantRegister, times_1, 0));
1614 leap(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1617 leap(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1620 leap(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1623 LoadSmiConstant(dst, constant);
1631 void MacroAssembler::SmiAddConstant(const Operand& dst, Smi* constant) {
1632 if (constant->value() != 0) {
1633 if (SmiValuesAre32Bits()) {
1634 addl(Operand(dst, kSmiShift / kBitsPerByte),
1635 Immediate(constant->value()));
1637 DCHECK(SmiValuesAre31Bits());
1638 addp(dst, Immediate(constant));
1644 void MacroAssembler::SmiAddConstant(Register dst,
1647 SmiOperationExecutionMode mode,
1648 Label* bailout_label,
1649 Label::Distance near_jump) {
1650 if (constant->value() == 0) {
1654 } else if (dst.is(src)) {
1655 DCHECK(!dst.is(kScratchRegister));
1656 LoadSmiConstant(kScratchRegister, constant);
1657 addp(dst, kScratchRegister);
1658 if (mode.Contains(BAILOUT_ON_NO_OVERFLOW)) {
1659 j(no_overflow, bailout_label, near_jump);
1660 DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1661 subp(dst, kScratchRegister);
1662 } else if (mode.Contains(BAILOUT_ON_OVERFLOW)) {
1663 if (mode.Contains(PRESERVE_SOURCE_REGISTER)) {
1665 j(no_overflow, &done, Label::kNear);
1666 subp(dst, kScratchRegister);
1667 jmp(bailout_label, near_jump);
1670 // Bailout if overflow without reserving src.
1671 j(overflow, bailout_label, near_jump);
1674 CHECK(mode.IsEmpty());
1677 DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1678 DCHECK(mode.Contains(BAILOUT_ON_OVERFLOW));
1679 LoadSmiConstant(dst, constant);
1681 j(overflow, bailout_label, near_jump);
1686 void MacroAssembler::SmiSubConstant(Register dst, Register src, Smi* constant) {
1687 if (constant->value() == 0) {
1691 } else if (dst.is(src)) {
1692 DCHECK(!dst.is(kScratchRegister));
1693 Register constant_reg = GetSmiConstant(constant);
1694 subp(dst, constant_reg);
1696 if (constant->value() == Smi::kMinValue) {
1697 LoadSmiConstant(dst, constant);
1698 // Adding and subtracting the min-value gives the same result, it only
1699 // differs on the overflow bit, which we don't check here.
1702 // Subtract by adding the negation.
1703 LoadSmiConstant(dst, Smi::FromInt(-constant->value()));
1710 void MacroAssembler::SmiSubConstant(Register dst,
1713 SmiOperationExecutionMode mode,
1714 Label* bailout_label,
1715 Label::Distance near_jump) {
1716 if (constant->value() == 0) {
1720 } else if (dst.is(src)) {
1721 DCHECK(!dst.is(kScratchRegister));
1722 LoadSmiConstant(kScratchRegister, constant);
1723 subp(dst, kScratchRegister);
1724 if (mode.Contains(BAILOUT_ON_NO_OVERFLOW)) {
1725 j(no_overflow, bailout_label, near_jump);
1726 DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1727 addp(dst, kScratchRegister);
1728 } else if (mode.Contains(BAILOUT_ON_OVERFLOW)) {
1729 if (mode.Contains(PRESERVE_SOURCE_REGISTER)) {
1731 j(no_overflow, &done, Label::kNear);
1732 addp(dst, kScratchRegister);
1733 jmp(bailout_label, near_jump);
1736 // Bailout if overflow without reserving src.
1737 j(overflow, bailout_label, near_jump);
1740 CHECK(mode.IsEmpty());
1743 DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1744 DCHECK(mode.Contains(BAILOUT_ON_OVERFLOW));
1745 if (constant->value() == Smi::kMinValue) {
1746 DCHECK(!dst.is(kScratchRegister));
1748 LoadSmiConstant(kScratchRegister, constant);
1749 subp(dst, kScratchRegister);
1750 j(overflow, bailout_label, near_jump);
1752 // Subtract by adding the negation.
1753 LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1755 j(overflow, bailout_label, near_jump);
1761 void MacroAssembler::SmiNeg(Register dst,
1763 Label* on_smi_result,
1764 Label::Distance near_jump) {
1766 DCHECK(!dst.is(kScratchRegister));
1767 movp(kScratchRegister, src);
1768 negp(dst); // Low 32 bits are retained as zero by negation.
1769 // Test if result is zero or Smi::kMinValue.
1770 cmpp(dst, kScratchRegister);
1771 j(not_equal, on_smi_result, near_jump);
1772 movp(src, kScratchRegister);
1777 // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1778 j(not_equal, on_smi_result, near_jump);
1784 static void SmiAddHelper(MacroAssembler* masm,
1788 Label* on_not_smi_result,
1789 Label::Distance near_jump) {
1792 masm->addp(dst, src2);
1793 masm->j(no_overflow, &done, Label::kNear);
1795 masm->subp(dst, src2);
1796 masm->jmp(on_not_smi_result, near_jump);
1799 masm->movp(dst, src1);
1800 masm->addp(dst, src2);
1801 masm->j(overflow, on_not_smi_result, near_jump);
1806 void MacroAssembler::SmiAdd(Register dst,
1809 Label* on_not_smi_result,
1810 Label::Distance near_jump) {
1811 DCHECK_NOT_NULL(on_not_smi_result);
1812 DCHECK(!dst.is(src2));
1813 SmiAddHelper<Register>(this, dst, src1, src2, on_not_smi_result, near_jump);
1817 void MacroAssembler::SmiAdd(Register dst,
1819 const Operand& src2,
1820 Label* on_not_smi_result,
1821 Label::Distance near_jump) {
1822 DCHECK_NOT_NULL(on_not_smi_result);
1823 DCHECK(!src2.AddressUsesRegister(dst));
1824 SmiAddHelper<Operand>(this, dst, src1, src2, on_not_smi_result, near_jump);
1828 void MacroAssembler::SmiAdd(Register dst,
1831 // No overflow checking. Use only when it's known that
1832 // overflowing is impossible.
1833 if (!dst.is(src1)) {
1834 if (emit_debug_code()) {
1835 movp(kScratchRegister, src1);
1836 addp(kScratchRegister, src2);
1837 Check(no_overflow, kSmiAdditionOverflow);
1839 leap(dst, Operand(src1, src2, times_1, 0));
1842 Assert(no_overflow, kSmiAdditionOverflow);
1848 static void SmiSubHelper(MacroAssembler* masm,
1852 Label* on_not_smi_result,
1853 Label::Distance near_jump) {
1856 masm->subp(dst, src2);
1857 masm->j(no_overflow, &done, Label::kNear);
1859 masm->addp(dst, src2);
1860 masm->jmp(on_not_smi_result, near_jump);
1863 masm->movp(dst, src1);
1864 masm->subp(dst, src2);
1865 masm->j(overflow, on_not_smi_result, near_jump);
1870 void MacroAssembler::SmiSub(Register dst,
1873 Label* on_not_smi_result,
1874 Label::Distance near_jump) {
1875 DCHECK_NOT_NULL(on_not_smi_result);
1876 DCHECK(!dst.is(src2));
1877 SmiSubHelper<Register>(this, dst, src1, src2, on_not_smi_result, near_jump);
1881 void MacroAssembler::SmiSub(Register dst,
1883 const Operand& src2,
1884 Label* on_not_smi_result,
1885 Label::Distance near_jump) {
1886 DCHECK_NOT_NULL(on_not_smi_result);
1887 DCHECK(!src2.AddressUsesRegister(dst));
1888 SmiSubHelper<Operand>(this, dst, src1, src2, on_not_smi_result, near_jump);
1893 static void SmiSubNoOverflowHelper(MacroAssembler* masm,
1897 // No overflow checking. Use only when it's known that
1898 // overflowing is impossible (e.g., subtracting two positive smis).
1899 if (!dst.is(src1)) {
1900 masm->movp(dst, src1);
1902 masm->subp(dst, src2);
1903 masm->Assert(no_overflow, kSmiSubtractionOverflow);
1907 void MacroAssembler::SmiSub(Register dst, Register src1, Register src2) {
1908 DCHECK(!dst.is(src2));
1909 SmiSubNoOverflowHelper<Register>(this, dst, src1, src2);
1913 void MacroAssembler::SmiSub(Register dst,
1915 const Operand& src2) {
1916 SmiSubNoOverflowHelper<Operand>(this, dst, src1, src2);
1920 void MacroAssembler::SmiMul(Register dst,
1923 Label* on_not_smi_result,
1924 Label::Distance near_jump) {
1925 DCHECK(!dst.is(src2));
1926 DCHECK(!dst.is(kScratchRegister));
1927 DCHECK(!src1.is(kScratchRegister));
1928 DCHECK(!src2.is(kScratchRegister));
1931 Label failure, zero_correct_result;
1932 movp(kScratchRegister, src1); // Create backup for later testing.
1933 SmiToInteger64(dst, src1);
1935 j(overflow, &failure, Label::kNear);
1937 // Check for negative zero result. If product is zero, and one
1938 // argument is negative, go to slow case.
1939 Label correct_result;
1941 j(not_zero, &correct_result, Label::kNear);
1943 movp(dst, kScratchRegister);
1945 // Result was positive zero.
1946 j(positive, &zero_correct_result, Label::kNear);
1948 bind(&failure); // Reused failure exit, restores src1.
1949 movp(src1, kScratchRegister);
1950 jmp(on_not_smi_result, near_jump);
1952 bind(&zero_correct_result);
1955 bind(&correct_result);
1957 SmiToInteger64(dst, src1);
1959 j(overflow, on_not_smi_result, near_jump);
1960 // Check for negative zero result. If product is zero, and one
1961 // argument is negative, go to slow case.
1962 Label correct_result;
1964 j(not_zero, &correct_result, Label::kNear);
1965 // One of src1 and src2 is zero, the check whether the other is
1967 movp(kScratchRegister, src1);
1968 xorp(kScratchRegister, src2);
1969 j(negative, on_not_smi_result, near_jump);
1970 bind(&correct_result);
1975 void MacroAssembler::SmiDiv(Register dst,
1978 Label* on_not_smi_result,
1979 Label::Distance near_jump) {
1980 DCHECK(!src1.is(kScratchRegister));
1981 DCHECK(!src2.is(kScratchRegister));
1982 DCHECK(!dst.is(kScratchRegister));
1983 DCHECK(!src2.is(rax));
1984 DCHECK(!src2.is(rdx));
1985 DCHECK(!src1.is(rdx));
1987 // Check for 0 divisor (result is +/-Infinity).
1989 j(zero, on_not_smi_result, near_jump);
1992 movp(kScratchRegister, src1);
1994 SmiToInteger32(rax, src1);
1995 // We need to rule out dividing Smi::kMinValue by -1, since that would
1996 // overflow in idiv and raise an exception.
1997 // We combine this with negative zero test (negative zero only happens
1998 // when dividing zero by a negative number).
2000 // We overshoot a little and go to slow case if we divide min-value
2001 // by any negative value, not just -1.
2003 testl(rax, Immediate(~Smi::kMinValue));
2004 j(not_zero, &safe_div, Label::kNear);
2007 j(positive, &safe_div, Label::kNear);
2008 movp(src1, kScratchRegister);
2009 jmp(on_not_smi_result, near_jump);
2011 j(negative, on_not_smi_result, near_jump);
2015 SmiToInteger32(src2, src2);
2016 // Sign extend src1 into edx:eax.
2019 Integer32ToSmi(src2, src2);
2020 // Check that the remainder is zero.
2024 j(zero, &smi_result, Label::kNear);
2025 movp(src1, kScratchRegister);
2026 jmp(on_not_smi_result, near_jump);
2029 j(not_zero, on_not_smi_result, near_jump);
2031 if (!dst.is(src1) && src1.is(rax)) {
2032 movp(src1, kScratchRegister);
2034 Integer32ToSmi(dst, rax);
2038 void MacroAssembler::SmiMod(Register dst,
2041 Label* on_not_smi_result,
2042 Label::Distance near_jump) {
2043 DCHECK(!dst.is(kScratchRegister));
2044 DCHECK(!src1.is(kScratchRegister));
2045 DCHECK(!src2.is(kScratchRegister));
2046 DCHECK(!src2.is(rax));
2047 DCHECK(!src2.is(rdx));
2048 DCHECK(!src1.is(rdx));
2049 DCHECK(!src1.is(src2));
2052 j(zero, on_not_smi_result, near_jump);
2055 movp(kScratchRegister, src1);
2057 SmiToInteger32(rax, src1);
2058 SmiToInteger32(src2, src2);
2060 // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
2062 cmpl(rax, Immediate(Smi::kMinValue));
2063 j(not_equal, &safe_div, Label::kNear);
2064 cmpl(src2, Immediate(-1));
2065 j(not_equal, &safe_div, Label::kNear);
2066 // Retag inputs and go slow case.
2067 Integer32ToSmi(src2, src2);
2069 movp(src1, kScratchRegister);
2071 jmp(on_not_smi_result, near_jump);
2074 // Sign extend eax into edx:eax.
2077 // Restore smi tags on inputs.
2078 Integer32ToSmi(src2, src2);
2080 movp(src1, kScratchRegister);
2082 // Check for a negative zero result. If the result is zero, and the
2083 // dividend is negative, go slow to return a floating point negative zero.
2086 j(not_zero, &smi_result, Label::kNear);
2088 j(negative, on_not_smi_result, near_jump);
2090 Integer32ToSmi(dst, rdx);
2094 void MacroAssembler::SmiNot(Register dst, Register src) {
2095 DCHECK(!dst.is(kScratchRegister));
2096 DCHECK(!src.is(kScratchRegister));
2097 if (SmiValuesAre32Bits()) {
2098 // Set tag and padding bits before negating, so that they are zero
2100 movl(kScratchRegister, Immediate(~0));
2102 DCHECK(SmiValuesAre31Bits());
2103 movl(kScratchRegister, Immediate(1));
2106 xorp(dst, kScratchRegister);
2108 leap(dst, Operand(src, kScratchRegister, times_1, 0));
2114 void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
2115 DCHECK(!dst.is(src2));
2116 if (!dst.is(src1)) {
2123 void MacroAssembler::SmiAndConstant(Register dst, Register src, Smi* constant) {
2124 if (constant->value() == 0) {
2126 } else if (dst.is(src)) {
2127 DCHECK(!dst.is(kScratchRegister));
2128 Register constant_reg = GetSmiConstant(constant);
2129 andp(dst, constant_reg);
2131 LoadSmiConstant(dst, constant);
2137 void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
2138 if (!dst.is(src1)) {
2139 DCHECK(!src1.is(src2));
2146 void MacroAssembler::SmiOrConstant(Register dst, Register src, Smi* constant) {
2148 DCHECK(!dst.is(kScratchRegister));
2149 Register constant_reg = GetSmiConstant(constant);
2150 orp(dst, constant_reg);
2152 LoadSmiConstant(dst, constant);
2158 void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
2159 if (!dst.is(src1)) {
2160 DCHECK(!src1.is(src2));
2167 void MacroAssembler::SmiXorConstant(Register dst, Register src, Smi* constant) {
2169 DCHECK(!dst.is(kScratchRegister));
2170 Register constant_reg = GetSmiConstant(constant);
2171 xorp(dst, constant_reg);
2173 LoadSmiConstant(dst, constant);
2179 void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
2182 DCHECK(is_uint5(shift_value));
2183 if (shift_value > 0) {
2185 sarp(dst, Immediate(shift_value + kSmiShift));
2186 shlp(dst, Immediate(kSmiShift));
2188 UNIMPLEMENTED(); // Not used.
2194 void MacroAssembler::SmiShiftLeftConstant(Register dst,
2197 Label* on_not_smi_result,
2198 Label::Distance near_jump) {
2199 if (SmiValuesAre32Bits()) {
2203 if (shift_value > 0) {
2204 // Shift amount specified by lower 5 bits, not six as the shl opcode.
2205 shlq(dst, Immediate(shift_value & 0x1f));
2208 DCHECK(SmiValuesAre31Bits());
2210 UNIMPLEMENTED(); // Not used.
2212 SmiToInteger32(dst, src);
2213 shll(dst, Immediate(shift_value));
2214 JumpIfNotValidSmiValue(dst, on_not_smi_result, near_jump);
2215 Integer32ToSmi(dst, dst);
2221 void MacroAssembler::SmiShiftLogicalRightConstant(
2222 Register dst, Register src, int shift_value,
2223 Label* on_not_smi_result, Label::Distance near_jump) {
2224 // Logic right shift interprets its result as an *unsigned* number.
2226 UNIMPLEMENTED(); // Not used.
2228 if (shift_value == 0) {
2230 j(negative, on_not_smi_result, near_jump);
2232 if (SmiValuesAre32Bits()) {
2234 shrp(dst, Immediate(shift_value + kSmiShift));
2235 shlp(dst, Immediate(kSmiShift));
2237 DCHECK(SmiValuesAre31Bits());
2238 SmiToInteger32(dst, src);
2239 shrp(dst, Immediate(shift_value));
2240 JumpIfUIntNotValidSmiValue(dst, on_not_smi_result, near_jump);
2241 Integer32ToSmi(dst, dst);
2247 void MacroAssembler::SmiShiftLeft(Register dst,
2250 Label* on_not_smi_result,
2251 Label::Distance near_jump) {
2252 if (SmiValuesAre32Bits()) {
2253 DCHECK(!dst.is(rcx));
2254 if (!dst.is(src1)) {
2257 // Untag shift amount.
2258 SmiToInteger32(rcx, src2);
2259 // Shift amount specified by lower 5 bits, not six as the shl opcode.
2260 andp(rcx, Immediate(0x1f));
2263 DCHECK(SmiValuesAre31Bits());
2264 DCHECK(!dst.is(kScratchRegister));
2265 DCHECK(!src1.is(kScratchRegister));
2266 DCHECK(!src2.is(kScratchRegister));
2267 DCHECK(!dst.is(src2));
2268 DCHECK(!dst.is(rcx));
2270 if (src1.is(rcx) || src2.is(rcx)) {
2271 movq(kScratchRegister, rcx);
2274 UNIMPLEMENTED(); // Not used.
2277 SmiToInteger32(dst, src1);
2278 SmiToInteger32(rcx, src2);
2280 JumpIfValidSmiValue(dst, &valid_result, Label::kNear);
2281 // As src1 or src2 could not be dst, we do not need to restore them for
2283 if (src1.is(rcx) || src2.is(rcx)) {
2285 movq(src1, kScratchRegister);
2287 movq(src2, kScratchRegister);
2290 jmp(on_not_smi_result, near_jump);
2291 bind(&valid_result);
2292 Integer32ToSmi(dst, dst);
2298 void MacroAssembler::SmiShiftLogicalRight(Register dst,
2301 Label* on_not_smi_result,
2302 Label::Distance near_jump) {
2303 DCHECK(!dst.is(kScratchRegister));
2304 DCHECK(!src1.is(kScratchRegister));
2305 DCHECK(!src2.is(kScratchRegister));
2306 DCHECK(!dst.is(src2));
2307 DCHECK(!dst.is(rcx));
2308 if (src1.is(rcx) || src2.is(rcx)) {
2309 movq(kScratchRegister, rcx);
2312 UNIMPLEMENTED(); // Not used.
2315 SmiToInteger32(dst, src1);
2316 SmiToInteger32(rcx, src2);
2318 JumpIfUIntValidSmiValue(dst, &valid_result, Label::kNear);
2319 // As src1 or src2 could not be dst, we do not need to restore them for
2321 if (src1.is(rcx) || src2.is(rcx)) {
2323 movq(src1, kScratchRegister);
2325 movq(src2, kScratchRegister);
2328 jmp(on_not_smi_result, near_jump);
2329 bind(&valid_result);
2330 Integer32ToSmi(dst, dst);
2335 void MacroAssembler::SmiShiftArithmeticRight(Register dst,
2338 DCHECK(!dst.is(kScratchRegister));
2339 DCHECK(!src1.is(kScratchRegister));
2340 DCHECK(!src2.is(kScratchRegister));
2341 DCHECK(!dst.is(rcx));
2343 SmiToInteger32(rcx, src2);
2344 if (!dst.is(src1)) {
2347 SmiToInteger32(dst, dst);
2349 Integer32ToSmi(dst, dst);
2353 void MacroAssembler::SelectNonSmi(Register dst,
2357 Label::Distance near_jump) {
2358 DCHECK(!dst.is(kScratchRegister));
2359 DCHECK(!src1.is(kScratchRegister));
2360 DCHECK(!src2.is(kScratchRegister));
2361 DCHECK(!dst.is(src1));
2362 DCHECK(!dst.is(src2));
2363 // Both operands must not be smis.
2365 Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
2366 Check(not_both_smis, kBothRegistersWereSmisInSelectNonSmi);
2368 STATIC_ASSERT(kSmiTag == 0);
2369 DCHECK_EQ(0, Smi::FromInt(0));
2370 movl(kScratchRegister, Immediate(kSmiTagMask));
2371 andp(kScratchRegister, src1);
2372 testl(kScratchRegister, src2);
2373 // If non-zero then both are smis.
2374 j(not_zero, on_not_smis, near_jump);
2376 // Exactly one operand is a smi.
2377 DCHECK_EQ(1, static_cast<int>(kSmiTagMask));
2378 // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
2379 subp(kScratchRegister, Immediate(1));
2380 // If src1 is a smi, then scratch register all 1s, else it is all 0s.
2383 andp(dst, kScratchRegister);
2384 // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
2386 // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
2390 SmiIndex MacroAssembler::SmiToIndex(Register dst,
2393 if (SmiValuesAre32Bits()) {
2394 DCHECK(is_uint6(shift));
2395 // There is a possible optimization if shift is in the range 60-63, but that
2396 // will (and must) never happen.
2400 if (shift < kSmiShift) {
2401 sarp(dst, Immediate(kSmiShift - shift));
2403 shlp(dst, Immediate(shift - kSmiShift));
2405 return SmiIndex(dst, times_1);
2407 DCHECK(SmiValuesAre31Bits());
2408 DCHECK(shift >= times_1 && shift <= (static_cast<int>(times_8) + 1));
2412 // We have to sign extend the index register to 64-bit as the SMI might
2415 if (shift == times_1) {
2416 sarq(dst, Immediate(kSmiShift));
2417 return SmiIndex(dst, times_1);
2419 return SmiIndex(dst, static_cast<ScaleFactor>(shift - 1));
2424 SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
2427 if (SmiValuesAre32Bits()) {
2428 // Register src holds a positive smi.
2429 DCHECK(is_uint6(shift));
2434 if (shift < kSmiShift) {
2435 sarp(dst, Immediate(kSmiShift - shift));
2437 shlp(dst, Immediate(shift - kSmiShift));
2439 return SmiIndex(dst, times_1);
2441 DCHECK(SmiValuesAre31Bits());
2442 DCHECK(shift >= times_1 && shift <= (static_cast<int>(times_8) + 1));
2447 if (shift == times_1) {
2448 sarq(dst, Immediate(kSmiShift));
2449 return SmiIndex(dst, times_1);
2451 return SmiIndex(dst, static_cast<ScaleFactor>(shift - 1));
2456 void MacroAssembler::AddSmiField(Register dst, const Operand& src) {
2457 if (SmiValuesAre32Bits()) {
2458 DCHECK_EQ(0, kSmiShift % kBitsPerByte);
2459 addl(dst, Operand(src, kSmiShift / kBitsPerByte));
2461 DCHECK(SmiValuesAre31Bits());
2462 SmiToInteger32(kScratchRegister, src);
2463 addl(dst, kScratchRegister);
2468 void MacroAssembler::Push(Smi* source) {
2469 intptr_t smi = reinterpret_cast<intptr_t>(source);
2470 if (is_int32(smi)) {
2471 Push(Immediate(static_cast<int32_t>(smi)));
2473 Register constant = GetSmiConstant(source);
2479 void MacroAssembler::PushRegisterAsTwoSmis(Register src, Register scratch) {
2480 DCHECK(!src.is(scratch));
2483 shrp(src, Immediate(kPointerSize * kBitsPerByte - kSmiShift));
2484 shlp(src, Immediate(kSmiShift));
2487 shlp(scratch, Immediate(kSmiShift));
2492 void MacroAssembler::PopRegisterAsTwoSmis(Register dst, Register scratch) {
2493 DCHECK(!dst.is(scratch));
2496 shrp(scratch, Immediate(kSmiShift));
2498 shrp(dst, Immediate(kSmiShift));
2500 shlp(dst, Immediate(kPointerSize * kBitsPerByte - kSmiShift));
2505 void MacroAssembler::Test(const Operand& src, Smi* source) {
2506 if (SmiValuesAre32Bits()) {
2507 testl(Operand(src, kIntSize), Immediate(source->value()));
2509 DCHECK(SmiValuesAre31Bits());
2510 testl(src, Immediate(source));
2515 // ----------------------------------------------------------------------------
2518 void MacroAssembler::LookupNumberStringCache(Register object,
2523 // Use of registers. Register result is used as a temporary.
2524 Register number_string_cache = result;
2525 Register mask = scratch1;
2526 Register scratch = scratch2;
2528 // Load the number string cache.
2529 LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2531 // Make the hash mask from the length of the number string cache. It
2532 // contains two elements (number and string) for each cache entry.
2534 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2535 shrl(mask, Immediate(1));
2536 subp(mask, Immediate(1)); // Make mask.
2538 // Calculate the entry in the number string cache. The hash value in the
2539 // number string cache for smis is just the smi value, and the hash for
2540 // doubles is the xor of the upper and lower words. See
2541 // Heap::GetNumberStringCache.
2543 Label load_result_from_cache;
2544 JumpIfSmi(object, &is_smi);
2546 isolate()->factory()->heap_number_map(),
2550 STATIC_ASSERT(8 == kDoubleSize);
2551 movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2552 xorp(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2553 andp(scratch, mask);
2554 // Each entry in string cache consists of two pointer sized fields,
2555 // but times_twice_pointer_size (multiplication by 16) scale factor
2556 // is not supported by addrmode on x64 platform.
2557 // So we have to premultiply entry index before lookup.
2558 shlp(scratch, Immediate(kPointerSizeLog2 + 1));
2560 Register index = scratch;
2561 Register probe = mask;
2563 FieldOperand(number_string_cache,
2566 FixedArray::kHeaderSize));
2567 JumpIfSmi(probe, not_found);
2568 movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2569 ucomisd(xmm0, FieldOperand(probe, HeapNumber::kValueOffset));
2570 j(parity_even, not_found); // Bail out if NaN is involved.
2571 j(not_equal, not_found); // The cache did not contain this value.
2572 jmp(&load_result_from_cache);
2575 SmiToInteger32(scratch, object);
2576 andp(scratch, mask);
2577 // Each entry in string cache consists of two pointer sized fields,
2578 // but times_twice_pointer_size (multiplication by 16) scale factor
2579 // is not supported by addrmode on x64 platform.
2580 // So we have to premultiply entry index before lookup.
2581 shlp(scratch, Immediate(kPointerSizeLog2 + 1));
2583 // Check if the entry is the smi we are looking for.
2585 FieldOperand(number_string_cache,
2588 FixedArray::kHeaderSize));
2589 j(not_equal, not_found);
2591 // Get the result from the cache.
2592 bind(&load_result_from_cache);
2594 FieldOperand(number_string_cache,
2597 FixedArray::kHeaderSize + kPointerSize));
2598 IncrementCounter(isolate()->counters()->number_to_string_native(), 1);
2602 void MacroAssembler::JumpIfNotString(Register object,
2603 Register object_map,
2605 Label::Distance near_jump) {
2606 Condition is_smi = CheckSmi(object);
2607 j(is_smi, not_string, near_jump);
2608 CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
2609 j(above_equal, not_string, near_jump);
2613 void MacroAssembler::JumpIfNotBothSequentialAsciiStrings(
2614 Register first_object,
2615 Register second_object,
2619 Label::Distance near_jump) {
2620 // Check that both objects are not smis.
2621 Condition either_smi = CheckEitherSmi(first_object, second_object);
2622 j(either_smi, on_fail, near_jump);
2624 // Load instance type for both strings.
2625 movp(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
2626 movp(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
2627 movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2628 movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2630 // Check that both are flat ASCII strings.
2631 DCHECK(kNotStringTag != 0);
2632 const int kFlatAsciiStringMask =
2633 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2634 const int kFlatAsciiStringTag =
2635 kStringTag | kOneByteStringTag | kSeqStringTag;
2637 andl(scratch1, Immediate(kFlatAsciiStringMask));
2638 andl(scratch2, Immediate(kFlatAsciiStringMask));
2639 // Interleave the bits to check both scratch1 and scratch2 in one test.
2640 DCHECK_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2641 leap(scratch1, Operand(scratch1, scratch2, times_8, 0));
2643 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
2644 j(not_equal, on_fail, near_jump);
2648 void MacroAssembler::JumpIfInstanceTypeIsNotSequentialAscii(
2649 Register instance_type,
2652 Label::Distance near_jump) {
2653 if (!scratch.is(instance_type)) {
2654 movl(scratch, instance_type);
2657 const int kFlatAsciiStringMask =
2658 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2660 andl(scratch, Immediate(kFlatAsciiStringMask));
2661 cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kOneByteStringTag));
2662 j(not_equal, failure, near_jump);
2666 void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialAscii(
2667 Register first_object_instance_type,
2668 Register second_object_instance_type,
2672 Label::Distance near_jump) {
2673 // Load instance type for both strings.
2674 movp(scratch1, first_object_instance_type);
2675 movp(scratch2, second_object_instance_type);
2677 // Check that both are flat ASCII strings.
2678 DCHECK(kNotStringTag != 0);
2679 const int kFlatAsciiStringMask =
2680 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2681 const int kFlatAsciiStringTag =
2682 kStringTag | kOneByteStringTag | kSeqStringTag;
2684 andl(scratch1, Immediate(kFlatAsciiStringMask));
2685 andl(scratch2, Immediate(kFlatAsciiStringMask));
2686 // Interleave the bits to check both scratch1 and scratch2 in one test.
2687 DCHECK_EQ(0, kFlatAsciiStringMask & (kFlatAsciiStringMask << 3));
2688 leap(scratch1, Operand(scratch1, scratch2, times_8, 0));
2690 Immediate(kFlatAsciiStringTag + (kFlatAsciiStringTag << 3)));
2691 j(not_equal, on_fail, near_jump);
2696 static void JumpIfNotUniqueNameHelper(MacroAssembler* masm,
2697 T operand_or_register,
2698 Label* not_unique_name,
2699 Label::Distance distance) {
2700 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2702 masm->testb(operand_or_register,
2703 Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2704 masm->j(zero, &succeed, Label::kNear);
2705 masm->cmpb(operand_or_register, Immediate(static_cast<uint8_t>(SYMBOL_TYPE)));
2706 masm->j(not_equal, not_unique_name, distance);
2708 masm->bind(&succeed);
2712 void MacroAssembler::JumpIfNotUniqueName(Operand operand,
2713 Label* not_unique_name,
2714 Label::Distance distance) {
2715 JumpIfNotUniqueNameHelper<Operand>(this, operand, not_unique_name, distance);
2719 void MacroAssembler::JumpIfNotUniqueName(Register reg,
2720 Label* not_unique_name,
2721 Label::Distance distance) {
2722 JumpIfNotUniqueNameHelper<Register>(this, reg, not_unique_name, distance);
2726 void MacroAssembler::Move(Register dst, Register src) {
2733 void MacroAssembler::Move(Register dst, Handle<Object> source) {
2734 AllowDeferredHandleDereference smi_check;
2735 if (source->IsSmi()) {
2736 Move(dst, Smi::cast(*source));
2738 MoveHeapObject(dst, source);
2743 void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
2744 AllowDeferredHandleDereference smi_check;
2745 if (source->IsSmi()) {
2746 Move(dst, Smi::cast(*source));
2748 MoveHeapObject(kScratchRegister, source);
2749 movp(dst, kScratchRegister);
2754 void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
2755 AllowDeferredHandleDereference smi_check;
2756 if (source->IsSmi()) {
2757 Cmp(dst, Smi::cast(*source));
2759 MoveHeapObject(kScratchRegister, source);
2760 cmpp(dst, kScratchRegister);
2765 void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
2766 AllowDeferredHandleDereference smi_check;
2767 if (source->IsSmi()) {
2768 Cmp(dst, Smi::cast(*source));
2770 MoveHeapObject(kScratchRegister, source);
2771 cmpp(dst, kScratchRegister);
2776 void MacroAssembler::Push(Handle<Object> source) {
2777 AllowDeferredHandleDereference smi_check;
2778 if (source->IsSmi()) {
2779 Push(Smi::cast(*source));
2781 MoveHeapObject(kScratchRegister, source);
2782 Push(kScratchRegister);
2787 void MacroAssembler::MoveHeapObject(Register result,
2788 Handle<Object> object) {
2789 AllowDeferredHandleDereference using_raw_address;
2790 DCHECK(object->IsHeapObject());
2791 if (isolate()->heap()->InNewSpace(*object)) {
2792 Handle<Cell> cell = isolate()->factory()->NewCell(object);
2793 Move(result, cell, RelocInfo::CELL);
2794 movp(result, Operand(result, 0));
2796 Move(result, object, RelocInfo::EMBEDDED_OBJECT);
2801 void MacroAssembler::LoadGlobalCell(Register dst, Handle<Cell> cell) {
2803 AllowDeferredHandleDereference embedding_raw_address;
2804 load_rax(cell.location(), RelocInfo::CELL);
2806 Move(dst, cell, RelocInfo::CELL);
2807 movp(dst, Operand(dst, 0));
2812 void MacroAssembler::Drop(int stack_elements) {
2813 if (stack_elements > 0) {
2814 addp(rsp, Immediate(stack_elements * kPointerSize));
2819 void MacroAssembler::DropUnderReturnAddress(int stack_elements,
2821 DCHECK(stack_elements > 0);
2822 if (kPointerSize == kInt64Size && stack_elements == 1) {
2823 popq(MemOperand(rsp, 0));
2827 PopReturnAddressTo(scratch);
2828 Drop(stack_elements);
2829 PushReturnAddressFrom(scratch);
2833 void MacroAssembler::Push(Register src) {
2834 if (kPointerSize == kInt64Size) {
2837 // x32 uses 64-bit push for rbp in the prologue.
2838 DCHECK(src.code() != rbp.code());
2839 leal(rsp, Operand(rsp, -4));
2840 movp(Operand(rsp, 0), src);
2845 void MacroAssembler::Push(const Operand& src) {
2846 if (kPointerSize == kInt64Size) {
2849 movp(kScratchRegister, src);
2850 leal(rsp, Operand(rsp, -4));
2851 movp(Operand(rsp, 0), kScratchRegister);
2856 void MacroAssembler::PushQuad(const Operand& src) {
2857 if (kPointerSize == kInt64Size) {
2860 movp(kScratchRegister, src);
2861 pushq(kScratchRegister);
2866 void MacroAssembler::Push(Immediate value) {
2867 if (kPointerSize == kInt64Size) {
2870 leal(rsp, Operand(rsp, -4));
2871 movp(Operand(rsp, 0), value);
2876 void MacroAssembler::PushImm32(int32_t imm32) {
2877 if (kPointerSize == kInt64Size) {
2880 leal(rsp, Operand(rsp, -4));
2881 movp(Operand(rsp, 0), Immediate(imm32));
2886 void MacroAssembler::Pop(Register dst) {
2887 if (kPointerSize == kInt64Size) {
2890 // x32 uses 64-bit pop for rbp in the epilogue.
2891 DCHECK(dst.code() != rbp.code());
2892 movp(dst, Operand(rsp, 0));
2893 leal(rsp, Operand(rsp, 4));
2898 void MacroAssembler::Pop(const Operand& dst) {
2899 if (kPointerSize == kInt64Size) {
2902 Register scratch = dst.AddressUsesRegister(kScratchRegister)
2903 ? kSmiConstantRegister : kScratchRegister;
2904 movp(scratch, Operand(rsp, 0));
2906 leal(rsp, Operand(rsp, 4));
2907 if (scratch.is(kSmiConstantRegister)) {
2908 // Restore kSmiConstantRegister.
2909 movp(kSmiConstantRegister,
2910 reinterpret_cast<void*>(Smi::FromInt(kSmiConstantRegisterValue)),
2911 Assembler::RelocInfoNone());
2917 void MacroAssembler::PopQuad(const Operand& dst) {
2918 if (kPointerSize == kInt64Size) {
2921 popq(kScratchRegister);
2922 movp(dst, kScratchRegister);
2927 void MacroAssembler::LoadSharedFunctionInfoSpecialField(Register dst,
2930 DCHECK(offset > SharedFunctionInfo::kLengthOffset &&
2931 offset <= SharedFunctionInfo::kSize &&
2932 (((offset - SharedFunctionInfo::kLengthOffset) / kIntSize) % 2 == 1));
2933 if (kPointerSize == kInt64Size) {
2934 movsxlq(dst, FieldOperand(base, offset));
2936 movp(dst, FieldOperand(base, offset));
2937 SmiToInteger32(dst, dst);
2942 void MacroAssembler::TestBitSharedFunctionInfoSpecialField(Register base,
2945 DCHECK(offset > SharedFunctionInfo::kLengthOffset &&
2946 offset <= SharedFunctionInfo::kSize &&
2947 (((offset - SharedFunctionInfo::kLengthOffset) / kIntSize) % 2 == 1));
2948 if (kPointerSize == kInt32Size) {
2949 // On x32, this field is represented by SMI.
2952 int byte_offset = bits / kBitsPerByte;
2953 int bit_in_byte = bits & (kBitsPerByte - 1);
2954 testb(FieldOperand(base, offset + byte_offset), Immediate(1 << bit_in_byte));
2958 void MacroAssembler::Jump(ExternalReference ext) {
2959 LoadAddress(kScratchRegister, ext);
2960 jmp(kScratchRegister);
2964 void MacroAssembler::Jump(const Operand& op) {
2965 if (kPointerSize == kInt64Size) {
2968 movp(kScratchRegister, op);
2969 jmp(kScratchRegister);
2974 void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
2975 Move(kScratchRegister, destination, rmode);
2976 jmp(kScratchRegister);
2980 void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
2981 // TODO(X64): Inline this
2982 jmp(code_object, rmode);
2986 int MacroAssembler::CallSize(ExternalReference ext) {
2987 // Opcode for call kScratchRegister is: Rex.B FF D4 (three bytes).
2988 return LoadAddressSize(ext) +
2989 Assembler::kCallScratchRegisterInstructionLength;
2993 void MacroAssembler::Call(ExternalReference ext) {
2995 int end_position = pc_offset() + CallSize(ext);
2997 LoadAddress(kScratchRegister, ext);
2998 call(kScratchRegister);
3000 CHECK_EQ(end_position, pc_offset());
3005 void MacroAssembler::Call(const Operand& op) {
3006 if (kPointerSize == kInt64Size) {
3009 movp(kScratchRegister, op);
3010 call(kScratchRegister);
3015 void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
3017 int end_position = pc_offset() + CallSize(destination);
3019 Move(kScratchRegister, destination, rmode);
3020 call(kScratchRegister);
3022 CHECK_EQ(pc_offset(), end_position);
3027 void MacroAssembler::Call(Handle<Code> code_object,
3028 RelocInfo::Mode rmode,
3029 TypeFeedbackId ast_id) {
3031 int end_position = pc_offset() + CallSize(code_object);
3033 DCHECK(RelocInfo::IsCodeTarget(rmode) ||
3034 rmode == RelocInfo::CODE_AGE_SEQUENCE);
3035 call(code_object, rmode, ast_id);
3037 CHECK_EQ(end_position, pc_offset());
3042 void MacroAssembler::Pushad() {
3047 // Not pushing rsp or rbp.
3052 // r10 is kScratchRegister.
3054 // r12 is kSmiConstantRegister.
3055 // r13 is kRootRegister.
3058 STATIC_ASSERT(11 == kNumSafepointSavedRegisters);
3059 // Use lea for symmetry with Popad.
3061 (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
3062 leap(rsp, Operand(rsp, -sp_delta));
3066 void MacroAssembler::Popad() {
3067 // Popad must not change the flags, so use lea instead of addq.
3069 (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
3070 leap(rsp, Operand(rsp, sp_delta));
3085 void MacroAssembler::Dropad() {
3086 addp(rsp, Immediate(kNumSafepointRegisters * kPointerSize));
3090 // Order general registers are pushed by Pushad:
3091 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r14, r15.
3093 MacroAssembler::kSafepointPushRegisterIndices[Register::kNumRegisters] = {
3113 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst,
3114 const Immediate& imm) {
3115 movp(SafepointRegisterSlot(dst), imm);
3119 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
3120 movp(SafepointRegisterSlot(dst), src);
3124 void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
3125 movp(dst, SafepointRegisterSlot(src));
3129 Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
3130 return Operand(rsp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
3134 void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
3135 int handler_index) {
3136 // Adjust this code if not the case.
3137 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3139 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3140 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3141 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3142 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3143 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3145 // We will build up the handler from the bottom by pushing on the stack.
3146 // First push the frame pointer and context.
3147 if (kind == StackHandler::JS_ENTRY) {
3148 // The frame pointer does not point to a JS frame so we save NULL for
3149 // rbp. We expect the code throwing an exception to check rbp before
3150 // dereferencing it to restore the context.
3151 pushq(Immediate(0)); // NULL frame pointer.
3152 Push(Smi::FromInt(0)); // No context.
3158 // Push the state and the code object.
3160 StackHandler::IndexField::encode(handler_index) |
3161 StackHandler::KindField::encode(kind);
3162 Push(Immediate(state));
3165 // Link the current handler as the next handler.
3166 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3167 Push(ExternalOperand(handler_address));
3168 // Set this new handler as the current one.
3169 movp(ExternalOperand(handler_address), rsp);
3173 void MacroAssembler::PopTryHandler() {
3174 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3175 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3176 Pop(ExternalOperand(handler_address));
3177 addp(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
3181 void MacroAssembler::JumpToHandlerEntry() {
3182 // Compute the handler entry address and jump to it. The handler table is
3183 // a fixed array of (smi-tagged) code offsets.
3184 // rax = exception, rdi = code object, rdx = state.
3185 movp(rbx, FieldOperand(rdi, Code::kHandlerTableOffset));
3186 shrp(rdx, Immediate(StackHandler::kKindWidth));
3188 FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize));
3189 SmiToInteger64(rdx, rdx);
3190 leap(rdi, FieldOperand(rdi, rdx, times_1, Code::kHeaderSize));
3195 void MacroAssembler::Throw(Register value) {
3196 // Adjust this code if not the case.
3197 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3199 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3200 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3201 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3202 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3203 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3205 // The exception is expected in rax.
3206 if (!value.is(rax)) {
3209 // Drop the stack pointer to the top of the top handler.
3210 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3211 movp(rsp, ExternalOperand(handler_address));
3212 // Restore the next handler.
3213 Pop(ExternalOperand(handler_address));
3215 // Remove the code object and state, compute the handler address in rdi.
3216 Pop(rdi); // Code object.
3217 Pop(rdx); // Offset and state.
3219 // Restore the context and frame pointer.
3220 Pop(rsi); // Context.
3221 popq(rbp); // Frame pointer.
3223 // If the handler is a JS frame, restore the context to the frame.
3224 // (kind == ENTRY) == (rbp == 0) == (rsi == 0), so we could test either
3228 j(zero, &skip, Label::kNear);
3229 movp(Operand(rbp, StandardFrameConstants::kContextOffset), rsi);
3232 JumpToHandlerEntry();
3236 void MacroAssembler::ThrowUncatchable(Register value) {
3237 // Adjust this code if not the case.
3238 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3240 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3241 STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3242 STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3243 STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3244 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3246 // The exception is expected in rax.
3247 if (!value.is(rax)) {
3250 // Drop the stack pointer to the top of the top stack handler.
3251 ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3252 Load(rsp, handler_address);
3254 // Unwind the handlers until the top ENTRY handler is found.
3255 Label fetch_next, check_kind;
3256 jmp(&check_kind, Label::kNear);
3258 movp(rsp, Operand(rsp, StackHandlerConstants::kNextOffset));
3261 STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
3262 testl(Operand(rsp, StackHandlerConstants::kStateOffset),
3263 Immediate(StackHandler::KindField::kMask));
3264 j(not_zero, &fetch_next);
3266 // Set the top handler address to next handler past the top ENTRY handler.
3267 Pop(ExternalOperand(handler_address));
3269 // Remove the code object and state, compute the handler address in rdi.
3270 Pop(rdi); // Code object.
3271 Pop(rdx); // Offset and state.
3273 // Clear the context pointer and frame pointer (0 was saved in the handler).
3277 JumpToHandlerEntry();
3281 void MacroAssembler::Ret() {
3286 void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
3287 if (is_uint16(bytes_dropped)) {
3290 PopReturnAddressTo(scratch);
3291 addp(rsp, Immediate(bytes_dropped));
3292 PushReturnAddressFrom(scratch);
3298 void MacroAssembler::FCmp() {
3304 void MacroAssembler::CmpObjectType(Register heap_object,
3307 movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3308 CmpInstanceType(map, type);
3312 void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
3313 cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
3314 Immediate(static_cast<int8_t>(type)));
3318 void MacroAssembler::CheckFastElements(Register map,
3320 Label::Distance distance) {
3321 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3322 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3323 STATIC_ASSERT(FAST_ELEMENTS == 2);
3324 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3325 cmpb(FieldOperand(map, Map::kBitField2Offset),
3326 Immediate(Map::kMaximumBitField2FastHoleyElementValue));
3327 j(above, fail, distance);
3331 void MacroAssembler::CheckFastObjectElements(Register map,
3333 Label::Distance distance) {
3334 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3335 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3336 STATIC_ASSERT(FAST_ELEMENTS == 2);
3337 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3338 cmpb(FieldOperand(map, Map::kBitField2Offset),
3339 Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
3340 j(below_equal, fail, distance);
3341 cmpb(FieldOperand(map, Map::kBitField2Offset),
3342 Immediate(Map::kMaximumBitField2FastHoleyElementValue));
3343 j(above, fail, distance);
3347 void MacroAssembler::CheckFastSmiElements(Register map,
3349 Label::Distance distance) {
3350 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3351 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3352 cmpb(FieldOperand(map, Map::kBitField2Offset),
3353 Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
3354 j(above, fail, distance);
3358 void MacroAssembler::StoreNumberToDoubleElements(
3359 Register maybe_number,
3362 XMMRegister xmm_scratch,
3364 int elements_offset) {
3365 Label smi_value, is_nan, maybe_nan, not_nan, have_double_value, done;
3367 JumpIfSmi(maybe_number, &smi_value, Label::kNear);
3369 CheckMap(maybe_number,
3370 isolate()->factory()->heap_number_map(),
3374 // Double value, canonicalize NaN.
3375 uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
3376 cmpl(FieldOperand(maybe_number, offset),
3377 Immediate(kNaNOrInfinityLowerBoundUpper32));
3378 j(greater_equal, &maybe_nan, Label::kNear);
3381 movsd(xmm_scratch, FieldOperand(maybe_number, HeapNumber::kValueOffset));
3382 bind(&have_double_value);
3383 movsd(FieldOperand(elements, index, times_8,
3384 FixedDoubleArray::kHeaderSize - elements_offset),
3389 // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
3390 // it's an Infinity, and the non-NaN code path applies.
3391 j(greater, &is_nan, Label::kNear);
3392 cmpl(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
3395 // Convert all NaNs to the same canonical NaN value when they are stored in
3396 // the double array.
3397 Set(kScratchRegister, BitCast<uint64_t>(
3398 FixedDoubleArray::canonical_not_the_hole_nan_as_double()));
3399 movq(xmm_scratch, kScratchRegister);
3400 jmp(&have_double_value, Label::kNear);
3403 // Value is a smi. convert to a double and store.
3404 // Preserve original value.
3405 SmiToInteger32(kScratchRegister, maybe_number);
3406 Cvtlsi2sd(xmm_scratch, kScratchRegister);
3407 movsd(FieldOperand(elements, index, times_8,
3408 FixedDoubleArray::kHeaderSize - elements_offset),
3414 void MacroAssembler::CompareMap(Register obj, Handle<Map> map) {
3415 Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
3419 void MacroAssembler::CheckMap(Register obj,
3422 SmiCheckType smi_check_type) {
3423 if (smi_check_type == DO_SMI_CHECK) {
3424 JumpIfSmi(obj, fail);
3427 CompareMap(obj, map);
3432 void MacroAssembler::ClampUint8(Register reg) {
3434 testl(reg, Immediate(0xFFFFFF00));
3435 j(zero, &done, Label::kNear);
3436 setcc(negative, reg); // 1 if negative, 0 if positive.
3437 decb(reg); // 0 if negative, 255 if positive.
3442 void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
3443 XMMRegister temp_xmm_reg,
3444 Register result_reg) {
3447 xorps(temp_xmm_reg, temp_xmm_reg);
3448 cvtsd2si(result_reg, input_reg);
3449 testl(result_reg, Immediate(0xFFFFFF00));
3450 j(zero, &done, Label::kNear);
3451 cmpl(result_reg, Immediate(1));
3452 j(overflow, &conv_failure, Label::kNear);
3453 movl(result_reg, Immediate(0));
3454 setcc(sign, result_reg);
3455 subl(result_reg, Immediate(1));
3456 andl(result_reg, Immediate(255));
3457 jmp(&done, Label::kNear);
3458 bind(&conv_failure);
3460 ucomisd(input_reg, temp_xmm_reg);
3461 j(below, &done, Label::kNear);
3462 Set(result_reg, 255);
3467 void MacroAssembler::LoadUint32(XMMRegister dst,
3469 if (FLAG_debug_code) {
3470 cmpq(src, Immediate(0xffffffff));
3471 Assert(below_equal, kInputGPRIsExpectedToHaveUpper32Cleared);
3473 cvtqsi2sd(dst, src);
3477 void MacroAssembler::SlowTruncateToI(Register result_reg,
3480 DoubleToIStub stub(isolate(), input_reg, result_reg, offset, true);
3481 call(stub.GetCode(), RelocInfo::CODE_TARGET);
3485 void MacroAssembler::TruncateHeapNumberToI(Register result_reg,
3486 Register input_reg) {
3488 movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
3489 cvttsd2siq(result_reg, xmm0);
3490 cmpq(result_reg, Immediate(1));
3491 j(no_overflow, &done, Label::kNear);
3494 if (input_reg.is(result_reg)) {
3495 subp(rsp, Immediate(kDoubleSize));
3496 movsd(MemOperand(rsp, 0), xmm0);
3497 SlowTruncateToI(result_reg, rsp, 0);
3498 addp(rsp, Immediate(kDoubleSize));
3500 SlowTruncateToI(result_reg, input_reg);
3504 // Keep our invariant that the upper 32 bits are zero.
3505 movl(result_reg, result_reg);
3509 void MacroAssembler::TruncateDoubleToI(Register result_reg,
3510 XMMRegister input_reg) {
3512 cvttsd2siq(result_reg, input_reg);
3513 cmpq(result_reg, Immediate(1));
3514 j(no_overflow, &done, Label::kNear);
3516 subp(rsp, Immediate(kDoubleSize));
3517 movsd(MemOperand(rsp, 0), input_reg);
3518 SlowTruncateToI(result_reg, rsp, 0);
3519 addp(rsp, Immediate(kDoubleSize));
3522 // Keep our invariant that the upper 32 bits are zero.
3523 movl(result_reg, result_reg);
3527 void MacroAssembler::DoubleToI(Register result_reg,
3528 XMMRegister input_reg,
3529 XMMRegister scratch,
3530 MinusZeroMode minus_zero_mode,
3531 Label* conversion_failed,
3532 Label::Distance dst) {
3533 cvttsd2si(result_reg, input_reg);
3534 Cvtlsi2sd(xmm0, result_reg);
3535 ucomisd(xmm0, input_reg);
3536 j(not_equal, conversion_failed, dst);
3537 j(parity_even, conversion_failed, dst); // NaN.
3538 if (minus_zero_mode == FAIL_ON_MINUS_ZERO) {
3540 // The integer converted back is equal to the original. We
3541 // only have to test if we got -0 as an input.
3542 testl(result_reg, result_reg);
3543 j(not_zero, &done, Label::kNear);
3544 movmskpd(result_reg, input_reg);
3545 // Bit 0 contains the sign of the double in input_reg.
3546 // If input was positive, we are ok and return 0, otherwise
3547 // jump to conversion_failed.
3548 andl(result_reg, Immediate(1));
3549 j(not_zero, conversion_failed, dst);
3555 void MacroAssembler::TaggedToI(Register result_reg,
3558 MinusZeroMode minus_zero_mode,
3559 Label* lost_precision,
3560 Label::Distance dst) {
3562 DCHECK(!temp.is(xmm0));
3564 // Heap number map check.
3565 CompareRoot(FieldOperand(input_reg, HeapObject::kMapOffset),
3566 Heap::kHeapNumberMapRootIndex);
3567 j(not_equal, lost_precision, dst);
3569 movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
3570 cvttsd2si(result_reg, xmm0);
3571 Cvtlsi2sd(temp, result_reg);
3572 ucomisd(xmm0, temp);
3573 RecordComment("Deferred TaggedToI: lost precision");
3574 j(not_equal, lost_precision, dst);
3575 RecordComment("Deferred TaggedToI: NaN");
3576 j(parity_even, lost_precision, dst); // NaN.
3577 if (minus_zero_mode == FAIL_ON_MINUS_ZERO) {
3578 testl(result_reg, result_reg);
3579 j(not_zero, &done, Label::kNear);
3580 movmskpd(result_reg, xmm0);
3581 andl(result_reg, Immediate(1));
3582 j(not_zero, lost_precision, dst);
3588 void MacroAssembler::LoadInstanceDescriptors(Register map,
3589 Register descriptors) {
3590 movp(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
3594 void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
3595 movl(dst, FieldOperand(map, Map::kBitField3Offset));
3596 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
3600 void MacroAssembler::EnumLength(Register dst, Register map) {
3601 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3602 movl(dst, FieldOperand(map, Map::kBitField3Offset));
3603 andl(dst, Immediate(Map::EnumLengthBits::kMask));
3604 Integer32ToSmi(dst, dst);
3608 void MacroAssembler::DispatchMap(Register obj,
3611 Handle<Code> success,
3612 SmiCheckType smi_check_type) {
3614 if (smi_check_type == DO_SMI_CHECK) {
3615 JumpIfSmi(obj, &fail);
3617 Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
3618 j(equal, success, RelocInfo::CODE_TARGET);
3624 void MacroAssembler::AssertNumber(Register object) {
3625 if (emit_debug_code()) {
3627 Condition is_smi = CheckSmi(object);
3628 j(is_smi, &ok, Label::kNear);
3629 Cmp(FieldOperand(object, HeapObject::kMapOffset),
3630 isolate()->factory()->heap_number_map());
3631 Check(equal, kOperandIsNotANumber);
3637 void MacroAssembler::AssertNotSmi(Register object) {
3638 if (emit_debug_code()) {
3639 Condition is_smi = CheckSmi(object);
3640 Check(NegateCondition(is_smi), kOperandIsASmi);
3645 void MacroAssembler::AssertSmi(Register object) {
3646 if (emit_debug_code()) {
3647 Condition is_smi = CheckSmi(object);
3648 Check(is_smi, kOperandIsNotASmi);
3653 void MacroAssembler::AssertSmi(const Operand& object) {
3654 if (emit_debug_code()) {
3655 Condition is_smi = CheckSmi(object);
3656 Check(is_smi, kOperandIsNotASmi);
3661 void MacroAssembler::AssertZeroExtended(Register int32_register) {
3662 if (emit_debug_code()) {
3663 DCHECK(!int32_register.is(kScratchRegister));
3664 movq(kScratchRegister, V8_INT64_C(0x0000000100000000));
3665 cmpq(kScratchRegister, int32_register);
3666 Check(above_equal, k32BitValueInRegisterIsNotZeroExtended);
3671 void MacroAssembler::AssertString(Register object) {
3672 if (emit_debug_code()) {
3673 testb(object, Immediate(kSmiTagMask));
3674 Check(not_equal, kOperandIsASmiAndNotAString);
3676 movp(object, FieldOperand(object, HeapObject::kMapOffset));
3677 CmpInstanceType(object, FIRST_NONSTRING_TYPE);
3679 Check(below, kOperandIsNotAString);
3684 void MacroAssembler::AssertName(Register object) {
3685 if (emit_debug_code()) {
3686 testb(object, Immediate(kSmiTagMask));
3687 Check(not_equal, kOperandIsASmiAndNotAName);
3689 movp(object, FieldOperand(object, HeapObject::kMapOffset));
3690 CmpInstanceType(object, LAST_NAME_TYPE);
3692 Check(below_equal, kOperandIsNotAName);
3697 void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
3698 if (emit_debug_code()) {
3699 Label done_checking;
3700 AssertNotSmi(object);
3701 Cmp(object, isolate()->factory()->undefined_value());
3702 j(equal, &done_checking);
3703 Cmp(FieldOperand(object, 0), isolate()->factory()->allocation_site_map());
3704 Assert(equal, kExpectedUndefinedOrCell);
3705 bind(&done_checking);
3710 void MacroAssembler::AssertRootValue(Register src,
3711 Heap::RootListIndex root_value_index,
3712 BailoutReason reason) {
3713 if (emit_debug_code()) {
3714 DCHECK(!src.is(kScratchRegister));
3715 LoadRoot(kScratchRegister, root_value_index);
3716 cmpp(src, kScratchRegister);
3717 Check(equal, reason);
3723 Condition MacroAssembler::IsObjectStringType(Register heap_object,
3725 Register instance_type) {
3726 movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3727 movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3728 STATIC_ASSERT(kNotStringTag != 0);
3729 testb(instance_type, Immediate(kIsNotStringMask));
3734 Condition MacroAssembler::IsObjectNameType(Register heap_object,
3736 Register instance_type) {
3737 movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3738 movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3739 cmpb(instance_type, Immediate(static_cast<uint8_t>(LAST_NAME_TYPE)));
3744 void MacroAssembler::TryGetFunctionPrototype(Register function,
3747 bool miss_on_bound_function) {
3749 if (miss_on_bound_function) {
3750 // Check that the receiver isn't a smi.
3751 testl(function, Immediate(kSmiTagMask));
3754 // Check that the function really is a function.
3755 CmpObjectType(function, JS_FUNCTION_TYPE, result);
3758 movp(kScratchRegister,
3759 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3760 // It's not smi-tagged (stored in the top half of a smi-tagged 8-byte
3762 TestBitSharedFunctionInfoSpecialField(kScratchRegister,
3763 SharedFunctionInfo::kCompilerHintsOffset,
3764 SharedFunctionInfo::kBoundFunction);
3767 // Make sure that the function has an instance prototype.
3768 testb(FieldOperand(result, Map::kBitFieldOffset),
3769 Immediate(1 << Map::kHasNonInstancePrototype));
3770 j(not_zero, &non_instance, Label::kNear);
3773 // Get the prototype or initial map from the function.
3775 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3777 // If the prototype or initial map is the hole, don't return it and
3778 // simply miss the cache instead. This will allow us to allocate a
3779 // prototype object on-demand in the runtime system.
3780 CompareRoot(result, Heap::kTheHoleValueRootIndex);
3783 // If the function does not have an initial map, we're done.
3785 CmpObjectType(result, MAP_TYPE, kScratchRegister);
3786 j(not_equal, &done, Label::kNear);
3788 // Get the prototype from the initial map.
3789 movp(result, FieldOperand(result, Map::kPrototypeOffset));
3791 if (miss_on_bound_function) {
3792 jmp(&done, Label::kNear);
3794 // Non-instance prototype: Fetch prototype from constructor field
3796 bind(&non_instance);
3797 movp(result, FieldOperand(result, Map::kConstructorOffset));
3805 void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
3806 if (FLAG_native_code_counters && counter->Enabled()) {
3807 Operand counter_operand = ExternalOperand(ExternalReference(counter));
3808 movl(counter_operand, Immediate(value));
3813 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
3815 if (FLAG_native_code_counters && counter->Enabled()) {
3816 Operand counter_operand = ExternalOperand(ExternalReference(counter));
3818 incl(counter_operand);
3820 addl(counter_operand, Immediate(value));
3826 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
3828 if (FLAG_native_code_counters && counter->Enabled()) {
3829 Operand counter_operand = ExternalOperand(ExternalReference(counter));
3831 decl(counter_operand);
3833 subl(counter_operand, Immediate(value));
3839 void MacroAssembler::DebugBreak() {
3840 Set(rax, 0); // No arguments.
3841 LoadAddress(rbx, ExternalReference(Runtime::kDebugBreak, isolate()));
3842 CEntryStub ces(isolate(), 1);
3843 DCHECK(AllowThisStubCall(&ces));
3844 Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
3848 void MacroAssembler::InvokeCode(Register code,
3849 const ParameterCount& expected,
3850 const ParameterCount& actual,
3852 const CallWrapper& call_wrapper) {
3853 // You can't call a function without a valid frame.
3854 DCHECK(flag == JUMP_FUNCTION || has_frame());
3857 bool definitely_mismatches = false;
3858 InvokePrologue(expected,
3860 Handle<Code>::null(),
3863 &definitely_mismatches,
3867 if (!definitely_mismatches) {
3868 if (flag == CALL_FUNCTION) {
3869 call_wrapper.BeforeCall(CallSize(code));
3871 call_wrapper.AfterCall();
3873 DCHECK(flag == JUMP_FUNCTION);
3881 void MacroAssembler::InvokeFunction(Register function,
3882 const ParameterCount& actual,
3884 const CallWrapper& call_wrapper) {
3885 // You can't call a function without a valid frame.
3886 DCHECK(flag == JUMP_FUNCTION || has_frame());
3888 DCHECK(function.is(rdi));
3889 movp(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3890 movp(rsi, FieldOperand(function, JSFunction::kContextOffset));
3891 LoadSharedFunctionInfoSpecialField(rbx, rdx,
3892 SharedFunctionInfo::kFormalParameterCountOffset);
3893 // Advances rdx to the end of the Code object header, to the start of
3894 // the executable code.
3895 movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
3897 ParameterCount expected(rbx);
3898 InvokeCode(rdx, expected, actual, flag, call_wrapper);
3902 void MacroAssembler::InvokeFunction(Register function,
3903 const ParameterCount& expected,
3904 const ParameterCount& actual,
3906 const CallWrapper& call_wrapper) {
3907 // You can't call a function without a valid frame.
3908 DCHECK(flag == JUMP_FUNCTION || has_frame());
3910 DCHECK(function.is(rdi));
3911 movp(rsi, FieldOperand(function, JSFunction::kContextOffset));
3912 // Advances rdx to the end of the Code object header, to the start of
3913 // the executable code.
3914 movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
3916 InvokeCode(rdx, expected, actual, flag, call_wrapper);
3920 void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
3921 const ParameterCount& expected,
3922 const ParameterCount& actual,
3924 const CallWrapper& call_wrapper) {
3925 Move(rdi, function);
3926 InvokeFunction(rdi, expected, actual, flag, call_wrapper);
3930 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
3931 const ParameterCount& actual,
3932 Handle<Code> code_constant,
3933 Register code_register,
3935 bool* definitely_mismatches,
3937 Label::Distance near_jump,
3938 const CallWrapper& call_wrapper) {
3939 bool definitely_matches = false;
3940 *definitely_mismatches = false;
3942 if (expected.is_immediate()) {
3943 DCHECK(actual.is_immediate());
3944 if (expected.immediate() == actual.immediate()) {
3945 definitely_matches = true;
3947 Set(rax, actual.immediate());
3948 if (expected.immediate() ==
3949 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
3950 // Don't worry about adapting arguments for built-ins that
3951 // don't want that done. Skip adaption code by making it look
3952 // like we have a match between expected and actual number of
3954 definitely_matches = true;
3956 *definitely_mismatches = true;
3957 Set(rbx, expected.immediate());
3961 if (actual.is_immediate()) {
3962 // Expected is in register, actual is immediate. This is the
3963 // case when we invoke function values without going through the
3965 cmpp(expected.reg(), Immediate(actual.immediate()));
3966 j(equal, &invoke, Label::kNear);
3967 DCHECK(expected.reg().is(rbx));
3968 Set(rax, actual.immediate());
3969 } else if (!expected.reg().is(actual.reg())) {
3970 // Both expected and actual are in (different) registers. This
3971 // is the case when we invoke functions using call and apply.
3972 cmpp(expected.reg(), actual.reg());
3973 j(equal, &invoke, Label::kNear);
3974 DCHECK(actual.reg().is(rax));
3975 DCHECK(expected.reg().is(rbx));
3979 if (!definitely_matches) {
3980 Handle<Code> adaptor = isolate()->builtins()->ArgumentsAdaptorTrampoline();
3981 if (!code_constant.is_null()) {
3982 Move(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
3983 addp(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
3984 } else if (!code_register.is(rdx)) {
3985 movp(rdx, code_register);
3988 if (flag == CALL_FUNCTION) {
3989 call_wrapper.BeforeCall(CallSize(adaptor));
3990 Call(adaptor, RelocInfo::CODE_TARGET);
3991 call_wrapper.AfterCall();
3992 if (!*definitely_mismatches) {
3993 jmp(done, near_jump);
3996 Jump(adaptor, RelocInfo::CODE_TARGET);
4003 void MacroAssembler::StubPrologue() {
4004 pushq(rbp); // Caller's frame pointer.
4006 Push(rsi); // Callee's context.
4007 Push(Smi::FromInt(StackFrame::STUB));
4011 void MacroAssembler::Prologue(bool code_pre_aging) {
4012 PredictableCodeSizeScope predictible_code_size_scope(this,
4013 kNoCodeAgeSequenceLength);
4014 if (code_pre_aging) {
4015 // Pre-age the code.
4016 Call(isolate()->builtins()->MarkCodeAsExecutedOnce(),
4017 RelocInfo::CODE_AGE_SEQUENCE);
4018 Nop(kNoCodeAgeSequenceLength - Assembler::kShortCallInstructionLength);
4020 pushq(rbp); // Caller's frame pointer.
4022 Push(rsi); // Callee's context.
4023 Push(rdi); // Callee's JS function.
4028 void MacroAssembler::EnterFrame(StackFrame::Type type) {
4031 Push(rsi); // Context.
4032 Push(Smi::FromInt(type));
4033 Move(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
4034 Push(kScratchRegister);
4035 if (emit_debug_code()) {
4036 Move(kScratchRegister,
4037 isolate()->factory()->undefined_value(),
4038 RelocInfo::EMBEDDED_OBJECT);
4039 cmpp(Operand(rsp, 0), kScratchRegister);
4040 Check(not_equal, kCodeObjectNotProperlyPatched);
4045 void MacroAssembler::LeaveFrame(StackFrame::Type type) {
4046 if (emit_debug_code()) {
4047 Move(kScratchRegister, Smi::FromInt(type));
4048 cmpp(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
4049 Check(equal, kStackFrameTypesMustMatch);
4056 void MacroAssembler::EnterExitFramePrologue(bool save_rax) {
4057 // Set up the frame structure on the stack.
4058 // All constants are relative to the frame pointer of the exit frame.
4059 DCHECK(ExitFrameConstants::kCallerSPDisplacement ==
4060 kFPOnStackSize + kPCOnStackSize);
4061 DCHECK(ExitFrameConstants::kCallerPCOffset == kFPOnStackSize);
4062 DCHECK(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
4066 // Reserve room for entry stack pointer and push the code object.
4067 DCHECK(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
4068 Push(Immediate(0)); // Saved entry sp, patched before call.
4069 Move(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
4070 Push(kScratchRegister); // Accessed from EditFrame::code_slot.
4072 // Save the frame pointer and the context in top.
4074 movp(r14, rax); // Backup rax in callee-save register.
4077 Store(ExternalReference(Isolate::kCEntryFPAddress, isolate()), rbp);
4078 Store(ExternalReference(Isolate::kContextAddress, isolate()), rsi);
4082 void MacroAssembler::EnterExitFrameEpilogue(int arg_stack_space,
4083 bool save_doubles) {
4085 const int kShadowSpace = 4;
4086 arg_stack_space += kShadowSpace;
4088 // Optionally save all XMM registers.
4090 int space = XMMRegister::kMaxNumAllocatableRegisters * kDoubleSize +
4091 arg_stack_space * kRegisterSize;
4092 subp(rsp, Immediate(space));
4093 int offset = -2 * kPointerSize;
4094 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
4095 XMMRegister reg = XMMRegister::FromAllocationIndex(i);
4096 movsd(Operand(rbp, offset - ((i + 1) * kDoubleSize)), reg);
4098 } else if (arg_stack_space > 0) {
4099 subp(rsp, Immediate(arg_stack_space * kRegisterSize));
4102 // Get the required frame alignment for the OS.
4103 const int kFrameAlignment = base::OS::ActivationFrameAlignment();
4104 if (kFrameAlignment > 0) {
4105 DCHECK(IsPowerOf2(kFrameAlignment));
4106 DCHECK(is_int8(kFrameAlignment));
4107 andp(rsp, Immediate(-kFrameAlignment));
4110 // Patch the saved entry sp.
4111 movp(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
4115 void MacroAssembler::EnterExitFrame(int arg_stack_space, bool save_doubles) {
4116 EnterExitFramePrologue(true);
4118 // Set up argv in callee-saved register r15. It is reused in LeaveExitFrame,
4119 // so it must be retained across the C-call.
4120 int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
4121 leap(r15, Operand(rbp, r14, times_pointer_size, offset));
4123 EnterExitFrameEpilogue(arg_stack_space, save_doubles);
4127 void MacroAssembler::EnterApiExitFrame(int arg_stack_space) {
4128 EnterExitFramePrologue(false);
4129 EnterExitFrameEpilogue(arg_stack_space, false);
4133 void MacroAssembler::LeaveExitFrame(bool save_doubles) {
4137 int offset = -2 * kPointerSize;
4138 for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
4139 XMMRegister reg = XMMRegister::FromAllocationIndex(i);
4140 movsd(reg, Operand(rbp, offset - ((i + 1) * kDoubleSize)));
4143 // Get the return address from the stack and restore the frame pointer.
4144 movp(rcx, Operand(rbp, kFPOnStackSize));
4145 movp(rbp, Operand(rbp, 0 * kPointerSize));
4147 // Drop everything up to and including the arguments and the receiver
4148 // from the caller stack.
4149 leap(rsp, Operand(r15, 1 * kPointerSize));
4151 PushReturnAddressFrom(rcx);
4153 LeaveExitFrameEpilogue(true);
4157 void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
4161 LeaveExitFrameEpilogue(restore_context);
4165 void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
4166 // Restore current context from top and clear it in debug mode.
4167 ExternalReference context_address(Isolate::kContextAddress, isolate());
4168 Operand context_operand = ExternalOperand(context_address);
4169 if (restore_context) {
4170 movp(rsi, context_operand);
4173 movp(context_operand, Immediate(0));
4176 // Clear the top frame.
4177 ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
4179 Operand c_entry_fp_operand = ExternalOperand(c_entry_fp_address);
4180 movp(c_entry_fp_operand, Immediate(0));
4184 void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
4187 Label same_contexts;
4189 DCHECK(!holder_reg.is(scratch));
4190 DCHECK(!scratch.is(kScratchRegister));
4191 // Load current lexical context from the stack frame.
4192 movp(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
4194 // When generating debug code, make sure the lexical context is set.
4195 if (emit_debug_code()) {
4196 cmpp(scratch, Immediate(0));
4197 Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
4199 // Load the native context of the current context.
4201 Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
4202 movp(scratch, FieldOperand(scratch, offset));
4203 movp(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
4205 // Check the context is a native context.
4206 if (emit_debug_code()) {
4207 Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
4208 isolate()->factory()->native_context_map());
4209 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
4212 // Check if both contexts are the same.
4213 cmpp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4214 j(equal, &same_contexts);
4216 // Compare security tokens.
4217 // Check that the security token in the calling global object is
4218 // compatible with the security token in the receiving global
4221 // Check the context is a native context.
4222 if (emit_debug_code()) {
4223 // Preserve original value of holder_reg.
4226 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4227 CompareRoot(holder_reg, Heap::kNullValueRootIndex);
4228 Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
4230 // Read the first word and compare to native_context_map(),
4231 movp(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
4232 CompareRoot(holder_reg, Heap::kNativeContextMapRootIndex);
4233 Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
4237 movp(kScratchRegister,
4238 FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4240 Context::kHeaderSize + Context::SECURITY_TOKEN_INDEX * kPointerSize;
4241 movp(scratch, FieldOperand(scratch, token_offset));
4242 cmpp(scratch, FieldOperand(kScratchRegister, token_offset));
4245 bind(&same_contexts);
4249 // Compute the hash code from the untagged key. This must be kept in sync with
4250 // ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
4251 // code-stub-hydrogen.cc
4252 void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
4253 // First of all we assign the hash seed to scratch.
4254 LoadRoot(scratch, Heap::kHashSeedRootIndex);
4255 SmiToInteger32(scratch, scratch);
4257 // Xor original key with a seed.
4260 // Compute the hash code from the untagged key. This must be kept in sync
4261 // with ComputeIntegerHash in utils.h.
4263 // hash = ~hash + (hash << 15);
4266 shll(scratch, Immediate(15));
4268 // hash = hash ^ (hash >> 12);
4270 shrl(scratch, Immediate(12));
4272 // hash = hash + (hash << 2);
4273 leal(r0, Operand(r0, r0, times_4, 0));
4274 // hash = hash ^ (hash >> 4);
4276 shrl(scratch, Immediate(4));
4278 // hash = hash * 2057;
4279 imull(r0, r0, Immediate(2057));
4280 // hash = hash ^ (hash >> 16);
4282 shrl(scratch, Immediate(16));
4288 void MacroAssembler::LoadFromNumberDictionary(Label* miss,
4297 // elements - holds the slow-case elements of the receiver on entry.
4298 // Unchanged unless 'result' is the same register.
4300 // key - holds the smi key on entry.
4301 // Unchanged unless 'result' is the same register.
4303 // Scratch registers:
4305 // r0 - holds the untagged key on entry and holds the hash once computed.
4307 // r1 - used to hold the capacity mask of the dictionary
4309 // r2 - used for the index into the dictionary.
4311 // result - holds the result on exit if the load succeeded.
4312 // Allowed to be the same as 'key' or 'result'.
4313 // Unchanged on bailout so 'key' or 'result' can be used
4314 // in further computation.
4318 GetNumberHash(r0, r1);
4320 // Compute capacity mask.
4321 SmiToInteger32(r1, FieldOperand(elements,
4322 SeededNumberDictionary::kCapacityOffset));
4325 // Generate an unrolled loop that performs a few probes before giving up.
4326 for (int i = 0; i < kNumberDictionaryProbes; i++) {
4327 // Use r2 for index calculations and keep the hash intact in r0.
4329 // Compute the masked index: (hash + i + i * i) & mask.
4331 addl(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
4335 // Scale the index by multiplying by the entry size.
4336 DCHECK(SeededNumberDictionary::kEntrySize == 3);
4337 leap(r2, Operand(r2, r2, times_2, 0)); // r2 = r2 * 3
4339 // Check if the key matches.
4340 cmpp(key, FieldOperand(elements,
4343 SeededNumberDictionary::kElementsStartOffset));
4344 if (i != (kNumberDictionaryProbes - 1)) {
4352 // Check that the value is a normal propety.
4353 const int kDetailsOffset =
4354 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
4355 DCHECK_EQ(NORMAL, 0);
4356 Test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
4357 Smi::FromInt(PropertyDetails::TypeField::kMask));
4360 // Get the value at the masked, scaled index.
4361 const int kValueOffset =
4362 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
4363 movp(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
4367 void MacroAssembler::LoadAllocationTopHelper(Register result,
4369 AllocationFlags flags) {
4370 ExternalReference allocation_top =
4371 AllocationUtils::GetAllocationTopReference(isolate(), flags);
4373 // Just return if allocation top is already known.
4374 if ((flags & RESULT_CONTAINS_TOP) != 0) {
4375 // No use of scratch if allocation top is provided.
4376 DCHECK(!scratch.is_valid());
4378 // Assert that result actually contains top on entry.
4379 Operand top_operand = ExternalOperand(allocation_top);
4380 cmpp(result, top_operand);
4381 Check(equal, kUnexpectedAllocationTop);
4386 // Move address of new object to result. Use scratch register if available,
4387 // and keep address in scratch until call to UpdateAllocationTopHelper.
4388 if (scratch.is_valid()) {
4389 LoadAddress(scratch, allocation_top);
4390 movp(result, Operand(scratch, 0));
4392 Load(result, allocation_top);
4397 void MacroAssembler::MakeSureDoubleAlignedHelper(Register result,
4400 AllocationFlags flags) {
4401 if (kPointerSize == kDoubleSize) {
4402 if (FLAG_debug_code) {
4403 testl(result, Immediate(kDoubleAlignmentMask));
4404 Check(zero, kAllocationIsNotDoubleAligned);
4407 // Align the next allocation. Storing the filler map without checking top
4408 // is safe in new-space because the limit of the heap is aligned there.
4409 DCHECK(kPointerSize * 2 == kDoubleSize);
4410 DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
4411 DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
4412 // Make sure scratch is not clobbered by this function as it might be
4413 // used in UpdateAllocationTopHelper later.
4414 DCHECK(!scratch.is(kScratchRegister));
4416 testl(result, Immediate(kDoubleAlignmentMask));
4417 j(zero, &aligned, Label::kNear);
4418 if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
4419 ExternalReference allocation_limit =
4420 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4421 cmpp(result, ExternalOperand(allocation_limit));
4422 j(above_equal, gc_required);
4424 LoadRoot(kScratchRegister, Heap::kOnePointerFillerMapRootIndex);
4425 movp(Operand(result, 0), kScratchRegister);
4426 addp(result, Immediate(kDoubleSize / 2));
4432 void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
4434 AllocationFlags flags) {
4435 if (emit_debug_code()) {
4436 testp(result_end, Immediate(kObjectAlignmentMask));
4437 Check(zero, kUnalignedAllocationInNewSpace);
4440 ExternalReference allocation_top =
4441 AllocationUtils::GetAllocationTopReference(isolate(), flags);
4444 if (scratch.is_valid()) {
4445 // Scratch already contains address of allocation top.
4446 movp(Operand(scratch, 0), result_end);
4448 Store(allocation_top, result_end);
4453 void MacroAssembler::Allocate(int object_size,
4455 Register result_end,
4458 AllocationFlags flags) {
4459 DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
4460 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
4461 if (!FLAG_inline_new) {
4462 if (emit_debug_code()) {
4463 // Trash the registers to simulate an allocation failure.
4464 movl(result, Immediate(0x7091));
4465 if (result_end.is_valid()) {
4466 movl(result_end, Immediate(0x7191));
4468 if (scratch.is_valid()) {
4469 movl(scratch, Immediate(0x7291));
4475 DCHECK(!result.is(result_end));
4477 // Load address of new object into result.
4478 LoadAllocationTopHelper(result, scratch, flags);
4480 if ((flags & DOUBLE_ALIGNMENT) != 0) {
4481 MakeSureDoubleAlignedHelper(result, scratch, gc_required, flags);
4484 // Calculate new top and bail out if new space is exhausted.
4485 ExternalReference allocation_limit =
4486 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4488 Register top_reg = result_end.is_valid() ? result_end : result;
4490 if (!top_reg.is(result)) {
4491 movp(top_reg, result);
4493 addp(top_reg, Immediate(object_size));
4494 j(carry, gc_required);
4495 Operand limit_operand = ExternalOperand(allocation_limit);
4496 cmpp(top_reg, limit_operand);
4497 j(above, gc_required);
4499 // Update allocation top.
4500 UpdateAllocationTopHelper(top_reg, scratch, flags);
4502 bool tag_result = (flags & TAG_OBJECT) != 0;
4503 if (top_reg.is(result)) {
4505 subp(result, Immediate(object_size - kHeapObjectTag));
4507 subp(result, Immediate(object_size));
4509 } else if (tag_result) {
4510 // Tag the result if requested.
4511 DCHECK(kHeapObjectTag == 1);
4517 void MacroAssembler::Allocate(int header_size,
4518 ScaleFactor element_size,
4519 Register element_count,
4521 Register result_end,
4524 AllocationFlags flags) {
4525 DCHECK((flags & SIZE_IN_WORDS) == 0);
4526 leap(result_end, Operand(element_count, element_size, header_size));
4527 Allocate(result_end, result, result_end, scratch, gc_required, flags);
4531 void MacroAssembler::Allocate(Register object_size,
4533 Register result_end,
4536 AllocationFlags flags) {
4537 DCHECK((flags & SIZE_IN_WORDS) == 0);
4538 if (!FLAG_inline_new) {
4539 if (emit_debug_code()) {
4540 // Trash the registers to simulate an allocation failure.
4541 movl(result, Immediate(0x7091));
4542 movl(result_end, Immediate(0x7191));
4543 if (scratch.is_valid()) {
4544 movl(scratch, Immediate(0x7291));
4546 // object_size is left unchanged by this function.
4551 DCHECK(!result.is(result_end));
4553 // Load address of new object into result.
4554 LoadAllocationTopHelper(result, scratch, flags);
4556 if ((flags & DOUBLE_ALIGNMENT) != 0) {
4557 MakeSureDoubleAlignedHelper(result, scratch, gc_required, flags);
4560 // Calculate new top and bail out if new space is exhausted.
4561 ExternalReference allocation_limit =
4562 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4563 if (!object_size.is(result_end)) {
4564 movp(result_end, object_size);
4566 addp(result_end, result);
4567 j(carry, gc_required);
4568 Operand limit_operand = ExternalOperand(allocation_limit);
4569 cmpp(result_end, limit_operand);
4570 j(above, gc_required);
4572 // Update allocation top.
4573 UpdateAllocationTopHelper(result_end, scratch, flags);
4575 // Tag the result if requested.
4576 if ((flags & TAG_OBJECT) != 0) {
4577 addp(result, Immediate(kHeapObjectTag));
4582 void MacroAssembler::UndoAllocationInNewSpace(Register object) {
4583 ExternalReference new_space_allocation_top =
4584 ExternalReference::new_space_allocation_top_address(isolate());
4586 // Make sure the object has no tag before resetting top.
4587 andp(object, Immediate(~kHeapObjectTagMask));
4588 Operand top_operand = ExternalOperand(new_space_allocation_top);
4590 cmpp(object, top_operand);
4591 Check(below, kUndoAllocationOfNonAllocatedMemory);
4593 movp(top_operand, object);
4597 void MacroAssembler::AllocateHeapNumber(Register result,
4601 // Allocate heap number in new space.
4602 Allocate(HeapNumber::kSize, result, scratch, no_reg, gc_required, TAG_OBJECT);
4604 Heap::RootListIndex map_index = mode == MUTABLE
4605 ? Heap::kMutableHeapNumberMapRootIndex
4606 : Heap::kHeapNumberMapRootIndex;
4609 LoadRoot(kScratchRegister, map_index);
4610 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4614 void MacroAssembler::AllocateTwoByteString(Register result,
4619 Label* gc_required) {
4620 // Calculate the number of bytes needed for the characters in the string while
4621 // observing object alignment.
4622 const int kHeaderAlignment = SeqTwoByteString::kHeaderSize &
4623 kObjectAlignmentMask;
4624 DCHECK(kShortSize == 2);
4625 // scratch1 = length * 2 + kObjectAlignmentMask.
4626 leap(scratch1, Operand(length, length, times_1, kObjectAlignmentMask +
4628 andp(scratch1, Immediate(~kObjectAlignmentMask));
4629 if (kHeaderAlignment > 0) {
4630 subp(scratch1, Immediate(kHeaderAlignment));
4633 // Allocate two byte string in new space.
4634 Allocate(SeqTwoByteString::kHeaderSize,
4643 // Set the map, length and hash field.
4644 LoadRoot(kScratchRegister, Heap::kStringMapRootIndex);
4645 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4646 Integer32ToSmi(scratch1, length);
4647 movp(FieldOperand(result, String::kLengthOffset), scratch1);
4648 movp(FieldOperand(result, String::kHashFieldOffset),
4649 Immediate(String::kEmptyHashField));
4653 void MacroAssembler::AllocateAsciiString(Register result,
4658 Label* gc_required) {
4659 // Calculate the number of bytes needed for the characters in the string while
4660 // observing object alignment.
4661 const int kHeaderAlignment = SeqOneByteString::kHeaderSize &
4662 kObjectAlignmentMask;
4663 movl(scratch1, length);
4664 DCHECK(kCharSize == 1);
4665 addp(scratch1, Immediate(kObjectAlignmentMask + kHeaderAlignment));
4666 andp(scratch1, Immediate(~kObjectAlignmentMask));
4667 if (kHeaderAlignment > 0) {
4668 subp(scratch1, Immediate(kHeaderAlignment));
4671 // Allocate ASCII string in new space.
4672 Allocate(SeqOneByteString::kHeaderSize,
4681 // Set the map, length and hash field.
4682 LoadRoot(kScratchRegister, Heap::kAsciiStringMapRootIndex);
4683 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4684 Integer32ToSmi(scratch1, length);
4685 movp(FieldOperand(result, String::kLengthOffset), scratch1);
4686 movp(FieldOperand(result, String::kHashFieldOffset),
4687 Immediate(String::kEmptyHashField));
4691 void MacroAssembler::AllocateTwoByteConsString(Register result,
4694 Label* gc_required) {
4695 // Allocate heap number in new space.
4696 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
4699 // Set the map. The other fields are left uninitialized.
4700 LoadRoot(kScratchRegister, Heap::kConsStringMapRootIndex);
4701 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4705 void MacroAssembler::AllocateAsciiConsString(Register result,
4708 Label* gc_required) {
4709 Allocate(ConsString::kSize,
4716 // Set the map. The other fields are left uninitialized.
4717 LoadRoot(kScratchRegister, Heap::kConsAsciiStringMapRootIndex);
4718 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4722 void MacroAssembler::AllocateTwoByteSlicedString(Register result,
4725 Label* gc_required) {
4726 // Allocate heap number in new space.
4727 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
4730 // Set the map. The other fields are left uninitialized.
4731 LoadRoot(kScratchRegister, Heap::kSlicedStringMapRootIndex);
4732 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4736 void MacroAssembler::AllocateAsciiSlicedString(Register result,
4739 Label* gc_required) {
4740 // Allocate heap number in new space.
4741 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
4744 // Set the map. The other fields are left uninitialized.
4745 LoadRoot(kScratchRegister, Heap::kSlicedAsciiStringMapRootIndex);
4746 movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4750 // Copy memory, byte-by-byte, from source to destination. Not optimized for
4751 // long or aligned copies. The contents of scratch and length are destroyed.
4752 // Destination is incremented by length, source, length and scratch are
4754 // A simpler loop is faster on small copies, but slower on large ones.
4755 // The cld() instruction must have been emitted, to set the direction flag(),
4756 // before calling this function.
4757 void MacroAssembler::CopyBytes(Register destination,
4762 DCHECK(min_length >= 0);
4763 if (emit_debug_code()) {
4764 cmpl(length, Immediate(min_length));
4765 Assert(greater_equal, kInvalidMinLength);
4767 Label short_loop, len8, len16, len24, done, short_string;
4769 const int kLongStringLimit = 4 * kPointerSize;
4770 if (min_length <= kLongStringLimit) {
4771 cmpl(length, Immediate(kPointerSize));
4772 j(below, &short_string, Label::kNear);
4775 DCHECK(source.is(rsi));
4776 DCHECK(destination.is(rdi));
4777 DCHECK(length.is(rcx));
4779 if (min_length <= kLongStringLimit) {
4780 cmpl(length, Immediate(2 * kPointerSize));
4781 j(below_equal, &len8, Label::kNear);
4782 cmpl(length, Immediate(3 * kPointerSize));
4783 j(below_equal, &len16, Label::kNear);
4784 cmpl(length, Immediate(4 * kPointerSize));
4785 j(below_equal, &len24, Label::kNear);
4788 // Because source is 8-byte aligned in our uses of this function,
4789 // we keep source aligned for the rep movs operation by copying the odd bytes
4790 // at the end of the ranges.
4791 movp(scratch, length);
4792 shrl(length, Immediate(kPointerSizeLog2));
4794 // Move remaining bytes of length.
4795 andl(scratch, Immediate(kPointerSize - 1));
4796 movp(length, Operand(source, scratch, times_1, -kPointerSize));
4797 movp(Operand(destination, scratch, times_1, -kPointerSize), length);
4798 addp(destination, scratch);
4800 if (min_length <= kLongStringLimit) {
4801 jmp(&done, Label::kNear);
4803 movp(scratch, Operand(source, 2 * kPointerSize));
4804 movp(Operand(destination, 2 * kPointerSize), scratch);
4806 movp(scratch, Operand(source, kPointerSize));
4807 movp(Operand(destination, kPointerSize), scratch);
4809 movp(scratch, Operand(source, 0));
4810 movp(Operand(destination, 0), scratch);
4811 // Move remaining bytes of length.
4812 movp(scratch, Operand(source, length, times_1, -kPointerSize));
4813 movp(Operand(destination, length, times_1, -kPointerSize), scratch);
4814 addp(destination, length);
4815 jmp(&done, Label::kNear);
4817 bind(&short_string);
4818 if (min_length == 0) {
4819 testl(length, length);
4820 j(zero, &done, Label::kNear);
4824 movb(scratch, Operand(source, 0));
4825 movb(Operand(destination, 0), scratch);
4829 j(not_zero, &short_loop);
4836 void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
4837 Register end_offset,
4842 movp(Operand(start_offset, 0), filler);
4843 addp(start_offset, Immediate(kPointerSize));
4845 cmpp(start_offset, end_offset);
4850 void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
4851 if (context_chain_length > 0) {
4852 // Move up the chain of contexts to the context containing the slot.
4853 movp(dst, Operand(rsi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4854 for (int i = 1; i < context_chain_length; i++) {
4855 movp(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4858 // Slot is in the current function context. Move it into the
4859 // destination register in case we store into it (the write barrier
4860 // cannot be allowed to destroy the context in rsi).
4864 // We should not have found a with context by walking the context
4865 // chain (i.e., the static scope chain and runtime context chain do
4866 // not agree). A variable occurring in such a scope should have
4867 // slot type LOOKUP and not CONTEXT.
4868 if (emit_debug_code()) {
4869 CompareRoot(FieldOperand(dst, HeapObject::kMapOffset),
4870 Heap::kWithContextMapRootIndex);
4871 Check(not_equal, kVariableResolvedToWithContext);
4876 void MacroAssembler::LoadTransitionedArrayMapConditional(
4877 ElementsKind expected_kind,
4878 ElementsKind transitioned_kind,
4879 Register map_in_out,
4881 Label* no_map_match) {
4882 // Load the global or builtins object from the current context.
4884 Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4885 movp(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
4887 // Check that the function's map is the same as the expected cached map.
4888 movp(scratch, Operand(scratch,
4889 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
4891 int offset = expected_kind * kPointerSize +
4892 FixedArrayBase::kHeaderSize;
4893 cmpp(map_in_out, FieldOperand(scratch, offset));
4894 j(not_equal, no_map_match);
4896 // Use the transitioned cached map.
4897 offset = transitioned_kind * kPointerSize +
4898 FixedArrayBase::kHeaderSize;
4899 movp(map_in_out, FieldOperand(scratch, offset));
4904 static const int kRegisterPassedArguments = 4;
4906 static const int kRegisterPassedArguments = 6;
4909 void MacroAssembler::LoadGlobalFunction(int index, Register function) {
4910 // Load the global or builtins object from the current context.
4912 Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4913 // Load the native context from the global or builtins object.
4914 movp(function, FieldOperand(function, GlobalObject::kNativeContextOffset));
4915 // Load the function from the native context.
4916 movp(function, Operand(function, Context::SlotOffset(index)));
4920 void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
4922 // Load the initial map. The global functions all have initial maps.
4923 movp(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4924 if (emit_debug_code()) {
4926 CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
4929 Abort(kGlobalFunctionsMustHaveInitialMap);
4935 int MacroAssembler::ArgumentStackSlotsForCFunctionCall(int num_arguments) {
4936 // On Windows 64 stack slots are reserved by the caller for all arguments
4937 // including the ones passed in registers, and space is always allocated for
4938 // the four register arguments even if the function takes fewer than four
4940 // On AMD64 ABI (Linux/Mac) the first six arguments are passed in registers
4941 // and the caller does not reserve stack slots for them.
4942 DCHECK(num_arguments >= 0);
4944 const int kMinimumStackSlots = kRegisterPassedArguments;
4945 if (num_arguments < kMinimumStackSlots) return kMinimumStackSlots;
4946 return num_arguments;
4948 if (num_arguments < kRegisterPassedArguments) return 0;
4949 return num_arguments - kRegisterPassedArguments;
4954 void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
4957 uint32_t encoding_mask) {
4959 JumpIfNotSmi(string, &is_object);
4964 movp(value, FieldOperand(string, HeapObject::kMapOffset));
4965 movzxbp(value, FieldOperand(value, Map::kInstanceTypeOffset));
4967 andb(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
4968 cmpp(value, Immediate(encoding_mask));
4970 Check(equal, kUnexpectedStringType);
4972 // The index is assumed to be untagged coming in, tag it to compare with the
4973 // string length without using a temp register, it is restored at the end of
4975 Integer32ToSmi(index, index);
4976 SmiCompare(index, FieldOperand(string, String::kLengthOffset));
4977 Check(less, kIndexIsTooLarge);
4979 SmiCompare(index, Smi::FromInt(0));
4980 Check(greater_equal, kIndexIsNegative);
4982 // Restore the index
4983 SmiToInteger32(index, index);
4987 void MacroAssembler::PrepareCallCFunction(int num_arguments) {
4988 int frame_alignment = base::OS::ActivationFrameAlignment();
4989 DCHECK(frame_alignment != 0);
4990 DCHECK(num_arguments >= 0);
4992 // Make stack end at alignment and allocate space for arguments and old rsp.
4993 movp(kScratchRegister, rsp);
4994 DCHECK(IsPowerOf2(frame_alignment));
4995 int argument_slots_on_stack =
4996 ArgumentStackSlotsForCFunctionCall(num_arguments);
4997 subp(rsp, Immediate((argument_slots_on_stack + 1) * kRegisterSize));
4998 andp(rsp, Immediate(-frame_alignment));
4999 movp(Operand(rsp, argument_slots_on_stack * kRegisterSize), kScratchRegister);
5003 void MacroAssembler::CallCFunction(ExternalReference function,
5004 int num_arguments) {
5005 LoadAddress(rax, function);
5006 CallCFunction(rax, num_arguments);
5010 void MacroAssembler::CallCFunction(Register function, int num_arguments) {
5011 DCHECK(has_frame());
5012 // Check stack alignment.
5013 if (emit_debug_code()) {
5014 CheckStackAlignment();
5018 DCHECK(base::OS::ActivationFrameAlignment() != 0);
5019 DCHECK(num_arguments >= 0);
5020 int argument_slots_on_stack =
5021 ArgumentStackSlotsForCFunctionCall(num_arguments);
5022 movp(rsp, Operand(rsp, argument_slots_on_stack * kRegisterSize));
5027 bool AreAliased(Register reg1,
5035 int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
5036 reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
5037 reg7.is_valid() + reg8.is_valid();
5040 if (reg1.is_valid()) regs |= reg1.bit();
5041 if (reg2.is_valid()) regs |= reg2.bit();
5042 if (reg3.is_valid()) regs |= reg3.bit();
5043 if (reg4.is_valid()) regs |= reg4.bit();
5044 if (reg5.is_valid()) regs |= reg5.bit();
5045 if (reg6.is_valid()) regs |= reg6.bit();
5046 if (reg7.is_valid()) regs |= reg7.bit();
5047 if (reg8.is_valid()) regs |= reg8.bit();
5048 int n_of_non_aliasing_regs = NumRegs(regs);
5050 return n_of_valid_regs != n_of_non_aliasing_regs;
5055 CodePatcher::CodePatcher(byte* address, int size)
5056 : address_(address),
5058 masm_(NULL, address, size + Assembler::kGap) {
5059 // Create a new macro assembler pointing to the address of the code to patch.
5060 // The size is adjusted with kGap on order for the assembler to generate size
5061 // bytes of instructions without failing with buffer size constraints.
5062 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
5066 CodePatcher::~CodePatcher() {
5067 // Indicate that code has changed.
5068 CpuFeatures::FlushICache(address_, size_);
5070 // Check that the code was patched as expected.
5071 DCHECK(masm_.pc_ == address_ + size_);
5072 DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
5076 void MacroAssembler::CheckPageFlag(
5081 Label* condition_met,
5082 Label::Distance condition_met_distance) {
5083 DCHECK(cc == zero || cc == not_zero);
5084 if (scratch.is(object)) {
5085 andp(scratch, Immediate(~Page::kPageAlignmentMask));
5087 movp(scratch, Immediate(~Page::kPageAlignmentMask));
5088 andp(scratch, object);
5090 if (mask < (1 << kBitsPerByte)) {
5091 testb(Operand(scratch, MemoryChunk::kFlagsOffset),
5092 Immediate(static_cast<uint8_t>(mask)));
5094 testl(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
5096 j(cc, condition_met, condition_met_distance);
5100 void MacroAssembler::CheckMapDeprecated(Handle<Map> map,
5102 Label* if_deprecated) {
5103 if (map->CanBeDeprecated()) {
5105 movl(scratch, FieldOperand(scratch, Map::kBitField3Offset));
5106 andl(scratch, Immediate(Map::Deprecated::kMask));
5107 j(not_zero, if_deprecated);
5112 void MacroAssembler::JumpIfBlack(Register object,
5113 Register bitmap_scratch,
5114 Register mask_scratch,
5116 Label::Distance on_black_distance) {
5117 DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, rcx));
5118 GetMarkBits(object, bitmap_scratch, mask_scratch);
5120 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5121 // The mask_scratch register contains a 1 at the position of the first bit
5122 // and a 0 at all other positions, including the position of the second bit.
5123 movp(rcx, mask_scratch);
5124 // Make rcx into a mask that covers both marking bits using the operation
5125 // rcx = mask | (mask << 1).
5126 leap(rcx, Operand(mask_scratch, mask_scratch, times_2, 0));
5127 // Note that we are using a 4-byte aligned 8-byte load.
5128 andp(rcx, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
5129 cmpp(mask_scratch, rcx);
5130 j(equal, on_black, on_black_distance);
5134 // Detect some, but not all, common pointer-free objects. This is used by the
5135 // incremental write barrier which doesn't care about oddballs (they are always
5136 // marked black immediately so this code is not hit).
5137 void MacroAssembler::JumpIfDataObject(
5140 Label* not_data_object,
5141 Label::Distance not_data_object_distance) {
5142 Label is_data_object;
5143 movp(scratch, FieldOperand(value, HeapObject::kMapOffset));
5144 CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
5145 j(equal, &is_data_object, Label::kNear);
5146 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5147 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5148 // If it's a string and it's not a cons string then it's an object containing
5150 testb(FieldOperand(scratch, Map::kInstanceTypeOffset),
5151 Immediate(kIsIndirectStringMask | kIsNotStringMask));
5152 j(not_zero, not_data_object, not_data_object_distance);
5153 bind(&is_data_object);
5157 void MacroAssembler::GetMarkBits(Register addr_reg,
5158 Register bitmap_reg,
5159 Register mask_reg) {
5160 DCHECK(!AreAliased(addr_reg, bitmap_reg, mask_reg, rcx));
5161 movp(bitmap_reg, addr_reg);
5162 // Sign extended 32 bit immediate.
5163 andp(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
5164 movp(rcx, addr_reg);
5166 Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
5167 shrl(rcx, Immediate(shift));
5169 Immediate((Page::kPageAlignmentMask >> shift) &
5170 ~(Bitmap::kBytesPerCell - 1)));
5172 addp(bitmap_reg, rcx);
5173 movp(rcx, addr_reg);
5174 shrl(rcx, Immediate(kPointerSizeLog2));
5175 andp(rcx, Immediate((1 << Bitmap::kBitsPerCellLog2) - 1));
5176 movl(mask_reg, Immediate(1));
5181 void MacroAssembler::EnsureNotWhite(
5183 Register bitmap_scratch,
5184 Register mask_scratch,
5185 Label* value_is_white_and_not_data,
5186 Label::Distance distance) {
5187 DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, rcx));
5188 GetMarkBits(value, bitmap_scratch, mask_scratch);
5190 // If the value is black or grey we don't need to do anything.
5191 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
5192 DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5193 DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
5194 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
5198 // Since both black and grey have a 1 in the first position and white does
5199 // not have a 1 there we only need to check one bit.
5200 testp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5201 j(not_zero, &done, Label::kNear);
5203 if (emit_debug_code()) {
5204 // Check for impossible bit pattern.
5207 // shl. May overflow making the check conservative.
5208 addp(mask_scratch, mask_scratch);
5209 testp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5210 j(zero, &ok, Label::kNear);
5216 // Value is white. We check whether it is data that doesn't need scanning.
5217 // Currently only checks for HeapNumber and non-cons strings.
5218 Register map = rcx; // Holds map while checking type.
5219 Register length = rcx; // Holds length of object after checking type.
5220 Label not_heap_number;
5221 Label is_data_object;
5223 // Check for heap-number
5224 movp(map, FieldOperand(value, HeapObject::kMapOffset));
5225 CompareRoot(map, Heap::kHeapNumberMapRootIndex);
5226 j(not_equal, ¬_heap_number, Label::kNear);
5227 movp(length, Immediate(HeapNumber::kSize));
5228 jmp(&is_data_object, Label::kNear);
5230 bind(¬_heap_number);
5231 // Check for strings.
5232 DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5233 DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5234 // If it's a string and it's not a cons string then it's an object containing
5236 Register instance_type = rcx;
5237 movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
5238 testb(instance_type, Immediate(kIsIndirectStringMask | kIsNotStringMask));
5239 j(not_zero, value_is_white_and_not_data);
5240 // It's a non-indirect (non-cons and non-slice) string.
5241 // If it's external, the length is just ExternalString::kSize.
5242 // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
5244 // External strings are the only ones with the kExternalStringTag bit
5246 DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
5247 DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
5248 testb(instance_type, Immediate(kExternalStringTag));
5249 j(zero, ¬_external, Label::kNear);
5250 movp(length, Immediate(ExternalString::kSize));
5251 jmp(&is_data_object, Label::kNear);
5253 bind(¬_external);
5254 // Sequential string, either ASCII or UC16.
5255 DCHECK(kOneByteStringTag == 0x04);
5256 andp(length, Immediate(kStringEncodingMask));
5257 xorp(length, Immediate(kStringEncodingMask));
5258 addp(length, Immediate(0x04));
5259 // Value now either 4 (if ASCII) or 8 (if UC16), i.e. char-size shifted by 2.
5260 imulp(length, FieldOperand(value, String::kLengthOffset));
5261 shrp(length, Immediate(2 + kSmiTagSize + kSmiShiftSize));
5262 addp(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
5263 andp(length, Immediate(~kObjectAlignmentMask));
5265 bind(&is_data_object);
5266 // Value is a data object, and it is white. Mark it black. Since we know
5267 // that the object is white we can make it black by flipping one bit.
5268 orp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5270 andp(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
5271 addl(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset), length);
5277 void MacroAssembler::CheckEnumCache(Register null_value, Label* call_runtime) {
5279 Register empty_fixed_array_value = r8;
5280 LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
5283 // Check if the enum length field is properly initialized, indicating that
5284 // there is an enum cache.
5285 movp(rbx, FieldOperand(rcx, HeapObject::kMapOffset));
5287 EnumLength(rdx, rbx);
5288 Cmp(rdx, Smi::FromInt(kInvalidEnumCacheSentinel));
5289 j(equal, call_runtime);
5295 movp(rbx, FieldOperand(rcx, HeapObject::kMapOffset));
5297 // For all objects but the receiver, check that the cache is empty.
5298 EnumLength(rdx, rbx);
5299 Cmp(rdx, Smi::FromInt(0));
5300 j(not_equal, call_runtime);
5304 // Check that there are no elements. Register rcx contains the current JS
5305 // object we've reached through the prototype chain.
5307 cmpp(empty_fixed_array_value,
5308 FieldOperand(rcx, JSObject::kElementsOffset));
5309 j(equal, &no_elements);
5311 // Second chance, the object may be using the empty slow element dictionary.
5312 LoadRoot(kScratchRegister, Heap::kEmptySlowElementDictionaryRootIndex);
5313 cmpp(kScratchRegister, FieldOperand(rcx, JSObject::kElementsOffset));
5314 j(not_equal, call_runtime);
5317 movp(rcx, FieldOperand(rbx, Map::kPrototypeOffset));
5318 cmpp(rcx, null_value);
5319 j(not_equal, &next);
5322 void MacroAssembler::TestJSArrayForAllocationMemento(
5323 Register receiver_reg,
5324 Register scratch_reg,
5325 Label* no_memento_found) {
5326 ExternalReference new_space_start =
5327 ExternalReference::new_space_start(isolate());
5328 ExternalReference new_space_allocation_top =
5329 ExternalReference::new_space_allocation_top_address(isolate());
5331 leap(scratch_reg, Operand(receiver_reg,
5332 JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
5333 Move(kScratchRegister, new_space_start);
5334 cmpp(scratch_reg, kScratchRegister);
5335 j(less, no_memento_found);
5336 cmpp(scratch_reg, ExternalOperand(new_space_allocation_top));
5337 j(greater, no_memento_found);
5338 CompareRoot(MemOperand(scratch_reg, -AllocationMemento::kSize),
5339 Heap::kAllocationMementoMapRootIndex);
5343 void MacroAssembler::JumpIfDictionaryInPrototypeChain(
5348 DCHECK(!(scratch0.is(kScratchRegister) && scratch1.is(kScratchRegister)));
5349 DCHECK(!scratch1.is(scratch0));
5350 Register current = scratch0;
5353 movp(current, object);
5355 // Loop based on the map going up the prototype chain.
5357 movp(current, FieldOperand(current, HeapObject::kMapOffset));
5358 movp(scratch1, FieldOperand(current, Map::kBitField2Offset));
5359 DecodeField<Map::ElementsKindBits>(scratch1);
5360 cmpp(scratch1, Immediate(DICTIONARY_ELEMENTS));
5362 movp(current, FieldOperand(current, Map::kPrototypeOffset));
5363 CompareRoot(current, Heap::kNullValueRootIndex);
5364 j(not_equal, &loop_again);
5368 void MacroAssembler::TruncatingDiv(Register dividend, int32_t divisor) {
5369 DCHECK(!dividend.is(rax));
5370 DCHECK(!dividend.is(rdx));
5371 MultiplierAndShift ms(divisor);
5372 movl(rax, Immediate(ms.multiplier()));
5374 if (divisor > 0 && ms.multiplier() < 0) addl(rdx, dividend);
5375 if (divisor < 0 && ms.multiplier() > 0) subl(rdx, dividend);
5376 if (ms.shift() > 0) sarl(rdx, Immediate(ms.shift()));
5377 movl(rax, dividend);
5378 shrl(rax, Immediate(31));
5383 } } // namespace v8::internal
5385 #endif // V8_TARGET_ARCH_X64