1 // Copyright 2013 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.
5 #include "src/compiler/code-generator.h"
7 #include "src/compiler/code-generator-impl.h"
8 #include "src/compiler/gap-resolver.h"
9 #include "src/compiler/node-matchers.h"
10 #include "src/compiler/node-properties-inl.h"
11 #include "src/scopes.h"
12 #include "src/x64/assembler-x64.h"
13 #include "src/x64/macro-assembler-x64.h"
22 // Adds X64 specific methods for decoding operands.
23 class X64OperandConverter : public InstructionOperandConverter {
25 X64OperandConverter(CodeGenerator* gen, Instruction* instr)
26 : InstructionOperandConverter(gen, instr) {}
28 Immediate InputImmediate(int index) {
29 return ToImmediate(instr_->InputAt(index));
32 Operand InputOperand(int index) { return ToOperand(instr_->InputAt(index)); }
34 Operand OutputOperand() { return ToOperand(instr_->Output()); }
36 Immediate ToImmediate(InstructionOperand* operand) {
37 return Immediate(ToConstant(operand).ToInt32());
40 Operand ToOperand(InstructionOperand* op, int extra = 0) {
41 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
42 // The linkage computes where all spill slots are located.
43 FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
44 return Operand(offset.from_stack_pointer() ? rsp : rbp, offset.offset());
47 static int NextOffset(int* offset) {
53 static ScaleFactor ScaleFor(AddressingMode one, AddressingMode mode) {
54 STATIC_ASSERT(0 == static_cast<int>(times_1));
55 STATIC_ASSERT(1 == static_cast<int>(times_2));
56 STATIC_ASSERT(2 == static_cast<int>(times_4));
57 STATIC_ASSERT(3 == static_cast<int>(times_8));
58 int scale = static_cast<int>(mode - one);
59 DCHECK(scale >= 0 && scale < 4);
60 return static_cast<ScaleFactor>(scale);
63 Operand MemoryOperand(int* offset) {
64 AddressingMode mode = AddressingModeField::decode(instr_->opcode());
67 Register base = InputRegister(NextOffset(offset));
69 return Operand(base, disp);
72 Register base = InputRegister(NextOffset(offset));
73 int32_t disp = InputInt32(NextOffset(offset));
74 return Operand(base, disp);
80 Register base = InputRegister(NextOffset(offset));
81 Register index = InputRegister(NextOffset(offset));
82 ScaleFactor scale = ScaleFor(kMode_MR1, mode);
84 return Operand(base, index, scale, disp);
90 Register base = InputRegister(NextOffset(offset));
91 Register index = InputRegister(NextOffset(offset));
92 ScaleFactor scale = ScaleFor(kMode_MR1I, mode);
93 int32_t disp = InputInt32(NextOffset(offset));
94 return Operand(base, index, scale, disp);
100 Register index = InputRegister(NextOffset(offset));
101 ScaleFactor scale = ScaleFor(kMode_M1, mode);
103 return Operand(index, scale, disp);
109 Register index = InputRegister(NextOffset(offset));
110 ScaleFactor scale = ScaleFor(kMode_M1I, mode);
111 int32_t disp = InputInt32(NextOffset(offset));
112 return Operand(index, scale, disp);
116 return Operand(no_reg, 0);
119 return Operand(no_reg, 0);
122 Operand MemoryOperand() {
124 return MemoryOperand(&first_input);
129 static bool HasImmediateInput(Instruction* instr, int index) {
130 return instr->InputAt(index)->IsImmediate();
134 #define ASSEMBLE_UNOP(asm_instr) \
136 if (instr->Output()->IsRegister()) { \
137 __ asm_instr(i.OutputRegister()); \
139 __ asm_instr(i.OutputOperand()); \
144 #define ASSEMBLE_BINOP(asm_instr) \
146 if (HasImmediateInput(instr, 1)) { \
147 if (instr->InputAt(0)->IsRegister()) { \
148 __ asm_instr(i.InputRegister(0), i.InputImmediate(1)); \
150 __ asm_instr(i.InputOperand(0), i.InputImmediate(1)); \
153 if (instr->InputAt(1)->IsRegister()) { \
154 __ asm_instr(i.InputRegister(0), i.InputRegister(1)); \
156 __ asm_instr(i.InputRegister(0), i.InputOperand(1)); \
162 #define ASSEMBLE_MULT(asm_instr) \
164 if (HasImmediateInput(instr, 1)) { \
165 if (instr->InputAt(0)->IsRegister()) { \
166 __ asm_instr(i.OutputRegister(), i.InputRegister(0), \
167 i.InputImmediate(1)); \
169 __ asm_instr(i.OutputRegister(), i.InputOperand(0), \
170 i.InputImmediate(1)); \
173 if (instr->InputAt(1)->IsRegister()) { \
174 __ asm_instr(i.OutputRegister(), i.InputRegister(1)); \
176 __ asm_instr(i.OutputRegister(), i.InputOperand(1)); \
182 #define ASSEMBLE_SHIFT(asm_instr, width) \
184 if (HasImmediateInput(instr, 1)) { \
185 if (instr->Output()->IsRegister()) { \
186 __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
188 __ asm_instr(i.OutputOperand(), Immediate(i.InputInt##width(1))); \
191 if (instr->Output()->IsRegister()) { \
192 __ asm_instr##_cl(i.OutputRegister()); \
194 __ asm_instr##_cl(i.OutputOperand()); \
200 #define ASSEMBLE_DOUBLE_BINOP(asm_instr) \
202 if (instr->InputAt(1)->IsDoubleRegister()) { \
203 __ asm_instr(i.InputDoubleRegister(0), i.InputDoubleRegister(1)); \
205 __ asm_instr(i.InputDoubleRegister(0), i.InputOperand(1)); \
210 // Assembles an instruction after register allocation, producing machine code.
211 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
212 X64OperandConverter i(this, instr);
214 switch (ArchOpcodeField::decode(instr->opcode())) {
215 case kArchCallCodeObject: {
216 EnsureSpaceForLazyDeopt();
217 if (HasImmediateInput(instr, 0)) {
218 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
219 __ Call(code, RelocInfo::CODE_TARGET);
221 Register reg = i.InputRegister(0);
222 int entry = Code::kHeaderSize - kHeapObjectTag;
223 __ Call(Operand(reg, entry));
225 AddSafepointAndDeopt(instr);
228 case kArchCallJSFunction: {
229 EnsureSpaceForLazyDeopt();
230 Register func = i.InputRegister(0);
231 if (FLAG_debug_code) {
232 // Check the function's context matches the context argument.
233 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
234 __ Assert(equal, kWrongFunctionContext);
236 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
237 AddSafepointAndDeopt(instr);
241 __ jmp(GetLabel(i.InputRpo(0)));
244 // don't emit code for nops.
249 case kArchStackPointer:
250 __ movq(i.OutputRegister(), rsp);
252 case kArchTruncateDoubleToI:
253 __ TruncateDoubleToI(i.OutputRegister(), i.InputDoubleRegister(0));
256 ASSEMBLE_BINOP(addl);
259 ASSEMBLE_BINOP(addq);
262 ASSEMBLE_BINOP(subl);
265 ASSEMBLE_BINOP(subq);
268 ASSEMBLE_BINOP(andl);
271 ASSEMBLE_BINOP(andq);
274 ASSEMBLE_BINOP(cmpl);
277 ASSEMBLE_BINOP(cmpq);
280 ASSEMBLE_BINOP(testl);
283 ASSEMBLE_BINOP(testq);
286 ASSEMBLE_MULT(imull);
289 ASSEMBLE_MULT(imulq);
292 if (instr->InputAt(1)->IsRegister()) {
293 __ imull(i.InputRegister(1));
295 __ imull(i.InputOperand(1));
299 if (instr->InputAt(1)->IsRegister()) {
300 __ mull(i.InputRegister(1));
302 __ mull(i.InputOperand(1));
307 __ idivl(i.InputRegister(1));
311 __ idivq(i.InputRegister(1));
315 __ divl(i.InputRegister(1));
319 __ divq(i.InputRegister(1));
340 ASSEMBLE_BINOP(xorl);
343 ASSEMBLE_BINOP(xorq);
346 ASSEMBLE_SHIFT(shll, 5);
349 ASSEMBLE_SHIFT(shlq, 6);
352 ASSEMBLE_SHIFT(shrl, 5);
355 ASSEMBLE_SHIFT(shrq, 6);
358 ASSEMBLE_SHIFT(sarl, 5);
361 ASSEMBLE_SHIFT(sarq, 6);
364 ASSEMBLE_SHIFT(rorl, 5);
367 ASSEMBLE_SHIFT(rorq, 6);
370 ASSEMBLE_DOUBLE_BINOP(ucomisd);
373 ASSEMBLE_DOUBLE_BINOP(addsd);
376 ASSEMBLE_DOUBLE_BINOP(subsd);
379 ASSEMBLE_DOUBLE_BINOP(mulsd);
382 ASSEMBLE_DOUBLE_BINOP(divsd);
384 case kSSEFloat64Mod: {
385 __ subq(rsp, Immediate(kDoubleSize));
386 // Move values to st(0) and st(1).
387 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
388 __ fld_d(Operand(rsp, 0));
389 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
390 __ fld_d(Operand(rsp, 0));
391 // Loop while fprem isn't done.
394 // This instructions traps on all kinds inputs, but we are assuming the
395 // floating point control word is set to ignore them all.
397 // The following 2 instruction implicitly use rax.
399 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
402 __ shrl(rax, Immediate(8));
403 __ andl(rax, Immediate(0xFF));
407 __ j(parity_even, &mod_loop);
408 // Move output to stack and clean up.
410 __ fstp_d(Operand(rsp, 0));
411 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
412 __ addq(rsp, Immediate(kDoubleSize));
415 case kSSEFloat64Sqrt:
416 if (instr->InputAt(0)->IsDoubleRegister()) {
417 __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
419 __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
422 case kSSEFloat64Floor: {
423 CpuFeatureScope sse_scope(masm(), SSE4_1);
424 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
425 v8::internal::Assembler::kRoundDown);
428 case kSSEFloat64Ceil: {
429 CpuFeatureScope sse_scope(masm(), SSE4_1);
430 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
431 v8::internal::Assembler::kRoundUp);
434 case kSSEFloat64RoundTruncate: {
435 CpuFeatureScope sse_scope(masm(), SSE4_1);
436 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
437 v8::internal::Assembler::kRoundToZero);
441 if (instr->InputAt(0)->IsDoubleRegister()) {
442 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
444 __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
448 if (instr->InputAt(0)->IsDoubleRegister()) {
449 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
451 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
454 case kSSEFloat64ToInt32:
455 if (instr->InputAt(0)->IsDoubleRegister()) {
456 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
458 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
461 case kSSEFloat64ToUint32: {
462 if (instr->InputAt(0)->IsDoubleRegister()) {
463 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
465 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
467 __ AssertZeroExtended(i.OutputRegister());
470 case kSSEInt32ToFloat64:
471 if (instr->InputAt(0)->IsRegister()) {
472 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
474 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
477 case kSSEUint32ToFloat64:
478 if (instr->InputAt(0)->IsRegister()) {
479 __ movl(kScratchRegister, i.InputRegister(0));
481 __ movl(kScratchRegister, i.InputOperand(0));
483 __ cvtqsi2sd(i.OutputDoubleRegister(), kScratchRegister);
486 if (instr->addressing_mode() != kMode_None) {
487 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
488 } else if (instr->InputAt(0)->IsRegister()) {
489 __ movsxbl(i.OutputRegister(), i.InputRegister(0));
491 __ movsxbl(i.OutputRegister(), i.InputOperand(0));
493 __ AssertZeroExtended(i.OutputRegister());
496 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
500 Operand operand = i.MemoryOperand(&index);
501 if (HasImmediateInput(instr, index)) {
502 __ movb(operand, Immediate(i.InputInt8(index)));
504 __ movb(operand, i.InputRegister(index));
509 if (instr->addressing_mode() != kMode_None) {
510 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
511 } else if (instr->InputAt(0)->IsRegister()) {
512 __ movsxwl(i.OutputRegister(), i.InputRegister(0));
514 __ movsxwl(i.OutputRegister(), i.InputOperand(0));
516 __ AssertZeroExtended(i.OutputRegister());
519 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
520 __ AssertZeroExtended(i.OutputRegister());
524 Operand operand = i.MemoryOperand(&index);
525 if (HasImmediateInput(instr, index)) {
526 __ movw(operand, Immediate(i.InputInt16(index)));
528 __ movw(operand, i.InputRegister(index));
533 if (instr->HasOutput()) {
534 if (instr->addressing_mode() == kMode_None) {
535 if (instr->InputAt(0)->IsRegister()) {
536 __ movl(i.OutputRegister(), i.InputRegister(0));
538 __ movl(i.OutputRegister(), i.InputOperand(0));
541 __ movl(i.OutputRegister(), i.MemoryOperand());
543 __ AssertZeroExtended(i.OutputRegister());
546 Operand operand = i.MemoryOperand(&index);
547 if (HasImmediateInput(instr, index)) {
548 __ movl(operand, i.InputImmediate(index));
550 __ movl(operand, i.InputRegister(index));
555 if (instr->InputAt(0)->IsRegister()) {
556 __ movsxlq(i.OutputRegister(), i.InputRegister(0));
558 __ movsxlq(i.OutputRegister(), i.InputOperand(0));
563 if (instr->HasOutput()) {
564 __ movq(i.OutputRegister(), i.MemoryOperand());
567 Operand operand = i.MemoryOperand(&index);
568 if (HasImmediateInput(instr, index)) {
569 __ movq(operand, i.InputImmediate(index));
571 __ movq(operand, i.InputRegister(index));
576 if (instr->HasOutput()) {
577 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
580 Operand operand = i.MemoryOperand(&index);
581 __ movss(operand, i.InputDoubleRegister(index));
585 if (instr->HasOutput()) {
586 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
589 Operand operand = i.MemoryOperand(&index);
590 __ movsd(operand, i.InputDoubleRegister(index));
594 __ leal(i.OutputRegister(), i.MemoryOperand());
595 __ AssertZeroExtended(i.OutputRegister());
598 __ leaq(i.OutputRegister(), i.MemoryOperand());
601 if (HasImmediateInput(instr, 0)) {
602 __ pushq(i.InputImmediate(0));
604 if (instr->InputAt(0)->IsRegister()) {
605 __ pushq(i.InputRegister(0));
607 __ pushq(i.InputOperand(0));
611 case kX64StoreWriteBarrier: {
612 Register object = i.InputRegister(0);
613 Register index = i.InputRegister(1);
614 Register value = i.InputRegister(2);
615 __ movsxlq(index, index);
616 __ movq(Operand(object, index, times_1, 0), value);
617 __ leaq(index, Operand(object, index, times_1, 0));
618 SaveFPRegsMode mode =
619 frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
620 __ RecordWrite(object, index, value, mode);
627 // Assembles branches after this instruction.
628 void CodeGenerator::AssembleArchBranch(Instruction* instr,
629 FlagsCondition condition) {
630 X64OperandConverter i(this, instr);
633 // Emit a branch. The true and false targets are always the last two inputs
634 // to the instruction.
635 BasicBlock::RpoNumber tblock =
636 i.InputRpo(static_cast<int>(instr->InputCount()) - 2);
637 BasicBlock::RpoNumber fblock =
638 i.InputRpo(static_cast<int>(instr->InputCount()) - 1);
639 bool fallthru = IsNextInAssemblyOrder(fblock);
640 Label* tlabel = GetLabel(tblock);
641 Label* flabel = fallthru ? &done : GetLabel(fblock);
642 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
644 case kUnorderedEqual:
645 __ j(parity_even, flabel, flabel_distance);
650 case kUnorderedNotEqual:
651 __ j(parity_even, tlabel);
654 __ j(not_equal, tlabel);
656 case kSignedLessThan:
659 case kSignedGreaterThanOrEqual:
660 __ j(greater_equal, tlabel);
662 case kSignedLessThanOrEqual:
663 __ j(less_equal, tlabel);
665 case kSignedGreaterThan:
666 __ j(greater, tlabel);
668 case kUnorderedLessThan:
669 __ j(parity_even, flabel, flabel_distance);
671 case kUnsignedLessThan:
674 case kUnorderedGreaterThanOrEqual:
675 __ j(parity_even, tlabel);
677 case kUnsignedGreaterThanOrEqual:
678 __ j(above_equal, tlabel);
680 case kUnorderedLessThanOrEqual:
681 __ j(parity_even, flabel, flabel_distance);
683 case kUnsignedLessThanOrEqual:
684 __ j(below_equal, tlabel);
686 case kUnorderedGreaterThan:
687 __ j(parity_even, tlabel);
689 case kUnsignedGreaterThan:
693 __ j(overflow, tlabel);
696 __ j(no_overflow, tlabel);
699 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
704 // Assembles boolean materializations after this instruction.
705 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
706 FlagsCondition condition) {
707 X64OperandConverter i(this, instr);
710 // Materialize a full 64-bit 1 or 0 value. The result register is always the
711 // last output of the instruction.
713 DCHECK_NE(0, static_cast<int>(instr->OutputCount()));
714 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
715 Condition cc = no_condition;
717 case kUnorderedEqual:
718 __ j(parity_odd, &check, Label::kNear);
719 __ movl(reg, Immediate(0));
720 __ jmp(&done, Label::kNear);
725 case kUnorderedNotEqual:
726 __ j(parity_odd, &check, Label::kNear);
727 __ movl(reg, Immediate(1));
728 __ jmp(&done, Label::kNear);
733 case kSignedLessThan:
736 case kSignedGreaterThanOrEqual:
739 case kSignedLessThanOrEqual:
742 case kSignedGreaterThan:
745 case kUnorderedLessThan:
746 __ j(parity_odd, &check, Label::kNear);
747 __ movl(reg, Immediate(0));
748 __ jmp(&done, Label::kNear);
750 case kUnsignedLessThan:
753 case kUnorderedGreaterThanOrEqual:
754 __ j(parity_odd, &check, Label::kNear);
755 __ movl(reg, Immediate(1));
756 __ jmp(&done, Label::kNear);
758 case kUnsignedGreaterThanOrEqual:
761 case kUnorderedLessThanOrEqual:
762 __ j(parity_odd, &check, Label::kNear);
763 __ movl(reg, Immediate(0));
764 __ jmp(&done, Label::kNear);
766 case kUnsignedLessThanOrEqual:
769 case kUnorderedGreaterThan:
770 __ j(parity_odd, &check, Label::kNear);
771 __ movl(reg, Immediate(1));
772 __ jmp(&done, Label::kNear);
774 case kUnsignedGreaterThan:
786 __ movzxbl(reg, reg);
791 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
792 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
793 isolate(), deoptimization_id, Deoptimizer::LAZY);
794 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
798 void CodeGenerator::AssemblePrologue() {
799 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
800 int stack_slots = frame()->GetSpillSlotCount();
801 if (descriptor->kind() == CallDescriptor::kCallAddress) {
804 const RegList saves = descriptor->CalleeSavedRegisters();
805 if (saves != 0) { // Save callee-saved registers.
806 int register_save_area_size = 0;
807 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
808 if (!((1 << i) & saves)) continue;
809 __ pushq(Register::from_code(i));
810 register_save_area_size += kPointerSize;
812 frame()->SetRegisterSaveAreaSize(register_save_area_size);
814 } else if (descriptor->IsJSFunctionCall()) {
815 CompilationInfo* info = this->info();
816 __ Prologue(info->IsCodePreAgingActive());
817 frame()->SetRegisterSaveAreaSize(
818 StandardFrameConstants::kFixedFrameSizeFromFp);
821 frame()->SetRegisterSaveAreaSize(
822 StandardFrameConstants::kFixedFrameSizeFromFp);
824 if (stack_slots > 0) {
825 __ subq(rsp, Immediate(stack_slots * kPointerSize));
830 void CodeGenerator::AssembleReturn() {
831 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
832 if (descriptor->kind() == CallDescriptor::kCallAddress) {
833 if (frame()->GetRegisterSaveAreaSize() > 0) {
834 // Remove this frame's spill slots first.
835 int stack_slots = frame()->GetSpillSlotCount();
836 if (stack_slots > 0) {
837 __ addq(rsp, Immediate(stack_slots * kPointerSize));
839 const RegList saves = descriptor->CalleeSavedRegisters();
840 // Restore registers.
842 for (int i = 0; i < Register::kNumRegisters; i++) {
843 if (!((1 << i) & saves)) continue;
844 __ popq(Register::from_code(i));
847 __ popq(rbp); // Pop caller's frame pointer.
850 // No saved registers.
851 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
852 __ popq(rbp); // Pop caller's frame pointer.
856 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
857 __ popq(rbp); // Pop caller's frame pointer.
858 int pop_count = descriptor->IsJSFunctionCall()
859 ? static_cast<int>(descriptor->JSParameterCount())
861 __ ret(pop_count * kPointerSize);
866 void CodeGenerator::AssembleMove(InstructionOperand* source,
867 InstructionOperand* destination) {
868 X64OperandConverter g(this, NULL);
869 // Dispatch on the source and destination operand kinds. Not all
870 // combinations are possible.
871 if (source->IsRegister()) {
872 DCHECK(destination->IsRegister() || destination->IsStackSlot());
873 Register src = g.ToRegister(source);
874 if (destination->IsRegister()) {
875 __ movq(g.ToRegister(destination), src);
877 __ movq(g.ToOperand(destination), src);
879 } else if (source->IsStackSlot()) {
880 DCHECK(destination->IsRegister() || destination->IsStackSlot());
881 Operand src = g.ToOperand(source);
882 if (destination->IsRegister()) {
883 Register dst = g.ToRegister(destination);
886 // Spill on demand to use a temporary register for memory-to-memory
888 Register tmp = kScratchRegister;
889 Operand dst = g.ToOperand(destination);
893 } else if (source->IsConstant()) {
894 ConstantOperand* constant_source = ConstantOperand::cast(source);
895 Constant src = g.ToConstant(constant_source);
896 if (destination->IsRegister() || destination->IsStackSlot()) {
897 Register dst = destination->IsRegister() ? g.ToRegister(destination)
899 switch (src.type()) {
900 case Constant::kInt32:
901 // TODO(dcarney): don't need scratch in this case.
902 __ Set(dst, src.ToInt32());
904 case Constant::kInt64:
905 __ Set(dst, src.ToInt64());
907 case Constant::kFloat32:
909 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
911 case Constant::kFloat64:
913 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
915 case Constant::kExternalReference:
916 __ Move(dst, src.ToExternalReference());
918 case Constant::kHeapObject:
919 __ Move(dst, src.ToHeapObject());
922 if (destination->IsStackSlot()) {
923 __ movq(g.ToOperand(destination), kScratchRegister);
925 } else if (src.type() == Constant::kFloat32) {
926 // TODO(turbofan): Can we do better here?
927 uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
928 if (destination->IsDoubleRegister()) {
929 __ Move(g.ToDoubleRegister(destination), src_const);
931 DCHECK(destination->IsDoubleStackSlot());
932 Operand dst = g.ToOperand(destination);
933 __ movl(dst, Immediate(src_const));
936 DCHECK_EQ(Constant::kFloat64, src.type());
937 uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
938 if (destination->IsDoubleRegister()) {
939 __ Move(g.ToDoubleRegister(destination), src_const);
941 DCHECK(destination->IsDoubleStackSlot());
942 __ movq(kScratchRegister, src_const);
943 __ movq(g.ToOperand(destination), kScratchRegister);
946 } else if (source->IsDoubleRegister()) {
947 XMMRegister src = g.ToDoubleRegister(source);
948 if (destination->IsDoubleRegister()) {
949 XMMRegister dst = g.ToDoubleRegister(destination);
952 DCHECK(destination->IsDoubleStackSlot());
953 Operand dst = g.ToOperand(destination);
956 } else if (source->IsDoubleStackSlot()) {
957 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
958 Operand src = g.ToOperand(source);
959 if (destination->IsDoubleRegister()) {
960 XMMRegister dst = g.ToDoubleRegister(destination);
963 // We rely on having xmm0 available as a fixed scratch register.
964 Operand dst = g.ToOperand(destination);
974 void CodeGenerator::AssembleSwap(InstructionOperand* source,
975 InstructionOperand* destination) {
976 X64OperandConverter g(this, NULL);
977 // Dispatch on the source and destination operand kinds. Not all
978 // combinations are possible.
979 if (source->IsRegister() && destination->IsRegister()) {
980 // Register-register.
981 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
982 } else if (source->IsRegister() && destination->IsStackSlot()) {
983 Register src = g.ToRegister(source);
984 Operand dst = g.ToOperand(destination);
986 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
987 (source->IsDoubleStackSlot() &&
988 destination->IsDoubleStackSlot())) {
990 Register tmp = kScratchRegister;
991 Operand src = g.ToOperand(source);
992 Operand dst = g.ToOperand(destination);
996 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
997 // XMM register-register swap. We rely on having xmm0
998 // available as a fixed scratch register.
999 XMMRegister src = g.ToDoubleRegister(source);
1000 XMMRegister dst = g.ToDoubleRegister(destination);
1001 __ movsd(xmm0, src);
1003 __ movsd(dst, xmm0);
1004 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
1005 // XMM register-memory swap. We rely on having xmm0
1006 // available as a fixed scratch register.
1007 XMMRegister src = g.ToDoubleRegister(source);
1008 Operand dst = g.ToOperand(destination);
1009 __ movsd(xmm0, src);
1011 __ movsd(dst, xmm0);
1013 // No other combinations are possible.
1019 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1022 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1023 int space_needed = Deoptimizer::patch_size();
1024 if (!info()->IsStub()) {
1025 // Ensure that we have enough space after the previous lazy-bailout
1026 // instruction for patching the code here.
1027 int current_pc = masm()->pc_offset();
1028 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1029 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1030 __ Nop(padding_size);
1033 MarkLazyDeoptSite();
1038 } // namespace internal
1039 } // namespace compiler