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 __ imull(i.OutputRegister(), input.operand, i.InputImmediate(1));
283 RegisterOrOperand input = i.InputRegisterOrOperand(1);
284 if (input.type == kRegister) {
285 __ imull(i.OutputRegister(), input.reg);
287 __ imull(i.OutputRegister(), input.operand);
292 if (HasImmediateInput(instr, 1)) {
293 RegisterOrOperand input = i.InputRegisterOrOperand(0);
294 if (input.type == kRegister) {
295 __ imulq(i.OutputRegister(), input.reg, i.InputImmediate(1));
297 __ imulq(i.OutputRegister(), input.operand, i.InputImmediate(1));
300 RegisterOrOperand input = i.InputRegisterOrOperand(1);
301 if (input.type == kRegister) {
302 __ imulq(i.OutputRegister(), input.reg);
304 __ imulq(i.OutputRegister(), input.operand);
310 __ idivl(i.InputRegister(1));
314 __ idivq(i.InputRegister(1));
318 __ divl(i.InputRegister(1));
322 __ divq(i.InputRegister(1));
325 RegisterOrOperand output = i.OutputRegisterOrOperand();
326 if (output.type == kRegister) {
329 __ notq(output.operand);
334 RegisterOrOperand output = i.OutputRegisterOrOperand();
335 if (output.type == kRegister) {
338 __ notl(output.operand);
343 RegisterOrOperand output = i.OutputRegisterOrOperand();
344 if (output.type == kRegister) {
347 __ negq(output.operand);
352 RegisterOrOperand output = i.OutputRegisterOrOperand();
353 if (output.type == kRegister) {
356 __ negl(output.operand);
367 ASSEMBLE_BINOP(xorl);
370 ASSEMBLE_BINOP(xorq);
373 ASSEMBLE_SHIFT(shll, 5);
376 ASSEMBLE_SHIFT(shlq, 6);
379 ASSEMBLE_SHIFT(shrl, 5);
382 ASSEMBLE_SHIFT(shrq, 6);
385 ASSEMBLE_SHIFT(sarl, 5);
388 ASSEMBLE_SHIFT(sarq, 6);
391 ASSEMBLE_SHIFT(rorl, 5);
394 ASSEMBLE_SHIFT(rorq, 6);
396 case kSSEFloat64Cmp: {
397 RegisterOrOperand input = i.InputRegisterOrOperand(1);
398 if (input.type == kDoubleRegister) {
399 __ ucomisd(i.InputDoubleRegister(0), input.double_reg);
401 __ ucomisd(i.InputDoubleRegister(0), input.operand);
406 __ addsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
409 __ subsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
412 __ mulsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
415 __ divsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
417 case kSSEFloat64Mod: {
418 __ subq(rsp, Immediate(kDoubleSize));
419 // Move values to st(0) and st(1).
420 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
421 __ fld_d(Operand(rsp, 0));
422 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
423 __ fld_d(Operand(rsp, 0));
424 // Loop while fprem isn't done.
427 // This instructions traps on all kinds inputs, but we are assuming the
428 // floating point control word is set to ignore them all.
430 // The following 2 instruction implicitly use rax.
432 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
435 __ shrl(rax, Immediate(8));
436 __ andl(rax, Immediate(0xFF));
440 __ j(parity_even, &mod_loop);
441 // Move output to stack and clean up.
443 __ fstp_d(Operand(rsp, 0));
444 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
445 __ addq(rsp, Immediate(kDoubleSize));
448 case kSSEFloat64Sqrt: {
449 RegisterOrOperand input = i.InputRegisterOrOperand(0);
450 if (input.type == kDoubleRegister) {
451 __ sqrtsd(i.OutputDoubleRegister(), input.double_reg);
453 __ sqrtsd(i.OutputDoubleRegister(), input.operand);
458 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
461 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
463 case kSSEFloat64ToInt32: {
464 RegisterOrOperand input = i.InputRegisterOrOperand(0);
465 if (input.type == kDoubleRegister) {
466 __ cvttsd2si(i.OutputRegister(), input.double_reg);
468 __ cvttsd2si(i.OutputRegister(), input.operand);
472 case kSSEFloat64ToUint32: {
473 RegisterOrOperand input = i.InputRegisterOrOperand(0);
474 if (input.type == kDoubleRegister) {
475 __ cvttsd2siq(i.OutputRegister(), input.double_reg);
477 __ cvttsd2siq(i.OutputRegister(), input.operand);
479 __ andl(i.OutputRegister(), i.OutputRegister()); // clear upper bits.
480 // TODO(turbofan): generated code should not look at the upper 32 bits
481 // of the result, but those bits could escape to the outside world.
484 case kSSEInt32ToFloat64: {
485 RegisterOrOperand input = i.InputRegisterOrOperand(0);
486 if (input.type == kRegister) {
487 __ cvtlsi2sd(i.OutputDoubleRegister(), input.reg);
489 __ cvtlsi2sd(i.OutputDoubleRegister(), input.operand);
493 case kSSEUint32ToFloat64: {
494 // TODO(turbofan): X64 SSE cvtqsi2sd should support operands.
495 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
499 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
502 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
506 Operand operand = i.MemoryOperand(&index);
507 if (HasImmediateInput(instr, index)) {
508 __ movb(operand, Immediate(i.InputInt8(index)));
510 __ movb(operand, i.InputRegister(index));
515 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
518 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
522 Operand operand = i.MemoryOperand(&index);
523 if (HasImmediateInput(instr, index)) {
524 __ movw(operand, Immediate(i.InputInt16(index)));
526 __ movw(operand, i.InputRegister(index));
531 if (instr->HasOutput()) {
532 if (instr->addressing_mode() == kMode_None) {
533 RegisterOrOperand input = i.InputRegisterOrOperand(0);
534 if (input.type == kRegister) {
535 __ movl(i.OutputRegister(), input.reg);
537 __ movl(i.OutputRegister(), input.operand);
540 __ movl(i.OutputRegister(), i.MemoryOperand());
544 Operand operand = i.MemoryOperand(&index);
545 if (HasImmediateInput(instr, index)) {
546 __ movl(operand, i.InputImmediate(index));
548 __ movl(operand, i.InputRegister(index));
553 RegisterOrOperand input = i.InputRegisterOrOperand(0);
554 if (input.type == kRegister) {
555 __ movsxlq(i.OutputRegister(), input.reg);
557 __ movsxlq(i.OutputRegister(), input.operand);
562 if (instr->HasOutput()) {
563 __ movq(i.OutputRegister(), i.MemoryOperand());
566 Operand operand = i.MemoryOperand(&index);
567 if (HasImmediateInput(instr, index)) {
568 __ movq(operand, i.InputImmediate(index));
570 __ movq(operand, i.InputRegister(index));
575 if (instr->HasOutput()) {
576 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
579 Operand operand = i.MemoryOperand(&index);
580 __ movss(operand, i.InputDoubleRegister(index));
584 if (instr->HasOutput()) {
585 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
588 Operand operand = i.MemoryOperand(&index);
589 __ movsd(operand, i.InputDoubleRegister(index));
593 if (HasImmediateInput(instr, 0)) {
594 __ pushq(i.InputImmediate(0));
596 RegisterOrOperand input = i.InputRegisterOrOperand(0);
597 if (input.type == kRegister) {
600 __ pushq(input.operand);
604 case kX64StoreWriteBarrier: {
605 Register object = i.InputRegister(0);
606 Register index = i.InputRegister(1);
607 Register value = i.InputRegister(2);
608 __ movsxlq(index, index);
609 __ movq(Operand(object, index, times_1, 0), value);
610 __ leaq(index, Operand(object, index, times_1, 0));
611 SaveFPRegsMode mode = code_->frame()->DidAllocateDoubleRegisters()
614 __ RecordWrite(object, index, value, mode);
621 // Assembles branches after this instruction.
622 void CodeGenerator::AssembleArchBranch(Instruction* instr,
623 FlagsCondition condition) {
624 X64OperandConverter i(this, instr);
627 // Emit a branch. The true and false targets are always the last two inputs
628 // to the instruction.
629 BasicBlock* tblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 2);
630 BasicBlock* fblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 1);
631 bool fallthru = IsNextInAssemblyOrder(fblock);
632 Label* tlabel = code()->GetLabel(tblock);
633 Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
634 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
636 case kUnorderedEqual:
637 __ j(parity_even, flabel, flabel_distance);
642 case kUnorderedNotEqual:
643 __ j(parity_even, tlabel);
646 __ j(not_equal, tlabel);
648 case kSignedLessThan:
651 case kSignedGreaterThanOrEqual:
652 __ j(greater_equal, tlabel);
654 case kSignedLessThanOrEqual:
655 __ j(less_equal, tlabel);
657 case kSignedGreaterThan:
658 __ j(greater, tlabel);
660 case kUnorderedLessThan:
661 __ j(parity_even, flabel, flabel_distance);
663 case kUnsignedLessThan:
666 case kUnorderedGreaterThanOrEqual:
667 __ j(parity_even, tlabel);
669 case kUnsignedGreaterThanOrEqual:
670 __ j(above_equal, tlabel);
672 case kUnorderedLessThanOrEqual:
673 __ j(parity_even, flabel, flabel_distance);
675 case kUnsignedLessThanOrEqual:
676 __ j(below_equal, tlabel);
678 case kUnorderedGreaterThan:
679 __ j(parity_even, tlabel);
681 case kUnsignedGreaterThan:
685 __ j(overflow, tlabel);
688 __ j(no_overflow, tlabel);
691 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
696 // Assembles boolean materializations after this instruction.
697 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
698 FlagsCondition condition) {
699 X64OperandConverter i(this, instr);
702 // Materialize a full 64-bit 1 or 0 value. The result register is always the
703 // last output of the instruction.
705 DCHECK_NE(0, instr->OutputCount());
706 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
707 Condition cc = no_condition;
709 case kUnorderedEqual:
710 __ j(parity_odd, &check, Label::kNear);
711 __ movl(reg, Immediate(0));
712 __ jmp(&done, Label::kNear);
717 case kUnorderedNotEqual:
718 __ j(parity_odd, &check, Label::kNear);
719 __ movl(reg, Immediate(1));
720 __ jmp(&done, Label::kNear);
725 case kSignedLessThan:
728 case kSignedGreaterThanOrEqual:
731 case kSignedLessThanOrEqual:
734 case kSignedGreaterThan:
737 case kUnorderedLessThan:
738 __ j(parity_odd, &check, Label::kNear);
739 __ movl(reg, Immediate(0));
740 __ jmp(&done, Label::kNear);
742 case kUnsignedLessThan:
745 case kUnorderedGreaterThanOrEqual:
746 __ j(parity_odd, &check, Label::kNear);
747 __ movl(reg, Immediate(1));
748 __ jmp(&done, Label::kNear);
750 case kUnsignedGreaterThanOrEqual:
753 case kUnorderedLessThanOrEqual:
754 __ j(parity_odd, &check, Label::kNear);
755 __ movl(reg, Immediate(0));
756 __ jmp(&done, Label::kNear);
758 case kUnsignedLessThanOrEqual:
761 case kUnorderedGreaterThan:
762 __ j(parity_odd, &check, Label::kNear);
763 __ movl(reg, Immediate(1));
764 __ jmp(&done, Label::kNear);
766 case kUnsignedGreaterThan:
778 __ movzxbl(reg, reg);
783 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
784 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
785 isolate(), deoptimization_id, Deoptimizer::LAZY);
786 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
790 void CodeGenerator::AssemblePrologue() {
791 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
792 int stack_slots = frame()->GetSpillSlotCount();
793 if (descriptor->kind() == CallDescriptor::kCallAddress) {
796 const RegList saves = descriptor->CalleeSavedRegisters();
797 if (saves != 0) { // Save callee-saved registers.
798 int register_save_area_size = 0;
799 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
800 if (!((1 << i) & saves)) continue;
801 __ pushq(Register::from_code(i));
802 register_save_area_size += kPointerSize;
804 frame()->SetRegisterSaveAreaSize(register_save_area_size);
806 } else if (descriptor->IsJSFunctionCall()) {
807 CompilationInfo* info = linkage()->info();
808 __ Prologue(info->IsCodePreAgingActive());
809 frame()->SetRegisterSaveAreaSize(
810 StandardFrameConstants::kFixedFrameSizeFromFp);
812 // Sloppy mode functions and builtins need to replace the receiver with the
813 // global proxy when called as functions (without an explicit receiver
815 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
816 if (info->strict_mode() == SLOPPY && !info->is_native()) {
818 StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
819 __ movp(rcx, args.GetReceiverOperand());
820 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
821 __ j(not_equal, &ok, Label::kNear);
822 __ movp(rcx, GlobalObjectOperand());
823 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
824 __ movp(args.GetReceiverOperand(), rcx);
830 frame()->SetRegisterSaveAreaSize(
831 StandardFrameConstants::kFixedFrameSizeFromFp);
833 if (stack_slots > 0) {
834 __ subq(rsp, Immediate(stack_slots * kPointerSize));
839 void CodeGenerator::AssembleReturn() {
840 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
841 if (descriptor->kind() == CallDescriptor::kCallAddress) {
842 if (frame()->GetRegisterSaveAreaSize() > 0) {
843 // Remove this frame's spill slots first.
844 int stack_slots = frame()->GetSpillSlotCount();
845 if (stack_slots > 0) {
846 __ addq(rsp, Immediate(stack_slots * kPointerSize));
848 const RegList saves = descriptor->CalleeSavedRegisters();
849 // Restore registers.
851 for (int i = 0; i < Register::kNumRegisters; i++) {
852 if (!((1 << i) & saves)) continue;
853 __ popq(Register::from_code(i));
856 __ popq(rbp); // Pop caller's frame pointer.
859 // No saved registers.
860 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
861 __ popq(rbp); // Pop caller's frame pointer.
865 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
866 __ popq(rbp); // Pop caller's frame pointer.
867 int pop_count = descriptor->IsJSFunctionCall()
868 ? static_cast<int>(descriptor->JSParameterCount())
870 __ ret(pop_count * kPointerSize);
875 void CodeGenerator::AssembleMove(InstructionOperand* source,
876 InstructionOperand* destination) {
877 X64OperandConverter g(this, NULL);
878 // Dispatch on the source and destination operand kinds. Not all
879 // combinations are possible.
880 if (source->IsRegister()) {
881 DCHECK(destination->IsRegister() || destination->IsStackSlot());
882 Register src = g.ToRegister(source);
883 if (destination->IsRegister()) {
884 __ movq(g.ToRegister(destination), src);
886 __ movq(g.ToOperand(destination), src);
888 } else if (source->IsStackSlot()) {
889 DCHECK(destination->IsRegister() || destination->IsStackSlot());
890 Operand src = g.ToOperand(source);
891 if (destination->IsRegister()) {
892 Register dst = g.ToRegister(destination);
895 // Spill on demand to use a temporary register for memory-to-memory
897 Register tmp = kScratchRegister;
898 Operand dst = g.ToOperand(destination);
902 } else if (source->IsConstant()) {
903 ConstantOperand* constant_source = ConstantOperand::cast(source);
904 if (destination->IsRegister() || destination->IsStackSlot()) {
905 Register dst = destination->IsRegister() ? g.ToRegister(destination)
907 Immediate64 imm = g.ToImmediate64(constant_source);
910 __ Set(dst, imm.value);
912 case kImm64Reference:
913 __ Move(dst, imm.reference);
916 __ Move(dst, imm.handle);
919 if (destination->IsStackSlot()) {
920 __ movq(g.ToOperand(destination), kScratchRegister);
923 __ movq(kScratchRegister,
924 bit_cast<uint64_t, double>(g.ToDouble(constant_source)));
925 if (destination->IsDoubleRegister()) {
926 __ movq(g.ToDoubleRegister(destination), kScratchRegister);
928 DCHECK(destination->IsDoubleStackSlot());
929 __ movq(g.ToOperand(destination), kScratchRegister);
932 } else if (source->IsDoubleRegister()) {
933 XMMRegister src = g.ToDoubleRegister(source);
934 if (destination->IsDoubleRegister()) {
935 XMMRegister dst = g.ToDoubleRegister(destination);
938 DCHECK(destination->IsDoubleStackSlot());
939 Operand dst = g.ToOperand(destination);
942 } else if (source->IsDoubleStackSlot()) {
943 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
944 Operand src = g.ToOperand(source);
945 if (destination->IsDoubleRegister()) {
946 XMMRegister dst = g.ToDoubleRegister(destination);
949 // We rely on having xmm0 available as a fixed scratch register.
950 Operand dst = g.ToOperand(destination);
960 void CodeGenerator::AssembleSwap(InstructionOperand* source,
961 InstructionOperand* destination) {
962 X64OperandConverter g(this, NULL);
963 // Dispatch on the source and destination operand kinds. Not all
964 // combinations are possible.
965 if (source->IsRegister() && destination->IsRegister()) {
966 // Register-register.
967 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
968 } else if (source->IsRegister() && destination->IsStackSlot()) {
969 Register src = g.ToRegister(source);
970 Operand dst = g.ToOperand(destination);
972 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
973 (source->IsDoubleStackSlot() &&
974 destination->IsDoubleStackSlot())) {
976 Register tmp = kScratchRegister;
977 Operand src = g.ToOperand(source);
978 Operand dst = g.ToOperand(destination);
982 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
983 // XMM register-register swap. We rely on having xmm0
984 // available as a fixed scratch register.
985 XMMRegister src = g.ToDoubleRegister(source);
986 XMMRegister dst = g.ToDoubleRegister(destination);
990 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
991 // XMM register-memory swap. We rely on having xmm0
992 // available as a fixed scratch register.
993 XMMRegister src = g.ToDoubleRegister(source);
994 Operand dst = g.ToOperand(destination);
999 // No other combinations are possible.
1005 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1008 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1009 int space_needed = Deoptimizer::patch_size();
1010 if (!linkage()->info()->IsStub()) {
1011 // Ensure that we have enough space after the previous lazy-bailout
1012 // instruction for patching the code here.
1013 int current_pc = masm()->pc_offset();
1014 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1015 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1016 __ Nop(padding_size);
1019 MarkLazyDeoptSite();
1024 } // namespace internal
1025 } // namespace compiler