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 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
487 __ AssertZeroExtended(i.OutputRegister());
490 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
494 Operand operand = i.MemoryOperand(&index);
495 if (HasImmediateInput(instr, index)) {
496 __ movb(operand, Immediate(i.InputInt8(index)));
498 __ movb(operand, i.InputRegister(index));
503 if (instr->addressing_mode() != kMode_None) {
504 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
505 } else if (instr->InputAt(0)->IsRegister()) {
506 __ movsxwl(i.OutputRegister(), i.InputRegister(0));
508 __ movsxwl(i.OutputRegister(), i.InputOperand(0));
510 __ AssertZeroExtended(i.OutputRegister());
513 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
514 __ AssertZeroExtended(i.OutputRegister());
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 if (instr->InputAt(0)->IsRegister()) {
530 __ movl(i.OutputRegister(), i.InputRegister(0));
532 __ movl(i.OutputRegister(), i.InputOperand(0));
535 __ movl(i.OutputRegister(), i.MemoryOperand());
537 __ AssertZeroExtended(i.OutputRegister());
540 Operand operand = i.MemoryOperand(&index);
541 if (HasImmediateInput(instr, index)) {
542 __ movl(operand, i.InputImmediate(index));
544 __ movl(operand, i.InputRegister(index));
549 if (instr->InputAt(0)->IsRegister()) {
550 __ movsxlq(i.OutputRegister(), i.InputRegister(0));
552 __ movsxlq(i.OutputRegister(), i.InputOperand(0));
557 if (instr->HasOutput()) {
558 __ movq(i.OutputRegister(), i.MemoryOperand());
561 Operand operand = i.MemoryOperand(&index);
562 if (HasImmediateInput(instr, index)) {
563 __ movq(operand, i.InputImmediate(index));
565 __ movq(operand, i.InputRegister(index));
570 if (instr->HasOutput()) {
571 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
574 Operand operand = i.MemoryOperand(&index);
575 __ movss(operand, i.InputDoubleRegister(index));
579 if (instr->HasOutput()) {
580 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
583 Operand operand = i.MemoryOperand(&index);
584 __ movsd(operand, i.InputDoubleRegister(index));
588 __ leal(i.OutputRegister(), i.MemoryOperand());
589 __ AssertZeroExtended(i.OutputRegister());
592 __ leaq(i.OutputRegister(), i.MemoryOperand());
595 if (HasImmediateInput(instr, 0)) {
596 __ pushq(i.InputImmediate(0));
598 if (instr->InputAt(0)->IsRegister()) {
599 __ pushq(i.InputRegister(0));
601 __ pushq(i.InputOperand(0));
605 case kX64StoreWriteBarrier: {
606 Register object = i.InputRegister(0);
607 Register index = i.InputRegister(1);
608 Register value = i.InputRegister(2);
609 __ movsxlq(index, index);
610 __ movq(Operand(object, index, times_1, 0), value);
611 __ leaq(index, Operand(object, index, times_1, 0));
612 SaveFPRegsMode mode =
613 frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
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::RpoNumber tblock =
630 i.InputRpo(static_cast<int>(instr->InputCount()) - 2);
631 BasicBlock::RpoNumber fblock =
632 i.InputRpo(static_cast<int>(instr->InputCount()) - 1);
633 bool fallthru = IsNextInAssemblyOrder(fblock);
634 Label* tlabel = GetLabel(tblock);
635 Label* flabel = fallthru ? &done : GetLabel(fblock);
636 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
638 case kUnorderedEqual:
639 __ j(parity_even, flabel, flabel_distance);
644 case kUnorderedNotEqual:
645 __ j(parity_even, tlabel);
648 __ j(not_equal, tlabel);
650 case kSignedLessThan:
653 case kSignedGreaterThanOrEqual:
654 __ j(greater_equal, tlabel);
656 case kSignedLessThanOrEqual:
657 __ j(less_equal, tlabel);
659 case kSignedGreaterThan:
660 __ j(greater, tlabel);
662 case kUnorderedLessThan:
663 __ j(parity_even, flabel, flabel_distance);
665 case kUnsignedLessThan:
668 case kUnorderedGreaterThanOrEqual:
669 __ j(parity_even, tlabel);
671 case kUnsignedGreaterThanOrEqual:
672 __ j(above_equal, tlabel);
674 case kUnorderedLessThanOrEqual:
675 __ j(parity_even, flabel, flabel_distance);
677 case kUnsignedLessThanOrEqual:
678 __ j(below_equal, tlabel);
680 case kUnorderedGreaterThan:
681 __ j(parity_even, tlabel);
683 case kUnsignedGreaterThan:
687 __ j(overflow, tlabel);
690 __ j(no_overflow, tlabel);
693 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
698 // Assembles boolean materializations after this instruction.
699 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
700 FlagsCondition condition) {
701 X64OperandConverter i(this, instr);
704 // Materialize a full 64-bit 1 or 0 value. The result register is always the
705 // last output of the instruction.
707 DCHECK_NE(0, static_cast<int>(instr->OutputCount()));
708 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
709 Condition cc = no_condition;
711 case kUnorderedEqual:
712 __ j(parity_odd, &check, Label::kNear);
713 __ movl(reg, Immediate(0));
714 __ jmp(&done, Label::kNear);
719 case kUnorderedNotEqual:
720 __ j(parity_odd, &check, Label::kNear);
721 __ movl(reg, Immediate(1));
722 __ jmp(&done, Label::kNear);
727 case kSignedLessThan:
730 case kSignedGreaterThanOrEqual:
733 case kSignedLessThanOrEqual:
736 case kSignedGreaterThan:
739 case kUnorderedLessThan:
740 __ j(parity_odd, &check, Label::kNear);
741 __ movl(reg, Immediate(0));
742 __ jmp(&done, Label::kNear);
744 case kUnsignedLessThan:
747 case kUnorderedGreaterThanOrEqual:
748 __ j(parity_odd, &check, Label::kNear);
749 __ movl(reg, Immediate(1));
750 __ jmp(&done, Label::kNear);
752 case kUnsignedGreaterThanOrEqual:
755 case kUnorderedLessThanOrEqual:
756 __ j(parity_odd, &check, Label::kNear);
757 __ movl(reg, Immediate(0));
758 __ jmp(&done, Label::kNear);
760 case kUnsignedLessThanOrEqual:
763 case kUnorderedGreaterThan:
764 __ j(parity_odd, &check, Label::kNear);
765 __ movl(reg, Immediate(1));
766 __ jmp(&done, Label::kNear);
768 case kUnsignedGreaterThan:
780 __ movzxbl(reg, reg);
785 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
786 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
787 isolate(), deoptimization_id, Deoptimizer::LAZY);
788 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
792 void CodeGenerator::AssemblePrologue() {
793 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
794 int stack_slots = frame()->GetSpillSlotCount();
795 if (descriptor->kind() == CallDescriptor::kCallAddress) {
798 const RegList saves = descriptor->CalleeSavedRegisters();
799 if (saves != 0) { // Save callee-saved registers.
800 int register_save_area_size = 0;
801 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
802 if (!((1 << i) & saves)) continue;
803 __ pushq(Register::from_code(i));
804 register_save_area_size += kPointerSize;
806 frame()->SetRegisterSaveAreaSize(register_save_area_size);
808 } else if (descriptor->IsJSFunctionCall()) {
809 CompilationInfo* info = this->info();
810 __ Prologue(info->IsCodePreAgingActive());
811 frame()->SetRegisterSaveAreaSize(
812 StandardFrameConstants::kFixedFrameSizeFromFp);
815 frame()->SetRegisterSaveAreaSize(
816 StandardFrameConstants::kFixedFrameSizeFromFp);
818 if (stack_slots > 0) {
819 __ subq(rsp, Immediate(stack_slots * kPointerSize));
824 void CodeGenerator::AssembleReturn() {
825 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
826 if (descriptor->kind() == CallDescriptor::kCallAddress) {
827 if (frame()->GetRegisterSaveAreaSize() > 0) {
828 // Remove this frame's spill slots first.
829 int stack_slots = frame()->GetSpillSlotCount();
830 if (stack_slots > 0) {
831 __ addq(rsp, Immediate(stack_slots * kPointerSize));
833 const RegList saves = descriptor->CalleeSavedRegisters();
834 // Restore registers.
836 for (int i = 0; i < Register::kNumRegisters; i++) {
837 if (!((1 << i) & saves)) continue;
838 __ popq(Register::from_code(i));
841 __ popq(rbp); // Pop caller's frame pointer.
844 // No saved registers.
845 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
846 __ popq(rbp); // Pop caller's frame pointer.
850 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
851 __ popq(rbp); // Pop caller's frame pointer.
852 int pop_count = descriptor->IsJSFunctionCall()
853 ? static_cast<int>(descriptor->JSParameterCount())
855 __ ret(pop_count * kPointerSize);
860 void CodeGenerator::AssembleMove(InstructionOperand* source,
861 InstructionOperand* destination) {
862 X64OperandConverter g(this, NULL);
863 // Dispatch on the source and destination operand kinds. Not all
864 // combinations are possible.
865 if (source->IsRegister()) {
866 DCHECK(destination->IsRegister() || destination->IsStackSlot());
867 Register src = g.ToRegister(source);
868 if (destination->IsRegister()) {
869 __ movq(g.ToRegister(destination), src);
871 __ movq(g.ToOperand(destination), src);
873 } else if (source->IsStackSlot()) {
874 DCHECK(destination->IsRegister() || destination->IsStackSlot());
875 Operand src = g.ToOperand(source);
876 if (destination->IsRegister()) {
877 Register dst = g.ToRegister(destination);
880 // Spill on demand to use a temporary register for memory-to-memory
882 Register tmp = kScratchRegister;
883 Operand dst = g.ToOperand(destination);
887 } else if (source->IsConstant()) {
888 ConstantOperand* constant_source = ConstantOperand::cast(source);
889 Constant src = g.ToConstant(constant_source);
890 if (destination->IsRegister() || destination->IsStackSlot()) {
891 Register dst = destination->IsRegister() ? g.ToRegister(destination)
893 switch (src.type()) {
894 case Constant::kInt32:
895 // TODO(dcarney): don't need scratch in this case.
896 __ Set(dst, src.ToInt32());
898 case Constant::kInt64:
899 __ Set(dst, src.ToInt64());
901 case Constant::kFloat32:
903 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
905 case Constant::kFloat64:
907 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
909 case Constant::kExternalReference:
910 __ Move(dst, src.ToExternalReference());
912 case Constant::kHeapObject:
913 __ Move(dst, src.ToHeapObject());
916 if (destination->IsStackSlot()) {
917 __ movq(g.ToOperand(destination), kScratchRegister);
919 } else if (src.type() == Constant::kFloat32) {
920 // TODO(turbofan): Can we do better here?
921 uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
922 if (destination->IsDoubleRegister()) {
923 __ Move(g.ToDoubleRegister(destination), src_const);
925 DCHECK(destination->IsDoubleStackSlot());
926 Operand dst = g.ToOperand(destination);
927 __ movl(dst, Immediate(src_const));
930 DCHECK_EQ(Constant::kFloat64, src.type());
931 uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
932 if (destination->IsDoubleRegister()) {
933 __ Move(g.ToDoubleRegister(destination), src_const);
935 DCHECK(destination->IsDoubleStackSlot());
936 __ movq(kScratchRegister, src_const);
937 __ movq(g.ToOperand(destination), kScratchRegister);
940 } else if (source->IsDoubleRegister()) {
941 XMMRegister src = g.ToDoubleRegister(source);
942 if (destination->IsDoubleRegister()) {
943 XMMRegister dst = g.ToDoubleRegister(destination);
946 DCHECK(destination->IsDoubleStackSlot());
947 Operand dst = g.ToOperand(destination);
950 } else if (source->IsDoubleStackSlot()) {
951 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
952 Operand src = g.ToOperand(source);
953 if (destination->IsDoubleRegister()) {
954 XMMRegister dst = g.ToDoubleRegister(destination);
957 // We rely on having xmm0 available as a fixed scratch register.
958 Operand dst = g.ToOperand(destination);
968 void CodeGenerator::AssembleSwap(InstructionOperand* source,
969 InstructionOperand* destination) {
970 X64OperandConverter g(this, NULL);
971 // Dispatch on the source and destination operand kinds. Not all
972 // combinations are possible.
973 if (source->IsRegister() && destination->IsRegister()) {
974 // Register-register.
975 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
976 } else if (source->IsRegister() && destination->IsStackSlot()) {
977 Register src = g.ToRegister(source);
978 Operand dst = g.ToOperand(destination);
980 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
981 (source->IsDoubleStackSlot() &&
982 destination->IsDoubleStackSlot())) {
984 Register tmp = kScratchRegister;
985 Operand src = g.ToOperand(source);
986 Operand dst = g.ToOperand(destination);
990 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
991 // XMM register-register swap. We rely on having xmm0
992 // available as a fixed scratch register.
993 XMMRegister src = g.ToDoubleRegister(source);
994 XMMRegister dst = g.ToDoubleRegister(destination);
998 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
999 // XMM register-memory swap. We rely on having xmm0
1000 // available as a fixed scratch register.
1001 XMMRegister src = g.ToDoubleRegister(source);
1002 Operand dst = g.ToOperand(destination);
1003 __ movsd(xmm0, src);
1005 __ movsd(dst, xmm0);
1007 // No other combinations are possible.
1013 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1016 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1017 int space_needed = Deoptimizer::patch_size();
1018 if (!info()->IsStub()) {
1019 // Ensure that we have enough space after the previous lazy-bailout
1020 // instruction for patching the code here.
1021 int current_pc = masm()->pc_offset();
1022 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1023 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1024 __ Nop(padding_size);
1027 MarkLazyDeoptSite();
1032 } // namespace internal
1033 } // namespace compiler