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 // TODO(turbofan): Cleanup these hacks.
23 enum Immediate64Type { kImm64Value, kImm64Handle, kImm64Reference };
28 Handle<Object> handle;
29 ExternalReference reference;
34 enum RegisterOrOperandType { kRegister, kDoubleRegister, kOperand };
37 struct RegisterOrOperand {
38 RegisterOrOperand() : operand(no_reg, 0) {}
40 DoubleRegister double_reg;
42 RegisterOrOperandType type;
46 // Adds X64 specific methods for decoding operands.
47 class X64OperandConverter : public InstructionOperandConverter {
49 X64OperandConverter(CodeGenerator* gen, Instruction* instr)
50 : InstructionOperandConverter(gen, instr) {}
52 RegisterOrOperand InputRegisterOrOperand(int index) {
53 return ToRegisterOrOperand(instr_->InputAt(index));
56 Immediate InputImmediate(int index) {
57 return ToImmediate(instr_->InputAt(index));
60 RegisterOrOperand OutputRegisterOrOperand() {
61 return ToRegisterOrOperand(instr_->Output());
64 Immediate64 InputImmediate64(int index) {
65 return ToImmediate64(instr_->InputAt(index));
68 Immediate64 ToImmediate64(InstructionOperand* operand) {
69 Constant constant = ToConstant(operand);
70 Immediate64 immediate;
71 immediate.value = 0xbeefdeaddeefbeed;
72 immediate.type = kImm64Value;
73 switch (constant.type()) {
74 case Constant::kInt32:
75 case Constant::kInt64:
76 immediate.value = constant.ToInt64();
78 case Constant::kFloat64:
79 immediate.type = kImm64Handle;
81 isolate()->factory()->NewNumber(constant.ToFloat64(), TENURED);
83 case Constant::kExternalReference:
84 immediate.type = kImm64Reference;
85 immediate.reference = constant.ToExternalReference();
87 case Constant::kHeapObject:
88 immediate.type = kImm64Handle;
89 immediate.handle = constant.ToHeapObject();
96 Immediate ToImmediate(InstructionOperand* operand) {
97 Constant constant = ToConstant(operand);
98 switch (constant.type()) {
99 case Constant::kInt32:
100 return Immediate(constant.ToInt32());
101 case Constant::kInt64:
102 case Constant::kFloat64:
103 case Constant::kExternalReference:
104 case Constant::kHeapObject:
108 return Immediate(-1);
111 Operand ToOperand(InstructionOperand* op, int extra = 0) {
112 RegisterOrOperand result = ToRegisterOrOperand(op, extra);
113 DCHECK_EQ(kOperand, result.type);
114 return result.operand;
117 RegisterOrOperand ToRegisterOrOperand(InstructionOperand* op, int extra = 0) {
118 RegisterOrOperand result;
119 if (op->IsRegister()) {
121 result.type = kRegister;
122 result.reg = ToRegister(op);
124 } else if (op->IsDoubleRegister()) {
127 result.type = kDoubleRegister;
128 result.double_reg = ToDoubleRegister(op);
132 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
134 result.type = kOperand;
135 // The linkage computes where all spill slots are located.
136 FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
138 Operand(offset.from_stack_pointer() ? rsp : rbp, offset.offset());
142 Operand MemoryOperand(int* first_input) {
143 const int offset = *first_input;
144 switch (AddressingModeField::decode(instr_->opcode())) {
147 Register index = InputRegister(offset + 1);
148 return Operand(InputRegister(offset + 0), index, times_1,
149 0); // TODO(dcarney): K != 0
153 return Operand(InputRegister(offset + 0), InputInt32(offset + 1));
156 return Operand(no_reg, 0);
160 Operand MemoryOperand() {
162 return MemoryOperand(&first_input);
167 static bool HasImmediateInput(Instruction* instr, int index) {
168 return instr->InputAt(index)->IsImmediate();
172 #define ASSEMBLE_BINOP(asm_instr) \
174 if (HasImmediateInput(instr, 1)) { \
175 RegisterOrOperand input = i.InputRegisterOrOperand(0); \
176 if (input.type == kRegister) { \
177 __ asm_instr(input.reg, i.InputImmediate(1)); \
179 __ asm_instr(input.operand, i.InputImmediate(1)); \
182 RegisterOrOperand input = i.InputRegisterOrOperand(1); \
183 if (input.type == kRegister) { \
184 __ asm_instr(i.InputRegister(0), input.reg); \
186 __ asm_instr(i.InputRegister(0), input.operand); \
192 #define ASSEMBLE_SHIFT(asm_instr, width) \
194 if (HasImmediateInput(instr, 1)) { \
195 __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
197 __ asm_instr##_cl(i.OutputRegister()); \
202 // Assembles an instruction after register allocation, producing machine code.
203 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
204 X64OperandConverter i(this, instr);
206 switch (ArchOpcodeField::decode(instr->opcode())) {
207 case kArchCallCodeObject: {
208 EnsureSpaceForLazyDeopt();
209 if (HasImmediateInput(instr, 0)) {
210 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
211 __ Call(code, RelocInfo::CODE_TARGET);
213 Register reg = i.InputRegister(0);
214 int entry = Code::kHeaderSize - kHeapObjectTag;
215 __ Call(Operand(reg, entry));
217 AddSafepointAndDeopt(instr);
220 case kArchCallJSFunction: {
221 EnsureSpaceForLazyDeopt();
222 Register func = i.InputRegister(0);
223 if (FLAG_debug_code) {
224 // Check the function's context matches the context argument.
225 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
226 __ Assert(equal, kWrongFunctionContext);
228 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
229 AddSafepointAndDeopt(instr);
233 __ jmp(code_->GetLabel(i.InputBlock(0)));
236 // don't emit code for nops.
241 case kArchTruncateDoubleToI:
242 __ TruncateDoubleToI(i.OutputRegister(), i.InputDoubleRegister(0));
245 ASSEMBLE_BINOP(addl);
248 ASSEMBLE_BINOP(addq);
251 ASSEMBLE_BINOP(subl);
254 ASSEMBLE_BINOP(subq);
257 ASSEMBLE_BINOP(andl);
260 ASSEMBLE_BINOP(andq);
263 ASSEMBLE_BINOP(cmpl);
266 ASSEMBLE_BINOP(cmpq);
269 ASSEMBLE_BINOP(testl);
272 ASSEMBLE_BINOP(testq);
275 if (HasImmediateInput(instr, 1)) {
276 RegisterOrOperand input = i.InputRegisterOrOperand(0);
277 if (input.type == kRegister) {
278 __ imull(i.OutputRegister(), input.reg, i.InputImmediate(1));
280 __ movq(kScratchRegister, input.operand);
281 __ imull(i.OutputRegister(), kScratchRegister, i.InputImmediate(1));
284 RegisterOrOperand input = i.InputRegisterOrOperand(1);
285 if (input.type == kRegister) {
286 __ imull(i.OutputRegister(), input.reg);
288 __ imull(i.OutputRegister(), input.operand);
293 if (HasImmediateInput(instr, 1)) {
294 RegisterOrOperand input = i.InputRegisterOrOperand(0);
295 if (input.type == kRegister) {
296 __ imulq(i.OutputRegister(), input.reg, i.InputImmediate(1));
298 __ movq(kScratchRegister, input.operand);
299 __ imulq(i.OutputRegister(), kScratchRegister, i.InputImmediate(1));
302 RegisterOrOperand input = i.InputRegisterOrOperand(1);
303 if (input.type == kRegister) {
304 __ imulq(i.OutputRegister(), input.reg);
306 __ imulq(i.OutputRegister(), input.operand);
312 __ idivl(i.InputRegister(1));
316 __ idivq(i.InputRegister(1));
320 __ divl(i.InputRegister(1));
324 __ divq(i.InputRegister(1));
327 RegisterOrOperand output = i.OutputRegisterOrOperand();
328 if (output.type == kRegister) {
331 __ notq(output.operand);
336 RegisterOrOperand output = i.OutputRegisterOrOperand();
337 if (output.type == kRegister) {
340 __ notl(output.operand);
345 RegisterOrOperand output = i.OutputRegisterOrOperand();
346 if (output.type == kRegister) {
349 __ negq(output.operand);
354 RegisterOrOperand output = i.OutputRegisterOrOperand();
355 if (output.type == kRegister) {
358 __ negl(output.operand);
369 ASSEMBLE_BINOP(xorl);
372 ASSEMBLE_BINOP(xorq);
375 ASSEMBLE_SHIFT(shll, 5);
378 ASSEMBLE_SHIFT(shlq, 6);
381 ASSEMBLE_SHIFT(shrl, 5);
384 ASSEMBLE_SHIFT(shrq, 6);
387 ASSEMBLE_SHIFT(sarl, 5);
390 ASSEMBLE_SHIFT(sarq, 6);
393 ASSEMBLE_SHIFT(rorl, 5);
396 ASSEMBLE_SHIFT(rorq, 6);
398 case kSSEFloat64Cmp: {
399 RegisterOrOperand input = i.InputRegisterOrOperand(1);
400 if (input.type == kDoubleRegister) {
401 __ ucomisd(i.InputDoubleRegister(0), input.double_reg);
403 __ ucomisd(i.InputDoubleRegister(0), input.operand);
408 __ addsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
411 __ subsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
414 __ mulsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
417 __ divsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
419 case kSSEFloat64Mod: {
420 __ subq(rsp, Immediate(kDoubleSize));
421 // Move values to st(0) and st(1).
422 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
423 __ fld_d(Operand(rsp, 0));
424 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
425 __ fld_d(Operand(rsp, 0));
426 // Loop while fprem isn't done.
429 // This instructions traps on all kinds inputs, but we are assuming the
430 // floating point control word is set to ignore them all.
432 // The following 2 instruction implicitly use rax.
434 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
437 __ shrl(rax, Immediate(8));
438 __ andl(rax, Immediate(0xFF));
442 __ j(parity_even, &mod_loop);
443 // Move output to stack and clean up.
445 __ fstp_d(Operand(rsp, 0));
446 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
447 __ addq(rsp, Immediate(kDoubleSize));
450 case kSSEFloat64Sqrt: {
451 RegisterOrOperand input = i.InputRegisterOrOperand(0);
452 if (input.type == kDoubleRegister) {
453 __ sqrtsd(i.OutputDoubleRegister(), input.double_reg);
455 __ sqrtsd(i.OutputDoubleRegister(), input.operand);
459 case kSSEFloat64ToInt32: {
460 RegisterOrOperand input = i.InputRegisterOrOperand(0);
461 if (input.type == kDoubleRegister) {
462 __ cvttsd2si(i.OutputRegister(), input.double_reg);
464 __ cvttsd2si(i.OutputRegister(), input.operand);
468 case kSSEFloat64ToUint32: {
469 RegisterOrOperand input = i.InputRegisterOrOperand(0);
470 if (input.type == kDoubleRegister) {
471 __ cvttsd2siq(i.OutputRegister(), input.double_reg);
473 __ cvttsd2siq(i.OutputRegister(), input.operand);
475 __ andl(i.OutputRegister(), i.OutputRegister()); // clear upper bits.
476 // TODO(turbofan): generated code should not look at the upper 32 bits
477 // of the result, but those bits could escape to the outside world.
480 case kSSEInt32ToFloat64: {
481 RegisterOrOperand input = i.InputRegisterOrOperand(0);
482 if (input.type == kRegister) {
483 __ cvtlsi2sd(i.OutputDoubleRegister(), input.reg);
485 __ cvtlsi2sd(i.OutputDoubleRegister(), input.operand);
489 case kSSEUint32ToFloat64: {
490 // TODO(turbofan): X64 SSE cvtqsi2sd should support operands.
491 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
495 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
498 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
502 Operand operand = i.MemoryOperand(&index);
503 if (HasImmediateInput(instr, index)) {
504 __ movb(operand, Immediate(i.InputInt8(index)));
506 __ movb(operand, i.InputRegister(index));
511 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
514 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
518 Operand operand = i.MemoryOperand(&index);
519 if (HasImmediateInput(instr, index)) {
520 __ movw(operand, Immediate(i.InputInt16(index)));
522 __ movw(operand, i.InputRegister(index));
527 if (instr->HasOutput()) {
528 if (instr->addressing_mode() == kMode_None) {
529 RegisterOrOperand input = i.InputRegisterOrOperand(0);
530 if (input.type == kRegister) {
531 __ movl(i.OutputRegister(), input.reg);
533 __ movl(i.OutputRegister(), input.operand);
536 __ movl(i.OutputRegister(), i.MemoryOperand());
540 Operand operand = i.MemoryOperand(&index);
541 if (HasImmediateInput(instr, index)) {
542 __ movl(operand, i.InputImmediate(index));
544 __ movl(operand, i.InputRegister(index));
549 RegisterOrOperand input = i.InputRegisterOrOperand(0);
550 if (input.type == kRegister) {
551 __ movsxlq(i.OutputRegister(), input.reg);
553 __ movsxlq(i.OutputRegister(), input.operand);
558 if (instr->HasOutput()) {
559 __ movq(i.OutputRegister(), i.MemoryOperand());
562 Operand operand = i.MemoryOperand(&index);
563 if (HasImmediateInput(instr, index)) {
564 __ movq(operand, i.InputImmediate(index));
566 __ movq(operand, i.InputRegister(index));
571 if (instr->HasOutput()) {
572 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
573 __ cvtss2sd(i.OutputDoubleRegister(), i.OutputDoubleRegister());
576 Operand operand = i.MemoryOperand(&index);
577 __ cvtsd2ss(xmm0, i.InputDoubleRegister(index));
578 __ movss(operand, xmm0);
582 if (instr->HasOutput()) {
583 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
586 Operand operand = i.MemoryOperand(&index);
587 __ movsd(operand, i.InputDoubleRegister(index));
591 if (HasImmediateInput(instr, 0)) {
592 __ pushq(i.InputImmediate(0));
594 RegisterOrOperand input = i.InputRegisterOrOperand(0);
595 if (input.type == kRegister) {
598 __ pushq(input.operand);
602 case kX64StoreWriteBarrier: {
603 Register object = i.InputRegister(0);
604 Register index = i.InputRegister(1);
605 Register value = i.InputRegister(2);
606 __ movsxlq(index, index);
607 __ movq(Operand(object, index, times_1, 0), value);
608 __ leaq(index, Operand(object, index, times_1, 0));
609 SaveFPRegsMode mode = code_->frame()->DidAllocateDoubleRegisters()
612 __ RecordWrite(object, index, value, mode);
619 // Assembles branches after this instruction.
620 void CodeGenerator::AssembleArchBranch(Instruction* instr,
621 FlagsCondition condition) {
622 X64OperandConverter i(this, instr);
625 // Emit a branch. The true and false targets are always the last two inputs
626 // to the instruction.
627 BasicBlock* tblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 2);
628 BasicBlock* fblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 1);
629 bool fallthru = IsNextInAssemblyOrder(fblock);
630 Label* tlabel = code()->GetLabel(tblock);
631 Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
632 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
634 case kUnorderedEqual:
635 __ j(parity_even, flabel, flabel_distance);
640 case kUnorderedNotEqual:
641 __ j(parity_even, tlabel);
644 __ j(not_equal, tlabel);
646 case kSignedLessThan:
649 case kSignedGreaterThanOrEqual:
650 __ j(greater_equal, tlabel);
652 case kSignedLessThanOrEqual:
653 __ j(less_equal, tlabel);
655 case kSignedGreaterThan:
656 __ j(greater, tlabel);
658 case kUnorderedLessThan:
659 __ j(parity_even, flabel, flabel_distance);
661 case kUnsignedLessThan:
664 case kUnorderedGreaterThanOrEqual:
665 __ j(parity_even, tlabel);
667 case kUnsignedGreaterThanOrEqual:
668 __ j(above_equal, tlabel);
670 case kUnorderedLessThanOrEqual:
671 __ j(parity_even, flabel, flabel_distance);
673 case kUnsignedLessThanOrEqual:
674 __ j(below_equal, tlabel);
676 case kUnorderedGreaterThan:
677 __ j(parity_even, tlabel);
679 case kUnsignedGreaterThan:
683 __ j(overflow, tlabel);
686 __ j(no_overflow, tlabel);
689 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
694 // Assembles boolean materializations after this instruction.
695 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
696 FlagsCondition condition) {
697 X64OperandConverter i(this, instr);
700 // Materialize a full 64-bit 1 or 0 value. The result register is always the
701 // last output of the instruction.
703 DCHECK_NE(0, instr->OutputCount());
704 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
705 Condition cc = no_condition;
707 case kUnorderedEqual:
708 __ j(parity_odd, &check, Label::kNear);
709 __ movl(reg, Immediate(0));
710 __ jmp(&done, Label::kNear);
715 case kUnorderedNotEqual:
716 __ j(parity_odd, &check, Label::kNear);
717 __ movl(reg, Immediate(1));
718 __ jmp(&done, Label::kNear);
723 case kSignedLessThan:
726 case kSignedGreaterThanOrEqual:
729 case kSignedLessThanOrEqual:
732 case kSignedGreaterThan:
735 case kUnorderedLessThan:
736 __ j(parity_odd, &check, Label::kNear);
737 __ movl(reg, Immediate(0));
738 __ jmp(&done, Label::kNear);
740 case kUnsignedLessThan:
743 case kUnorderedGreaterThanOrEqual:
744 __ j(parity_odd, &check, Label::kNear);
745 __ movl(reg, Immediate(1));
746 __ jmp(&done, Label::kNear);
748 case kUnsignedGreaterThanOrEqual:
751 case kUnorderedLessThanOrEqual:
752 __ j(parity_odd, &check, Label::kNear);
753 __ movl(reg, Immediate(0));
754 __ jmp(&done, Label::kNear);
756 case kUnsignedLessThanOrEqual:
759 case kUnorderedGreaterThan:
760 __ j(parity_odd, &check, Label::kNear);
761 __ movl(reg, Immediate(1));
762 __ jmp(&done, Label::kNear);
764 case kUnsignedGreaterThan:
776 __ movzxbl(reg, reg);
781 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
782 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
783 isolate(), deoptimization_id, Deoptimizer::LAZY);
784 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
788 void CodeGenerator::AssemblePrologue() {
789 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
790 int stack_slots = frame()->GetSpillSlotCount();
791 if (descriptor->kind() == CallDescriptor::kCallAddress) {
794 const RegList saves = descriptor->CalleeSavedRegisters();
795 if (saves != 0) { // Save callee-saved registers.
796 int register_save_area_size = 0;
797 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
798 if (!((1 << i) & saves)) continue;
799 __ pushq(Register::from_code(i));
800 register_save_area_size += kPointerSize;
802 frame()->SetRegisterSaveAreaSize(register_save_area_size);
804 } else if (descriptor->IsJSFunctionCall()) {
805 CompilationInfo* info = linkage()->info();
806 __ Prologue(info->IsCodePreAgingActive());
807 frame()->SetRegisterSaveAreaSize(
808 StandardFrameConstants::kFixedFrameSizeFromFp);
810 // Sloppy mode functions and builtins need to replace the receiver with the
811 // global proxy when called as functions (without an explicit receiver
813 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
814 if (info->strict_mode() == SLOPPY && !info->is_native()) {
816 StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
817 __ movp(rcx, args.GetReceiverOperand());
818 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
819 __ j(not_equal, &ok, Label::kNear);
820 __ movp(rcx, GlobalObjectOperand());
821 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
822 __ movp(args.GetReceiverOperand(), rcx);
828 frame()->SetRegisterSaveAreaSize(
829 StandardFrameConstants::kFixedFrameSizeFromFp);
831 if (stack_slots > 0) {
832 __ subq(rsp, Immediate(stack_slots * kPointerSize));
837 void CodeGenerator::AssembleReturn() {
838 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
839 if (descriptor->kind() == CallDescriptor::kCallAddress) {
840 if (frame()->GetRegisterSaveAreaSize() > 0) {
841 // Remove this frame's spill slots first.
842 int stack_slots = frame()->GetSpillSlotCount();
843 if (stack_slots > 0) {
844 __ addq(rsp, Immediate(stack_slots * kPointerSize));
846 const RegList saves = descriptor->CalleeSavedRegisters();
847 // Restore registers.
849 for (int i = 0; i < Register::kNumRegisters; i++) {
850 if (!((1 << i) & saves)) continue;
851 __ popq(Register::from_code(i));
854 __ popq(rbp); // Pop caller's frame pointer.
857 // No saved registers.
858 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
859 __ popq(rbp); // Pop caller's frame pointer.
863 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
864 __ popq(rbp); // Pop caller's frame pointer.
865 int pop_count = descriptor->IsJSFunctionCall()
866 ? static_cast<int>(descriptor->JSParameterCount())
868 __ ret(pop_count * kPointerSize);
873 void CodeGenerator::AssembleMove(InstructionOperand* source,
874 InstructionOperand* destination) {
875 X64OperandConverter g(this, NULL);
876 // Dispatch on the source and destination operand kinds. Not all
877 // combinations are possible.
878 if (source->IsRegister()) {
879 DCHECK(destination->IsRegister() || destination->IsStackSlot());
880 Register src = g.ToRegister(source);
881 if (destination->IsRegister()) {
882 __ movq(g.ToRegister(destination), src);
884 __ movq(g.ToOperand(destination), src);
886 } else if (source->IsStackSlot()) {
887 DCHECK(destination->IsRegister() || destination->IsStackSlot());
888 Operand src = g.ToOperand(source);
889 if (destination->IsRegister()) {
890 Register dst = g.ToRegister(destination);
893 // Spill on demand to use a temporary register for memory-to-memory
895 Register tmp = kScratchRegister;
896 Operand dst = g.ToOperand(destination);
900 } else if (source->IsConstant()) {
901 ConstantOperand* constant_source = ConstantOperand::cast(source);
902 if (destination->IsRegister() || destination->IsStackSlot()) {
903 Register dst = destination->IsRegister() ? g.ToRegister(destination)
905 Immediate64 imm = g.ToImmediate64(constant_source);
908 __ Set(dst, imm.value);
910 case kImm64Reference:
911 __ Move(dst, imm.reference);
914 __ Move(dst, imm.handle);
917 if (destination->IsStackSlot()) {
918 __ movq(g.ToOperand(destination), kScratchRegister);
921 __ movq(kScratchRegister,
922 bit_cast<uint64_t, double>(g.ToDouble(constant_source)));
923 if (destination->IsDoubleRegister()) {
924 __ movq(g.ToDoubleRegister(destination), kScratchRegister);
926 DCHECK(destination->IsDoubleStackSlot());
927 __ movq(g.ToOperand(destination), kScratchRegister);
930 } else if (source->IsDoubleRegister()) {
931 XMMRegister src = g.ToDoubleRegister(source);
932 if (destination->IsDoubleRegister()) {
933 XMMRegister dst = g.ToDoubleRegister(destination);
936 DCHECK(destination->IsDoubleStackSlot());
937 Operand dst = g.ToOperand(destination);
940 } else if (source->IsDoubleStackSlot()) {
941 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
942 Operand src = g.ToOperand(source);
943 if (destination->IsDoubleRegister()) {
944 XMMRegister dst = g.ToDoubleRegister(destination);
947 // We rely on having xmm0 available as a fixed scratch register.
948 Operand dst = g.ToOperand(destination);
958 void CodeGenerator::AssembleSwap(InstructionOperand* source,
959 InstructionOperand* destination) {
960 X64OperandConverter g(this, NULL);
961 // Dispatch on the source and destination operand kinds. Not all
962 // combinations are possible.
963 if (source->IsRegister() && destination->IsRegister()) {
964 // Register-register.
965 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
966 } else if (source->IsRegister() && destination->IsStackSlot()) {
967 Register src = g.ToRegister(source);
968 Operand dst = g.ToOperand(destination);
970 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
971 (source->IsDoubleStackSlot() &&
972 destination->IsDoubleStackSlot())) {
974 Register tmp = kScratchRegister;
975 Operand src = g.ToOperand(source);
976 Operand dst = g.ToOperand(destination);
980 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
981 // XMM register-register swap. We rely on having xmm0
982 // available as a fixed scratch register.
983 XMMRegister src = g.ToDoubleRegister(source);
984 XMMRegister dst = g.ToDoubleRegister(destination);
988 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
989 // XMM register-memory swap. We rely on having xmm0
990 // available as a fixed scratch register.
991 XMMRegister src = g.ToDoubleRegister(source);
992 Operand dst = g.ToOperand(destination);
997 // No other combinations are possible.
1003 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1006 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1007 int space_needed = Deoptimizer::patch_size();
1008 if (!linkage()->info()->IsStub()) {
1009 // Ensure that we have enough space after the previous lazy-bailout
1010 // instruction for patching the code here.
1011 int current_pc = masm()->pc_offset();
1012 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1013 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1014 __ Nop(padding_size);
1017 MarkLazyDeoptSite();
1022 } // namespace internal
1023 } // namespace compiler