Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / macro-assembler-x64.cc
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.
4
5 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_X64
8
9 #include "src/base/bits.h"
10 #include "src/base/division-by-constant.h"
11 #include "src/bootstrapper.h"
12 #include "src/codegen.h"
13 #include "src/cpu-profiler.h"
14 #include "src/debug.h"
15 #include "src/heap/heap.h"
16 #include "src/isolate-inl.h"
17 #include "src/serialize.h"
18 #include "src/x64/assembler-x64.h"
19 #include "src/x64/macro-assembler-x64.h"
20
21 namespace v8 {
22 namespace internal {
23
24 MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
25     : Assembler(arg_isolate, buffer, size),
26       generating_stub_(false),
27       has_frame_(false),
28       root_array_available_(true) {
29   if (isolate() != NULL) {
30     code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
31                                   isolate());
32   }
33 }
34
35
36 static const int64_t kInvalidRootRegisterDelta = -1;
37
38
39 int64_t MacroAssembler::RootRegisterDelta(ExternalReference other) {
40   if (predictable_code_size() &&
41       (other.address() < reinterpret_cast<Address>(isolate()) ||
42        other.address() >= reinterpret_cast<Address>(isolate() + 1))) {
43     return kInvalidRootRegisterDelta;
44   }
45   Address roots_register_value = kRootRegisterBias +
46       reinterpret_cast<Address>(isolate()->heap()->roots_array_start());
47
48   int64_t delta = kInvalidRootRegisterDelta;  // Bogus initialization.
49   if (kPointerSize == kInt64Size) {
50     delta = other.address() - roots_register_value;
51   } else {
52     // For x32, zero extend the address to 64-bit and calculate the delta.
53     uint64_t o = static_cast<uint32_t>(
54         reinterpret_cast<intptr_t>(other.address()));
55     uint64_t r = static_cast<uint32_t>(
56         reinterpret_cast<intptr_t>(roots_register_value));
57     delta = o - r;
58   }
59   return delta;
60 }
61
62
63 Operand MacroAssembler::ExternalOperand(ExternalReference target,
64                                         Register scratch) {
65   if (root_array_available_ && !serializer_enabled()) {
66     int64_t delta = RootRegisterDelta(target);
67     if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
68       return Operand(kRootRegister, static_cast<int32_t>(delta));
69     }
70   }
71   Move(scratch, target);
72   return Operand(scratch, 0);
73 }
74
75
76 void MacroAssembler::Load(Register destination, ExternalReference source) {
77   if (root_array_available_ && !serializer_enabled()) {
78     int64_t delta = RootRegisterDelta(source);
79     if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
80       movp(destination, Operand(kRootRegister, static_cast<int32_t>(delta)));
81       return;
82     }
83   }
84   // Safe code.
85   if (destination.is(rax)) {
86     load_rax(source);
87   } else {
88     Move(kScratchRegister, source);
89     movp(destination, Operand(kScratchRegister, 0));
90   }
91 }
92
93
94 void MacroAssembler::Store(ExternalReference destination, Register source) {
95   if (root_array_available_ && !serializer_enabled()) {
96     int64_t delta = RootRegisterDelta(destination);
97     if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
98       movp(Operand(kRootRegister, static_cast<int32_t>(delta)), source);
99       return;
100     }
101   }
102   // Safe code.
103   if (source.is(rax)) {
104     store_rax(destination);
105   } else {
106     Move(kScratchRegister, destination);
107     movp(Operand(kScratchRegister, 0), source);
108   }
109 }
110
111
112 void MacroAssembler::LoadAddress(Register destination,
113                                  ExternalReference source) {
114   if (root_array_available_ && !serializer_enabled()) {
115     int64_t delta = RootRegisterDelta(source);
116     if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
117       leap(destination, Operand(kRootRegister, static_cast<int32_t>(delta)));
118       return;
119     }
120   }
121   // Safe code.
122   Move(destination, source);
123 }
124
125
126 int MacroAssembler::LoadAddressSize(ExternalReference source) {
127   if (root_array_available_ && !serializer_enabled()) {
128     // This calculation depends on the internals of LoadAddress.
129     // It's correctness is ensured by the asserts in the Call
130     // instruction below.
131     int64_t delta = RootRegisterDelta(source);
132     if (delta != kInvalidRootRegisterDelta && is_int32(delta)) {
133       // Operand is leap(scratch, Operand(kRootRegister, delta));
134       // Opcodes : REX.W 8D ModRM Disp8/Disp32  - 4 or 7.
135       int size = 4;
136       if (!is_int8(static_cast<int32_t>(delta))) {
137         size += 3;  // Need full four-byte displacement in lea.
138       }
139       return size;
140     }
141   }
142   // Size of movp(destination, src);
143   return Assembler::kMoveAddressIntoScratchRegisterInstructionLength;
144 }
145
146
147 void MacroAssembler::PushAddress(ExternalReference source) {
148   int64_t address = reinterpret_cast<int64_t>(source.address());
149   if (is_int32(address) && !serializer_enabled()) {
150     if (emit_debug_code()) {
151       Move(kScratchRegister, kZapValue, Assembler::RelocInfoNone());
152     }
153     Push(Immediate(static_cast<int32_t>(address)));
154     return;
155   }
156   LoadAddress(kScratchRegister, source);
157   Push(kScratchRegister);
158 }
159
160
161 void MacroAssembler::LoadRoot(Register destination, Heap::RootListIndex index) {
162   DCHECK(root_array_available_);
163   movp(destination, Operand(kRootRegister,
164                             (index << kPointerSizeLog2) - kRootRegisterBias));
165 }
166
167
168 void MacroAssembler::LoadRootIndexed(Register destination,
169                                      Register variable_offset,
170                                      int fixed_offset) {
171   DCHECK(root_array_available_);
172   movp(destination,
173        Operand(kRootRegister,
174                variable_offset, times_pointer_size,
175                (fixed_offset << kPointerSizeLog2) - kRootRegisterBias));
176 }
177
178
179 void MacroAssembler::StoreRoot(Register source, Heap::RootListIndex index) {
180   DCHECK(root_array_available_);
181   movp(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias),
182        source);
183 }
184
185
186 void MacroAssembler::PushRoot(Heap::RootListIndex index) {
187   DCHECK(root_array_available_);
188   Push(Operand(kRootRegister, (index << kPointerSizeLog2) - kRootRegisterBias));
189 }
190
191
192 void MacroAssembler::CompareRoot(Register with, Heap::RootListIndex index) {
193   DCHECK(root_array_available_);
194   cmpp(with, Operand(kRootRegister,
195                      (index << kPointerSizeLog2) - kRootRegisterBias));
196 }
197
198
199 void MacroAssembler::CompareRoot(const Operand& with,
200                                  Heap::RootListIndex index) {
201   DCHECK(root_array_available_);
202   DCHECK(!with.AddressUsesRegister(kScratchRegister));
203   LoadRoot(kScratchRegister, index);
204   cmpp(with, kScratchRegister);
205 }
206
207
208 void MacroAssembler::RememberedSetHelper(Register object,  // For debug tests.
209                                          Register addr,
210                                          Register scratch,
211                                          SaveFPRegsMode save_fp,
212                                          RememberedSetFinalAction and_then) {
213   if (emit_debug_code()) {
214     Label ok;
215     JumpIfNotInNewSpace(object, scratch, &ok, Label::kNear);
216     int3();
217     bind(&ok);
218   }
219   // Load store buffer top.
220   LoadRoot(scratch, Heap::kStoreBufferTopRootIndex);
221   // Store pointer to buffer.
222   movp(Operand(scratch, 0), addr);
223   // Increment buffer top.
224   addp(scratch, Immediate(kPointerSize));
225   // Write back new top of buffer.
226   StoreRoot(scratch, Heap::kStoreBufferTopRootIndex);
227   // Call stub on end of buffer.
228   Label done;
229   // Check for end of buffer.
230   testp(scratch, Immediate(StoreBuffer::kStoreBufferOverflowBit));
231   if (and_then == kReturnAtEnd) {
232     Label buffer_overflowed;
233     j(not_equal, &buffer_overflowed, Label::kNear);
234     ret(0);
235     bind(&buffer_overflowed);
236   } else {
237     DCHECK(and_then == kFallThroughAtEnd);
238     j(equal, &done, Label::kNear);
239   }
240   StoreBufferOverflowStub store_buffer_overflow(isolate(), save_fp);
241   CallStub(&store_buffer_overflow);
242   if (and_then == kReturnAtEnd) {
243     ret(0);
244   } else {
245     DCHECK(and_then == kFallThroughAtEnd);
246     bind(&done);
247   }
248 }
249
250
251 void MacroAssembler::InNewSpace(Register object,
252                                 Register scratch,
253                                 Condition cc,
254                                 Label* branch,
255                                 Label::Distance distance) {
256   if (serializer_enabled()) {
257     // Can't do arithmetic on external references if it might get serialized.
258     // The mask isn't really an address.  We load it as an external reference in
259     // case the size of the new space is different between the snapshot maker
260     // and the running system.
261     if (scratch.is(object)) {
262       Move(kScratchRegister, ExternalReference::new_space_mask(isolate()));
263       andp(scratch, kScratchRegister);
264     } else {
265       Move(scratch, ExternalReference::new_space_mask(isolate()));
266       andp(scratch, object);
267     }
268     Move(kScratchRegister, ExternalReference::new_space_start(isolate()));
269     cmpp(scratch, kScratchRegister);
270     j(cc, branch, distance);
271   } else {
272     DCHECK(kPointerSize == kInt64Size
273         ? is_int32(static_cast<int64_t>(isolate()->heap()->NewSpaceMask()))
274         : kPointerSize == kInt32Size);
275     intptr_t new_space_start =
276         reinterpret_cast<intptr_t>(isolate()->heap()->NewSpaceStart());
277     Move(kScratchRegister, reinterpret_cast<Address>(-new_space_start),
278          Assembler::RelocInfoNone());
279     if (scratch.is(object)) {
280       addp(scratch, kScratchRegister);
281     } else {
282       leap(scratch, Operand(object, kScratchRegister, times_1, 0));
283     }
284     andp(scratch,
285          Immediate(static_cast<int32_t>(isolate()->heap()->NewSpaceMask())));
286     j(cc, branch, distance);
287   }
288 }
289
290
291 void MacroAssembler::RecordWriteField(
292     Register object,
293     int offset,
294     Register value,
295     Register dst,
296     SaveFPRegsMode save_fp,
297     RememberedSetAction remembered_set_action,
298     SmiCheck smi_check,
299     PointersToHereCheck pointers_to_here_check_for_value) {
300   // First, check if a write barrier is even needed. The tests below
301   // catch stores of Smis.
302   Label done;
303
304   // Skip barrier if writing a smi.
305   if (smi_check == INLINE_SMI_CHECK) {
306     JumpIfSmi(value, &done);
307   }
308
309   // Although the object register is tagged, the offset is relative to the start
310   // of the object, so so offset must be a multiple of kPointerSize.
311   DCHECK(IsAligned(offset, kPointerSize));
312
313   leap(dst, FieldOperand(object, offset));
314   if (emit_debug_code()) {
315     Label ok;
316     testb(dst, Immediate((1 << kPointerSizeLog2) - 1));
317     j(zero, &ok, Label::kNear);
318     int3();
319     bind(&ok);
320   }
321
322   RecordWrite(object, dst, value, save_fp, remembered_set_action,
323               OMIT_SMI_CHECK, pointers_to_here_check_for_value);
324
325   bind(&done);
326
327   // Clobber clobbered input registers when running with the debug-code flag
328   // turned on to provoke errors.
329   if (emit_debug_code()) {
330     Move(value, kZapValue, Assembler::RelocInfoNone());
331     Move(dst, kZapValue, Assembler::RelocInfoNone());
332   }
333 }
334
335
336 void MacroAssembler::RecordWriteArray(
337     Register object,
338     Register value,
339     Register index,
340     SaveFPRegsMode save_fp,
341     RememberedSetAction remembered_set_action,
342     SmiCheck smi_check,
343     PointersToHereCheck pointers_to_here_check_for_value) {
344   // First, check if a write barrier is even needed. The tests below
345   // catch stores of Smis.
346   Label done;
347
348   // Skip barrier if writing a smi.
349   if (smi_check == INLINE_SMI_CHECK) {
350     JumpIfSmi(value, &done);
351   }
352
353   // Array access: calculate the destination address. Index is not a smi.
354   Register dst = index;
355   leap(dst, Operand(object, index, times_pointer_size,
356                    FixedArray::kHeaderSize - kHeapObjectTag));
357
358   RecordWrite(object, dst, value, save_fp, remembered_set_action,
359               OMIT_SMI_CHECK, pointers_to_here_check_for_value);
360
361   bind(&done);
362
363   // Clobber clobbered input registers when running with the debug-code flag
364   // turned on to provoke errors.
365   if (emit_debug_code()) {
366     Move(value, kZapValue, Assembler::RelocInfoNone());
367     Move(index, kZapValue, Assembler::RelocInfoNone());
368   }
369 }
370
371
372 void MacroAssembler::RecordWriteForMap(Register object,
373                                        Register map,
374                                        Register dst,
375                                        SaveFPRegsMode fp_mode) {
376   DCHECK(!object.is(kScratchRegister));
377   DCHECK(!object.is(map));
378   DCHECK(!object.is(dst));
379   DCHECK(!map.is(dst));
380   AssertNotSmi(object);
381
382   if (emit_debug_code()) {
383     Label ok;
384     if (map.is(kScratchRegister)) pushq(map);
385     CompareMap(map, isolate()->factory()->meta_map());
386     if (map.is(kScratchRegister)) popq(map);
387     j(equal, &ok, Label::kNear);
388     int3();
389     bind(&ok);
390   }
391
392   if (!FLAG_incremental_marking) {
393     return;
394   }
395
396   if (emit_debug_code()) {
397     Label ok;
398     if (map.is(kScratchRegister)) pushq(map);
399     cmpp(map, FieldOperand(object, HeapObject::kMapOffset));
400     if (map.is(kScratchRegister)) popq(map);
401     j(equal, &ok, Label::kNear);
402     int3();
403     bind(&ok);
404   }
405
406   // Compute the address.
407   leap(dst, FieldOperand(object, HeapObject::kMapOffset));
408
409   // First, check if a write barrier is even needed. The tests below
410   // catch stores of smis and stores into the young generation.
411   Label done;
412
413   // A single check of the map's pages interesting flag suffices, since it is
414   // only set during incremental collection, and then it's also guaranteed that
415   // the from object's page's interesting flag is also set.  This optimization
416   // relies on the fact that maps can never be in new space.
417   CheckPageFlag(map,
418                 map,  // Used as scratch.
419                 MemoryChunk::kPointersToHereAreInterestingMask,
420                 zero,
421                 &done,
422                 Label::kNear);
423
424   RecordWriteStub stub(isolate(), object, map, dst, OMIT_REMEMBERED_SET,
425                        fp_mode);
426   CallStub(&stub);
427
428   bind(&done);
429
430   // Count number of write barriers in generated code.
431   isolate()->counters()->write_barriers_static()->Increment();
432   IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
433
434   // Clobber clobbered registers when running with the debug-code flag
435   // turned on to provoke errors.
436   if (emit_debug_code()) {
437     Move(dst, kZapValue, Assembler::RelocInfoNone());
438     Move(map, kZapValue, Assembler::RelocInfoNone());
439   }
440 }
441
442
443 void MacroAssembler::RecordWrite(
444     Register object,
445     Register address,
446     Register value,
447     SaveFPRegsMode fp_mode,
448     RememberedSetAction remembered_set_action,
449     SmiCheck smi_check,
450     PointersToHereCheck pointers_to_here_check_for_value) {
451   DCHECK(!object.is(value));
452   DCHECK(!object.is(address));
453   DCHECK(!value.is(address));
454   AssertNotSmi(object);
455
456   if (remembered_set_action == OMIT_REMEMBERED_SET &&
457       !FLAG_incremental_marking) {
458     return;
459   }
460
461   if (emit_debug_code()) {
462     Label ok;
463     cmpp(value, Operand(address, 0));
464     j(equal, &ok, Label::kNear);
465     int3();
466     bind(&ok);
467   }
468
469   // First, check if a write barrier is even needed. The tests below
470   // catch stores of smis and stores into the young generation.
471   Label done;
472
473   if (smi_check == INLINE_SMI_CHECK) {
474     // Skip barrier if writing a smi.
475     JumpIfSmi(value, &done);
476   }
477
478   if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
479     CheckPageFlag(value,
480                   value,  // Used as scratch.
481                   MemoryChunk::kPointersToHereAreInterestingMask,
482                   zero,
483                   &done,
484                   Label::kNear);
485   }
486
487   CheckPageFlag(object,
488                 value,  // Used as scratch.
489                 MemoryChunk::kPointersFromHereAreInterestingMask,
490                 zero,
491                 &done,
492                 Label::kNear);
493
494   RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
495                        fp_mode);
496   CallStub(&stub);
497
498   bind(&done);
499
500   // Count number of write barriers in generated code.
501   isolate()->counters()->write_barriers_static()->Increment();
502   IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1);
503
504   // Clobber clobbered registers when running with the debug-code flag
505   // turned on to provoke errors.
506   if (emit_debug_code()) {
507     Move(address, kZapValue, Assembler::RelocInfoNone());
508     Move(value, kZapValue, Assembler::RelocInfoNone());
509   }
510 }
511
512
513 void MacroAssembler::Assert(Condition cc, BailoutReason reason) {
514   if (emit_debug_code()) Check(cc, reason);
515 }
516
517
518 void MacroAssembler::AssertFastElements(Register elements) {
519   if (emit_debug_code()) {
520     Label ok;
521     CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
522                 Heap::kFixedArrayMapRootIndex);
523     j(equal, &ok, Label::kNear);
524     CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
525                 Heap::kFixedDoubleArrayMapRootIndex);
526     j(equal, &ok, Label::kNear);
527     CompareRoot(FieldOperand(elements, HeapObject::kMapOffset),
528                 Heap::kFixedCOWArrayMapRootIndex);
529     j(equal, &ok, Label::kNear);
530     Abort(kJSObjectWithFastElementsMapHasSlowElements);
531     bind(&ok);
532   }
533 }
534
535
536 void MacroAssembler::Check(Condition cc, BailoutReason reason) {
537   Label L;
538   j(cc, &L, Label::kNear);
539   Abort(reason);
540   // Control will not return here.
541   bind(&L);
542 }
543
544
545 void MacroAssembler::CheckStackAlignment() {
546   int frame_alignment = base::OS::ActivationFrameAlignment();
547   int frame_alignment_mask = frame_alignment - 1;
548   if (frame_alignment > kPointerSize) {
549     DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
550     Label alignment_as_expected;
551     testp(rsp, Immediate(frame_alignment_mask));
552     j(zero, &alignment_as_expected, Label::kNear);
553     // Abort if stack is not aligned.
554     int3();
555     bind(&alignment_as_expected);
556   }
557 }
558
559
560 void MacroAssembler::NegativeZeroTest(Register result,
561                                       Register op,
562                                       Label* then_label) {
563   Label ok;
564   testl(result, result);
565   j(not_zero, &ok, Label::kNear);
566   testl(op, op);
567   j(sign, then_label);
568   bind(&ok);
569 }
570
571
572 void MacroAssembler::Abort(BailoutReason reason) {
573 #ifdef DEBUG
574   const char* msg = GetBailoutReason(reason);
575   if (msg != NULL) {
576     RecordComment("Abort message: ");
577     RecordComment(msg);
578   }
579
580   if (FLAG_trap_on_abort) {
581     int3();
582     return;
583   }
584 #endif
585
586   Move(kScratchRegister, Smi::FromInt(static_cast<int>(reason)),
587        Assembler::RelocInfoNone());
588   Push(kScratchRegister);
589
590   if (!has_frame_) {
591     // We don't actually want to generate a pile of code for this, so just
592     // claim there is a stack frame, without generating one.
593     FrameScope scope(this, StackFrame::NONE);
594     CallRuntime(Runtime::kAbort, 1);
595   } else {
596     CallRuntime(Runtime::kAbort, 1);
597   }
598   // Control will not return here.
599   int3();
600 }
601
602
603 void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
604   DCHECK(AllowThisStubCall(stub));  // Calls are not allowed in some stubs
605   Call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
606 }
607
608
609 void MacroAssembler::TailCallStub(CodeStub* stub) {
610   Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
611 }
612
613
614 void MacroAssembler::StubReturn(int argc) {
615   DCHECK(argc >= 1 && generating_stub());
616   ret((argc - 1) * kPointerSize);
617 }
618
619
620 bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
621   return has_frame_ || !stub->SometimesSetsUpAFrame();
622 }
623
624
625 void MacroAssembler::IndexFromHash(Register hash, Register index) {
626   // The assert checks that the constants for the maximum number of digits
627   // for an array index cached in the hash field and the number of bits
628   // reserved for it does not conflict.
629   DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
630          (1 << String::kArrayIndexValueBits));
631   if (!hash.is(index)) {
632     movl(index, hash);
633   }
634   DecodeFieldToSmi<String::ArrayIndexValueBits>(index);
635 }
636
637
638 void MacroAssembler::CallRuntime(const Runtime::Function* f,
639                                  int num_arguments,
640                                  SaveFPRegsMode save_doubles) {
641   // If the expected number of arguments of the runtime function is
642   // constant, we check that the actual number of arguments match the
643   // expectation.
644   CHECK(f->nargs < 0 || f->nargs == num_arguments);
645
646   // TODO(1236192): Most runtime routines don't need the number of
647   // arguments passed in because it is constant. At some point we
648   // should remove this need and make the runtime routine entry code
649   // smarter.
650   Set(rax, num_arguments);
651   LoadAddress(rbx, ExternalReference(f, isolate()));
652   CEntryStub ces(isolate(), f->result_size, save_doubles);
653   CallStub(&ces);
654 }
655
656
657 void MacroAssembler::CallExternalReference(const ExternalReference& ext,
658                                            int num_arguments) {
659   Set(rax, num_arguments);
660   LoadAddress(rbx, ext);
661
662   CEntryStub stub(isolate(), 1);
663   CallStub(&stub);
664 }
665
666
667 void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
668                                                int num_arguments,
669                                                int result_size) {
670   // ----------- S t a t e -------------
671   //  -- rsp[0]                 : return address
672   //  -- rsp[8]                 : argument num_arguments - 1
673   //  ...
674   //  -- rsp[8 * num_arguments] : argument 0 (receiver)
675   // -----------------------------------
676
677   // TODO(1236192): Most runtime routines don't need the number of
678   // arguments passed in because it is constant. At some point we
679   // should remove this need and make the runtime routine entry code
680   // smarter.
681   Set(rax, num_arguments);
682   JumpToExternalReference(ext, result_size);
683 }
684
685
686 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
687                                      int num_arguments,
688                                      int result_size) {
689   TailCallExternalReference(ExternalReference(fid, isolate()),
690                             num_arguments,
691                             result_size);
692 }
693
694
695 static int Offset(ExternalReference ref0, ExternalReference ref1) {
696   int64_t offset = (ref0.address() - ref1.address());
697   // Check that fits into int.
698   DCHECK(static_cast<int>(offset) == offset);
699   return static_cast<int>(offset);
700 }
701
702
703 void MacroAssembler::PrepareCallApiFunction(int arg_stack_space) {
704   EnterApiExitFrame(arg_stack_space);
705 }
706
707
708 void MacroAssembler::CallApiFunctionAndReturn(
709     Register function_address,
710     ExternalReference thunk_ref,
711     Register thunk_last_arg,
712     int stack_space,
713     Operand return_value_operand,
714     Operand* context_restore_operand) {
715   Label prologue;
716   Label promote_scheduled_exception;
717   Label exception_handled;
718   Label delete_allocated_handles;
719   Label leave_exit_frame;
720   Label write_back;
721
722   Factory* factory = isolate()->factory();
723   ExternalReference next_address =
724       ExternalReference::handle_scope_next_address(isolate());
725   const int kNextOffset = 0;
726   const int kLimitOffset = Offset(
727       ExternalReference::handle_scope_limit_address(isolate()),
728       next_address);
729   const int kLevelOffset = Offset(
730       ExternalReference::handle_scope_level_address(isolate()),
731       next_address);
732   ExternalReference scheduled_exception_address =
733       ExternalReference::scheduled_exception_address(isolate());
734
735   DCHECK(rdx.is(function_address) || r8.is(function_address));
736   // Allocate HandleScope in callee-save registers.
737   Register prev_next_address_reg = r14;
738   Register prev_limit_reg = rbx;
739   Register base_reg = r15;
740   Move(base_reg, next_address);
741   movp(prev_next_address_reg, Operand(base_reg, kNextOffset));
742   movp(prev_limit_reg, Operand(base_reg, kLimitOffset));
743   addl(Operand(base_reg, kLevelOffset), Immediate(1));
744
745   if (FLAG_log_timer_events) {
746     FrameScope frame(this, StackFrame::MANUAL);
747     PushSafepointRegisters();
748     PrepareCallCFunction(1);
749     LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
750     CallCFunction(ExternalReference::log_enter_external_function(isolate()), 1);
751     PopSafepointRegisters();
752   }
753
754
755   Label profiler_disabled;
756   Label end_profiler_check;
757   Move(rax, ExternalReference::is_profiling_address(isolate()));
758   cmpb(Operand(rax, 0), Immediate(0));
759   j(zero, &profiler_disabled);
760
761   // Third parameter is the address of the actual getter function.
762   Move(thunk_last_arg, function_address);
763   Move(rax, thunk_ref);
764   jmp(&end_profiler_check);
765
766   bind(&profiler_disabled);
767   // Call the api function!
768   Move(rax, function_address);
769
770   bind(&end_profiler_check);
771
772   // Call the api function!
773   call(rax);
774
775   if (FLAG_log_timer_events) {
776     FrameScope frame(this, StackFrame::MANUAL);
777     PushSafepointRegisters();
778     PrepareCallCFunction(1);
779     LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
780     CallCFunction(ExternalReference::log_leave_external_function(isolate()), 1);
781     PopSafepointRegisters();
782   }
783
784   // Load the value from ReturnValue
785   movp(rax, return_value_operand);
786   bind(&prologue);
787
788   // No more valid handles (the result handle was the last one). Restore
789   // previous handle scope.
790   subl(Operand(base_reg, kLevelOffset), Immediate(1));
791   movp(Operand(base_reg, kNextOffset), prev_next_address_reg);
792   cmpp(prev_limit_reg, Operand(base_reg, kLimitOffset));
793   j(not_equal, &delete_allocated_handles);
794   bind(&leave_exit_frame);
795
796   // Check if the function scheduled an exception.
797   Move(rsi, scheduled_exception_address);
798   Cmp(Operand(rsi, 0), factory->the_hole_value());
799   j(not_equal, &promote_scheduled_exception);
800   bind(&exception_handled);
801
802 #if ENABLE_EXTRA_CHECKS
803   // Check if the function returned a valid JavaScript value.
804   Label ok;
805   Register return_value = rax;
806   Register map = rcx;
807
808   JumpIfSmi(return_value, &ok, Label::kNear);
809   movp(map, FieldOperand(return_value, HeapObject::kMapOffset));
810
811   CmpInstanceType(map, FIRST_NONSTRING_TYPE);
812   j(below, &ok, Label::kNear);
813
814   CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
815   j(above_equal, &ok, Label::kNear);
816
817   CompareRoot(map, Heap::kHeapNumberMapRootIndex);
818   j(equal, &ok, Label::kNear);
819
820   CompareRoot(return_value, Heap::kUndefinedValueRootIndex);
821   j(equal, &ok, Label::kNear);
822
823   CompareRoot(return_value, Heap::kTrueValueRootIndex);
824   j(equal, &ok, Label::kNear);
825
826   CompareRoot(return_value, Heap::kFalseValueRootIndex);
827   j(equal, &ok, Label::kNear);
828
829   CompareRoot(return_value, Heap::kNullValueRootIndex);
830   j(equal, &ok, Label::kNear);
831
832   Abort(kAPICallReturnedInvalidObject);
833
834   bind(&ok);
835 #endif
836
837   bool restore_context = context_restore_operand != NULL;
838   if (restore_context) {
839     movp(rsi, *context_restore_operand);
840   }
841   LeaveApiExitFrame(!restore_context);
842   ret(stack_space * kPointerSize);
843
844   bind(&promote_scheduled_exception);
845   {
846     FrameScope frame(this, StackFrame::INTERNAL);
847     CallRuntime(Runtime::kPromoteScheduledException, 0);
848   }
849   jmp(&exception_handled);
850
851   // HandleScope limit has changed. Delete allocated extensions.
852   bind(&delete_allocated_handles);
853   movp(Operand(base_reg, kLimitOffset), prev_limit_reg);
854   movp(prev_limit_reg, rax);
855   LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate()));
856   LoadAddress(rax,
857               ExternalReference::delete_handle_scope_extensions(isolate()));
858   call(rax);
859   movp(rax, prev_limit_reg);
860   jmp(&leave_exit_frame);
861 }
862
863
864 void MacroAssembler::JumpToExternalReference(const ExternalReference& ext,
865                                              int result_size) {
866   // Set the entry point and jump to the C entry runtime stub.
867   LoadAddress(rbx, ext);
868   CEntryStub ces(isolate(), result_size);
869   jmp(ces.GetCode(), RelocInfo::CODE_TARGET);
870 }
871
872
873 void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
874                                    InvokeFlag flag,
875                                    const CallWrapper& call_wrapper) {
876   // You can't call a builtin without a valid frame.
877   DCHECK(flag == JUMP_FUNCTION || has_frame());
878
879   // Rely on the assertion to check that the number of provided
880   // arguments match the expected number of arguments. Fake a
881   // parameter count to avoid emitting code to do the check.
882   ParameterCount expected(0);
883   GetBuiltinEntry(rdx, id);
884   InvokeCode(rdx, expected, expected, flag, call_wrapper);
885 }
886
887
888 void MacroAssembler::GetBuiltinFunction(Register target,
889                                         Builtins::JavaScript id) {
890   // Load the builtins object into target register.
891   movp(target, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
892   movp(target, FieldOperand(target, GlobalObject::kBuiltinsOffset));
893   movp(target, FieldOperand(target,
894                             JSBuiltinsObject::OffsetOfFunctionWithId(id)));
895 }
896
897
898 void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
899   DCHECK(!target.is(rdi));
900   // Load the JavaScript builtin function from the builtins object.
901   GetBuiltinFunction(rdi, id);
902   movp(target, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
903 }
904
905
906 #define REG(Name) { kRegister_ ## Name ## _Code }
907
908 static const Register saved_regs[] = {
909   REG(rax), REG(rcx), REG(rdx), REG(rbx), REG(rbp), REG(rsi), REG(rdi), REG(r8),
910   REG(r9), REG(r10), REG(r11)
911 };
912
913 #undef REG
914
915 static const int kNumberOfSavedRegs = sizeof(saved_regs) / sizeof(Register);
916
917
918 void MacroAssembler::PushCallerSaved(SaveFPRegsMode fp_mode,
919                                      Register exclusion1,
920                                      Register exclusion2,
921                                      Register exclusion3) {
922   // We don't allow a GC during a store buffer overflow so there is no need to
923   // store the registers in any particular way, but we do have to store and
924   // restore them.
925   for (int i = 0; i < kNumberOfSavedRegs; i++) {
926     Register reg = saved_regs[i];
927     if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
928       pushq(reg);
929     }
930   }
931   // R12 to r15 are callee save on all platforms.
932   if (fp_mode == kSaveFPRegs) {
933     subp(rsp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
934     for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
935       XMMRegister reg = XMMRegister::from_code(i);
936       movsd(Operand(rsp, i * kDoubleSize), reg);
937     }
938   }
939 }
940
941
942 void MacroAssembler::PopCallerSaved(SaveFPRegsMode fp_mode,
943                                     Register exclusion1,
944                                     Register exclusion2,
945                                     Register exclusion3) {
946   if (fp_mode == kSaveFPRegs) {
947     for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
948       XMMRegister reg = XMMRegister::from_code(i);
949       movsd(reg, Operand(rsp, i * kDoubleSize));
950     }
951     addp(rsp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
952   }
953   for (int i = kNumberOfSavedRegs - 1; i >= 0; i--) {
954     Register reg = saved_regs[i];
955     if (!reg.is(exclusion1) && !reg.is(exclusion2) && !reg.is(exclusion3)) {
956       popq(reg);
957     }
958   }
959 }
960
961
962 void MacroAssembler::Cvtlsi2sd(XMMRegister dst, Register src) {
963   xorps(dst, dst);
964   cvtlsi2sd(dst, src);
965 }
966
967
968 void MacroAssembler::Cvtlsi2sd(XMMRegister dst, const Operand& src) {
969   xorps(dst, dst);
970   cvtlsi2sd(dst, src);
971 }
972
973
974 void MacroAssembler::Load(Register dst, const Operand& src, Representation r) {
975   DCHECK(!r.IsDouble());
976   if (r.IsInteger8()) {
977     movsxbq(dst, src);
978   } else if (r.IsUInteger8()) {
979     movzxbl(dst, src);
980   } else if (r.IsInteger16()) {
981     movsxwq(dst, src);
982   } else if (r.IsUInteger16()) {
983     movzxwl(dst, src);
984   } else if (r.IsInteger32()) {
985     movl(dst, src);
986   } else {
987     movp(dst, src);
988   }
989 }
990
991
992 void MacroAssembler::Store(const Operand& dst, Register src, Representation r) {
993   DCHECK(!r.IsDouble());
994   if (r.IsInteger8() || r.IsUInteger8()) {
995     movb(dst, src);
996   } else if (r.IsInteger16() || r.IsUInteger16()) {
997     movw(dst, src);
998   } else if (r.IsInteger32()) {
999     movl(dst, src);
1000   } else {
1001     if (r.IsHeapObject()) {
1002       AssertNotSmi(src);
1003     } else if (r.IsSmi()) {
1004       AssertSmi(src);
1005     }
1006     movp(dst, src);
1007   }
1008 }
1009
1010
1011 void MacroAssembler::Set(Register dst, int64_t x) {
1012   if (x == 0) {
1013     xorl(dst, dst);
1014   } else if (is_uint32(x)) {
1015     movl(dst, Immediate(static_cast<uint32_t>(x)));
1016   } else if (is_int32(x)) {
1017     movq(dst, Immediate(static_cast<int32_t>(x)));
1018   } else {
1019     movq(dst, x);
1020   }
1021 }
1022
1023
1024 void MacroAssembler::Set(const Operand& dst, intptr_t x) {
1025   if (kPointerSize == kInt64Size) {
1026     if (is_int32(x)) {
1027       movp(dst, Immediate(static_cast<int32_t>(x)));
1028     } else {
1029       Set(kScratchRegister, x);
1030       movp(dst, kScratchRegister);
1031     }
1032   } else {
1033     movp(dst, Immediate(static_cast<int32_t>(x)));
1034   }
1035 }
1036
1037
1038 // ----------------------------------------------------------------------------
1039 // Smi tagging, untagging and tag detection.
1040
1041 bool MacroAssembler::IsUnsafeInt(const int32_t x) {
1042   static const int kMaxBits = 17;
1043   return !is_intn(x, kMaxBits);
1044 }
1045
1046
1047 void MacroAssembler::SafeMove(Register dst, Smi* src) {
1048   DCHECK(!dst.is(kScratchRegister));
1049   if (IsUnsafeInt(src->value()) && jit_cookie() != 0) {
1050     if (SmiValuesAre32Bits()) {
1051       // JIT cookie can be converted to Smi.
1052       Move(dst, Smi::FromInt(src->value() ^ jit_cookie()));
1053       Move(kScratchRegister, Smi::FromInt(jit_cookie()));
1054       xorp(dst, kScratchRegister);
1055     } else {
1056       DCHECK(SmiValuesAre31Bits());
1057       int32_t value = static_cast<int32_t>(reinterpret_cast<intptr_t>(src));
1058       movp(dst, Immediate(value ^ jit_cookie()));
1059       xorp(dst, Immediate(jit_cookie()));
1060     }
1061   } else {
1062     Move(dst, src);
1063   }
1064 }
1065
1066
1067 void MacroAssembler::SafePush(Smi* src) {
1068   if (IsUnsafeInt(src->value()) && jit_cookie() != 0) {
1069     if (SmiValuesAre32Bits()) {
1070       // JIT cookie can be converted to Smi.
1071       Push(Smi::FromInt(src->value() ^ jit_cookie()));
1072       Move(kScratchRegister, Smi::FromInt(jit_cookie()));
1073       xorp(Operand(rsp, 0), kScratchRegister);
1074     } else {
1075       DCHECK(SmiValuesAre31Bits());
1076       int32_t value = static_cast<int32_t>(reinterpret_cast<intptr_t>(src));
1077       Push(Immediate(value ^ jit_cookie()));
1078       xorp(Operand(rsp, 0), Immediate(jit_cookie()));
1079     }
1080   } else {
1081     Push(src);
1082   }
1083 }
1084
1085
1086 Register MacroAssembler::GetSmiConstant(Smi* source) {
1087   int value = source->value();
1088   if (value == 0) {
1089     xorl(kScratchRegister, kScratchRegister);
1090     return kScratchRegister;
1091   }
1092   if (value == 1) {
1093     return kSmiConstantRegister;
1094   }
1095   LoadSmiConstant(kScratchRegister, source);
1096   return kScratchRegister;
1097 }
1098
1099
1100 void MacroAssembler::LoadSmiConstant(Register dst, Smi* source) {
1101   if (emit_debug_code()) {
1102     Move(dst, Smi::FromInt(kSmiConstantRegisterValue),
1103          Assembler::RelocInfoNone());
1104     cmpp(dst, kSmiConstantRegister);
1105     Assert(equal, kUninitializedKSmiConstantRegister);
1106   }
1107   int value = source->value();
1108   if (value == 0) {
1109     xorl(dst, dst);
1110     return;
1111   }
1112   bool negative = value < 0;
1113   unsigned int uvalue = negative ? -value : value;
1114
1115   switch (uvalue) {
1116     case 9:
1117       leap(dst,
1118            Operand(kSmiConstantRegister, kSmiConstantRegister, times_8, 0));
1119       break;
1120     case 8:
1121       xorl(dst, dst);
1122       leap(dst, Operand(dst, kSmiConstantRegister, times_8, 0));
1123       break;
1124     case 4:
1125       xorl(dst, dst);
1126       leap(dst, Operand(dst, kSmiConstantRegister, times_4, 0));
1127       break;
1128     case 5:
1129       leap(dst,
1130            Operand(kSmiConstantRegister, kSmiConstantRegister, times_4, 0));
1131       break;
1132     case 3:
1133       leap(dst,
1134            Operand(kSmiConstantRegister, kSmiConstantRegister, times_2, 0));
1135       break;
1136     case 2:
1137       leap(dst,
1138            Operand(kSmiConstantRegister, kSmiConstantRegister, times_1, 0));
1139       break;
1140     case 1:
1141       movp(dst, kSmiConstantRegister);
1142       break;
1143     case 0:
1144       UNREACHABLE();
1145       return;
1146     default:
1147       Move(dst, source, Assembler::RelocInfoNone());
1148       return;
1149   }
1150   if (negative) {
1151     negp(dst);
1152   }
1153 }
1154
1155
1156 void MacroAssembler::Integer32ToSmi(Register dst, Register src) {
1157   STATIC_ASSERT(kSmiTag == 0);
1158   if (!dst.is(src)) {
1159     movl(dst, src);
1160   }
1161   shlp(dst, Immediate(kSmiShift));
1162 }
1163
1164
1165 void MacroAssembler::Integer32ToSmiField(const Operand& dst, Register src) {
1166   if (emit_debug_code()) {
1167     testb(dst, Immediate(0x01));
1168     Label ok;
1169     j(zero, &ok, Label::kNear);
1170     Abort(kInteger32ToSmiFieldWritingToNonSmiLocation);
1171     bind(&ok);
1172   }
1173
1174   if (SmiValuesAre32Bits()) {
1175     DCHECK(kSmiShift % kBitsPerByte == 0);
1176     movl(Operand(dst, kSmiShift / kBitsPerByte), src);
1177   } else {
1178     DCHECK(SmiValuesAre31Bits());
1179     Integer32ToSmi(kScratchRegister, src);
1180     movp(dst, kScratchRegister);
1181   }
1182 }
1183
1184
1185 void MacroAssembler::Integer64PlusConstantToSmi(Register dst,
1186                                                 Register src,
1187                                                 int constant) {
1188   if (dst.is(src)) {
1189     addl(dst, Immediate(constant));
1190   } else {
1191     leal(dst, Operand(src, constant));
1192   }
1193   shlp(dst, Immediate(kSmiShift));
1194 }
1195
1196
1197 void MacroAssembler::SmiToInteger32(Register dst, Register src) {
1198   STATIC_ASSERT(kSmiTag == 0);
1199   if (!dst.is(src)) {
1200     movp(dst, src);
1201   }
1202
1203   if (SmiValuesAre32Bits()) {
1204     shrp(dst, Immediate(kSmiShift));
1205   } else {
1206     DCHECK(SmiValuesAre31Bits());
1207     sarl(dst, Immediate(kSmiShift));
1208   }
1209 }
1210
1211
1212 void MacroAssembler::SmiToInteger32(Register dst, const Operand& src) {
1213   if (SmiValuesAre32Bits()) {
1214     movl(dst, Operand(src, kSmiShift / kBitsPerByte));
1215   } else {
1216     DCHECK(SmiValuesAre31Bits());
1217     movl(dst, src);
1218     sarl(dst, Immediate(kSmiShift));
1219   }
1220 }
1221
1222
1223 void MacroAssembler::SmiToInteger64(Register dst, Register src) {
1224   STATIC_ASSERT(kSmiTag == 0);
1225   if (!dst.is(src)) {
1226     movp(dst, src);
1227   }
1228   sarp(dst, Immediate(kSmiShift));
1229   if (kPointerSize == kInt32Size) {
1230     // Sign extend to 64-bit.
1231     movsxlq(dst, dst);
1232   }
1233 }
1234
1235
1236 void MacroAssembler::SmiToInteger64(Register dst, const Operand& src) {
1237   if (SmiValuesAre32Bits()) {
1238     movsxlq(dst, Operand(src, kSmiShift / kBitsPerByte));
1239   } else {
1240     DCHECK(SmiValuesAre31Bits());
1241     movp(dst, src);
1242     SmiToInteger64(dst, dst);
1243   }
1244 }
1245
1246
1247 void MacroAssembler::SmiTest(Register src) {
1248   AssertSmi(src);
1249   testp(src, src);
1250 }
1251
1252
1253 void MacroAssembler::SmiCompare(Register smi1, Register smi2) {
1254   AssertSmi(smi1);
1255   AssertSmi(smi2);
1256   cmpp(smi1, smi2);
1257 }
1258
1259
1260 void MacroAssembler::SmiCompare(Register dst, Smi* src) {
1261   AssertSmi(dst);
1262   Cmp(dst, src);
1263 }
1264
1265
1266 void MacroAssembler::Cmp(Register dst, Smi* src) {
1267   DCHECK(!dst.is(kScratchRegister));
1268   if (src->value() == 0) {
1269     testp(dst, dst);
1270   } else {
1271     Register constant_reg = GetSmiConstant(src);
1272     cmpp(dst, constant_reg);
1273   }
1274 }
1275
1276
1277 void MacroAssembler::SmiCompare(Register dst, const Operand& src) {
1278   AssertSmi(dst);
1279   AssertSmi(src);
1280   cmpp(dst, src);
1281 }
1282
1283
1284 void MacroAssembler::SmiCompare(const Operand& dst, Register src) {
1285   AssertSmi(dst);
1286   AssertSmi(src);
1287   cmpp(dst, src);
1288 }
1289
1290
1291 void MacroAssembler::SmiCompare(const Operand& dst, Smi* src) {
1292   AssertSmi(dst);
1293   if (SmiValuesAre32Bits()) {
1294     cmpl(Operand(dst, kSmiShift / kBitsPerByte), Immediate(src->value()));
1295   } else {
1296     DCHECK(SmiValuesAre31Bits());
1297     cmpl(dst, Immediate(src));
1298   }
1299 }
1300
1301
1302 void MacroAssembler::Cmp(const Operand& dst, Smi* src) {
1303   // The Operand cannot use the smi register.
1304   Register smi_reg = GetSmiConstant(src);
1305   DCHECK(!dst.AddressUsesRegister(smi_reg));
1306   cmpp(dst, smi_reg);
1307 }
1308
1309
1310 void MacroAssembler::SmiCompareInteger32(const Operand& dst, Register src) {
1311   if (SmiValuesAre32Bits()) {
1312     cmpl(Operand(dst, kSmiShift / kBitsPerByte), src);
1313   } else {
1314     DCHECK(SmiValuesAre31Bits());
1315     SmiToInteger32(kScratchRegister, dst);
1316     cmpl(kScratchRegister, src);
1317   }
1318 }
1319
1320
1321 void MacroAssembler::PositiveSmiTimesPowerOfTwoToInteger64(Register dst,
1322                                                            Register src,
1323                                                            int power) {
1324   DCHECK(power >= 0);
1325   DCHECK(power < 64);
1326   if (power == 0) {
1327     SmiToInteger64(dst, src);
1328     return;
1329   }
1330   if (!dst.is(src)) {
1331     movp(dst, src);
1332   }
1333   if (power < kSmiShift) {
1334     sarp(dst, Immediate(kSmiShift - power));
1335   } else if (power > kSmiShift) {
1336     shlp(dst, Immediate(power - kSmiShift));
1337   }
1338 }
1339
1340
1341 void MacroAssembler::PositiveSmiDivPowerOfTwoToInteger32(Register dst,
1342                                                          Register src,
1343                                                          int power) {
1344   DCHECK((0 <= power) && (power < 32));
1345   if (dst.is(src)) {
1346     shrp(dst, Immediate(power + kSmiShift));
1347   } else {
1348     UNIMPLEMENTED();  // Not used.
1349   }
1350 }
1351
1352
1353 void MacroAssembler::SmiOrIfSmis(Register dst, Register src1, Register src2,
1354                                  Label* on_not_smis,
1355                                  Label::Distance near_jump) {
1356   if (dst.is(src1) || dst.is(src2)) {
1357     DCHECK(!src1.is(kScratchRegister));
1358     DCHECK(!src2.is(kScratchRegister));
1359     movp(kScratchRegister, src1);
1360     orp(kScratchRegister, src2);
1361     JumpIfNotSmi(kScratchRegister, on_not_smis, near_jump);
1362     movp(dst, kScratchRegister);
1363   } else {
1364     movp(dst, src1);
1365     orp(dst, src2);
1366     JumpIfNotSmi(dst, on_not_smis, near_jump);
1367   }
1368 }
1369
1370
1371 Condition MacroAssembler::CheckSmi(Register src) {
1372   STATIC_ASSERT(kSmiTag == 0);
1373   testb(src, Immediate(kSmiTagMask));
1374   return zero;
1375 }
1376
1377
1378 Condition MacroAssembler::CheckSmi(const Operand& src) {
1379   STATIC_ASSERT(kSmiTag == 0);
1380   testb(src, Immediate(kSmiTagMask));
1381   return zero;
1382 }
1383
1384
1385 Condition MacroAssembler::CheckNonNegativeSmi(Register src) {
1386   STATIC_ASSERT(kSmiTag == 0);
1387   // Test that both bits of the mask 0x8000000000000001 are zero.
1388   movp(kScratchRegister, src);
1389   rolp(kScratchRegister, Immediate(1));
1390   testb(kScratchRegister, Immediate(3));
1391   return zero;
1392 }
1393
1394
1395 Condition MacroAssembler::CheckBothSmi(Register first, Register second) {
1396   if (first.is(second)) {
1397     return CheckSmi(first);
1398   }
1399   STATIC_ASSERT(kSmiTag == 0 && kHeapObjectTag == 1 && kHeapObjectTagMask == 3);
1400   if (SmiValuesAre32Bits()) {
1401     leal(kScratchRegister, Operand(first, second, times_1, 0));
1402     testb(kScratchRegister, Immediate(0x03));
1403   } else {
1404     DCHECK(SmiValuesAre31Bits());
1405     movl(kScratchRegister, first);
1406     orl(kScratchRegister, second);
1407     testb(kScratchRegister, Immediate(kSmiTagMask));
1408   }
1409   return zero;
1410 }
1411
1412
1413 Condition MacroAssembler::CheckBothNonNegativeSmi(Register first,
1414                                                   Register second) {
1415   if (first.is(second)) {
1416     return CheckNonNegativeSmi(first);
1417   }
1418   movp(kScratchRegister, first);
1419   orp(kScratchRegister, second);
1420   rolp(kScratchRegister, Immediate(1));
1421   testl(kScratchRegister, Immediate(3));
1422   return zero;
1423 }
1424
1425
1426 Condition MacroAssembler::CheckEitherSmi(Register first,
1427                                          Register second,
1428                                          Register scratch) {
1429   if (first.is(second)) {
1430     return CheckSmi(first);
1431   }
1432   if (scratch.is(second)) {
1433     andl(scratch, first);
1434   } else {
1435     if (!scratch.is(first)) {
1436       movl(scratch, first);
1437     }
1438     andl(scratch, second);
1439   }
1440   testb(scratch, Immediate(kSmiTagMask));
1441   return zero;
1442 }
1443
1444
1445 Condition MacroAssembler::CheckIsMinSmi(Register src) {
1446   DCHECK(!src.is(kScratchRegister));
1447   // If we overflow by subtracting one, it's the minimal smi value.
1448   cmpp(src, kSmiConstantRegister);
1449   return overflow;
1450 }
1451
1452
1453 Condition MacroAssembler::CheckInteger32ValidSmiValue(Register src) {
1454   if (SmiValuesAre32Bits()) {
1455     // A 32-bit integer value can always be converted to a smi.
1456     return always;
1457   } else {
1458     DCHECK(SmiValuesAre31Bits());
1459     cmpl(src, Immediate(0xc0000000));
1460     return positive;
1461   }
1462 }
1463
1464
1465 Condition MacroAssembler::CheckUInteger32ValidSmiValue(Register src) {
1466   if (SmiValuesAre32Bits()) {
1467     // An unsigned 32-bit integer value is valid as long as the high bit
1468     // is not set.
1469     testl(src, src);
1470     return positive;
1471   } else {
1472     DCHECK(SmiValuesAre31Bits());
1473     testl(src, Immediate(0xc0000000));
1474     return zero;
1475   }
1476 }
1477
1478
1479 void MacroAssembler::CheckSmiToIndicator(Register dst, Register src) {
1480   if (dst.is(src)) {
1481     andl(dst, Immediate(kSmiTagMask));
1482   } else {
1483     movl(dst, Immediate(kSmiTagMask));
1484     andl(dst, src);
1485   }
1486 }
1487
1488
1489 void MacroAssembler::CheckSmiToIndicator(Register dst, const Operand& src) {
1490   if (!(src.AddressUsesRegister(dst))) {
1491     movl(dst, Immediate(kSmiTagMask));
1492     andl(dst, src);
1493   } else {
1494     movl(dst, src);
1495     andl(dst, Immediate(kSmiTagMask));
1496   }
1497 }
1498
1499
1500 void MacroAssembler::JumpIfValidSmiValue(Register src,
1501                                          Label* on_valid,
1502                                          Label::Distance near_jump) {
1503   Condition is_valid = CheckInteger32ValidSmiValue(src);
1504   j(is_valid, on_valid, near_jump);
1505 }
1506
1507
1508 void MacroAssembler::JumpIfNotValidSmiValue(Register src,
1509                                             Label* on_invalid,
1510                                             Label::Distance near_jump) {
1511   Condition is_valid = CheckInteger32ValidSmiValue(src);
1512   j(NegateCondition(is_valid), on_invalid, near_jump);
1513 }
1514
1515
1516 void MacroAssembler::JumpIfUIntValidSmiValue(Register src,
1517                                              Label* on_valid,
1518                                              Label::Distance near_jump) {
1519   Condition is_valid = CheckUInteger32ValidSmiValue(src);
1520   j(is_valid, on_valid, near_jump);
1521 }
1522
1523
1524 void MacroAssembler::JumpIfUIntNotValidSmiValue(Register src,
1525                                                 Label* on_invalid,
1526                                                 Label::Distance near_jump) {
1527   Condition is_valid = CheckUInteger32ValidSmiValue(src);
1528   j(NegateCondition(is_valid), on_invalid, near_jump);
1529 }
1530
1531
1532 void MacroAssembler::JumpIfSmi(Register src,
1533                                Label* on_smi,
1534                                Label::Distance near_jump) {
1535   Condition smi = CheckSmi(src);
1536   j(smi, on_smi, near_jump);
1537 }
1538
1539
1540 void MacroAssembler::JumpIfNotSmi(Register src,
1541                                   Label* on_not_smi,
1542                                   Label::Distance near_jump) {
1543   Condition smi = CheckSmi(src);
1544   j(NegateCondition(smi), on_not_smi, near_jump);
1545 }
1546
1547
1548 void MacroAssembler::JumpUnlessNonNegativeSmi(
1549     Register src, Label* on_not_smi_or_negative,
1550     Label::Distance near_jump) {
1551   Condition non_negative_smi = CheckNonNegativeSmi(src);
1552   j(NegateCondition(non_negative_smi), on_not_smi_or_negative, near_jump);
1553 }
1554
1555
1556 void MacroAssembler::JumpIfSmiEqualsConstant(Register src,
1557                                              Smi* constant,
1558                                              Label* on_equals,
1559                                              Label::Distance near_jump) {
1560   SmiCompare(src, constant);
1561   j(equal, on_equals, near_jump);
1562 }
1563
1564
1565 void MacroAssembler::JumpIfNotBothSmi(Register src1,
1566                                       Register src2,
1567                                       Label* on_not_both_smi,
1568                                       Label::Distance near_jump) {
1569   Condition both_smi = CheckBothSmi(src1, src2);
1570   j(NegateCondition(both_smi), on_not_both_smi, near_jump);
1571 }
1572
1573
1574 void MacroAssembler::JumpUnlessBothNonNegativeSmi(Register src1,
1575                                                   Register src2,
1576                                                   Label* on_not_both_smi,
1577                                                   Label::Distance near_jump) {
1578   Condition both_smi = CheckBothNonNegativeSmi(src1, src2);
1579   j(NegateCondition(both_smi), on_not_both_smi, near_jump);
1580 }
1581
1582
1583 void MacroAssembler::SmiAddConstant(Register dst, Register src, Smi* constant) {
1584   if (constant->value() == 0) {
1585     if (!dst.is(src)) {
1586       movp(dst, src);
1587     }
1588     return;
1589   } else if (dst.is(src)) {
1590     DCHECK(!dst.is(kScratchRegister));
1591     switch (constant->value()) {
1592       case 1:
1593         addp(dst, kSmiConstantRegister);
1594         return;
1595       case 2:
1596         leap(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1597         return;
1598       case 4:
1599         leap(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1600         return;
1601       case 8:
1602         leap(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1603         return;
1604       default:
1605         Register constant_reg = GetSmiConstant(constant);
1606         addp(dst, constant_reg);
1607         return;
1608     }
1609   } else {
1610     switch (constant->value()) {
1611       case 1:
1612         leap(dst, Operand(src, kSmiConstantRegister, times_1, 0));
1613         return;
1614       case 2:
1615         leap(dst, Operand(src, kSmiConstantRegister, times_2, 0));
1616         return;
1617       case 4:
1618         leap(dst, Operand(src, kSmiConstantRegister, times_4, 0));
1619         return;
1620       case 8:
1621         leap(dst, Operand(src, kSmiConstantRegister, times_8, 0));
1622         return;
1623       default:
1624         LoadSmiConstant(dst, constant);
1625         addp(dst, src);
1626         return;
1627     }
1628   }
1629 }
1630
1631
1632 void MacroAssembler::SmiAddConstant(const Operand& dst, Smi* constant) {
1633   if (constant->value() != 0) {
1634     if (SmiValuesAre32Bits()) {
1635       addl(Operand(dst, kSmiShift / kBitsPerByte),
1636            Immediate(constant->value()));
1637     } else {
1638       DCHECK(SmiValuesAre31Bits());
1639       addp(dst, Immediate(constant));
1640     }
1641   }
1642 }
1643
1644
1645 void MacroAssembler::SmiAddConstant(Register dst,
1646                                     Register src,
1647                                     Smi* constant,
1648                                     SmiOperationExecutionMode mode,
1649                                     Label* bailout_label,
1650                                     Label::Distance near_jump) {
1651   if (constant->value() == 0) {
1652     if (!dst.is(src)) {
1653       movp(dst, src);
1654     }
1655   } else if (dst.is(src)) {
1656     DCHECK(!dst.is(kScratchRegister));
1657     LoadSmiConstant(kScratchRegister, constant);
1658     addp(dst, kScratchRegister);
1659     if (mode.Contains(BAILOUT_ON_NO_OVERFLOW)) {
1660       j(no_overflow, bailout_label, near_jump);
1661       DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1662       subp(dst, kScratchRegister);
1663     } else if (mode.Contains(BAILOUT_ON_OVERFLOW)) {
1664       if (mode.Contains(PRESERVE_SOURCE_REGISTER)) {
1665         Label done;
1666         j(no_overflow, &done, Label::kNear);
1667         subp(dst, kScratchRegister);
1668         jmp(bailout_label, near_jump);
1669         bind(&done);
1670       } else {
1671         // Bailout if overflow without reserving src.
1672         j(overflow, bailout_label, near_jump);
1673       }
1674     } else {
1675       CHECK(mode.IsEmpty());
1676     }
1677   } else {
1678     DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1679     DCHECK(mode.Contains(BAILOUT_ON_OVERFLOW));
1680     LoadSmiConstant(dst, constant);
1681     addp(dst, src);
1682     j(overflow, bailout_label, near_jump);
1683   }
1684 }
1685
1686
1687 void MacroAssembler::SmiSubConstant(Register dst, Register src, Smi* constant) {
1688   if (constant->value() == 0) {
1689     if (!dst.is(src)) {
1690       movp(dst, src);
1691     }
1692   } else if (dst.is(src)) {
1693     DCHECK(!dst.is(kScratchRegister));
1694     Register constant_reg = GetSmiConstant(constant);
1695     subp(dst, constant_reg);
1696   } else {
1697     if (constant->value() == Smi::kMinValue) {
1698       LoadSmiConstant(dst, constant);
1699       // Adding and subtracting the min-value gives the same result, it only
1700       // differs on the overflow bit, which we don't check here.
1701       addp(dst, src);
1702     } else {
1703       // Subtract by adding the negation.
1704       LoadSmiConstant(dst, Smi::FromInt(-constant->value()));
1705       addp(dst, src);
1706     }
1707   }
1708 }
1709
1710
1711 void MacroAssembler::SmiSubConstant(Register dst,
1712                                     Register src,
1713                                     Smi* constant,
1714                                     SmiOperationExecutionMode mode,
1715                                     Label* bailout_label,
1716                                     Label::Distance near_jump) {
1717   if (constant->value() == 0) {
1718     if (!dst.is(src)) {
1719       movp(dst, src);
1720     }
1721   } else if (dst.is(src)) {
1722     DCHECK(!dst.is(kScratchRegister));
1723     LoadSmiConstant(kScratchRegister, constant);
1724     subp(dst, kScratchRegister);
1725     if (mode.Contains(BAILOUT_ON_NO_OVERFLOW)) {
1726       j(no_overflow, bailout_label, near_jump);
1727       DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1728       addp(dst, kScratchRegister);
1729     } else if (mode.Contains(BAILOUT_ON_OVERFLOW)) {
1730       if (mode.Contains(PRESERVE_SOURCE_REGISTER)) {
1731         Label done;
1732         j(no_overflow, &done, Label::kNear);
1733         addp(dst, kScratchRegister);
1734         jmp(bailout_label, near_jump);
1735         bind(&done);
1736       } else {
1737         // Bailout if overflow without reserving src.
1738         j(overflow, bailout_label, near_jump);
1739       }
1740     } else {
1741       CHECK(mode.IsEmpty());
1742     }
1743   } else {
1744     DCHECK(mode.Contains(PRESERVE_SOURCE_REGISTER));
1745     DCHECK(mode.Contains(BAILOUT_ON_OVERFLOW));
1746     if (constant->value() == Smi::kMinValue) {
1747       DCHECK(!dst.is(kScratchRegister));
1748       movp(dst, src);
1749       LoadSmiConstant(kScratchRegister, constant);
1750       subp(dst, kScratchRegister);
1751       j(overflow, bailout_label, near_jump);
1752     } else {
1753       // Subtract by adding the negation.
1754       LoadSmiConstant(dst, Smi::FromInt(-(constant->value())));
1755       addp(dst, src);
1756       j(overflow, bailout_label, near_jump);
1757     }
1758   }
1759 }
1760
1761
1762 void MacroAssembler::SmiNeg(Register dst,
1763                             Register src,
1764                             Label* on_smi_result,
1765                             Label::Distance near_jump) {
1766   if (dst.is(src)) {
1767     DCHECK(!dst.is(kScratchRegister));
1768     movp(kScratchRegister, src);
1769     negp(dst);  // Low 32 bits are retained as zero by negation.
1770     // Test if result is zero or Smi::kMinValue.
1771     cmpp(dst, kScratchRegister);
1772     j(not_equal, on_smi_result, near_jump);
1773     movp(src, kScratchRegister);
1774   } else {
1775     movp(dst, src);
1776     negp(dst);
1777     cmpp(dst, src);
1778     // If the result is zero or Smi::kMinValue, negation failed to create a smi.
1779     j(not_equal, on_smi_result, near_jump);
1780   }
1781 }
1782
1783
1784 template<class T>
1785 static void SmiAddHelper(MacroAssembler* masm,
1786                          Register dst,
1787                          Register src1,
1788                          T src2,
1789                          Label* on_not_smi_result,
1790                          Label::Distance near_jump) {
1791   if (dst.is(src1)) {
1792     Label done;
1793     masm->addp(dst, src2);
1794     masm->j(no_overflow, &done, Label::kNear);
1795     // Restore src1.
1796     masm->subp(dst, src2);
1797     masm->jmp(on_not_smi_result, near_jump);
1798     masm->bind(&done);
1799   } else {
1800     masm->movp(dst, src1);
1801     masm->addp(dst, src2);
1802     masm->j(overflow, on_not_smi_result, near_jump);
1803   }
1804 }
1805
1806
1807 void MacroAssembler::SmiAdd(Register dst,
1808                             Register src1,
1809                             Register src2,
1810                             Label* on_not_smi_result,
1811                             Label::Distance near_jump) {
1812   DCHECK_NOT_NULL(on_not_smi_result);
1813   DCHECK(!dst.is(src2));
1814   SmiAddHelper<Register>(this, dst, src1, src2, on_not_smi_result, near_jump);
1815 }
1816
1817
1818 void MacroAssembler::SmiAdd(Register dst,
1819                             Register src1,
1820                             const Operand& src2,
1821                             Label* on_not_smi_result,
1822                             Label::Distance near_jump) {
1823   DCHECK_NOT_NULL(on_not_smi_result);
1824   DCHECK(!src2.AddressUsesRegister(dst));
1825   SmiAddHelper<Operand>(this, dst, src1, src2, on_not_smi_result, near_jump);
1826 }
1827
1828
1829 void MacroAssembler::SmiAdd(Register dst,
1830                             Register src1,
1831                             Register src2) {
1832   // No overflow checking. Use only when it's known that
1833   // overflowing is impossible.
1834   if (!dst.is(src1)) {
1835     if (emit_debug_code()) {
1836       movp(kScratchRegister, src1);
1837       addp(kScratchRegister, src2);
1838       Check(no_overflow, kSmiAdditionOverflow);
1839     }
1840     leap(dst, Operand(src1, src2, times_1, 0));
1841   } else {
1842     addp(dst, src2);
1843     Assert(no_overflow, kSmiAdditionOverflow);
1844   }
1845 }
1846
1847
1848 template<class T>
1849 static void SmiSubHelper(MacroAssembler* masm,
1850                          Register dst,
1851                          Register src1,
1852                          T src2,
1853                          Label* on_not_smi_result,
1854                          Label::Distance near_jump) {
1855   if (dst.is(src1)) {
1856     Label done;
1857     masm->subp(dst, src2);
1858     masm->j(no_overflow, &done, Label::kNear);
1859     // Restore src1.
1860     masm->addp(dst, src2);
1861     masm->jmp(on_not_smi_result, near_jump);
1862     masm->bind(&done);
1863   } else {
1864     masm->movp(dst, src1);
1865     masm->subp(dst, src2);
1866     masm->j(overflow, on_not_smi_result, near_jump);
1867   }
1868 }
1869
1870
1871 void MacroAssembler::SmiSub(Register dst,
1872                             Register src1,
1873                             Register src2,
1874                             Label* on_not_smi_result,
1875                             Label::Distance near_jump) {
1876   DCHECK_NOT_NULL(on_not_smi_result);
1877   DCHECK(!dst.is(src2));
1878   SmiSubHelper<Register>(this, dst, src1, src2, on_not_smi_result, near_jump);
1879 }
1880
1881
1882 void MacroAssembler::SmiSub(Register dst,
1883                             Register src1,
1884                             const Operand& src2,
1885                             Label* on_not_smi_result,
1886                             Label::Distance near_jump) {
1887   DCHECK_NOT_NULL(on_not_smi_result);
1888   DCHECK(!src2.AddressUsesRegister(dst));
1889   SmiSubHelper<Operand>(this, dst, src1, src2, on_not_smi_result, near_jump);
1890 }
1891
1892
1893 template<class T>
1894 static void SmiSubNoOverflowHelper(MacroAssembler* masm,
1895                                    Register dst,
1896                                    Register src1,
1897                                    T src2) {
1898   // No overflow checking. Use only when it's known that
1899   // overflowing is impossible (e.g., subtracting two positive smis).
1900   if (!dst.is(src1)) {
1901     masm->movp(dst, src1);
1902   }
1903   masm->subp(dst, src2);
1904   masm->Assert(no_overflow, kSmiSubtractionOverflow);
1905 }
1906
1907
1908 void MacroAssembler::SmiSub(Register dst, Register src1, Register src2) {
1909   DCHECK(!dst.is(src2));
1910   SmiSubNoOverflowHelper<Register>(this, dst, src1, src2);
1911 }
1912
1913
1914 void MacroAssembler::SmiSub(Register dst,
1915                             Register src1,
1916                             const Operand& src2) {
1917   SmiSubNoOverflowHelper<Operand>(this, dst, src1, src2);
1918 }
1919
1920
1921 void MacroAssembler::SmiMul(Register dst,
1922                             Register src1,
1923                             Register src2,
1924                             Label* on_not_smi_result,
1925                             Label::Distance near_jump) {
1926   DCHECK(!dst.is(src2));
1927   DCHECK(!dst.is(kScratchRegister));
1928   DCHECK(!src1.is(kScratchRegister));
1929   DCHECK(!src2.is(kScratchRegister));
1930
1931   if (dst.is(src1)) {
1932     Label failure, zero_correct_result;
1933     movp(kScratchRegister, src1);  // Create backup for later testing.
1934     SmiToInteger64(dst, src1);
1935     imulp(dst, src2);
1936     j(overflow, &failure, Label::kNear);
1937
1938     // Check for negative zero result.  If product is zero, and one
1939     // argument is negative, go to slow case.
1940     Label correct_result;
1941     testp(dst, dst);
1942     j(not_zero, &correct_result, Label::kNear);
1943
1944     movp(dst, kScratchRegister);
1945     xorp(dst, src2);
1946     // Result was positive zero.
1947     j(positive, &zero_correct_result, Label::kNear);
1948
1949     bind(&failure);  // Reused failure exit, restores src1.
1950     movp(src1, kScratchRegister);
1951     jmp(on_not_smi_result, near_jump);
1952
1953     bind(&zero_correct_result);
1954     Set(dst, 0);
1955
1956     bind(&correct_result);
1957   } else {
1958     SmiToInteger64(dst, src1);
1959     imulp(dst, src2);
1960     j(overflow, on_not_smi_result, near_jump);
1961     // Check for negative zero result.  If product is zero, and one
1962     // argument is negative, go to slow case.
1963     Label correct_result;
1964     testp(dst, dst);
1965     j(not_zero, &correct_result, Label::kNear);
1966     // One of src1 and src2 is zero, the check whether the other is
1967     // negative.
1968     movp(kScratchRegister, src1);
1969     xorp(kScratchRegister, src2);
1970     j(negative, on_not_smi_result, near_jump);
1971     bind(&correct_result);
1972   }
1973 }
1974
1975
1976 void MacroAssembler::SmiDiv(Register dst,
1977                             Register src1,
1978                             Register src2,
1979                             Label* on_not_smi_result,
1980                             Label::Distance near_jump) {
1981   DCHECK(!src1.is(kScratchRegister));
1982   DCHECK(!src2.is(kScratchRegister));
1983   DCHECK(!dst.is(kScratchRegister));
1984   DCHECK(!src2.is(rax));
1985   DCHECK(!src2.is(rdx));
1986   DCHECK(!src1.is(rdx));
1987
1988   // Check for 0 divisor (result is +/-Infinity).
1989   testp(src2, src2);
1990   j(zero, on_not_smi_result, near_jump);
1991
1992   if (src1.is(rax)) {
1993     movp(kScratchRegister, src1);
1994   }
1995   SmiToInteger32(rax, src1);
1996   // We need to rule out dividing Smi::kMinValue by -1, since that would
1997   // overflow in idiv and raise an exception.
1998   // We combine this with negative zero test (negative zero only happens
1999   // when dividing zero by a negative number).
2000
2001   // We overshoot a little and go to slow case if we divide min-value
2002   // by any negative value, not just -1.
2003   Label safe_div;
2004   testl(rax, Immediate(~Smi::kMinValue));
2005   j(not_zero, &safe_div, Label::kNear);
2006   testp(src2, src2);
2007   if (src1.is(rax)) {
2008     j(positive, &safe_div, Label::kNear);
2009     movp(src1, kScratchRegister);
2010     jmp(on_not_smi_result, near_jump);
2011   } else {
2012     j(negative, on_not_smi_result, near_jump);
2013   }
2014   bind(&safe_div);
2015
2016   SmiToInteger32(src2, src2);
2017   // Sign extend src1 into edx:eax.
2018   cdq();
2019   idivl(src2);
2020   Integer32ToSmi(src2, src2);
2021   // Check that the remainder is zero.
2022   testl(rdx, rdx);
2023   if (src1.is(rax)) {
2024     Label smi_result;
2025     j(zero, &smi_result, Label::kNear);
2026     movp(src1, kScratchRegister);
2027     jmp(on_not_smi_result, near_jump);
2028     bind(&smi_result);
2029   } else {
2030     j(not_zero, on_not_smi_result, near_jump);
2031   }
2032   if (!dst.is(src1) && src1.is(rax)) {
2033     movp(src1, kScratchRegister);
2034   }
2035   Integer32ToSmi(dst, rax);
2036 }
2037
2038
2039 void MacroAssembler::SmiMod(Register dst,
2040                             Register src1,
2041                             Register src2,
2042                             Label* on_not_smi_result,
2043                             Label::Distance near_jump) {
2044   DCHECK(!dst.is(kScratchRegister));
2045   DCHECK(!src1.is(kScratchRegister));
2046   DCHECK(!src2.is(kScratchRegister));
2047   DCHECK(!src2.is(rax));
2048   DCHECK(!src2.is(rdx));
2049   DCHECK(!src1.is(rdx));
2050   DCHECK(!src1.is(src2));
2051
2052   testp(src2, src2);
2053   j(zero, on_not_smi_result, near_jump);
2054
2055   if (src1.is(rax)) {
2056     movp(kScratchRegister, src1);
2057   }
2058   SmiToInteger32(rax, src1);
2059   SmiToInteger32(src2, src2);
2060
2061   // Test for the edge case of dividing Smi::kMinValue by -1 (will overflow).
2062   Label safe_div;
2063   cmpl(rax, Immediate(Smi::kMinValue));
2064   j(not_equal, &safe_div, Label::kNear);
2065   cmpl(src2, Immediate(-1));
2066   j(not_equal, &safe_div, Label::kNear);
2067   // Retag inputs and go slow case.
2068   Integer32ToSmi(src2, src2);
2069   if (src1.is(rax)) {
2070     movp(src1, kScratchRegister);
2071   }
2072   jmp(on_not_smi_result, near_jump);
2073   bind(&safe_div);
2074
2075   // Sign extend eax into edx:eax.
2076   cdq();
2077   idivl(src2);
2078   // Restore smi tags on inputs.
2079   Integer32ToSmi(src2, src2);
2080   if (src1.is(rax)) {
2081     movp(src1, kScratchRegister);
2082   }
2083   // Check for a negative zero result.  If the result is zero, and the
2084   // dividend is negative, go slow to return a floating point negative zero.
2085   Label smi_result;
2086   testl(rdx, rdx);
2087   j(not_zero, &smi_result, Label::kNear);
2088   testp(src1, src1);
2089   j(negative, on_not_smi_result, near_jump);
2090   bind(&smi_result);
2091   Integer32ToSmi(dst, rdx);
2092 }
2093
2094
2095 void MacroAssembler::SmiNot(Register dst, Register src) {
2096   DCHECK(!dst.is(kScratchRegister));
2097   DCHECK(!src.is(kScratchRegister));
2098   if (SmiValuesAre32Bits()) {
2099     // Set tag and padding bits before negating, so that they are zero
2100     // afterwards.
2101     movl(kScratchRegister, Immediate(~0));
2102   } else {
2103     DCHECK(SmiValuesAre31Bits());
2104     movl(kScratchRegister, Immediate(1));
2105   }
2106   if (dst.is(src)) {
2107     xorp(dst, kScratchRegister);
2108   } else {
2109     leap(dst, Operand(src, kScratchRegister, times_1, 0));
2110   }
2111   notp(dst);
2112 }
2113
2114
2115 void MacroAssembler::SmiAnd(Register dst, Register src1, Register src2) {
2116   DCHECK(!dst.is(src2));
2117   if (!dst.is(src1)) {
2118     movp(dst, src1);
2119   }
2120   andp(dst, src2);
2121 }
2122
2123
2124 void MacroAssembler::SmiAndConstant(Register dst, Register src, Smi* constant) {
2125   if (constant->value() == 0) {
2126     Set(dst, 0);
2127   } else if (dst.is(src)) {
2128     DCHECK(!dst.is(kScratchRegister));
2129     Register constant_reg = GetSmiConstant(constant);
2130     andp(dst, constant_reg);
2131   } else {
2132     LoadSmiConstant(dst, constant);
2133     andp(dst, src);
2134   }
2135 }
2136
2137
2138 void MacroAssembler::SmiOr(Register dst, Register src1, Register src2) {
2139   if (!dst.is(src1)) {
2140     DCHECK(!src1.is(src2));
2141     movp(dst, src1);
2142   }
2143   orp(dst, src2);
2144 }
2145
2146
2147 void MacroAssembler::SmiOrConstant(Register dst, Register src, Smi* constant) {
2148   if (dst.is(src)) {
2149     DCHECK(!dst.is(kScratchRegister));
2150     Register constant_reg = GetSmiConstant(constant);
2151     orp(dst, constant_reg);
2152   } else {
2153     LoadSmiConstant(dst, constant);
2154     orp(dst, src);
2155   }
2156 }
2157
2158
2159 void MacroAssembler::SmiXor(Register dst, Register src1, Register src2) {
2160   if (!dst.is(src1)) {
2161     DCHECK(!src1.is(src2));
2162     movp(dst, src1);
2163   }
2164   xorp(dst, src2);
2165 }
2166
2167
2168 void MacroAssembler::SmiXorConstant(Register dst, Register src, Smi* constant) {
2169   if (dst.is(src)) {
2170     DCHECK(!dst.is(kScratchRegister));
2171     Register constant_reg = GetSmiConstant(constant);
2172     xorp(dst, constant_reg);
2173   } else {
2174     LoadSmiConstant(dst, constant);
2175     xorp(dst, src);
2176   }
2177 }
2178
2179
2180 void MacroAssembler::SmiShiftArithmeticRightConstant(Register dst,
2181                                                      Register src,
2182                                                      int shift_value) {
2183   DCHECK(is_uint5(shift_value));
2184   if (shift_value > 0) {
2185     if (dst.is(src)) {
2186       sarp(dst, Immediate(shift_value + kSmiShift));
2187       shlp(dst, Immediate(kSmiShift));
2188     } else {
2189       UNIMPLEMENTED();  // Not used.
2190     }
2191   }
2192 }
2193
2194
2195 void MacroAssembler::SmiShiftLeftConstant(Register dst,
2196                                           Register src,
2197                                           int shift_value,
2198                                           Label* on_not_smi_result,
2199                                           Label::Distance near_jump) {
2200   if (SmiValuesAre32Bits()) {
2201     if (!dst.is(src)) {
2202       movp(dst, src);
2203     }
2204     if (shift_value > 0) {
2205       // Shift amount specified by lower 5 bits, not six as the shl opcode.
2206       shlq(dst, Immediate(shift_value & 0x1f));
2207     }
2208   } else {
2209     DCHECK(SmiValuesAre31Bits());
2210     if (dst.is(src)) {
2211       UNIMPLEMENTED();  // Not used.
2212     } else {
2213       SmiToInteger32(dst, src);
2214       shll(dst, Immediate(shift_value));
2215       JumpIfNotValidSmiValue(dst, on_not_smi_result, near_jump);
2216       Integer32ToSmi(dst, dst);
2217     }
2218   }
2219 }
2220
2221
2222 void MacroAssembler::SmiShiftLogicalRightConstant(
2223     Register dst, Register src, int shift_value,
2224     Label* on_not_smi_result, Label::Distance near_jump) {
2225   // Logic right shift interprets its result as an *unsigned* number.
2226   if (dst.is(src)) {
2227     UNIMPLEMENTED();  // Not used.
2228   } else {
2229     if (shift_value == 0) {
2230       testp(src, src);
2231       j(negative, on_not_smi_result, near_jump);
2232     }
2233     if (SmiValuesAre32Bits()) {
2234       movp(dst, src);
2235       shrp(dst, Immediate(shift_value + kSmiShift));
2236       shlp(dst, Immediate(kSmiShift));
2237     } else {
2238       DCHECK(SmiValuesAre31Bits());
2239       SmiToInteger32(dst, src);
2240       shrp(dst, Immediate(shift_value));
2241       JumpIfUIntNotValidSmiValue(dst, on_not_smi_result, near_jump);
2242       Integer32ToSmi(dst, dst);
2243     }
2244   }
2245 }
2246
2247
2248 void MacroAssembler::SmiShiftLeft(Register dst,
2249                                   Register src1,
2250                                   Register src2,
2251                                   Label* on_not_smi_result,
2252                                   Label::Distance near_jump) {
2253   if (SmiValuesAre32Bits()) {
2254     DCHECK(!dst.is(rcx));
2255     if (!dst.is(src1)) {
2256       movp(dst, src1);
2257     }
2258     // Untag shift amount.
2259     SmiToInteger32(rcx, src2);
2260     // Shift amount specified by lower 5 bits, not six as the shl opcode.
2261     andp(rcx, Immediate(0x1f));
2262     shlq_cl(dst);
2263   } else {
2264     DCHECK(SmiValuesAre31Bits());
2265     DCHECK(!dst.is(kScratchRegister));
2266     DCHECK(!src1.is(kScratchRegister));
2267     DCHECK(!src2.is(kScratchRegister));
2268     DCHECK(!dst.is(src2));
2269     DCHECK(!dst.is(rcx));
2270
2271     if (src1.is(rcx) || src2.is(rcx)) {
2272       movq(kScratchRegister, rcx);
2273     }
2274     if (dst.is(src1)) {
2275       UNIMPLEMENTED();  // Not used.
2276     } else {
2277       Label valid_result;
2278       SmiToInteger32(dst, src1);
2279       SmiToInteger32(rcx, src2);
2280       shll_cl(dst);
2281       JumpIfValidSmiValue(dst, &valid_result, Label::kNear);
2282       // As src1 or src2 could not be dst, we do not need to restore them for
2283       // clobbering dst.
2284       if (src1.is(rcx) || src2.is(rcx)) {
2285         if (src1.is(rcx)) {
2286           movq(src1, kScratchRegister);
2287         } else {
2288           movq(src2, kScratchRegister);
2289         }
2290       }
2291       jmp(on_not_smi_result, near_jump);
2292       bind(&valid_result);
2293       Integer32ToSmi(dst, dst);
2294     }
2295   }
2296 }
2297
2298
2299 void MacroAssembler::SmiShiftLogicalRight(Register dst,
2300                                           Register src1,
2301                                           Register src2,
2302                                           Label* on_not_smi_result,
2303                                           Label::Distance near_jump) {
2304   DCHECK(!dst.is(kScratchRegister));
2305   DCHECK(!src1.is(kScratchRegister));
2306   DCHECK(!src2.is(kScratchRegister));
2307   DCHECK(!dst.is(src2));
2308   DCHECK(!dst.is(rcx));
2309   if (src1.is(rcx) || src2.is(rcx)) {
2310     movq(kScratchRegister, rcx);
2311   }
2312   if (dst.is(src1)) {
2313     UNIMPLEMENTED();  // Not used.
2314   } else {
2315     Label valid_result;
2316     SmiToInteger32(dst, src1);
2317     SmiToInteger32(rcx, src2);
2318     shrl_cl(dst);
2319     JumpIfUIntValidSmiValue(dst, &valid_result, Label::kNear);
2320     // As src1 or src2 could not be dst, we do not need to restore them for
2321     // clobbering dst.
2322     if (src1.is(rcx) || src2.is(rcx)) {
2323       if (src1.is(rcx)) {
2324         movq(src1, kScratchRegister);
2325       } else {
2326         movq(src2, kScratchRegister);
2327       }
2328      }
2329     jmp(on_not_smi_result, near_jump);
2330     bind(&valid_result);
2331     Integer32ToSmi(dst, dst);
2332   }
2333 }
2334
2335
2336 void MacroAssembler::SmiShiftArithmeticRight(Register dst,
2337                                              Register src1,
2338                                              Register src2) {
2339   DCHECK(!dst.is(kScratchRegister));
2340   DCHECK(!src1.is(kScratchRegister));
2341   DCHECK(!src2.is(kScratchRegister));
2342   DCHECK(!dst.is(rcx));
2343
2344   SmiToInteger32(rcx, src2);
2345   if (!dst.is(src1)) {
2346     movp(dst, src1);
2347   }
2348   SmiToInteger32(dst, dst);
2349   sarl_cl(dst);
2350   Integer32ToSmi(dst, dst);
2351 }
2352
2353
2354 void MacroAssembler::SelectNonSmi(Register dst,
2355                                   Register src1,
2356                                   Register src2,
2357                                   Label* on_not_smis,
2358                                   Label::Distance near_jump) {
2359   DCHECK(!dst.is(kScratchRegister));
2360   DCHECK(!src1.is(kScratchRegister));
2361   DCHECK(!src2.is(kScratchRegister));
2362   DCHECK(!dst.is(src1));
2363   DCHECK(!dst.is(src2));
2364   // Both operands must not be smis.
2365 #ifdef DEBUG
2366   Condition not_both_smis = NegateCondition(CheckBothSmi(src1, src2));
2367   Check(not_both_smis, kBothRegistersWereSmisInSelectNonSmi);
2368 #endif
2369   STATIC_ASSERT(kSmiTag == 0);
2370   DCHECK_EQ(0, Smi::FromInt(0));
2371   movl(kScratchRegister, Immediate(kSmiTagMask));
2372   andp(kScratchRegister, src1);
2373   testl(kScratchRegister, src2);
2374   // If non-zero then both are smis.
2375   j(not_zero, on_not_smis, near_jump);
2376
2377   // Exactly one operand is a smi.
2378   DCHECK_EQ(1, static_cast<int>(kSmiTagMask));
2379   // kScratchRegister still holds src1 & kSmiTag, which is either zero or one.
2380   subp(kScratchRegister, Immediate(1));
2381   // If src1 is a smi, then scratch register all 1s, else it is all 0s.
2382   movp(dst, src1);
2383   xorp(dst, src2);
2384   andp(dst, kScratchRegister);
2385   // If src1 is a smi, dst holds src1 ^ src2, else it is zero.
2386   xorp(dst, src1);
2387   // If src1 is a smi, dst is src2, else it is src1, i.e., the non-smi.
2388 }
2389
2390
2391 SmiIndex MacroAssembler::SmiToIndex(Register dst,
2392                                     Register src,
2393                                     int shift) {
2394   if (SmiValuesAre32Bits()) {
2395     DCHECK(is_uint6(shift));
2396     // There is a possible optimization if shift is in the range 60-63, but that
2397     // will (and must) never happen.
2398     if (!dst.is(src)) {
2399       movp(dst, src);
2400     }
2401     if (shift < kSmiShift) {
2402       sarp(dst, Immediate(kSmiShift - shift));
2403     } else {
2404       shlp(dst, Immediate(shift - kSmiShift));
2405     }
2406     return SmiIndex(dst, times_1);
2407   } else {
2408     DCHECK(SmiValuesAre31Bits());
2409     DCHECK(shift >= times_1 && shift <= (static_cast<int>(times_8) + 1));
2410     if (!dst.is(src)) {
2411       movp(dst, src);
2412     }
2413     // We have to sign extend the index register to 64-bit as the SMI might
2414     // be negative.
2415     movsxlq(dst, dst);
2416     if (shift == times_1) {
2417       sarq(dst, Immediate(kSmiShift));
2418       return SmiIndex(dst, times_1);
2419     }
2420     return SmiIndex(dst, static_cast<ScaleFactor>(shift - 1));
2421   }
2422 }
2423
2424
2425 SmiIndex MacroAssembler::SmiToNegativeIndex(Register dst,
2426                                             Register src,
2427                                             int shift) {
2428   if (SmiValuesAre32Bits()) {
2429     // Register src holds a positive smi.
2430     DCHECK(is_uint6(shift));
2431     if (!dst.is(src)) {
2432       movp(dst, src);
2433     }
2434     negp(dst);
2435     if (shift < kSmiShift) {
2436       sarp(dst, Immediate(kSmiShift - shift));
2437     } else {
2438       shlp(dst, Immediate(shift - kSmiShift));
2439     }
2440     return SmiIndex(dst, times_1);
2441   } else {
2442     DCHECK(SmiValuesAre31Bits());
2443     DCHECK(shift >= times_1 && shift <= (static_cast<int>(times_8) + 1));
2444     if (!dst.is(src)) {
2445       movp(dst, src);
2446     }
2447     negq(dst);
2448     if (shift == times_1) {
2449       sarq(dst, Immediate(kSmiShift));
2450       return SmiIndex(dst, times_1);
2451     }
2452     return SmiIndex(dst, static_cast<ScaleFactor>(shift - 1));
2453   }
2454 }
2455
2456
2457 void MacroAssembler::AddSmiField(Register dst, const Operand& src) {
2458   if (SmiValuesAre32Bits()) {
2459     DCHECK_EQ(0, kSmiShift % kBitsPerByte);
2460     addl(dst, Operand(src, kSmiShift / kBitsPerByte));
2461   } else {
2462     DCHECK(SmiValuesAre31Bits());
2463     SmiToInteger32(kScratchRegister, src);
2464     addl(dst, kScratchRegister);
2465   }
2466 }
2467
2468
2469 void MacroAssembler::Push(Smi* source) {
2470   intptr_t smi = reinterpret_cast<intptr_t>(source);
2471   if (is_int32(smi)) {
2472     Push(Immediate(static_cast<int32_t>(smi)));
2473   } else {
2474     Register constant = GetSmiConstant(source);
2475     Push(constant);
2476   }
2477 }
2478
2479
2480 void MacroAssembler::PushRegisterAsTwoSmis(Register src, Register scratch) {
2481   DCHECK(!src.is(scratch));
2482   movp(scratch, src);
2483   // High bits.
2484   shrp(src, Immediate(kPointerSize * kBitsPerByte - kSmiShift));
2485   shlp(src, Immediate(kSmiShift));
2486   Push(src);
2487   // Low bits.
2488   shlp(scratch, Immediate(kSmiShift));
2489   Push(scratch);
2490 }
2491
2492
2493 void MacroAssembler::PopRegisterAsTwoSmis(Register dst, Register scratch) {
2494   DCHECK(!dst.is(scratch));
2495   Pop(scratch);
2496   // Low bits.
2497   shrp(scratch, Immediate(kSmiShift));
2498   Pop(dst);
2499   shrp(dst, Immediate(kSmiShift));
2500   // High bits.
2501   shlp(dst, Immediate(kPointerSize * kBitsPerByte - kSmiShift));
2502   orp(dst, scratch);
2503 }
2504
2505
2506 void MacroAssembler::Test(const Operand& src, Smi* source) {
2507   if (SmiValuesAre32Bits()) {
2508     testl(Operand(src, kIntSize), Immediate(source->value()));
2509   } else {
2510     DCHECK(SmiValuesAre31Bits());
2511     testl(src, Immediate(source));
2512   }
2513 }
2514
2515
2516 // ----------------------------------------------------------------------------
2517
2518
2519 void MacroAssembler::LookupNumberStringCache(Register object,
2520                                              Register result,
2521                                              Register scratch1,
2522                                              Register scratch2,
2523                                              Label* not_found) {
2524   // Use of registers. Register result is used as a temporary.
2525   Register number_string_cache = result;
2526   Register mask = scratch1;
2527   Register scratch = scratch2;
2528
2529   // Load the number string cache.
2530   LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2531
2532   // Make the hash mask from the length of the number string cache. It
2533   // contains two elements (number and string) for each cache entry.
2534   SmiToInteger32(
2535       mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2536   shrl(mask, Immediate(1));
2537   subp(mask, Immediate(1));  // Make mask.
2538
2539   // Calculate the entry in the number string cache. The hash value in the
2540   // number string cache for smis is just the smi value, and the hash for
2541   // doubles is the xor of the upper and lower words. See
2542   // Heap::GetNumberStringCache.
2543   Label is_smi;
2544   Label load_result_from_cache;
2545   JumpIfSmi(object, &is_smi);
2546   CheckMap(object,
2547            isolate()->factory()->heap_number_map(),
2548            not_found,
2549            DONT_DO_SMI_CHECK);
2550
2551   STATIC_ASSERT(8 == kDoubleSize);
2552   movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2553   xorp(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2554   andp(scratch, mask);
2555   // Each entry in string cache consists of two pointer sized fields,
2556   // but times_twice_pointer_size (multiplication by 16) scale factor
2557   // is not supported by addrmode on x64 platform.
2558   // So we have to premultiply entry index before lookup.
2559   shlp(scratch, Immediate(kPointerSizeLog2 + 1));
2560
2561   Register index = scratch;
2562   Register probe = mask;
2563   movp(probe,
2564        FieldOperand(number_string_cache,
2565                     index,
2566                     times_1,
2567                     FixedArray::kHeaderSize));
2568   JumpIfSmi(probe, not_found);
2569   movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2570   ucomisd(xmm0, FieldOperand(probe, HeapNumber::kValueOffset));
2571   j(parity_even, not_found);  // Bail out if NaN is involved.
2572   j(not_equal, not_found);  // The cache did not contain this value.
2573   jmp(&load_result_from_cache);
2574
2575   bind(&is_smi);
2576   SmiToInteger32(scratch, object);
2577   andp(scratch, mask);
2578   // Each entry in string cache consists of two pointer sized fields,
2579   // but times_twice_pointer_size (multiplication by 16) scale factor
2580   // is not supported by addrmode on x64 platform.
2581   // So we have to premultiply entry index before lookup.
2582   shlp(scratch, Immediate(kPointerSizeLog2 + 1));
2583
2584   // Check if the entry is the smi we are looking for.
2585   cmpp(object,
2586        FieldOperand(number_string_cache,
2587                     index,
2588                     times_1,
2589                     FixedArray::kHeaderSize));
2590   j(not_equal, not_found);
2591
2592   // Get the result from the cache.
2593   bind(&load_result_from_cache);
2594   movp(result,
2595        FieldOperand(number_string_cache,
2596                     index,
2597                     times_1,
2598                     FixedArray::kHeaderSize + kPointerSize));
2599   IncrementCounter(isolate()->counters()->number_to_string_native(), 1);
2600 }
2601
2602
2603 void MacroAssembler::JumpIfNotString(Register object,
2604                                      Register object_map,
2605                                      Label* not_string,
2606                                      Label::Distance near_jump) {
2607   Condition is_smi = CheckSmi(object);
2608   j(is_smi, not_string, near_jump);
2609   CmpObjectType(object, FIRST_NONSTRING_TYPE, object_map);
2610   j(above_equal, not_string, near_jump);
2611 }
2612
2613
2614 void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(
2615     Register first_object, Register second_object, Register scratch1,
2616     Register scratch2, Label* on_fail, Label::Distance near_jump) {
2617   // Check that both objects are not smis.
2618   Condition either_smi = CheckEitherSmi(first_object, second_object);
2619   j(either_smi, on_fail, near_jump);
2620
2621   // Load instance type for both strings.
2622   movp(scratch1, FieldOperand(first_object, HeapObject::kMapOffset));
2623   movp(scratch2, FieldOperand(second_object, HeapObject::kMapOffset));
2624   movzxbl(scratch1, FieldOperand(scratch1, Map::kInstanceTypeOffset));
2625   movzxbl(scratch2, FieldOperand(scratch2, Map::kInstanceTypeOffset));
2626
2627   // Check that both are flat one-byte strings.
2628   DCHECK(kNotStringTag != 0);
2629   const int kFlatOneByteStringMask =
2630       kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2631   const int kFlatOneByteStringTag =
2632       kStringTag | kOneByteStringTag | kSeqStringTag;
2633
2634   andl(scratch1, Immediate(kFlatOneByteStringMask));
2635   andl(scratch2, Immediate(kFlatOneByteStringMask));
2636   // Interleave the bits to check both scratch1 and scratch2 in one test.
2637   DCHECK_EQ(0, kFlatOneByteStringMask & (kFlatOneByteStringMask << 3));
2638   leap(scratch1, Operand(scratch1, scratch2, times_8, 0));
2639   cmpl(scratch1,
2640        Immediate(kFlatOneByteStringTag + (kFlatOneByteStringTag << 3)));
2641   j(not_equal, on_fail, near_jump);
2642 }
2643
2644
2645 void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(
2646     Register instance_type, Register scratch, Label* failure,
2647     Label::Distance near_jump) {
2648   if (!scratch.is(instance_type)) {
2649     movl(scratch, instance_type);
2650   }
2651
2652   const int kFlatOneByteStringMask =
2653       kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2654
2655   andl(scratch, Immediate(kFlatOneByteStringMask));
2656   cmpl(scratch, Immediate(kStringTag | kSeqStringTag | kOneByteStringTag));
2657   j(not_equal, failure, near_jump);
2658 }
2659
2660
2661 void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialOneByte(
2662     Register first_object_instance_type, Register second_object_instance_type,
2663     Register scratch1, Register scratch2, Label* on_fail,
2664     Label::Distance near_jump) {
2665   // Load instance type for both strings.
2666   movp(scratch1, first_object_instance_type);
2667   movp(scratch2, second_object_instance_type);
2668
2669   // Check that both are flat one-byte strings.
2670   DCHECK(kNotStringTag != 0);
2671   const int kFlatOneByteStringMask =
2672       kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
2673   const int kFlatOneByteStringTag =
2674       kStringTag | kOneByteStringTag | kSeqStringTag;
2675
2676   andl(scratch1, Immediate(kFlatOneByteStringMask));
2677   andl(scratch2, Immediate(kFlatOneByteStringMask));
2678   // Interleave the bits to check both scratch1 and scratch2 in one test.
2679   DCHECK_EQ(0, kFlatOneByteStringMask & (kFlatOneByteStringMask << 3));
2680   leap(scratch1, Operand(scratch1, scratch2, times_8, 0));
2681   cmpl(scratch1,
2682        Immediate(kFlatOneByteStringTag + (kFlatOneByteStringTag << 3)));
2683   j(not_equal, on_fail, near_jump);
2684 }
2685
2686
2687 template<class T>
2688 static void JumpIfNotUniqueNameHelper(MacroAssembler* masm,
2689                                       T operand_or_register,
2690                                       Label* not_unique_name,
2691                                       Label::Distance distance) {
2692   STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2693   Label succeed;
2694   masm->testb(operand_or_register,
2695               Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2696   masm->j(zero, &succeed, Label::kNear);
2697   masm->cmpb(operand_or_register, Immediate(static_cast<uint8_t>(SYMBOL_TYPE)));
2698   masm->j(not_equal, not_unique_name, distance);
2699
2700   masm->bind(&succeed);
2701 }
2702
2703
2704 void MacroAssembler::JumpIfNotUniqueNameInstanceType(Operand operand,
2705                                                      Label* not_unique_name,
2706                                                      Label::Distance distance) {
2707   JumpIfNotUniqueNameHelper<Operand>(this, operand, not_unique_name, distance);
2708 }
2709
2710
2711 void MacroAssembler::JumpIfNotUniqueNameInstanceType(Register reg,
2712                                                      Label* not_unique_name,
2713                                                      Label::Distance distance) {
2714   JumpIfNotUniqueNameHelper<Register>(this, reg, not_unique_name, distance);
2715 }
2716
2717
2718 void MacroAssembler::Move(Register dst, Register src) {
2719   if (!dst.is(src)) {
2720     movp(dst, src);
2721   }
2722 }
2723
2724
2725 void MacroAssembler::Move(Register dst, Handle<Object> source) {
2726   AllowDeferredHandleDereference smi_check;
2727   if (source->IsSmi()) {
2728     Move(dst, Smi::cast(*source));
2729   } else {
2730     MoveHeapObject(dst, source);
2731   }
2732 }
2733
2734
2735 void MacroAssembler::Move(const Operand& dst, Handle<Object> source) {
2736   AllowDeferredHandleDereference smi_check;
2737   if (source->IsSmi()) {
2738     Move(dst, Smi::cast(*source));
2739   } else {
2740     MoveHeapObject(kScratchRegister, source);
2741     movp(dst, kScratchRegister);
2742   }
2743 }
2744
2745
2746 void MacroAssembler::Move(XMMRegister dst, uint32_t src) {
2747   if (src == 0) {
2748     xorps(dst, dst);
2749   } else {
2750     unsigned cnt = base::bits::CountPopulation32(src);
2751     unsigned nlz = base::bits::CountLeadingZeros32(src);
2752     unsigned ntz = base::bits::CountTrailingZeros32(src);
2753     if (nlz + cnt + ntz == 32) {
2754       pcmpeqd(dst, dst);
2755       if (ntz == 0) {
2756         psrld(dst, 32 - cnt);
2757       } else {
2758         pslld(dst, 32 - cnt);
2759         if (nlz != 0) psrld(dst, nlz);
2760       }
2761     } else {
2762       movl(kScratchRegister, Immediate(src));
2763       movq(dst, kScratchRegister);
2764     }
2765   }
2766 }
2767
2768
2769 void MacroAssembler::Move(XMMRegister dst, uint64_t src) {
2770   uint32_t lower = static_cast<uint32_t>(src);
2771   uint32_t upper = static_cast<uint32_t>(src >> 32);
2772   if (upper == 0) {
2773     Move(dst, lower);
2774   } else {
2775     unsigned cnt = base::bits::CountPopulation64(src);
2776     unsigned nlz = base::bits::CountLeadingZeros64(src);
2777     unsigned ntz = base::bits::CountTrailingZeros64(src);
2778     if (nlz + cnt + ntz == 64) {
2779       pcmpeqd(dst, dst);
2780       if (ntz == 0) {
2781         psrlq(dst, 64 - cnt);
2782       } else {
2783         psllq(dst, 64 - cnt);
2784         if (nlz != 0) psrlq(dst, nlz);
2785       }
2786     } else if (lower == 0) {
2787       Move(dst, upper);
2788       psllq(dst, 32);
2789     } else {
2790       movq(kScratchRegister, src);
2791       movq(dst, kScratchRegister);
2792     }
2793   }
2794 }
2795
2796
2797 void MacroAssembler::Cmp(Register dst, Handle<Object> source) {
2798   AllowDeferredHandleDereference smi_check;
2799   if (source->IsSmi()) {
2800     Cmp(dst, Smi::cast(*source));
2801   } else {
2802     MoveHeapObject(kScratchRegister, source);
2803     cmpp(dst, kScratchRegister);
2804   }
2805 }
2806
2807
2808 void MacroAssembler::Cmp(const Operand& dst, Handle<Object> source) {
2809   AllowDeferredHandleDereference smi_check;
2810   if (source->IsSmi()) {
2811     Cmp(dst, Smi::cast(*source));
2812   } else {
2813     MoveHeapObject(kScratchRegister, source);
2814     cmpp(dst, kScratchRegister);
2815   }
2816 }
2817
2818
2819 void MacroAssembler::Push(Handle<Object> source) {
2820   AllowDeferredHandleDereference smi_check;
2821   if (source->IsSmi()) {
2822     Push(Smi::cast(*source));
2823   } else {
2824     MoveHeapObject(kScratchRegister, source);
2825     Push(kScratchRegister);
2826   }
2827 }
2828
2829
2830 void MacroAssembler::MoveHeapObject(Register result,
2831                                     Handle<Object> object) {
2832   AllowDeferredHandleDereference using_raw_address;
2833   DCHECK(object->IsHeapObject());
2834   if (isolate()->heap()->InNewSpace(*object)) {
2835     Handle<Cell> cell = isolate()->factory()->NewCell(object);
2836     Move(result, cell, RelocInfo::CELL);
2837     movp(result, Operand(result, 0));
2838   } else {
2839     Move(result, object, RelocInfo::EMBEDDED_OBJECT);
2840   }
2841 }
2842
2843
2844 void MacroAssembler::LoadGlobalCell(Register dst, Handle<Cell> cell) {
2845   if (dst.is(rax)) {
2846     AllowDeferredHandleDereference embedding_raw_address;
2847     load_rax(cell.location(), RelocInfo::CELL);
2848   } else {
2849     Move(dst, cell, RelocInfo::CELL);
2850     movp(dst, Operand(dst, 0));
2851   }
2852 }
2853
2854
2855 void MacroAssembler::Drop(int stack_elements) {
2856   if (stack_elements > 0) {
2857     addp(rsp, Immediate(stack_elements * kPointerSize));
2858   }
2859 }
2860
2861
2862 void MacroAssembler::DropUnderReturnAddress(int stack_elements,
2863                                             Register scratch) {
2864   DCHECK(stack_elements > 0);
2865   if (kPointerSize == kInt64Size && stack_elements == 1) {
2866     popq(MemOperand(rsp, 0));
2867     return;
2868   }
2869
2870   PopReturnAddressTo(scratch);
2871   Drop(stack_elements);
2872   PushReturnAddressFrom(scratch);
2873 }
2874
2875
2876 void MacroAssembler::Push(Register src) {
2877   if (kPointerSize == kInt64Size) {
2878     pushq(src);
2879   } else {
2880     // x32 uses 64-bit push for rbp in the prologue.
2881     DCHECK(src.code() != rbp.code());
2882     leal(rsp, Operand(rsp, -4));
2883     movp(Operand(rsp, 0), src);
2884   }
2885 }
2886
2887
2888 void MacroAssembler::Push(const Operand& src) {
2889   if (kPointerSize == kInt64Size) {
2890     pushq(src);
2891   } else {
2892     movp(kScratchRegister, src);
2893     leal(rsp, Operand(rsp, -4));
2894     movp(Operand(rsp, 0), kScratchRegister);
2895   }
2896 }
2897
2898
2899 void MacroAssembler::PushQuad(const Operand& src) {
2900   if (kPointerSize == kInt64Size) {
2901     pushq(src);
2902   } else {
2903     movp(kScratchRegister, src);
2904     pushq(kScratchRegister);
2905   }
2906 }
2907
2908
2909 void MacroAssembler::Push(Immediate value) {
2910   if (kPointerSize == kInt64Size) {
2911     pushq(value);
2912   } else {
2913     leal(rsp, Operand(rsp, -4));
2914     movp(Operand(rsp, 0), value);
2915   }
2916 }
2917
2918
2919 void MacroAssembler::PushImm32(int32_t imm32) {
2920   if (kPointerSize == kInt64Size) {
2921     pushq_imm32(imm32);
2922   } else {
2923     leal(rsp, Operand(rsp, -4));
2924     movp(Operand(rsp, 0), Immediate(imm32));
2925   }
2926 }
2927
2928
2929 void MacroAssembler::Pop(Register dst) {
2930   if (kPointerSize == kInt64Size) {
2931     popq(dst);
2932   } else {
2933     // x32 uses 64-bit pop for rbp in the epilogue.
2934     DCHECK(dst.code() != rbp.code());
2935     movp(dst, Operand(rsp, 0));
2936     leal(rsp, Operand(rsp, 4));
2937   }
2938 }
2939
2940
2941 void MacroAssembler::Pop(const Operand& dst) {
2942   if (kPointerSize == kInt64Size) {
2943     popq(dst);
2944   } else {
2945     Register scratch = dst.AddressUsesRegister(kScratchRegister)
2946         ? kSmiConstantRegister : kScratchRegister;
2947     movp(scratch, Operand(rsp, 0));
2948     movp(dst, scratch);
2949     leal(rsp, Operand(rsp, 4));
2950     if (scratch.is(kSmiConstantRegister)) {
2951       // Restore kSmiConstantRegister.
2952       movp(kSmiConstantRegister,
2953            reinterpret_cast<void*>(Smi::FromInt(kSmiConstantRegisterValue)),
2954            Assembler::RelocInfoNone());
2955     }
2956   }
2957 }
2958
2959
2960 void MacroAssembler::PopQuad(const Operand& dst) {
2961   if (kPointerSize == kInt64Size) {
2962     popq(dst);
2963   } else {
2964     popq(kScratchRegister);
2965     movp(dst, kScratchRegister);
2966   }
2967 }
2968
2969
2970 void MacroAssembler::LoadSharedFunctionInfoSpecialField(Register dst,
2971                                                         Register base,
2972                                                         int offset) {
2973   DCHECK(offset > SharedFunctionInfo::kLengthOffset &&
2974          offset <= SharedFunctionInfo::kSize &&
2975          (((offset - SharedFunctionInfo::kLengthOffset) / kIntSize) % 2 == 1));
2976   if (kPointerSize == kInt64Size) {
2977     movsxlq(dst, FieldOperand(base, offset));
2978   } else {
2979     movp(dst, FieldOperand(base, offset));
2980     SmiToInteger32(dst, dst);
2981   }
2982 }
2983
2984
2985 void MacroAssembler::TestBitSharedFunctionInfoSpecialField(Register base,
2986                                                            int offset,
2987                                                            int bits) {
2988   DCHECK(offset > SharedFunctionInfo::kLengthOffset &&
2989          offset <= SharedFunctionInfo::kSize &&
2990          (((offset - SharedFunctionInfo::kLengthOffset) / kIntSize) % 2 == 1));
2991   if (kPointerSize == kInt32Size) {
2992     // On x32, this field is represented by SMI.
2993     bits += kSmiShift;
2994   }
2995   int byte_offset = bits / kBitsPerByte;
2996   int bit_in_byte = bits & (kBitsPerByte - 1);
2997   testb(FieldOperand(base, offset + byte_offset), Immediate(1 << bit_in_byte));
2998 }
2999
3000
3001 void MacroAssembler::Jump(ExternalReference ext) {
3002   LoadAddress(kScratchRegister, ext);
3003   jmp(kScratchRegister);
3004 }
3005
3006
3007 void MacroAssembler::Jump(const Operand& op) {
3008   if (kPointerSize == kInt64Size) {
3009     jmp(op);
3010   } else {
3011     movp(kScratchRegister, op);
3012     jmp(kScratchRegister);
3013   }
3014 }
3015
3016
3017 void MacroAssembler::Jump(Address destination, RelocInfo::Mode rmode) {
3018   Move(kScratchRegister, destination, rmode);
3019   jmp(kScratchRegister);
3020 }
3021
3022
3023 void MacroAssembler::Jump(Handle<Code> code_object, RelocInfo::Mode rmode) {
3024   // TODO(X64): Inline this
3025   jmp(code_object, rmode);
3026 }
3027
3028
3029 int MacroAssembler::CallSize(ExternalReference ext) {
3030   // Opcode for call kScratchRegister is: Rex.B FF D4 (three bytes).
3031   return LoadAddressSize(ext) +
3032          Assembler::kCallScratchRegisterInstructionLength;
3033 }
3034
3035
3036 void MacroAssembler::Call(ExternalReference ext) {
3037 #ifdef DEBUG
3038   int end_position = pc_offset() + CallSize(ext);
3039 #endif
3040   LoadAddress(kScratchRegister, ext);
3041   call(kScratchRegister);
3042 #ifdef DEBUG
3043   CHECK_EQ(end_position, pc_offset());
3044 #endif
3045 }
3046
3047
3048 void MacroAssembler::Call(const Operand& op) {
3049   if (kPointerSize == kInt64Size) {
3050     call(op);
3051   } else {
3052     movp(kScratchRegister, op);
3053     call(kScratchRegister);
3054   }
3055 }
3056
3057
3058 void MacroAssembler::Call(Address destination, RelocInfo::Mode rmode) {
3059 #ifdef DEBUG
3060   int end_position = pc_offset() + CallSize(destination);
3061 #endif
3062   Move(kScratchRegister, destination, rmode);
3063   call(kScratchRegister);
3064 #ifdef DEBUG
3065   CHECK_EQ(pc_offset(), end_position);
3066 #endif
3067 }
3068
3069
3070 void MacroAssembler::Call(Handle<Code> code_object,
3071                           RelocInfo::Mode rmode,
3072                           TypeFeedbackId ast_id) {
3073 #ifdef DEBUG
3074   int end_position = pc_offset() + CallSize(code_object);
3075 #endif
3076   DCHECK(RelocInfo::IsCodeTarget(rmode) ||
3077       rmode == RelocInfo::CODE_AGE_SEQUENCE);
3078   call(code_object, rmode, ast_id);
3079 #ifdef DEBUG
3080   CHECK_EQ(end_position, pc_offset());
3081 #endif
3082 }
3083
3084
3085 void MacroAssembler::Pushad() {
3086   Push(rax);
3087   Push(rcx);
3088   Push(rdx);
3089   Push(rbx);
3090   // Not pushing rsp or rbp.
3091   Push(rsi);
3092   Push(rdi);
3093   Push(r8);
3094   Push(r9);
3095   // r10 is kScratchRegister.
3096   Push(r11);
3097   // r12 is kSmiConstantRegister.
3098   // r13 is kRootRegister.
3099   Push(r14);
3100   Push(r15);
3101   STATIC_ASSERT(11 == kNumSafepointSavedRegisters);
3102   // Use lea for symmetry with Popad.
3103   int sp_delta =
3104       (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
3105   leap(rsp, Operand(rsp, -sp_delta));
3106 }
3107
3108
3109 void MacroAssembler::Popad() {
3110   // Popad must not change the flags, so use lea instead of addq.
3111   int sp_delta =
3112       (kNumSafepointRegisters - kNumSafepointSavedRegisters) * kPointerSize;
3113   leap(rsp, Operand(rsp, sp_delta));
3114   Pop(r15);
3115   Pop(r14);
3116   Pop(r11);
3117   Pop(r9);
3118   Pop(r8);
3119   Pop(rdi);
3120   Pop(rsi);
3121   Pop(rbx);
3122   Pop(rdx);
3123   Pop(rcx);
3124   Pop(rax);
3125 }
3126
3127
3128 void MacroAssembler::Dropad() {
3129   addp(rsp, Immediate(kNumSafepointRegisters * kPointerSize));
3130 }
3131
3132
3133 // Order general registers are pushed by Pushad:
3134 // rax, rcx, rdx, rbx, rsi, rdi, r8, r9, r11, r14, r15.
3135 const int
3136 MacroAssembler::kSafepointPushRegisterIndices[Register::kNumRegisters] = {
3137     0,
3138     1,
3139     2,
3140     3,
3141     -1,
3142     -1,
3143     4,
3144     5,
3145     6,
3146     7,
3147     -1,
3148     8,
3149     -1,
3150     -1,
3151     9,
3152     10
3153 };
3154
3155
3156 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst,
3157                                                   const Immediate& imm) {
3158   movp(SafepointRegisterSlot(dst), imm);
3159 }
3160
3161
3162 void MacroAssembler::StoreToSafepointRegisterSlot(Register dst, Register src) {
3163   movp(SafepointRegisterSlot(dst), src);
3164 }
3165
3166
3167 void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
3168   movp(dst, SafepointRegisterSlot(src));
3169 }
3170
3171
3172 Operand MacroAssembler::SafepointRegisterSlot(Register reg) {
3173   return Operand(rsp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
3174 }
3175
3176
3177 void MacroAssembler::PushTryHandler(StackHandler::Kind kind,
3178                                     int handler_index) {
3179   // Adjust this code if not the case.
3180   STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3181                                                 kFPOnStackSize);
3182   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3183   STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3184   STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3185   STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3186   STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3187
3188   // We will build up the handler from the bottom by pushing on the stack.
3189   // First push the frame pointer and context.
3190   if (kind == StackHandler::JS_ENTRY) {
3191     // The frame pointer does not point to a JS frame so we save NULL for
3192     // rbp. We expect the code throwing an exception to check rbp before
3193     // dereferencing it to restore the context.
3194     pushq(Immediate(0));  // NULL frame pointer.
3195     Push(Smi::FromInt(0));  // No context.
3196   } else {
3197     pushq(rbp);
3198     Push(rsi);
3199   }
3200
3201   // Push the state and the code object.
3202   unsigned state =
3203       StackHandler::IndexField::encode(handler_index) |
3204       StackHandler::KindField::encode(kind);
3205   Push(Immediate(state));
3206   Push(CodeObject());
3207
3208   // Link the current handler as the next handler.
3209   ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3210   Push(ExternalOperand(handler_address));
3211   // Set this new handler as the current one.
3212   movp(ExternalOperand(handler_address), rsp);
3213 }
3214
3215
3216 void MacroAssembler::PopTryHandler() {
3217   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3218   ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3219   Pop(ExternalOperand(handler_address));
3220   addp(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
3221 }
3222
3223
3224 void MacroAssembler::JumpToHandlerEntry() {
3225   // Compute the handler entry address and jump to it.  The handler table is
3226   // a fixed array of (smi-tagged) code offsets.
3227   // rax = exception, rdi = code object, rdx = state.
3228   movp(rbx, FieldOperand(rdi, Code::kHandlerTableOffset));
3229   shrp(rdx, Immediate(StackHandler::kKindWidth));
3230   movp(rdx,
3231        FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize));
3232   SmiToInteger64(rdx, rdx);
3233   leap(rdi, FieldOperand(rdi, rdx, times_1, Code::kHeaderSize));
3234   jmp(rdi);
3235 }
3236
3237
3238 void MacroAssembler::Throw(Register value) {
3239   // Adjust this code if not the case.
3240   STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3241                                                 kFPOnStackSize);
3242   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3243   STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3244   STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3245   STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3246   STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3247
3248   // The exception is expected in rax.
3249   if (!value.is(rax)) {
3250     movp(rax, value);
3251   }
3252   // Drop the stack pointer to the top of the top handler.
3253   ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3254   movp(rsp, ExternalOperand(handler_address));
3255   // Restore the next handler.
3256   Pop(ExternalOperand(handler_address));
3257
3258   // Remove the code object and state, compute the handler address in rdi.
3259   Pop(rdi);  // Code object.
3260   Pop(rdx);  // Offset and state.
3261
3262   // Restore the context and frame pointer.
3263   Pop(rsi);  // Context.
3264   popq(rbp);  // Frame pointer.
3265
3266   // If the handler is a JS frame, restore the context to the frame.
3267   // (kind == ENTRY) == (rbp == 0) == (rsi == 0), so we could test either
3268   // rbp or rsi.
3269   Label skip;
3270   testp(rsi, rsi);
3271   j(zero, &skip, Label::kNear);
3272   movp(Operand(rbp, StandardFrameConstants::kContextOffset), rsi);
3273   bind(&skip);
3274
3275   JumpToHandlerEntry();
3276 }
3277
3278
3279 void MacroAssembler::ThrowUncatchable(Register value) {
3280   // Adjust this code if not the case.
3281   STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize +
3282                                                 kFPOnStackSize);
3283   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3284   STATIC_ASSERT(StackHandlerConstants::kCodeOffset == 1 * kPointerSize);
3285   STATIC_ASSERT(StackHandlerConstants::kStateOffset == 2 * kPointerSize);
3286   STATIC_ASSERT(StackHandlerConstants::kContextOffset == 3 * kPointerSize);
3287   STATIC_ASSERT(StackHandlerConstants::kFPOffset == 4 * kPointerSize);
3288
3289   // The exception is expected in rax.
3290   if (!value.is(rax)) {
3291     movp(rax, value);
3292   }
3293   // Drop the stack pointer to the top of the top stack handler.
3294   ExternalReference handler_address(Isolate::kHandlerAddress, isolate());
3295   Load(rsp, handler_address);
3296
3297   // Unwind the handlers until the top ENTRY handler is found.
3298   Label fetch_next, check_kind;
3299   jmp(&check_kind, Label::kNear);
3300   bind(&fetch_next);
3301   movp(rsp, Operand(rsp, StackHandlerConstants::kNextOffset));
3302
3303   bind(&check_kind);
3304   STATIC_ASSERT(StackHandler::JS_ENTRY == 0);
3305   testl(Operand(rsp, StackHandlerConstants::kStateOffset),
3306         Immediate(StackHandler::KindField::kMask));
3307   j(not_zero, &fetch_next);
3308
3309   // Set the top handler address to next handler past the top ENTRY handler.
3310   Pop(ExternalOperand(handler_address));
3311
3312   // Remove the code object and state, compute the handler address in rdi.
3313   Pop(rdi);  // Code object.
3314   Pop(rdx);  // Offset and state.
3315
3316   // Clear the context pointer and frame pointer (0 was saved in the handler).
3317   Pop(rsi);
3318   popq(rbp);
3319
3320   JumpToHandlerEntry();
3321 }
3322
3323
3324 void MacroAssembler::Ret() {
3325   ret(0);
3326 }
3327
3328
3329 void MacroAssembler::Ret(int bytes_dropped, Register scratch) {
3330   if (is_uint16(bytes_dropped)) {
3331     ret(bytes_dropped);
3332   } else {
3333     PopReturnAddressTo(scratch);
3334     addp(rsp, Immediate(bytes_dropped));
3335     PushReturnAddressFrom(scratch);
3336     ret(0);
3337   }
3338 }
3339
3340
3341 void MacroAssembler::FCmp() {
3342   fucomip();
3343   fstp(0);
3344 }
3345
3346
3347 void MacroAssembler::CmpObjectType(Register heap_object,
3348                                    InstanceType type,
3349                                    Register map) {
3350   movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3351   CmpInstanceType(map, type);
3352 }
3353
3354
3355 void MacroAssembler::CmpInstanceType(Register map, InstanceType type) {
3356   cmpb(FieldOperand(map, Map::kInstanceTypeOffset),
3357        Immediate(static_cast<int8_t>(type)));
3358 }
3359
3360
3361 void MacroAssembler::CheckFastElements(Register map,
3362                                        Label* fail,
3363                                        Label::Distance distance) {
3364   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3365   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3366   STATIC_ASSERT(FAST_ELEMENTS == 2);
3367   STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3368   cmpb(FieldOperand(map, Map::kBitField2Offset),
3369        Immediate(Map::kMaximumBitField2FastHoleyElementValue));
3370   j(above, fail, distance);
3371 }
3372
3373
3374 void MacroAssembler::CheckFastObjectElements(Register map,
3375                                              Label* fail,
3376                                              Label::Distance distance) {
3377   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3378   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3379   STATIC_ASSERT(FAST_ELEMENTS == 2);
3380   STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3381   cmpb(FieldOperand(map, Map::kBitField2Offset),
3382        Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
3383   j(below_equal, fail, distance);
3384   cmpb(FieldOperand(map, Map::kBitField2Offset),
3385        Immediate(Map::kMaximumBitField2FastHoleyElementValue));
3386   j(above, fail, distance);
3387 }
3388
3389
3390 void MacroAssembler::CheckFastSmiElements(Register map,
3391                                           Label* fail,
3392                                           Label::Distance distance) {
3393   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3394   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3395   cmpb(FieldOperand(map, Map::kBitField2Offset),
3396        Immediate(Map::kMaximumBitField2FastHoleySmiElementValue));
3397   j(above, fail, distance);
3398 }
3399
3400
3401 void MacroAssembler::StoreNumberToDoubleElements(
3402     Register maybe_number,
3403     Register elements,
3404     Register index,
3405     XMMRegister xmm_scratch,
3406     Label* fail,
3407     int elements_offset) {
3408   Label smi_value, is_nan, maybe_nan, not_nan, have_double_value, done;
3409
3410   JumpIfSmi(maybe_number, &smi_value, Label::kNear);
3411
3412   CheckMap(maybe_number,
3413            isolate()->factory()->heap_number_map(),
3414            fail,
3415            DONT_DO_SMI_CHECK);
3416
3417   // Double value, canonicalize NaN.
3418   uint32_t offset = HeapNumber::kValueOffset + sizeof(kHoleNanLower32);
3419   cmpl(FieldOperand(maybe_number, offset),
3420        Immediate(kNaNOrInfinityLowerBoundUpper32));
3421   j(greater_equal, &maybe_nan, Label::kNear);
3422
3423   bind(&not_nan);
3424   movsd(xmm_scratch, FieldOperand(maybe_number, HeapNumber::kValueOffset));
3425   bind(&have_double_value);
3426   movsd(FieldOperand(elements, index, times_8,
3427                      FixedDoubleArray::kHeaderSize - elements_offset),
3428         xmm_scratch);
3429   jmp(&done);
3430
3431   bind(&maybe_nan);
3432   // Could be NaN or Infinity. If fraction is not zero, it's NaN, otherwise
3433   // it's an Infinity, and the non-NaN code path applies.
3434   j(greater, &is_nan, Label::kNear);
3435   cmpl(FieldOperand(maybe_number, HeapNumber::kValueOffset), Immediate(0));
3436   j(zero, &not_nan);
3437   bind(&is_nan);
3438   // Convert all NaNs to the same canonical NaN value when they are stored in
3439   // the double array.
3440   Set(kScratchRegister,
3441       bit_cast<uint64_t>(
3442           FixedDoubleArray::canonical_not_the_hole_nan_as_double()));
3443   movq(xmm_scratch, kScratchRegister);
3444   jmp(&have_double_value, Label::kNear);
3445
3446   bind(&smi_value);
3447   // Value is a smi. convert to a double and store.
3448   // Preserve original value.
3449   SmiToInteger32(kScratchRegister, maybe_number);
3450   Cvtlsi2sd(xmm_scratch, kScratchRegister);
3451   movsd(FieldOperand(elements, index, times_8,
3452                      FixedDoubleArray::kHeaderSize - elements_offset),
3453         xmm_scratch);
3454   bind(&done);
3455 }
3456
3457
3458 void MacroAssembler::CompareMap(Register obj, Handle<Map> map) {
3459   Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
3460 }
3461
3462
3463 void MacroAssembler::CheckMap(Register obj,
3464                               Handle<Map> map,
3465                               Label* fail,
3466                               SmiCheckType smi_check_type) {
3467   if (smi_check_type == DO_SMI_CHECK) {
3468     JumpIfSmi(obj, fail);
3469   }
3470
3471   CompareMap(obj, map);
3472   j(not_equal, fail);
3473 }
3474
3475
3476 void MacroAssembler::ClampUint8(Register reg) {
3477   Label done;
3478   testl(reg, Immediate(0xFFFFFF00));
3479   j(zero, &done, Label::kNear);
3480   setcc(negative, reg);  // 1 if negative, 0 if positive.
3481   decb(reg);  // 0 if negative, 255 if positive.
3482   bind(&done);
3483 }
3484
3485
3486 void MacroAssembler::ClampDoubleToUint8(XMMRegister input_reg,
3487                                         XMMRegister temp_xmm_reg,
3488                                         Register result_reg) {
3489   Label done;
3490   Label conv_failure;
3491   xorps(temp_xmm_reg, temp_xmm_reg);
3492   cvtsd2si(result_reg, input_reg);
3493   testl(result_reg, Immediate(0xFFFFFF00));
3494   j(zero, &done, Label::kNear);
3495   cmpl(result_reg, Immediate(1));
3496   j(overflow, &conv_failure, Label::kNear);
3497   movl(result_reg, Immediate(0));
3498   setcc(sign, result_reg);
3499   subl(result_reg, Immediate(1));
3500   andl(result_reg, Immediate(255));
3501   jmp(&done, Label::kNear);
3502   bind(&conv_failure);
3503   Set(result_reg, 0);
3504   ucomisd(input_reg, temp_xmm_reg);
3505   j(below, &done, Label::kNear);
3506   Set(result_reg, 255);
3507   bind(&done);
3508 }
3509
3510
3511 void MacroAssembler::LoadUint32(XMMRegister dst,
3512                                 Register src) {
3513   if (FLAG_debug_code) {
3514     cmpq(src, Immediate(0xffffffff));
3515     Assert(below_equal, kInputGPRIsExpectedToHaveUpper32Cleared);
3516   }
3517   cvtqsi2sd(dst, src);
3518 }
3519
3520
3521 void MacroAssembler::SlowTruncateToI(Register result_reg,
3522                                      Register input_reg,
3523                                      int offset) {
3524   DoubleToIStub stub(isolate(), input_reg, result_reg, offset, true);
3525   call(stub.GetCode(), RelocInfo::CODE_TARGET);
3526 }
3527
3528
3529 void MacroAssembler::TruncateHeapNumberToI(Register result_reg,
3530                                            Register input_reg) {
3531   Label done;
3532   movsd(xmm0, FieldOperand(input_reg, HeapNumber::kValueOffset));
3533   cvttsd2siq(result_reg, xmm0);
3534   cmpq(result_reg, Immediate(1));
3535   j(no_overflow, &done, Label::kNear);
3536
3537   // Slow case.
3538   if (input_reg.is(result_reg)) {
3539     subp(rsp, Immediate(kDoubleSize));
3540     movsd(MemOperand(rsp, 0), xmm0);
3541     SlowTruncateToI(result_reg, rsp, 0);
3542     addp(rsp, Immediate(kDoubleSize));
3543   } else {
3544     SlowTruncateToI(result_reg, input_reg);
3545   }
3546
3547   bind(&done);
3548   // Keep our invariant that the upper 32 bits are zero.
3549   movl(result_reg, result_reg);
3550 }
3551
3552
3553 void MacroAssembler::TruncateDoubleToI(Register result_reg,
3554                                        XMMRegister input_reg) {
3555   Label done;
3556   cvttsd2siq(result_reg, input_reg);
3557   cmpq(result_reg, Immediate(1));
3558   j(no_overflow, &done, Label::kNear);
3559
3560   subp(rsp, Immediate(kDoubleSize));
3561   movsd(MemOperand(rsp, 0), input_reg);
3562   SlowTruncateToI(result_reg, rsp, 0);
3563   addp(rsp, Immediate(kDoubleSize));
3564
3565   bind(&done);
3566   // Keep our invariant that the upper 32 bits are zero.
3567   movl(result_reg, result_reg);
3568 }
3569
3570
3571 void MacroAssembler::DoubleToI(Register result_reg, XMMRegister input_reg,
3572                                XMMRegister scratch,
3573                                MinusZeroMode minus_zero_mode,
3574                                Label* lost_precision, Label* is_nan,
3575                                Label* minus_zero, Label::Distance dst) {
3576   cvttsd2si(result_reg, input_reg);
3577   Cvtlsi2sd(xmm0, result_reg);
3578   ucomisd(xmm0, input_reg);
3579   j(not_equal, lost_precision, dst);
3580   j(parity_even, is_nan, dst);  // NaN.
3581   if (minus_zero_mode == FAIL_ON_MINUS_ZERO) {
3582     Label done;
3583     // The integer converted back is equal to the original. We
3584     // only have to test if we got -0 as an input.
3585     testl(result_reg, result_reg);
3586     j(not_zero, &done, Label::kNear);
3587     movmskpd(result_reg, input_reg);
3588     // Bit 0 contains the sign of the double in input_reg.
3589     // If input was positive, we are ok and return 0, otherwise
3590     // jump to minus_zero.
3591     andl(result_reg, Immediate(1));
3592     j(not_zero, minus_zero, dst);
3593     bind(&done);
3594   }
3595 }
3596
3597
3598 void MacroAssembler::LoadInstanceDescriptors(Register map,
3599                                              Register descriptors) {
3600   movp(descriptors, FieldOperand(map, Map::kDescriptorsOffset));
3601 }
3602
3603
3604 void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
3605   movl(dst, FieldOperand(map, Map::kBitField3Offset));
3606   DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
3607 }
3608
3609
3610 void MacroAssembler::EnumLength(Register dst, Register map) {
3611   STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
3612   movl(dst, FieldOperand(map, Map::kBitField3Offset));
3613   andl(dst, Immediate(Map::EnumLengthBits::kMask));
3614   Integer32ToSmi(dst, dst);
3615 }
3616
3617
3618 void MacroAssembler::DispatchMap(Register obj,
3619                                  Register unused,
3620                                  Handle<Map> map,
3621                                  Handle<Code> success,
3622                                  SmiCheckType smi_check_type) {
3623   Label fail;
3624   if (smi_check_type == DO_SMI_CHECK) {
3625     JumpIfSmi(obj, &fail);
3626   }
3627   Cmp(FieldOperand(obj, HeapObject::kMapOffset), map);
3628   j(equal, success, RelocInfo::CODE_TARGET);
3629
3630   bind(&fail);
3631 }
3632
3633
3634 void MacroAssembler::AssertNumber(Register object) {
3635   if (emit_debug_code()) {
3636     Label ok;
3637     Condition is_smi = CheckSmi(object);
3638     j(is_smi, &ok, Label::kNear);
3639     Cmp(FieldOperand(object, HeapObject::kMapOffset),
3640         isolate()->factory()->heap_number_map());
3641     Check(equal, kOperandIsNotANumber);
3642     bind(&ok);
3643   }
3644 }
3645
3646
3647 void MacroAssembler::AssertNotSmi(Register object) {
3648   if (emit_debug_code()) {
3649     Condition is_smi = CheckSmi(object);
3650     Check(NegateCondition(is_smi), kOperandIsASmi);
3651   }
3652 }
3653
3654
3655 void MacroAssembler::AssertSmi(Register object) {
3656   if (emit_debug_code()) {
3657     Condition is_smi = CheckSmi(object);
3658     Check(is_smi, kOperandIsNotASmi);
3659   }
3660 }
3661
3662
3663 void MacroAssembler::AssertSmi(const Operand& object) {
3664   if (emit_debug_code()) {
3665     Condition is_smi = CheckSmi(object);
3666     Check(is_smi, kOperandIsNotASmi);
3667   }
3668 }
3669
3670
3671 void MacroAssembler::AssertZeroExtended(Register int32_register) {
3672   if (emit_debug_code()) {
3673     DCHECK(!int32_register.is(kScratchRegister));
3674     movq(kScratchRegister, V8_INT64_C(0x0000000100000000));
3675     cmpq(kScratchRegister, int32_register);
3676     Check(above_equal, k32BitValueInRegisterIsNotZeroExtended);
3677   }
3678 }
3679
3680
3681 void MacroAssembler::AssertString(Register object) {
3682   if (emit_debug_code()) {
3683     testb(object, Immediate(kSmiTagMask));
3684     Check(not_equal, kOperandIsASmiAndNotAString);
3685     Push(object);
3686     movp(object, FieldOperand(object, HeapObject::kMapOffset));
3687     CmpInstanceType(object, FIRST_NONSTRING_TYPE);
3688     Pop(object);
3689     Check(below, kOperandIsNotAString);
3690   }
3691 }
3692
3693
3694 void MacroAssembler::AssertName(Register object) {
3695   if (emit_debug_code()) {
3696     testb(object, Immediate(kSmiTagMask));
3697     Check(not_equal, kOperandIsASmiAndNotAName);
3698     Push(object);
3699     movp(object, FieldOperand(object, HeapObject::kMapOffset));
3700     CmpInstanceType(object, LAST_NAME_TYPE);
3701     Pop(object);
3702     Check(below_equal, kOperandIsNotAName);
3703   }
3704 }
3705
3706
3707 void MacroAssembler::AssertUndefinedOrAllocationSite(Register object) {
3708   if (emit_debug_code()) {
3709     Label done_checking;
3710     AssertNotSmi(object);
3711     Cmp(object, isolate()->factory()->undefined_value());
3712     j(equal, &done_checking);
3713     Cmp(FieldOperand(object, 0), isolate()->factory()->allocation_site_map());
3714     Assert(equal, kExpectedUndefinedOrCell);
3715     bind(&done_checking);
3716   }
3717 }
3718
3719
3720 void MacroAssembler::AssertRootValue(Register src,
3721                                      Heap::RootListIndex root_value_index,
3722                                      BailoutReason reason) {
3723   if (emit_debug_code()) {
3724     DCHECK(!src.is(kScratchRegister));
3725     LoadRoot(kScratchRegister, root_value_index);
3726     cmpp(src, kScratchRegister);
3727     Check(equal, reason);
3728   }
3729 }
3730
3731
3732
3733 Condition MacroAssembler::IsObjectStringType(Register heap_object,
3734                                              Register map,
3735                                              Register instance_type) {
3736   movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3737   movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3738   STATIC_ASSERT(kNotStringTag != 0);
3739   testb(instance_type, Immediate(kIsNotStringMask));
3740   return zero;
3741 }
3742
3743
3744 Condition MacroAssembler::IsObjectNameType(Register heap_object,
3745                                            Register map,
3746                                            Register instance_type) {
3747   movp(map, FieldOperand(heap_object, HeapObject::kMapOffset));
3748   movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
3749   cmpb(instance_type, Immediate(static_cast<uint8_t>(LAST_NAME_TYPE)));
3750   return below_equal;
3751 }
3752
3753
3754 void MacroAssembler::TryGetFunctionPrototype(Register function,
3755                                              Register result,
3756                                              Label* miss,
3757                                              bool miss_on_bound_function) {
3758   Label non_instance;
3759   if (miss_on_bound_function) {
3760     // Check that the receiver isn't a smi.
3761     testl(function, Immediate(kSmiTagMask));
3762     j(zero, miss);
3763
3764     // Check that the function really is a function.
3765     CmpObjectType(function, JS_FUNCTION_TYPE, result);
3766     j(not_equal, miss);
3767
3768     movp(kScratchRegister,
3769          FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3770     // It's not smi-tagged (stored in the top half of a smi-tagged 8-byte
3771     // field).
3772     TestBitSharedFunctionInfoSpecialField(kScratchRegister,
3773         SharedFunctionInfo::kCompilerHintsOffset,
3774         SharedFunctionInfo::kBoundFunction);
3775     j(not_zero, miss);
3776
3777     // Make sure that the function has an instance prototype.
3778     testb(FieldOperand(result, Map::kBitFieldOffset),
3779           Immediate(1 << Map::kHasNonInstancePrototype));
3780     j(not_zero, &non_instance, Label::kNear);
3781   }
3782
3783   // Get the prototype or initial map from the function.
3784   movp(result,
3785        FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3786
3787   // If the prototype or initial map is the hole, don't return it and
3788   // simply miss the cache instead. This will allow us to allocate a
3789   // prototype object on-demand in the runtime system.
3790   CompareRoot(result, Heap::kTheHoleValueRootIndex);
3791   j(equal, miss);
3792
3793   // If the function does not have an initial map, we're done.
3794   Label done;
3795   CmpObjectType(result, MAP_TYPE, kScratchRegister);
3796   j(not_equal, &done, Label::kNear);
3797
3798   // Get the prototype from the initial map.
3799   movp(result, FieldOperand(result, Map::kPrototypeOffset));
3800
3801   if (miss_on_bound_function) {
3802     jmp(&done, Label::kNear);
3803
3804     // Non-instance prototype: Fetch prototype from constructor field
3805     // in initial map.
3806     bind(&non_instance);
3807     movp(result, FieldOperand(result, Map::kConstructorOffset));
3808   }
3809
3810   // All done.
3811   bind(&done);
3812 }
3813
3814
3815 void MacroAssembler::SetCounter(StatsCounter* counter, int value) {
3816   if (FLAG_native_code_counters && counter->Enabled()) {
3817     Operand counter_operand = ExternalOperand(ExternalReference(counter));
3818     movl(counter_operand, Immediate(value));
3819   }
3820 }
3821
3822
3823 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value) {
3824   DCHECK(value > 0);
3825   if (FLAG_native_code_counters && counter->Enabled()) {
3826     Operand counter_operand = ExternalOperand(ExternalReference(counter));
3827     if (value == 1) {
3828       incl(counter_operand);
3829     } else {
3830       addl(counter_operand, Immediate(value));
3831     }
3832   }
3833 }
3834
3835
3836 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value) {
3837   DCHECK(value > 0);
3838   if (FLAG_native_code_counters && counter->Enabled()) {
3839     Operand counter_operand = ExternalOperand(ExternalReference(counter));
3840     if (value == 1) {
3841       decl(counter_operand);
3842     } else {
3843       subl(counter_operand, Immediate(value));
3844     }
3845   }
3846 }
3847
3848
3849 void MacroAssembler::DebugBreak() {
3850   Set(rax, 0);  // No arguments.
3851   LoadAddress(rbx, ExternalReference(Runtime::kDebugBreak, isolate()));
3852   CEntryStub ces(isolate(), 1);
3853   DCHECK(AllowThisStubCall(&ces));
3854   Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
3855 }
3856
3857
3858 void MacroAssembler::InvokeCode(Register code,
3859                                 const ParameterCount& expected,
3860                                 const ParameterCount& actual,
3861                                 InvokeFlag flag,
3862                                 const CallWrapper& call_wrapper) {
3863   // You can't call a function without a valid frame.
3864   DCHECK(flag == JUMP_FUNCTION || has_frame());
3865
3866   Label done;
3867   bool definitely_mismatches = false;
3868   InvokePrologue(expected,
3869                  actual,
3870                  Handle<Code>::null(),
3871                  code,
3872                  &done,
3873                  &definitely_mismatches,
3874                  flag,
3875                  Label::kNear,
3876                  call_wrapper);
3877   if (!definitely_mismatches) {
3878     if (flag == CALL_FUNCTION) {
3879       call_wrapper.BeforeCall(CallSize(code));
3880       call(code);
3881       call_wrapper.AfterCall();
3882     } else {
3883       DCHECK(flag == JUMP_FUNCTION);
3884       jmp(code);
3885     }
3886     bind(&done);
3887   }
3888 }
3889
3890
3891 void MacroAssembler::InvokeFunction(Register function,
3892                                     const ParameterCount& actual,
3893                                     InvokeFlag flag,
3894                                     const CallWrapper& call_wrapper) {
3895   // You can't call a function without a valid frame.
3896   DCHECK(flag == JUMP_FUNCTION || has_frame());
3897
3898   DCHECK(function.is(rdi));
3899   movp(rdx, FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
3900   movp(rsi, FieldOperand(function, JSFunction::kContextOffset));
3901   LoadSharedFunctionInfoSpecialField(rbx, rdx,
3902       SharedFunctionInfo::kFormalParameterCountOffset);
3903   // Advances rdx to the end of the Code object header, to the start of
3904   // the executable code.
3905   movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
3906
3907   ParameterCount expected(rbx);
3908   InvokeCode(rdx, expected, actual, flag, call_wrapper);
3909 }
3910
3911
3912 void MacroAssembler::InvokeFunction(Register function,
3913                                     const ParameterCount& expected,
3914                                     const ParameterCount& actual,
3915                                     InvokeFlag flag,
3916                                     const CallWrapper& call_wrapper) {
3917   // You can't call a function without a valid frame.
3918   DCHECK(flag == JUMP_FUNCTION || has_frame());
3919
3920   DCHECK(function.is(rdi));
3921   movp(rsi, FieldOperand(function, JSFunction::kContextOffset));
3922   // Advances rdx to the end of the Code object header, to the start of
3923   // the executable code.
3924   movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
3925
3926   InvokeCode(rdx, expected, actual, flag, call_wrapper);
3927 }
3928
3929
3930 void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
3931                                     const ParameterCount& expected,
3932                                     const ParameterCount& actual,
3933                                     InvokeFlag flag,
3934                                     const CallWrapper& call_wrapper) {
3935   Move(rdi, function);
3936   InvokeFunction(rdi, expected, actual, flag, call_wrapper);
3937 }
3938
3939
3940 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
3941                                     const ParameterCount& actual,
3942                                     Handle<Code> code_constant,
3943                                     Register code_register,
3944                                     Label* done,
3945                                     bool* definitely_mismatches,
3946                                     InvokeFlag flag,
3947                                     Label::Distance near_jump,
3948                                     const CallWrapper& call_wrapper) {
3949   bool definitely_matches = false;
3950   *definitely_mismatches = false;
3951   Label invoke;
3952   if (expected.is_immediate()) {
3953     DCHECK(actual.is_immediate());
3954     if (expected.immediate() == actual.immediate()) {
3955       definitely_matches = true;
3956     } else {
3957       Set(rax, actual.immediate());
3958       if (expected.immediate() ==
3959               SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
3960         // Don't worry about adapting arguments for built-ins that
3961         // don't want that done. Skip adaption code by making it look
3962         // like we have a match between expected and actual number of
3963         // arguments.
3964         definitely_matches = true;
3965       } else {
3966         *definitely_mismatches = true;
3967         Set(rbx, expected.immediate());
3968       }
3969     }
3970   } else {
3971     if (actual.is_immediate()) {
3972       // Expected is in register, actual is immediate. This is the
3973       // case when we invoke function values without going through the
3974       // IC mechanism.
3975       cmpp(expected.reg(), Immediate(actual.immediate()));
3976       j(equal, &invoke, Label::kNear);
3977       DCHECK(expected.reg().is(rbx));
3978       Set(rax, actual.immediate());
3979     } else if (!expected.reg().is(actual.reg())) {
3980       // Both expected and actual are in (different) registers. This
3981       // is the case when we invoke functions using call and apply.
3982       cmpp(expected.reg(), actual.reg());
3983       j(equal, &invoke, Label::kNear);
3984       DCHECK(actual.reg().is(rax));
3985       DCHECK(expected.reg().is(rbx));
3986     }
3987   }
3988
3989   if (!definitely_matches) {
3990     Handle<Code> adaptor = isolate()->builtins()->ArgumentsAdaptorTrampoline();
3991     if (!code_constant.is_null()) {
3992       Move(rdx, code_constant, RelocInfo::EMBEDDED_OBJECT);
3993       addp(rdx, Immediate(Code::kHeaderSize - kHeapObjectTag));
3994     } else if (!code_register.is(rdx)) {
3995       movp(rdx, code_register);
3996     }
3997
3998     if (flag == CALL_FUNCTION) {
3999       call_wrapper.BeforeCall(CallSize(adaptor));
4000       Call(adaptor, RelocInfo::CODE_TARGET);
4001       call_wrapper.AfterCall();
4002       if (!*definitely_mismatches) {
4003         jmp(done, near_jump);
4004       }
4005     } else {
4006       Jump(adaptor, RelocInfo::CODE_TARGET);
4007     }
4008     bind(&invoke);
4009   }
4010 }
4011
4012
4013 void MacroAssembler::StubPrologue() {
4014     pushq(rbp);  // Caller's frame pointer.
4015     movp(rbp, rsp);
4016     Push(rsi);  // Callee's context.
4017     Push(Smi::FromInt(StackFrame::STUB));
4018 }
4019
4020
4021 void MacroAssembler::Prologue(bool code_pre_aging) {
4022   PredictableCodeSizeScope predictible_code_size_scope(this,
4023       kNoCodeAgeSequenceLength);
4024   if (code_pre_aging) {
4025       // Pre-age the code.
4026     Call(isolate()->builtins()->MarkCodeAsExecutedOnce(),
4027          RelocInfo::CODE_AGE_SEQUENCE);
4028     Nop(kNoCodeAgeSequenceLength - Assembler::kShortCallInstructionLength);
4029   } else {
4030     pushq(rbp);  // Caller's frame pointer.
4031     movp(rbp, rsp);
4032     Push(rsi);  // Callee's context.
4033     Push(rdi);  // Callee's JS function.
4034   }
4035 }
4036
4037
4038 void MacroAssembler::EnterFrame(StackFrame::Type type,
4039                                 bool load_constant_pool_pointer_reg) {
4040   // Out-of-line constant pool not implemented on x64.
4041   UNREACHABLE();
4042 }
4043
4044
4045 void MacroAssembler::EnterFrame(StackFrame::Type type) {
4046   pushq(rbp);
4047   movp(rbp, rsp);
4048   Push(rsi);  // Context.
4049   Push(Smi::FromInt(type));
4050   Move(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
4051   Push(kScratchRegister);
4052   if (emit_debug_code()) {
4053     Move(kScratchRegister,
4054          isolate()->factory()->undefined_value(),
4055          RelocInfo::EMBEDDED_OBJECT);
4056     cmpp(Operand(rsp, 0), kScratchRegister);
4057     Check(not_equal, kCodeObjectNotProperlyPatched);
4058   }
4059 }
4060
4061
4062 void MacroAssembler::LeaveFrame(StackFrame::Type type) {
4063   if (emit_debug_code()) {
4064     Move(kScratchRegister, Smi::FromInt(type));
4065     cmpp(Operand(rbp, StandardFrameConstants::kMarkerOffset), kScratchRegister);
4066     Check(equal, kStackFrameTypesMustMatch);
4067   }
4068   movp(rsp, rbp);
4069   popq(rbp);
4070 }
4071
4072
4073 void MacroAssembler::EnterExitFramePrologue(bool save_rax) {
4074   // Set up the frame structure on the stack.
4075   // All constants are relative to the frame pointer of the exit frame.
4076   DCHECK(ExitFrameConstants::kCallerSPDisplacement ==
4077          kFPOnStackSize + kPCOnStackSize);
4078   DCHECK(ExitFrameConstants::kCallerPCOffset == kFPOnStackSize);
4079   DCHECK(ExitFrameConstants::kCallerFPOffset == 0 * kPointerSize);
4080   pushq(rbp);
4081   movp(rbp, rsp);
4082
4083   // Reserve room for entry stack pointer and push the code object.
4084   DCHECK(ExitFrameConstants::kSPOffset == -1 * kPointerSize);
4085   Push(Immediate(0));  // Saved entry sp, patched before call.
4086   Move(kScratchRegister, CodeObject(), RelocInfo::EMBEDDED_OBJECT);
4087   Push(kScratchRegister);  // Accessed from EditFrame::code_slot.
4088
4089   // Save the frame pointer and the context in top.
4090   if (save_rax) {
4091     movp(r14, rax);  // Backup rax in callee-save register.
4092   }
4093
4094   Store(ExternalReference(Isolate::kCEntryFPAddress, isolate()), rbp);
4095   Store(ExternalReference(Isolate::kContextAddress, isolate()), rsi);
4096   Store(ExternalReference(Isolate::kCFunctionAddress, isolate()), rbx);
4097 }
4098
4099
4100 void MacroAssembler::EnterExitFrameEpilogue(int arg_stack_space,
4101                                             bool save_doubles) {
4102 #ifdef _WIN64
4103   const int kShadowSpace = 4;
4104   arg_stack_space += kShadowSpace;
4105 #endif
4106   // Optionally save all XMM registers.
4107   if (save_doubles) {
4108     int space = XMMRegister::kMaxNumAllocatableRegisters * kDoubleSize +
4109         arg_stack_space * kRegisterSize;
4110     subp(rsp, Immediate(space));
4111     int offset = -2 * kPointerSize;
4112     for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
4113       XMMRegister reg = XMMRegister::FromAllocationIndex(i);
4114       movsd(Operand(rbp, offset - ((i + 1) * kDoubleSize)), reg);
4115     }
4116   } else if (arg_stack_space > 0) {
4117     subp(rsp, Immediate(arg_stack_space * kRegisterSize));
4118   }
4119
4120   // Get the required frame alignment for the OS.
4121   const int kFrameAlignment = base::OS::ActivationFrameAlignment();
4122   if (kFrameAlignment > 0) {
4123     DCHECK(base::bits::IsPowerOfTwo32(kFrameAlignment));
4124     DCHECK(is_int8(kFrameAlignment));
4125     andp(rsp, Immediate(-kFrameAlignment));
4126   }
4127
4128   // Patch the saved entry sp.
4129   movp(Operand(rbp, ExitFrameConstants::kSPOffset), rsp);
4130 }
4131
4132
4133 void MacroAssembler::EnterExitFrame(int arg_stack_space, bool save_doubles) {
4134   EnterExitFramePrologue(true);
4135
4136   // Set up argv in callee-saved register r15. It is reused in LeaveExitFrame,
4137   // so it must be retained across the C-call.
4138   int offset = StandardFrameConstants::kCallerSPOffset - kPointerSize;
4139   leap(r15, Operand(rbp, r14, times_pointer_size, offset));
4140
4141   EnterExitFrameEpilogue(arg_stack_space, save_doubles);
4142 }
4143
4144
4145 void MacroAssembler::EnterApiExitFrame(int arg_stack_space) {
4146   EnterExitFramePrologue(false);
4147   EnterExitFrameEpilogue(arg_stack_space, false);
4148 }
4149
4150
4151 void MacroAssembler::LeaveExitFrame(bool save_doubles) {
4152   // Registers:
4153   // r15 : argv
4154   if (save_doubles) {
4155     int offset = -2 * kPointerSize;
4156     for (int i = 0; i < XMMRegister::NumAllocatableRegisters(); i++) {
4157       XMMRegister reg = XMMRegister::FromAllocationIndex(i);
4158       movsd(reg, Operand(rbp, offset - ((i + 1) * kDoubleSize)));
4159     }
4160   }
4161   // Get the return address from the stack and restore the frame pointer.
4162   movp(rcx, Operand(rbp, kFPOnStackSize));
4163   movp(rbp, Operand(rbp, 0 * kPointerSize));
4164
4165   // Drop everything up to and including the arguments and the receiver
4166   // from the caller stack.
4167   leap(rsp, Operand(r15, 1 * kPointerSize));
4168
4169   PushReturnAddressFrom(rcx);
4170
4171   LeaveExitFrameEpilogue(true);
4172 }
4173
4174
4175 void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
4176   movp(rsp, rbp);
4177   popq(rbp);
4178
4179   LeaveExitFrameEpilogue(restore_context);
4180 }
4181
4182
4183 void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
4184   // Restore current context from top and clear it in debug mode.
4185   ExternalReference context_address(Isolate::kContextAddress, isolate());
4186   Operand context_operand = ExternalOperand(context_address);
4187   if (restore_context) {
4188     movp(rsi, context_operand);
4189   }
4190 #ifdef DEBUG
4191   movp(context_operand, Immediate(0));
4192 #endif
4193
4194   // Clear the top frame.
4195   ExternalReference c_entry_fp_address(Isolate::kCEntryFPAddress,
4196                                        isolate());
4197   Operand c_entry_fp_operand = ExternalOperand(c_entry_fp_address);
4198   movp(c_entry_fp_operand, Immediate(0));
4199 }
4200
4201
4202 void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
4203                                             Register scratch,
4204                                             Label* miss) {
4205   Label same_contexts;
4206
4207   DCHECK(!holder_reg.is(scratch));
4208   DCHECK(!scratch.is(kScratchRegister));
4209   // Load current lexical context from the stack frame.
4210   movp(scratch, Operand(rbp, StandardFrameConstants::kContextOffset));
4211
4212   // When generating debug code, make sure the lexical context is set.
4213   if (emit_debug_code()) {
4214     cmpp(scratch, Immediate(0));
4215     Check(not_equal, kWeShouldNotHaveAnEmptyLexicalContext);
4216   }
4217   // Load the native context of the current context.
4218   int offset =
4219       Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
4220   movp(scratch, FieldOperand(scratch, offset));
4221   movp(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
4222
4223   // Check the context is a native context.
4224   if (emit_debug_code()) {
4225     Cmp(FieldOperand(scratch, HeapObject::kMapOffset),
4226         isolate()->factory()->native_context_map());
4227     Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
4228   }
4229
4230   // Check if both contexts are the same.
4231   cmpp(scratch, FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4232   j(equal, &same_contexts);
4233
4234   // Compare security tokens.
4235   // Check that the security token in the calling global object is
4236   // compatible with the security token in the receiving global
4237   // object.
4238
4239   // Check the context is a native context.
4240   if (emit_debug_code()) {
4241     // Preserve original value of holder_reg.
4242     Push(holder_reg);
4243     movp(holder_reg,
4244          FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4245     CompareRoot(holder_reg, Heap::kNullValueRootIndex);
4246     Check(not_equal, kJSGlobalProxyContextShouldNotBeNull);
4247
4248     // Read the first word and compare to native_context_map(),
4249     movp(holder_reg, FieldOperand(holder_reg, HeapObject::kMapOffset));
4250     CompareRoot(holder_reg, Heap::kNativeContextMapRootIndex);
4251     Check(equal, kJSGlobalObjectNativeContextShouldBeANativeContext);
4252     Pop(holder_reg);
4253   }
4254
4255   movp(kScratchRegister,
4256        FieldOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
4257   int token_offset =
4258       Context::kHeaderSize + Context::SECURITY_TOKEN_INDEX * kPointerSize;
4259   movp(scratch, FieldOperand(scratch, token_offset));
4260   cmpp(scratch, FieldOperand(kScratchRegister, token_offset));
4261   j(not_equal, miss);
4262
4263   bind(&same_contexts);
4264 }
4265
4266
4267 // Compute the hash code from the untagged key.  This must be kept in sync with
4268 // ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
4269 // code-stub-hydrogen.cc
4270 void MacroAssembler::GetNumberHash(Register r0, Register scratch) {
4271   // First of all we assign the hash seed to scratch.
4272   LoadRoot(scratch, Heap::kHashSeedRootIndex);
4273   SmiToInteger32(scratch, scratch);
4274
4275   // Xor original key with a seed.
4276   xorl(r0, scratch);
4277
4278   // Compute the hash code from the untagged key.  This must be kept in sync
4279   // with ComputeIntegerHash in utils.h.
4280   //
4281   // hash = ~hash + (hash << 15);
4282   movl(scratch, r0);
4283   notl(r0);
4284   shll(scratch, Immediate(15));
4285   addl(r0, scratch);
4286   // hash = hash ^ (hash >> 12);
4287   movl(scratch, r0);
4288   shrl(scratch, Immediate(12));
4289   xorl(r0, scratch);
4290   // hash = hash + (hash << 2);
4291   leal(r0, Operand(r0, r0, times_4, 0));
4292   // hash = hash ^ (hash >> 4);
4293   movl(scratch, r0);
4294   shrl(scratch, Immediate(4));
4295   xorl(r0, scratch);
4296   // hash = hash * 2057;
4297   imull(r0, r0, Immediate(2057));
4298   // hash = hash ^ (hash >> 16);
4299   movl(scratch, r0);
4300   shrl(scratch, Immediate(16));
4301   xorl(r0, scratch);
4302 }
4303
4304
4305
4306 void MacroAssembler::LoadFromNumberDictionary(Label* miss,
4307                                               Register elements,
4308                                               Register key,
4309                                               Register r0,
4310                                               Register r1,
4311                                               Register r2,
4312                                               Register result) {
4313   // Register use:
4314   //
4315   // elements - holds the slow-case elements of the receiver on entry.
4316   //            Unchanged unless 'result' is the same register.
4317   //
4318   // key      - holds the smi key on entry.
4319   //            Unchanged unless 'result' is the same register.
4320   //
4321   // Scratch registers:
4322   //
4323   // r0 - holds the untagged key on entry and holds the hash once computed.
4324   //
4325   // r1 - used to hold the capacity mask of the dictionary
4326   //
4327   // r2 - used for the index into the dictionary.
4328   //
4329   // result - holds the result on exit if the load succeeded.
4330   //          Allowed to be the same as 'key' or 'result'.
4331   //          Unchanged on bailout so 'key' or 'result' can be used
4332   //          in further computation.
4333
4334   Label done;
4335
4336   GetNumberHash(r0, r1);
4337
4338   // Compute capacity mask.
4339   SmiToInteger32(r1, FieldOperand(elements,
4340                                   SeededNumberDictionary::kCapacityOffset));
4341   decl(r1);
4342
4343   // Generate an unrolled loop that performs a few probes before giving up.
4344   for (int i = 0; i < kNumberDictionaryProbes; i++) {
4345     // Use r2 for index calculations and keep the hash intact in r0.
4346     movp(r2, r0);
4347     // Compute the masked index: (hash + i + i * i) & mask.
4348     if (i > 0) {
4349       addl(r2, Immediate(SeededNumberDictionary::GetProbeOffset(i)));
4350     }
4351     andp(r2, r1);
4352
4353     // Scale the index by multiplying by the entry size.
4354     DCHECK(SeededNumberDictionary::kEntrySize == 3);
4355     leap(r2, Operand(r2, r2, times_2, 0));  // r2 = r2 * 3
4356
4357     // Check if the key matches.
4358     cmpp(key, FieldOperand(elements,
4359                            r2,
4360                            times_pointer_size,
4361                            SeededNumberDictionary::kElementsStartOffset));
4362     if (i != (kNumberDictionaryProbes - 1)) {
4363       j(equal, &done);
4364     } else {
4365       j(not_equal, miss);
4366     }
4367   }
4368
4369   bind(&done);
4370   // Check that the value is a normal propety.
4371   const int kDetailsOffset =
4372       SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
4373   DCHECK_EQ(NORMAL, 0);
4374   Test(FieldOperand(elements, r2, times_pointer_size, kDetailsOffset),
4375        Smi::FromInt(PropertyDetails::TypeField::kMask));
4376   j(not_zero, miss);
4377
4378   // Get the value at the masked, scaled index.
4379   const int kValueOffset =
4380       SeededNumberDictionary::kElementsStartOffset + kPointerSize;
4381   movp(result, FieldOperand(elements, r2, times_pointer_size, kValueOffset));
4382 }
4383
4384
4385 void MacroAssembler::LoadAllocationTopHelper(Register result,
4386                                              Register scratch,
4387                                              AllocationFlags flags) {
4388   ExternalReference allocation_top =
4389       AllocationUtils::GetAllocationTopReference(isolate(), flags);
4390
4391   // Just return if allocation top is already known.
4392   if ((flags & RESULT_CONTAINS_TOP) != 0) {
4393     // No use of scratch if allocation top is provided.
4394     DCHECK(!scratch.is_valid());
4395 #ifdef DEBUG
4396     // Assert that result actually contains top on entry.
4397     Operand top_operand = ExternalOperand(allocation_top);
4398     cmpp(result, top_operand);
4399     Check(equal, kUnexpectedAllocationTop);
4400 #endif
4401     return;
4402   }
4403
4404   // Move address of new object to result. Use scratch register if available,
4405   // and keep address in scratch until call to UpdateAllocationTopHelper.
4406   if (scratch.is_valid()) {
4407     LoadAddress(scratch, allocation_top);
4408     movp(result, Operand(scratch, 0));
4409   } else {
4410     Load(result, allocation_top);
4411   }
4412 }
4413
4414
4415 void MacroAssembler::MakeSureDoubleAlignedHelper(Register result,
4416                                                  Register scratch,
4417                                                  Label* gc_required,
4418                                                  AllocationFlags flags) {
4419   if (kPointerSize == kDoubleSize) {
4420     if (FLAG_debug_code) {
4421       testl(result, Immediate(kDoubleAlignmentMask));
4422       Check(zero, kAllocationIsNotDoubleAligned);
4423     }
4424   } else {
4425     // Align the next allocation. Storing the filler map without checking top
4426     // is safe in new-space because the limit of the heap is aligned there.
4427     DCHECK(kPointerSize * 2 == kDoubleSize);
4428     DCHECK((flags & PRETENURE_OLD_POINTER_SPACE) == 0);
4429     DCHECK(kPointerAlignment * 2 == kDoubleAlignment);
4430     // Make sure scratch is not clobbered by this function as it might be
4431     // used in UpdateAllocationTopHelper later.
4432     DCHECK(!scratch.is(kScratchRegister));
4433     Label aligned;
4434     testl(result, Immediate(kDoubleAlignmentMask));
4435     j(zero, &aligned, Label::kNear);
4436     if ((flags & PRETENURE_OLD_DATA_SPACE) != 0) {
4437       ExternalReference allocation_limit =
4438           AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4439       cmpp(result, ExternalOperand(allocation_limit));
4440       j(above_equal, gc_required);
4441     }
4442     LoadRoot(kScratchRegister, Heap::kOnePointerFillerMapRootIndex);
4443     movp(Operand(result, 0), kScratchRegister);
4444     addp(result, Immediate(kDoubleSize / 2));
4445     bind(&aligned);
4446   }
4447 }
4448
4449
4450 void MacroAssembler::UpdateAllocationTopHelper(Register result_end,
4451                                                Register scratch,
4452                                                AllocationFlags flags) {
4453   if (emit_debug_code()) {
4454     testp(result_end, Immediate(kObjectAlignmentMask));
4455     Check(zero, kUnalignedAllocationInNewSpace);
4456   }
4457
4458   ExternalReference allocation_top =
4459       AllocationUtils::GetAllocationTopReference(isolate(), flags);
4460
4461   // Update new top.
4462   if (scratch.is_valid()) {
4463     // Scratch already contains address of allocation top.
4464     movp(Operand(scratch, 0), result_end);
4465   } else {
4466     Store(allocation_top, result_end);
4467   }
4468 }
4469
4470
4471 void MacroAssembler::Allocate(int object_size,
4472                               Register result,
4473                               Register result_end,
4474                               Register scratch,
4475                               Label* gc_required,
4476                               AllocationFlags flags) {
4477   DCHECK((flags & (RESULT_CONTAINS_TOP | SIZE_IN_WORDS)) == 0);
4478   DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
4479   if (!FLAG_inline_new) {
4480     if (emit_debug_code()) {
4481       // Trash the registers to simulate an allocation failure.
4482       movl(result, Immediate(0x7091));
4483       if (result_end.is_valid()) {
4484         movl(result_end, Immediate(0x7191));
4485       }
4486       if (scratch.is_valid()) {
4487         movl(scratch, Immediate(0x7291));
4488       }
4489     }
4490     jmp(gc_required);
4491     return;
4492   }
4493   DCHECK(!result.is(result_end));
4494
4495   // Load address of new object into result.
4496   LoadAllocationTopHelper(result, scratch, flags);
4497
4498   if ((flags & DOUBLE_ALIGNMENT) != 0) {
4499     MakeSureDoubleAlignedHelper(result, scratch, gc_required, flags);
4500   }
4501
4502   // Calculate new top and bail out if new space is exhausted.
4503   ExternalReference allocation_limit =
4504       AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4505
4506   Register top_reg = result_end.is_valid() ? result_end : result;
4507
4508   if (!top_reg.is(result)) {
4509     movp(top_reg, result);
4510   }
4511   addp(top_reg, Immediate(object_size));
4512   j(carry, gc_required);
4513   Operand limit_operand = ExternalOperand(allocation_limit);
4514   cmpp(top_reg, limit_operand);
4515   j(above, gc_required);
4516
4517   // Update allocation top.
4518   UpdateAllocationTopHelper(top_reg, scratch, flags);
4519
4520   bool tag_result = (flags & TAG_OBJECT) != 0;
4521   if (top_reg.is(result)) {
4522     if (tag_result) {
4523       subp(result, Immediate(object_size - kHeapObjectTag));
4524     } else {
4525       subp(result, Immediate(object_size));
4526     }
4527   } else if (tag_result) {
4528     // Tag the result if requested.
4529     DCHECK(kHeapObjectTag == 1);
4530     incp(result);
4531   }
4532 }
4533
4534
4535 void MacroAssembler::Allocate(int header_size,
4536                               ScaleFactor element_size,
4537                               Register element_count,
4538                               Register result,
4539                               Register result_end,
4540                               Register scratch,
4541                               Label* gc_required,
4542                               AllocationFlags flags) {
4543   DCHECK((flags & SIZE_IN_WORDS) == 0);
4544   leap(result_end, Operand(element_count, element_size, header_size));
4545   Allocate(result_end, result, result_end, scratch, gc_required, flags);
4546 }
4547
4548
4549 void MacroAssembler::Allocate(Register object_size,
4550                               Register result,
4551                               Register result_end,
4552                               Register scratch,
4553                               Label* gc_required,
4554                               AllocationFlags flags) {
4555   DCHECK((flags & SIZE_IN_WORDS) == 0);
4556   if (!FLAG_inline_new) {
4557     if (emit_debug_code()) {
4558       // Trash the registers to simulate an allocation failure.
4559       movl(result, Immediate(0x7091));
4560       movl(result_end, Immediate(0x7191));
4561       if (scratch.is_valid()) {
4562         movl(scratch, Immediate(0x7291));
4563       }
4564       // object_size is left unchanged by this function.
4565     }
4566     jmp(gc_required);
4567     return;
4568   }
4569   DCHECK(!result.is(result_end));
4570
4571   // Load address of new object into result.
4572   LoadAllocationTopHelper(result, scratch, flags);
4573
4574   if ((flags & DOUBLE_ALIGNMENT) != 0) {
4575     MakeSureDoubleAlignedHelper(result, scratch, gc_required, flags);
4576   }
4577
4578   // Calculate new top and bail out if new space is exhausted.
4579   ExternalReference allocation_limit =
4580       AllocationUtils::GetAllocationLimitReference(isolate(), flags);
4581   if (!object_size.is(result_end)) {
4582     movp(result_end, object_size);
4583   }
4584   addp(result_end, result);
4585   j(carry, gc_required);
4586   Operand limit_operand = ExternalOperand(allocation_limit);
4587   cmpp(result_end, limit_operand);
4588   j(above, gc_required);
4589
4590   // Update allocation top.
4591   UpdateAllocationTopHelper(result_end, scratch, flags);
4592
4593   // Tag the result if requested.
4594   if ((flags & TAG_OBJECT) != 0) {
4595     addp(result, Immediate(kHeapObjectTag));
4596   }
4597 }
4598
4599
4600 void MacroAssembler::UndoAllocationInNewSpace(Register object) {
4601   ExternalReference new_space_allocation_top =
4602       ExternalReference::new_space_allocation_top_address(isolate());
4603
4604   // Make sure the object has no tag before resetting top.
4605   andp(object, Immediate(~kHeapObjectTagMask));
4606   Operand top_operand = ExternalOperand(new_space_allocation_top);
4607 #ifdef DEBUG
4608   cmpp(object, top_operand);
4609   Check(below, kUndoAllocationOfNonAllocatedMemory);
4610 #endif
4611   movp(top_operand, object);
4612 }
4613
4614
4615 void MacroAssembler::AllocateHeapNumber(Register result,
4616                                         Register scratch,
4617                                         Label* gc_required,
4618                                         MutableMode mode) {
4619   // Allocate heap number in new space.
4620   Allocate(HeapNumber::kSize, result, scratch, no_reg, gc_required, TAG_OBJECT);
4621
4622   Heap::RootListIndex map_index = mode == MUTABLE
4623       ? Heap::kMutableHeapNumberMapRootIndex
4624       : Heap::kHeapNumberMapRootIndex;
4625
4626   // Set the map.
4627   LoadRoot(kScratchRegister, map_index);
4628   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4629 }
4630
4631
4632 void MacroAssembler::AllocateTwoByteString(Register result,
4633                                            Register length,
4634                                            Register scratch1,
4635                                            Register scratch2,
4636                                            Register scratch3,
4637                                            Label* gc_required) {
4638   // Calculate the number of bytes needed for the characters in the string while
4639   // observing object alignment.
4640   const int kHeaderAlignment = SeqTwoByteString::kHeaderSize &
4641                                kObjectAlignmentMask;
4642   DCHECK(kShortSize == 2);
4643   // scratch1 = length * 2 + kObjectAlignmentMask.
4644   leap(scratch1, Operand(length, length, times_1, kObjectAlignmentMask +
4645                 kHeaderAlignment));
4646   andp(scratch1, Immediate(~kObjectAlignmentMask));
4647   if (kHeaderAlignment > 0) {
4648     subp(scratch1, Immediate(kHeaderAlignment));
4649   }
4650
4651   // Allocate two byte string in new space.
4652   Allocate(SeqTwoByteString::kHeaderSize,
4653            times_1,
4654            scratch1,
4655            result,
4656            scratch2,
4657            scratch3,
4658            gc_required,
4659            TAG_OBJECT);
4660
4661   // Set the map, length and hash field.
4662   LoadRoot(kScratchRegister, Heap::kStringMapRootIndex);
4663   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4664   Integer32ToSmi(scratch1, length);
4665   movp(FieldOperand(result, String::kLengthOffset), scratch1);
4666   movp(FieldOperand(result, String::kHashFieldOffset),
4667        Immediate(String::kEmptyHashField));
4668 }
4669
4670
4671 void MacroAssembler::AllocateOneByteString(Register result, Register length,
4672                                            Register scratch1, Register scratch2,
4673                                            Register scratch3,
4674                                            Label* gc_required) {
4675   // Calculate the number of bytes needed for the characters in the string while
4676   // observing object alignment.
4677   const int kHeaderAlignment = SeqOneByteString::kHeaderSize &
4678                                kObjectAlignmentMask;
4679   movl(scratch1, length);
4680   DCHECK(kCharSize == 1);
4681   addp(scratch1, Immediate(kObjectAlignmentMask + kHeaderAlignment));
4682   andp(scratch1, Immediate(~kObjectAlignmentMask));
4683   if (kHeaderAlignment > 0) {
4684     subp(scratch1, Immediate(kHeaderAlignment));
4685   }
4686
4687   // Allocate one-byte string in new space.
4688   Allocate(SeqOneByteString::kHeaderSize,
4689            times_1,
4690            scratch1,
4691            result,
4692            scratch2,
4693            scratch3,
4694            gc_required,
4695            TAG_OBJECT);
4696
4697   // Set the map, length and hash field.
4698   LoadRoot(kScratchRegister, Heap::kOneByteStringMapRootIndex);
4699   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4700   Integer32ToSmi(scratch1, length);
4701   movp(FieldOperand(result, String::kLengthOffset), scratch1);
4702   movp(FieldOperand(result, String::kHashFieldOffset),
4703        Immediate(String::kEmptyHashField));
4704 }
4705
4706
4707 void MacroAssembler::AllocateTwoByteConsString(Register result,
4708                                         Register scratch1,
4709                                         Register scratch2,
4710                                         Label* gc_required) {
4711   // Allocate heap number in new space.
4712   Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
4713            TAG_OBJECT);
4714
4715   // Set the map. The other fields are left uninitialized.
4716   LoadRoot(kScratchRegister, Heap::kConsStringMapRootIndex);
4717   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4718 }
4719
4720
4721 void MacroAssembler::AllocateOneByteConsString(Register result,
4722                                                Register scratch1,
4723                                                Register scratch2,
4724                                                Label* gc_required) {
4725   Allocate(ConsString::kSize,
4726            result,
4727            scratch1,
4728            scratch2,
4729            gc_required,
4730            TAG_OBJECT);
4731
4732   // Set the map. The other fields are left uninitialized.
4733   LoadRoot(kScratchRegister, Heap::kConsOneByteStringMapRootIndex);
4734   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4735 }
4736
4737
4738 void MacroAssembler::AllocateTwoByteSlicedString(Register result,
4739                                           Register scratch1,
4740                                           Register scratch2,
4741                                           Label* gc_required) {
4742   // Allocate heap number in new space.
4743   Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
4744            TAG_OBJECT);
4745
4746   // Set the map. The other fields are left uninitialized.
4747   LoadRoot(kScratchRegister, Heap::kSlicedStringMapRootIndex);
4748   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4749 }
4750
4751
4752 void MacroAssembler::AllocateOneByteSlicedString(Register result,
4753                                                  Register scratch1,
4754                                                  Register scratch2,
4755                                                  Label* gc_required) {
4756   // Allocate heap number in new space.
4757   Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
4758            TAG_OBJECT);
4759
4760   // Set the map. The other fields are left uninitialized.
4761   LoadRoot(kScratchRegister, Heap::kSlicedOneByteStringMapRootIndex);
4762   movp(FieldOperand(result, HeapObject::kMapOffset), kScratchRegister);
4763 }
4764
4765
4766 // Copy memory, byte-by-byte, from source to destination.  Not optimized for
4767 // long or aligned copies.  The contents of scratch and length are destroyed.
4768 // Destination is incremented by length, source, length and scratch are
4769 // clobbered.
4770 // A simpler loop is faster on small copies, but slower on large ones.
4771 // The cld() instruction must have been emitted, to set the direction flag(),
4772 // before calling this function.
4773 void MacroAssembler::CopyBytes(Register destination,
4774                                Register source,
4775                                Register length,
4776                                int min_length,
4777                                Register scratch) {
4778   DCHECK(min_length >= 0);
4779   if (emit_debug_code()) {
4780     cmpl(length, Immediate(min_length));
4781     Assert(greater_equal, kInvalidMinLength);
4782   }
4783   Label short_loop, len8, len16, len24, done, short_string;
4784
4785   const int kLongStringLimit = 4 * kPointerSize;
4786   if (min_length <= kLongStringLimit) {
4787     cmpl(length, Immediate(kPointerSize));
4788     j(below, &short_string, Label::kNear);
4789   }
4790
4791   DCHECK(source.is(rsi));
4792   DCHECK(destination.is(rdi));
4793   DCHECK(length.is(rcx));
4794
4795   if (min_length <= kLongStringLimit) {
4796     cmpl(length, Immediate(2 * kPointerSize));
4797     j(below_equal, &len8, Label::kNear);
4798     cmpl(length, Immediate(3 * kPointerSize));
4799     j(below_equal, &len16, Label::kNear);
4800     cmpl(length, Immediate(4 * kPointerSize));
4801     j(below_equal, &len24, Label::kNear);
4802   }
4803
4804   // Because source is 8-byte aligned in our uses of this function,
4805   // we keep source aligned for the rep movs operation by copying the odd bytes
4806   // at the end of the ranges.
4807   movp(scratch, length);
4808   shrl(length, Immediate(kPointerSizeLog2));
4809   repmovsp();
4810   // Move remaining bytes of length.
4811   andl(scratch, Immediate(kPointerSize - 1));
4812   movp(length, Operand(source, scratch, times_1, -kPointerSize));
4813   movp(Operand(destination, scratch, times_1, -kPointerSize), length);
4814   addp(destination, scratch);
4815
4816   if (min_length <= kLongStringLimit) {
4817     jmp(&done, Label::kNear);
4818     bind(&len24);
4819     movp(scratch, Operand(source, 2 * kPointerSize));
4820     movp(Operand(destination, 2 * kPointerSize), scratch);
4821     bind(&len16);
4822     movp(scratch, Operand(source, kPointerSize));
4823     movp(Operand(destination, kPointerSize), scratch);
4824     bind(&len8);
4825     movp(scratch, Operand(source, 0));
4826     movp(Operand(destination, 0), scratch);
4827     // Move remaining bytes of length.
4828     movp(scratch, Operand(source, length, times_1, -kPointerSize));
4829     movp(Operand(destination, length, times_1, -kPointerSize), scratch);
4830     addp(destination, length);
4831     jmp(&done, Label::kNear);
4832
4833     bind(&short_string);
4834     if (min_length == 0) {
4835       testl(length, length);
4836       j(zero, &done, Label::kNear);
4837     }
4838
4839     bind(&short_loop);
4840     movb(scratch, Operand(source, 0));
4841     movb(Operand(destination, 0), scratch);
4842     incp(source);
4843     incp(destination);
4844     decl(length);
4845     j(not_zero, &short_loop);
4846   }
4847
4848   bind(&done);
4849 }
4850
4851
4852 void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
4853                                                 Register end_offset,
4854                                                 Register filler) {
4855   Label loop, entry;
4856   jmp(&entry);
4857   bind(&loop);
4858   movp(Operand(start_offset, 0), filler);
4859   addp(start_offset, Immediate(kPointerSize));
4860   bind(&entry);
4861   cmpp(start_offset, end_offset);
4862   j(less, &loop);
4863 }
4864
4865
4866 void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
4867   if (context_chain_length > 0) {
4868     // Move up the chain of contexts to the context containing the slot.
4869     movp(dst, Operand(rsi, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4870     for (int i = 1; i < context_chain_length; i++) {
4871       movp(dst, Operand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4872     }
4873   } else {
4874     // Slot is in the current function context.  Move it into the
4875     // destination register in case we store into it (the write barrier
4876     // cannot be allowed to destroy the context in rsi).
4877     movp(dst, rsi);
4878   }
4879
4880   // We should not have found a with context by walking the context
4881   // chain (i.e., the static scope chain and runtime context chain do
4882   // not agree).  A variable occurring in such a scope should have
4883   // slot type LOOKUP and not CONTEXT.
4884   if (emit_debug_code()) {
4885     CompareRoot(FieldOperand(dst, HeapObject::kMapOffset),
4886                 Heap::kWithContextMapRootIndex);
4887     Check(not_equal, kVariableResolvedToWithContext);
4888   }
4889 }
4890
4891
4892 void MacroAssembler::LoadTransitionedArrayMapConditional(
4893     ElementsKind expected_kind,
4894     ElementsKind transitioned_kind,
4895     Register map_in_out,
4896     Register scratch,
4897     Label* no_map_match) {
4898   // Load the global or builtins object from the current context.
4899   movp(scratch,
4900        Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4901   movp(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
4902
4903   // Check that the function's map is the same as the expected cached map.
4904   movp(scratch, Operand(scratch,
4905                         Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
4906
4907   int offset = expected_kind * kPointerSize +
4908       FixedArrayBase::kHeaderSize;
4909   cmpp(map_in_out, FieldOperand(scratch, offset));
4910   j(not_equal, no_map_match);
4911
4912   // Use the transitioned cached map.
4913   offset = transitioned_kind * kPointerSize +
4914       FixedArrayBase::kHeaderSize;
4915   movp(map_in_out, FieldOperand(scratch, offset));
4916 }
4917
4918
4919 #ifdef _WIN64
4920 static const int kRegisterPassedArguments = 4;
4921 #else
4922 static const int kRegisterPassedArguments = 6;
4923 #endif
4924
4925 void MacroAssembler::LoadGlobalFunction(int index, Register function) {
4926   // Load the global or builtins object from the current context.
4927   movp(function,
4928        Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4929   // Load the native context from the global or builtins object.
4930   movp(function, FieldOperand(function, GlobalObject::kNativeContextOffset));
4931   // Load the function from the native context.
4932   movp(function, Operand(function, Context::SlotOffset(index)));
4933 }
4934
4935
4936 void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
4937                                                   Register map) {
4938   // Load the initial map.  The global functions all have initial maps.
4939   movp(map, FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4940   if (emit_debug_code()) {
4941     Label ok, fail;
4942     CheckMap(map, isolate()->factory()->meta_map(), &fail, DO_SMI_CHECK);
4943     jmp(&ok);
4944     bind(&fail);
4945     Abort(kGlobalFunctionsMustHaveInitialMap);
4946     bind(&ok);
4947   }
4948 }
4949
4950
4951 int MacroAssembler::ArgumentStackSlotsForCFunctionCall(int num_arguments) {
4952   // On Windows 64 stack slots are reserved by the caller for all arguments
4953   // including the ones passed in registers, and space is always allocated for
4954   // the four register arguments even if the function takes fewer than four
4955   // arguments.
4956   // On AMD64 ABI (Linux/Mac) the first six arguments are passed in registers
4957   // and the caller does not reserve stack slots for them.
4958   DCHECK(num_arguments >= 0);
4959 #ifdef _WIN64
4960   const int kMinimumStackSlots = kRegisterPassedArguments;
4961   if (num_arguments < kMinimumStackSlots) return kMinimumStackSlots;
4962   return num_arguments;
4963 #else
4964   if (num_arguments < kRegisterPassedArguments) return 0;
4965   return num_arguments - kRegisterPassedArguments;
4966 #endif
4967 }
4968
4969
4970 void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
4971                                                Register index,
4972                                                Register value,
4973                                                uint32_t encoding_mask) {
4974   Label is_object;
4975   JumpIfNotSmi(string, &is_object);
4976   Abort(kNonObject);
4977   bind(&is_object);
4978
4979   Push(value);
4980   movp(value, FieldOperand(string, HeapObject::kMapOffset));
4981   movzxbp(value, FieldOperand(value, Map::kInstanceTypeOffset));
4982
4983   andb(value, Immediate(kStringRepresentationMask | kStringEncodingMask));
4984   cmpp(value, Immediate(encoding_mask));
4985   Pop(value);
4986   Check(equal, kUnexpectedStringType);
4987
4988   // The index is assumed to be untagged coming in, tag it to compare with the
4989   // string length without using a temp register, it is restored at the end of
4990   // this function.
4991   Integer32ToSmi(index, index);
4992   SmiCompare(index, FieldOperand(string, String::kLengthOffset));
4993   Check(less, kIndexIsTooLarge);
4994
4995   SmiCompare(index, Smi::FromInt(0));
4996   Check(greater_equal, kIndexIsNegative);
4997
4998   // Restore the index
4999   SmiToInteger32(index, index);
5000 }
5001
5002
5003 void MacroAssembler::PrepareCallCFunction(int num_arguments) {
5004   int frame_alignment = base::OS::ActivationFrameAlignment();
5005   DCHECK(frame_alignment != 0);
5006   DCHECK(num_arguments >= 0);
5007
5008   // Make stack end at alignment and allocate space for arguments and old rsp.
5009   movp(kScratchRegister, rsp);
5010   DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
5011   int argument_slots_on_stack =
5012       ArgumentStackSlotsForCFunctionCall(num_arguments);
5013   subp(rsp, Immediate((argument_slots_on_stack + 1) * kRegisterSize));
5014   andp(rsp, Immediate(-frame_alignment));
5015   movp(Operand(rsp, argument_slots_on_stack * kRegisterSize), kScratchRegister);
5016 }
5017
5018
5019 void MacroAssembler::CallCFunction(ExternalReference function,
5020                                    int num_arguments) {
5021   LoadAddress(rax, function);
5022   CallCFunction(rax, num_arguments);
5023 }
5024
5025
5026 void MacroAssembler::CallCFunction(Register function, int num_arguments) {
5027   DCHECK(has_frame());
5028   // Check stack alignment.
5029   if (emit_debug_code()) {
5030     CheckStackAlignment();
5031   }
5032
5033   call(function);
5034   DCHECK(base::OS::ActivationFrameAlignment() != 0);
5035   DCHECK(num_arguments >= 0);
5036   int argument_slots_on_stack =
5037       ArgumentStackSlotsForCFunctionCall(num_arguments);
5038   movp(rsp, Operand(rsp, argument_slots_on_stack * kRegisterSize));
5039 }
5040
5041
5042 #ifdef DEBUG
5043 bool AreAliased(Register reg1,
5044                 Register reg2,
5045                 Register reg3,
5046                 Register reg4,
5047                 Register reg5,
5048                 Register reg6,
5049                 Register reg7,
5050                 Register reg8) {
5051   int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
5052       reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
5053       reg7.is_valid() + reg8.is_valid();
5054
5055   RegList regs = 0;
5056   if (reg1.is_valid()) regs |= reg1.bit();
5057   if (reg2.is_valid()) regs |= reg2.bit();
5058   if (reg3.is_valid()) regs |= reg3.bit();
5059   if (reg4.is_valid()) regs |= reg4.bit();
5060   if (reg5.is_valid()) regs |= reg5.bit();
5061   if (reg6.is_valid()) regs |= reg6.bit();
5062   if (reg7.is_valid()) regs |= reg7.bit();
5063   if (reg8.is_valid()) regs |= reg8.bit();
5064   int n_of_non_aliasing_regs = NumRegs(regs);
5065
5066   return n_of_valid_regs != n_of_non_aliasing_regs;
5067 }
5068 #endif
5069
5070
5071 CodePatcher::CodePatcher(byte* address, int size)
5072     : address_(address),
5073       size_(size),
5074       masm_(NULL, address, size + Assembler::kGap) {
5075   // Create a new macro assembler pointing to the address of the code to patch.
5076   // The size is adjusted with kGap on order for the assembler to generate size
5077   // bytes of instructions without failing with buffer size constraints.
5078   DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
5079 }
5080
5081
5082 CodePatcher::~CodePatcher() {
5083   // Indicate that code has changed.
5084   CpuFeatures::FlushICache(address_, size_);
5085
5086   // Check that the code was patched as expected.
5087   DCHECK(masm_.pc_ == address_ + size_);
5088   DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
5089 }
5090
5091
5092 void MacroAssembler::CheckPageFlag(
5093     Register object,
5094     Register scratch,
5095     int mask,
5096     Condition cc,
5097     Label* condition_met,
5098     Label::Distance condition_met_distance) {
5099   DCHECK(cc == zero || cc == not_zero);
5100   if (scratch.is(object)) {
5101     andp(scratch, Immediate(~Page::kPageAlignmentMask));
5102   } else {
5103     movp(scratch, Immediate(~Page::kPageAlignmentMask));
5104     andp(scratch, object);
5105   }
5106   if (mask < (1 << kBitsPerByte)) {
5107     testb(Operand(scratch, MemoryChunk::kFlagsOffset),
5108           Immediate(static_cast<uint8_t>(mask)));
5109   } else {
5110     testl(Operand(scratch, MemoryChunk::kFlagsOffset), Immediate(mask));
5111   }
5112   j(cc, condition_met, condition_met_distance);
5113 }
5114
5115
5116 void MacroAssembler::CheckMapDeprecated(Handle<Map> map,
5117                                         Register scratch,
5118                                         Label* if_deprecated) {
5119   if (map->CanBeDeprecated()) {
5120     Move(scratch, map);
5121     movl(scratch, FieldOperand(scratch, Map::kBitField3Offset));
5122     andl(scratch, Immediate(Map::Deprecated::kMask));
5123     j(not_zero, if_deprecated);
5124   }
5125 }
5126
5127
5128 void MacroAssembler::JumpIfBlack(Register object,
5129                                  Register bitmap_scratch,
5130                                  Register mask_scratch,
5131                                  Label* on_black,
5132                                  Label::Distance on_black_distance) {
5133   DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, rcx));
5134   GetMarkBits(object, bitmap_scratch, mask_scratch);
5135
5136   DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5137   // The mask_scratch register contains a 1 at the position of the first bit
5138   // and a 0 at all other positions, including the position of the second bit.
5139   movp(rcx, mask_scratch);
5140   // Make rcx into a mask that covers both marking bits using the operation
5141   // rcx = mask | (mask << 1).
5142   leap(rcx, Operand(mask_scratch, mask_scratch, times_2, 0));
5143   // Note that we are using a 4-byte aligned 8-byte load.
5144   andp(rcx, Operand(bitmap_scratch, MemoryChunk::kHeaderSize));
5145   cmpp(mask_scratch, rcx);
5146   j(equal, on_black, on_black_distance);
5147 }
5148
5149
5150 // Detect some, but not all, common pointer-free objects.  This is used by the
5151 // incremental write barrier which doesn't care about oddballs (they are always
5152 // marked black immediately so this code is not hit).
5153 void MacroAssembler::JumpIfDataObject(
5154     Register value,
5155     Register scratch,
5156     Label* not_data_object,
5157     Label::Distance not_data_object_distance) {
5158   Label is_data_object;
5159   movp(scratch, FieldOperand(value, HeapObject::kMapOffset));
5160   CompareRoot(scratch, Heap::kHeapNumberMapRootIndex);
5161   j(equal, &is_data_object, Label::kNear);
5162   DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5163   DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5164   // If it's a string and it's not a cons string then it's an object containing
5165   // no GC pointers.
5166   testb(FieldOperand(scratch, Map::kInstanceTypeOffset),
5167         Immediate(kIsIndirectStringMask | kIsNotStringMask));
5168   j(not_zero, not_data_object, not_data_object_distance);
5169   bind(&is_data_object);
5170 }
5171
5172
5173 void MacroAssembler::GetMarkBits(Register addr_reg,
5174                                  Register bitmap_reg,
5175                                  Register mask_reg) {
5176   DCHECK(!AreAliased(addr_reg, bitmap_reg, mask_reg, rcx));
5177   movp(bitmap_reg, addr_reg);
5178   // Sign extended 32 bit immediate.
5179   andp(bitmap_reg, Immediate(~Page::kPageAlignmentMask));
5180   movp(rcx, addr_reg);
5181   int shift =
5182       Bitmap::kBitsPerCellLog2 + kPointerSizeLog2 - Bitmap::kBytesPerCellLog2;
5183   shrl(rcx, Immediate(shift));
5184   andp(rcx,
5185        Immediate((Page::kPageAlignmentMask >> shift) &
5186                  ~(Bitmap::kBytesPerCell - 1)));
5187
5188   addp(bitmap_reg, rcx);
5189   movp(rcx, addr_reg);
5190   shrl(rcx, Immediate(kPointerSizeLog2));
5191   andp(rcx, Immediate((1 << Bitmap::kBitsPerCellLog2) - 1));
5192   movl(mask_reg, Immediate(1));
5193   shlp_cl(mask_reg);
5194 }
5195
5196
5197 void MacroAssembler::EnsureNotWhite(
5198     Register value,
5199     Register bitmap_scratch,
5200     Register mask_scratch,
5201     Label* value_is_white_and_not_data,
5202     Label::Distance distance) {
5203   DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, rcx));
5204   GetMarkBits(value, bitmap_scratch, mask_scratch);
5205
5206   // If the value is black or grey we don't need to do anything.
5207   DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
5208   DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5209   DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
5210   DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
5211
5212   Label done;
5213
5214   // Since both black and grey have a 1 in the first position and white does
5215   // not have a 1 there we only need to check one bit.
5216   testp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5217   j(not_zero, &done, Label::kNear);
5218
5219   if (emit_debug_code()) {
5220     // Check for impossible bit pattern.
5221     Label ok;
5222     Push(mask_scratch);
5223     // shl.  May overflow making the check conservative.
5224     addp(mask_scratch, mask_scratch);
5225     testp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5226     j(zero, &ok, Label::kNear);
5227     int3();
5228     bind(&ok);
5229     Pop(mask_scratch);
5230   }
5231
5232   // Value is white.  We check whether it is data that doesn't need scanning.
5233   // Currently only checks for HeapNumber and non-cons strings.
5234   Register map = rcx;  // Holds map while checking type.
5235   Register length = rcx;  // Holds length of object after checking type.
5236   Label not_heap_number;
5237   Label is_data_object;
5238
5239   // Check for heap-number
5240   movp(map, FieldOperand(value, HeapObject::kMapOffset));
5241   CompareRoot(map, Heap::kHeapNumberMapRootIndex);
5242   j(not_equal, &not_heap_number, Label::kNear);
5243   movp(length, Immediate(HeapNumber::kSize));
5244   jmp(&is_data_object, Label::kNear);
5245
5246   bind(&not_heap_number);
5247   // Check for strings.
5248   DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5249   DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5250   // If it's a string and it's not a cons string then it's an object containing
5251   // no GC pointers.
5252   Register instance_type = rcx;
5253   movzxbl(instance_type, FieldOperand(map, Map::kInstanceTypeOffset));
5254   testb(instance_type, Immediate(kIsIndirectStringMask | kIsNotStringMask));
5255   j(not_zero, value_is_white_and_not_data);
5256   // It's a non-indirect (non-cons and non-slice) string.
5257   // If it's external, the length is just ExternalString::kSize.
5258   // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
5259   Label not_external;
5260   // External strings are the only ones with the kExternalStringTag bit
5261   // set.
5262   DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
5263   DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
5264   testb(instance_type, Immediate(kExternalStringTag));
5265   j(zero, &not_external, Label::kNear);
5266   movp(length, Immediate(ExternalString::kSize));
5267   jmp(&is_data_object, Label::kNear);
5268
5269   bind(&not_external);
5270   // Sequential string, either Latin1 or UC16.
5271   DCHECK(kOneByteStringTag == 0x04);
5272   andp(length, Immediate(kStringEncodingMask));
5273   xorp(length, Immediate(kStringEncodingMask));
5274   addp(length, Immediate(0x04));
5275   // Value now either 4 (if Latin1) or 8 (if UC16), i.e. char-size shifted by 2.
5276   imulp(length, FieldOperand(value, String::kLengthOffset));
5277   shrp(length, Immediate(2 + kSmiTagSize + kSmiShiftSize));
5278   addp(length, Immediate(SeqString::kHeaderSize + kObjectAlignmentMask));
5279   andp(length, Immediate(~kObjectAlignmentMask));
5280
5281   bind(&is_data_object);
5282   // Value is a data object, and it is white.  Mark it black.  Since we know
5283   // that the object is white we can make it black by flipping one bit.
5284   orp(Operand(bitmap_scratch, MemoryChunk::kHeaderSize), mask_scratch);
5285
5286   andp(bitmap_scratch, Immediate(~Page::kPageAlignmentMask));
5287   addl(Operand(bitmap_scratch, MemoryChunk::kLiveBytesOffset), length);
5288
5289   bind(&done);
5290 }
5291
5292
5293 void MacroAssembler::CheckEnumCache(Register null_value, Label* call_runtime) {
5294   Label next, start;
5295   Register empty_fixed_array_value = r8;
5296   LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
5297   movp(rcx, rax);
5298
5299   // Check if the enum length field is properly initialized, indicating that
5300   // there is an enum cache.
5301   movp(rbx, FieldOperand(rcx, HeapObject::kMapOffset));
5302
5303   EnumLength(rdx, rbx);
5304   Cmp(rdx, Smi::FromInt(kInvalidEnumCacheSentinel));
5305   j(equal, call_runtime);
5306
5307   jmp(&start);
5308
5309   bind(&next);
5310
5311   movp(rbx, FieldOperand(rcx, HeapObject::kMapOffset));
5312
5313   // For all objects but the receiver, check that the cache is empty.
5314   EnumLength(rdx, rbx);
5315   Cmp(rdx, Smi::FromInt(0));
5316   j(not_equal, call_runtime);
5317
5318   bind(&start);
5319
5320   // Check that there are no elements. Register rcx contains the current JS
5321   // object we've reached through the prototype chain.
5322   Label no_elements;
5323   cmpp(empty_fixed_array_value,
5324        FieldOperand(rcx, JSObject::kElementsOffset));
5325   j(equal, &no_elements);
5326
5327   // Second chance, the object may be using the empty slow element dictionary.
5328   LoadRoot(kScratchRegister, Heap::kEmptySlowElementDictionaryRootIndex);
5329   cmpp(kScratchRegister, FieldOperand(rcx, JSObject::kElementsOffset));
5330   j(not_equal, call_runtime);
5331
5332   bind(&no_elements);
5333   movp(rcx, FieldOperand(rbx, Map::kPrototypeOffset));
5334   cmpp(rcx, null_value);
5335   j(not_equal, &next);
5336 }
5337
5338 void MacroAssembler::TestJSArrayForAllocationMemento(
5339     Register receiver_reg,
5340     Register scratch_reg,
5341     Label* no_memento_found) {
5342   ExternalReference new_space_start =
5343       ExternalReference::new_space_start(isolate());
5344   ExternalReference new_space_allocation_top =
5345       ExternalReference::new_space_allocation_top_address(isolate());
5346
5347   leap(scratch_reg, Operand(receiver_reg,
5348       JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
5349   Move(kScratchRegister, new_space_start);
5350   cmpp(scratch_reg, kScratchRegister);
5351   j(less, no_memento_found);
5352   cmpp(scratch_reg, ExternalOperand(new_space_allocation_top));
5353   j(greater, no_memento_found);
5354   CompareRoot(MemOperand(scratch_reg, -AllocationMemento::kSize),
5355               Heap::kAllocationMementoMapRootIndex);
5356 }
5357
5358
5359 void MacroAssembler::JumpIfDictionaryInPrototypeChain(
5360     Register object,
5361     Register scratch0,
5362     Register scratch1,
5363     Label* found) {
5364   DCHECK(!(scratch0.is(kScratchRegister) && scratch1.is(kScratchRegister)));
5365   DCHECK(!scratch1.is(scratch0));
5366   Register current = scratch0;
5367   Label loop_again;
5368
5369   movp(current, object);
5370
5371   // Loop based on the map going up the prototype chain.
5372   bind(&loop_again);
5373   movp(current, FieldOperand(current, HeapObject::kMapOffset));
5374   movp(scratch1, FieldOperand(current, Map::kBitField2Offset));
5375   DecodeField<Map::ElementsKindBits>(scratch1);
5376   cmpp(scratch1, Immediate(DICTIONARY_ELEMENTS));
5377   j(equal, found);
5378   movp(current, FieldOperand(current, Map::kPrototypeOffset));
5379   CompareRoot(current, Heap::kNullValueRootIndex);
5380   j(not_equal, &loop_again);
5381 }
5382
5383
5384 void MacroAssembler::TruncatingDiv(Register dividend, int32_t divisor) {
5385   DCHECK(!dividend.is(rax));
5386   DCHECK(!dividend.is(rdx));
5387   base::MagicNumbersForDivision<uint32_t> mag =
5388       base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
5389   movl(rax, Immediate(mag.multiplier));
5390   imull(dividend);
5391   bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
5392   if (divisor > 0 && neg) addl(rdx, dividend);
5393   if (divisor < 0 && !neg && mag.multiplier > 0) subl(rdx, dividend);
5394   if (mag.shift > 0) sarl(rdx, Immediate(mag.shift));
5395   movl(rax, dividend);
5396   shrl(rax, Immediate(31));
5397   addl(rdx, rax);
5398 }
5399
5400
5401 } }  // namespace v8::internal
5402
5403 #endif  // V8_TARGET_ARCH_X64