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 Constant constant = ToConstant(operand);
38 if (constant.type() == Constant::kInt32) {
39 return Immediate(constant.ToInt32());
45 Operand ToOperand(InstructionOperand* op, int extra = 0) {
46 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
47 // The linkage computes where all spill slots are located.
48 FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
49 return Operand(offset.from_stack_pointer() ? rsp : rbp, offset.offset());
52 static int NextOffset(int* offset) {
58 static ScaleFactor ScaleFor(AddressingMode one, AddressingMode mode) {
59 STATIC_ASSERT(0 == static_cast<int>(times_1));
60 STATIC_ASSERT(1 == static_cast<int>(times_2));
61 STATIC_ASSERT(2 == static_cast<int>(times_4));
62 STATIC_ASSERT(3 == static_cast<int>(times_8));
63 int scale = static_cast<int>(mode - one);
64 DCHECK(scale >= 0 && scale < 4);
65 return static_cast<ScaleFactor>(scale);
68 Operand MemoryOperand(int* offset) {
69 AddressingMode mode = AddressingModeField::decode(instr_->opcode());
72 Register base = InputRegister(NextOffset(offset));
74 return Operand(base, disp);
77 Register base = InputRegister(NextOffset(offset));
78 int32_t disp = InputInt32(NextOffset(offset));
79 return Operand(base, disp);
85 Register base = InputRegister(NextOffset(offset));
86 Register index = InputRegister(NextOffset(offset));
87 ScaleFactor scale = ScaleFor(kMode_MR1, mode);
89 return Operand(base, index, scale, disp);
95 Register base = InputRegister(NextOffset(offset));
96 Register index = InputRegister(NextOffset(offset));
97 ScaleFactor scale = ScaleFor(kMode_MR1I, mode);
98 int32_t disp = InputInt32(NextOffset(offset));
99 return Operand(base, index, scale, disp);
105 Register index = InputRegister(NextOffset(offset));
106 ScaleFactor scale = ScaleFor(kMode_M1, mode);
108 return Operand(index, scale, disp);
114 Register index = InputRegister(NextOffset(offset));
115 ScaleFactor scale = ScaleFor(kMode_M1I, mode);
116 int32_t disp = InputInt32(NextOffset(offset));
117 return Operand(index, scale, disp);
121 return Operand(no_reg, 0);
124 return Operand(no_reg, 0);
127 Operand MemoryOperand() {
129 return MemoryOperand(&first_input);
134 static bool HasImmediateInput(Instruction* instr, int index) {
135 return instr->InputAt(index)->IsImmediate();
139 #define ASSEMBLE_UNOP(asm_instr) \
141 if (instr->Output()->IsRegister()) { \
142 __ asm_instr(i.OutputRegister()); \
144 __ asm_instr(i.OutputOperand()); \
149 #define ASSEMBLE_BINOP(asm_instr) \
151 if (HasImmediateInput(instr, 1)) { \
152 if (instr->InputAt(0)->IsRegister()) { \
153 __ asm_instr(i.InputRegister(0), i.InputImmediate(1)); \
155 __ asm_instr(i.InputOperand(0), i.InputImmediate(1)); \
158 if (instr->InputAt(1)->IsRegister()) { \
159 __ asm_instr(i.InputRegister(0), i.InputRegister(1)); \
161 __ asm_instr(i.InputRegister(0), i.InputOperand(1)); \
167 #define ASSEMBLE_MULT(asm_instr) \
169 if (HasImmediateInput(instr, 1)) { \
170 if (instr->InputAt(0)->IsRegister()) { \
171 __ asm_instr(i.OutputRegister(), i.InputRegister(0), \
172 i.InputImmediate(1)); \
174 __ asm_instr(i.OutputRegister(), i.InputOperand(0), \
175 i.InputImmediate(1)); \
178 if (instr->InputAt(1)->IsRegister()) { \
179 __ asm_instr(i.OutputRegister(), i.InputRegister(1)); \
181 __ asm_instr(i.OutputRegister(), i.InputOperand(1)); \
187 #define ASSEMBLE_SHIFT(asm_instr, width) \
189 if (HasImmediateInput(instr, 1)) { \
190 __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
192 __ asm_instr##_cl(i.OutputRegister()); \
197 // Assembles an instruction after register allocation, producing machine code.
198 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
199 X64OperandConverter i(this, instr);
201 switch (ArchOpcodeField::decode(instr->opcode())) {
202 case kArchCallCodeObject: {
203 EnsureSpaceForLazyDeopt();
204 if (HasImmediateInput(instr, 0)) {
205 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
206 __ Call(code, RelocInfo::CODE_TARGET);
208 Register reg = i.InputRegister(0);
209 int entry = Code::kHeaderSize - kHeapObjectTag;
210 __ Call(Operand(reg, entry));
212 AddSafepointAndDeopt(instr);
215 case kArchCallJSFunction: {
216 EnsureSpaceForLazyDeopt();
217 Register func = i.InputRegister(0);
218 if (FLAG_debug_code) {
219 // Check the function's context matches the context argument.
220 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
221 __ Assert(equal, kWrongFunctionContext);
223 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
224 AddSafepointAndDeopt(instr);
228 __ jmp(code_->GetLabel(i.InputBlock(0)));
231 // don't emit code for nops.
236 case kArchStackPointer:
237 __ movq(i.OutputRegister(), rsp);
239 case kArchTruncateDoubleToI:
240 __ TruncateDoubleToI(i.OutputRegister(), i.InputDoubleRegister(0));
243 ASSEMBLE_BINOP(addl);
246 ASSEMBLE_BINOP(addq);
249 ASSEMBLE_BINOP(subl);
252 ASSEMBLE_BINOP(subq);
255 ASSEMBLE_BINOP(andl);
258 ASSEMBLE_BINOP(andq);
261 ASSEMBLE_BINOP(cmpl);
264 ASSEMBLE_BINOP(cmpq);
267 ASSEMBLE_BINOP(testl);
270 ASSEMBLE_BINOP(testq);
273 ASSEMBLE_MULT(imull);
276 ASSEMBLE_MULT(imulq);
280 __ idivl(i.InputRegister(1));
284 __ idivq(i.InputRegister(1));
288 __ divl(i.InputRegister(1));
292 __ divq(i.InputRegister(1));
313 ASSEMBLE_BINOP(xorl);
316 ASSEMBLE_BINOP(xorq);
319 ASSEMBLE_SHIFT(shll, 5);
322 ASSEMBLE_SHIFT(shlq, 6);
325 ASSEMBLE_SHIFT(shrl, 5);
328 ASSEMBLE_SHIFT(shrq, 6);
331 ASSEMBLE_SHIFT(sarl, 5);
334 ASSEMBLE_SHIFT(sarq, 6);
337 ASSEMBLE_SHIFT(rorl, 5);
340 ASSEMBLE_SHIFT(rorq, 6);
343 if (instr->InputAt(1)->IsDoubleRegister()) {
344 __ ucomisd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
346 __ ucomisd(i.InputDoubleRegister(0), i.InputOperand(1));
350 __ addsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
353 __ subsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
356 __ mulsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
359 __ divsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
361 case kSSEFloat64Mod: {
362 __ subq(rsp, Immediate(kDoubleSize));
363 // Move values to st(0) and st(1).
364 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
365 __ fld_d(Operand(rsp, 0));
366 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
367 __ fld_d(Operand(rsp, 0));
368 // Loop while fprem isn't done.
371 // This instructions traps on all kinds inputs, but we are assuming the
372 // floating point control word is set to ignore them all.
374 // The following 2 instruction implicitly use rax.
376 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
379 __ shrl(rax, Immediate(8));
380 __ andl(rax, Immediate(0xFF));
384 __ j(parity_even, &mod_loop);
385 // Move output to stack and clean up.
387 __ fstp_d(Operand(rsp, 0));
388 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
389 __ addq(rsp, Immediate(kDoubleSize));
392 case kSSEFloat64Sqrt:
393 if (instr->InputAt(0)->IsDoubleRegister()) {
394 __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
396 __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
400 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
403 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
405 case kSSEFloat64ToInt32:
406 if (instr->InputAt(0)->IsDoubleRegister()) {
407 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
409 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
412 case kSSEFloat64ToUint32:
413 if (instr->InputAt(0)->IsDoubleRegister()) {
414 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
416 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
418 __ andl(i.OutputRegister(), i.OutputRegister()); // clear upper bits.
419 // TODO(turbofan): generated code should not look at the upper 32 bits
420 // of the result, but those bits could escape to the outside world.
422 case kSSEInt32ToFloat64:
423 if (instr->InputAt(0)->IsRegister()) {
424 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
426 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
429 case kSSEUint32ToFloat64:
430 if (instr->InputAt(0)->IsRegister()) {
431 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
433 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
437 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
440 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
444 Operand operand = i.MemoryOperand(&index);
445 if (HasImmediateInput(instr, index)) {
446 __ movb(operand, Immediate(i.InputInt8(index)));
448 __ movb(operand, i.InputRegister(index));
453 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
456 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
460 Operand operand = i.MemoryOperand(&index);
461 if (HasImmediateInput(instr, index)) {
462 __ movw(operand, Immediate(i.InputInt16(index)));
464 __ movw(operand, i.InputRegister(index));
469 if (instr->HasOutput()) {
470 if (instr->addressing_mode() == kMode_None) {
471 if (instr->InputAt(0)->IsRegister()) {
472 __ movl(i.OutputRegister(), i.InputRegister(0));
474 __ movl(i.OutputRegister(), i.InputOperand(0));
477 __ movl(i.OutputRegister(), i.MemoryOperand());
481 Operand operand = i.MemoryOperand(&index);
482 if (HasImmediateInput(instr, index)) {
483 __ movl(operand, i.InputImmediate(index));
485 __ movl(operand, i.InputRegister(index));
490 if (instr->InputAt(0)->IsRegister()) {
491 __ movsxlq(i.OutputRegister(), i.InputRegister(0));
493 __ movsxlq(i.OutputRegister(), i.InputOperand(0));
498 if (instr->HasOutput()) {
499 __ movq(i.OutputRegister(), i.MemoryOperand());
502 Operand operand = i.MemoryOperand(&index);
503 if (HasImmediateInput(instr, index)) {
504 __ movq(operand, i.InputImmediate(index));
506 __ movq(operand, i.InputRegister(index));
511 if (instr->HasOutput()) {
512 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
515 Operand operand = i.MemoryOperand(&index);
516 __ movss(operand, i.InputDoubleRegister(index));
520 if (instr->HasOutput()) {
521 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
524 Operand operand = i.MemoryOperand(&index);
525 __ movsd(operand, i.InputDoubleRegister(index));
529 __ leal(i.OutputRegister(), i.MemoryOperand());
532 __ leaq(i.OutputRegister(), i.MemoryOperand());
535 if (HasImmediateInput(instr, 0)) {
536 __ pushq(i.InputImmediate(0));
538 if (instr->InputAt(0)->IsRegister()) {
539 __ pushq(i.InputRegister(0));
541 __ pushq(i.InputOperand(0));
545 case kX64StoreWriteBarrier: {
546 Register object = i.InputRegister(0);
547 Register index = i.InputRegister(1);
548 Register value = i.InputRegister(2);
549 __ movsxlq(index, index);
550 __ movq(Operand(object, index, times_1, 0), value);
551 __ leaq(index, Operand(object, index, times_1, 0));
552 SaveFPRegsMode mode = code_->frame()->DidAllocateDoubleRegisters()
555 __ RecordWrite(object, index, value, mode);
562 // Assembles branches after this instruction.
563 void CodeGenerator::AssembleArchBranch(Instruction* instr,
564 FlagsCondition condition) {
565 X64OperandConverter i(this, instr);
568 // Emit a branch. The true and false targets are always the last two inputs
569 // to the instruction.
570 BasicBlock* tblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 2);
571 BasicBlock* fblock = i.InputBlock(static_cast<int>(instr->InputCount()) - 1);
572 bool fallthru = IsNextInAssemblyOrder(fblock);
573 Label* tlabel = code()->GetLabel(tblock);
574 Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
575 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
577 case kUnorderedEqual:
578 __ j(parity_even, flabel, flabel_distance);
583 case kUnorderedNotEqual:
584 __ j(parity_even, tlabel);
587 __ j(not_equal, tlabel);
589 case kSignedLessThan:
592 case kSignedGreaterThanOrEqual:
593 __ j(greater_equal, tlabel);
595 case kSignedLessThanOrEqual:
596 __ j(less_equal, tlabel);
598 case kSignedGreaterThan:
599 __ j(greater, tlabel);
601 case kUnorderedLessThan:
602 __ j(parity_even, flabel, flabel_distance);
604 case kUnsignedLessThan:
607 case kUnorderedGreaterThanOrEqual:
608 __ j(parity_even, tlabel);
610 case kUnsignedGreaterThanOrEqual:
611 __ j(above_equal, tlabel);
613 case kUnorderedLessThanOrEqual:
614 __ j(parity_even, flabel, flabel_distance);
616 case kUnsignedLessThanOrEqual:
617 __ j(below_equal, tlabel);
619 case kUnorderedGreaterThan:
620 __ j(parity_even, tlabel);
622 case kUnsignedGreaterThan:
626 __ j(overflow, tlabel);
629 __ j(no_overflow, tlabel);
632 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
637 // Assembles boolean materializations after this instruction.
638 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
639 FlagsCondition condition) {
640 X64OperandConverter i(this, instr);
643 // Materialize a full 64-bit 1 or 0 value. The result register is always the
644 // last output of the instruction.
646 DCHECK_NE(0, instr->OutputCount());
647 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
648 Condition cc = no_condition;
650 case kUnorderedEqual:
651 __ j(parity_odd, &check, Label::kNear);
652 __ movl(reg, Immediate(0));
653 __ jmp(&done, Label::kNear);
658 case kUnorderedNotEqual:
659 __ j(parity_odd, &check, Label::kNear);
660 __ movl(reg, Immediate(1));
661 __ jmp(&done, Label::kNear);
666 case kSignedLessThan:
669 case kSignedGreaterThanOrEqual:
672 case kSignedLessThanOrEqual:
675 case kSignedGreaterThan:
678 case kUnorderedLessThan:
679 __ j(parity_odd, &check, Label::kNear);
680 __ movl(reg, Immediate(0));
681 __ jmp(&done, Label::kNear);
683 case kUnsignedLessThan:
686 case kUnorderedGreaterThanOrEqual:
687 __ j(parity_odd, &check, Label::kNear);
688 __ movl(reg, Immediate(1));
689 __ jmp(&done, Label::kNear);
691 case kUnsignedGreaterThanOrEqual:
694 case kUnorderedLessThanOrEqual:
695 __ j(parity_odd, &check, Label::kNear);
696 __ movl(reg, Immediate(0));
697 __ jmp(&done, Label::kNear);
699 case kUnsignedLessThanOrEqual:
702 case kUnorderedGreaterThan:
703 __ j(parity_odd, &check, Label::kNear);
704 __ movl(reg, Immediate(1));
705 __ jmp(&done, Label::kNear);
707 case kUnsignedGreaterThan:
719 __ movzxbl(reg, reg);
724 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
725 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
726 isolate(), deoptimization_id, Deoptimizer::LAZY);
727 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
731 void CodeGenerator::AssemblePrologue() {
732 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
733 int stack_slots = frame()->GetSpillSlotCount();
734 if (descriptor->kind() == CallDescriptor::kCallAddress) {
737 const RegList saves = descriptor->CalleeSavedRegisters();
738 if (saves != 0) { // Save callee-saved registers.
739 int register_save_area_size = 0;
740 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
741 if (!((1 << i) & saves)) continue;
742 __ pushq(Register::from_code(i));
743 register_save_area_size += kPointerSize;
745 frame()->SetRegisterSaveAreaSize(register_save_area_size);
747 } else if (descriptor->IsJSFunctionCall()) {
748 CompilationInfo* info = linkage()->info();
749 __ Prologue(info->IsCodePreAgingActive());
750 frame()->SetRegisterSaveAreaSize(
751 StandardFrameConstants::kFixedFrameSizeFromFp);
753 // Sloppy mode functions and builtins need to replace the receiver with the
754 // global proxy when called as functions (without an explicit receiver
756 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
757 if (info->strict_mode() == SLOPPY && !info->is_native()) {
759 StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
760 __ movp(rcx, args.GetReceiverOperand());
761 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
762 __ j(not_equal, &ok, Label::kNear);
763 __ movp(rcx, GlobalObjectOperand());
764 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
765 __ movp(args.GetReceiverOperand(), rcx);
771 frame()->SetRegisterSaveAreaSize(
772 StandardFrameConstants::kFixedFrameSizeFromFp);
774 if (stack_slots > 0) {
775 __ subq(rsp, Immediate(stack_slots * kPointerSize));
780 void CodeGenerator::AssembleReturn() {
781 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
782 if (descriptor->kind() == CallDescriptor::kCallAddress) {
783 if (frame()->GetRegisterSaveAreaSize() > 0) {
784 // Remove this frame's spill slots first.
785 int stack_slots = frame()->GetSpillSlotCount();
786 if (stack_slots > 0) {
787 __ addq(rsp, Immediate(stack_slots * kPointerSize));
789 const RegList saves = descriptor->CalleeSavedRegisters();
790 // Restore registers.
792 for (int i = 0; i < Register::kNumRegisters; i++) {
793 if (!((1 << i) & saves)) continue;
794 __ popq(Register::from_code(i));
797 __ popq(rbp); // Pop caller's frame pointer.
800 // No saved registers.
801 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
802 __ popq(rbp); // Pop caller's frame pointer.
806 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
807 __ popq(rbp); // Pop caller's frame pointer.
808 int pop_count = descriptor->IsJSFunctionCall()
809 ? static_cast<int>(descriptor->JSParameterCount())
811 __ ret(pop_count * kPointerSize);
816 void CodeGenerator::AssembleMove(InstructionOperand* source,
817 InstructionOperand* destination) {
818 X64OperandConverter g(this, NULL);
819 // Dispatch on the source and destination operand kinds. Not all
820 // combinations are possible.
821 if (source->IsRegister()) {
822 DCHECK(destination->IsRegister() || destination->IsStackSlot());
823 Register src = g.ToRegister(source);
824 if (destination->IsRegister()) {
825 __ movq(g.ToRegister(destination), src);
827 __ movq(g.ToOperand(destination), src);
829 } else if (source->IsStackSlot()) {
830 DCHECK(destination->IsRegister() || destination->IsStackSlot());
831 Operand src = g.ToOperand(source);
832 if (destination->IsRegister()) {
833 Register dst = g.ToRegister(destination);
836 // Spill on demand to use a temporary register for memory-to-memory
838 Register tmp = kScratchRegister;
839 Operand dst = g.ToOperand(destination);
843 } else if (source->IsConstant()) {
844 ConstantOperand* constant_source = ConstantOperand::cast(source);
845 Constant src = g.ToConstant(constant_source);
846 if (destination->IsRegister() || destination->IsStackSlot()) {
847 Register dst = destination->IsRegister() ? g.ToRegister(destination)
849 switch (src.type()) {
850 case Constant::kInt32:
851 // TODO(dcarney): don't need scratch in this case.
852 __ movq(dst, Immediate(src.ToInt32()));
854 case Constant::kInt64:
855 __ Set(dst, src.ToInt64());
857 case Constant::kFloat32:
859 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
861 case Constant::kFloat64:
863 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
865 case Constant::kExternalReference:
866 __ Move(dst, src.ToExternalReference());
868 case Constant::kHeapObject:
869 __ Move(dst, src.ToHeapObject());
872 if (destination->IsStackSlot()) {
873 __ movq(g.ToOperand(destination), kScratchRegister);
875 } else if (src.type() == Constant::kFloat32) {
876 // TODO(turbofan): Can we do better here?
877 __ movl(kScratchRegister, Immediate(bit_cast<int32_t>(src.ToFloat32())));
878 if (destination->IsDoubleRegister()) {
879 XMMRegister dst = g.ToDoubleRegister(destination);
880 __ movq(dst, kScratchRegister);
882 DCHECK(destination->IsDoubleStackSlot());
883 Operand dst = g.ToOperand(destination);
884 __ movl(dst, kScratchRegister);
887 DCHECK_EQ(Constant::kFloat64, src.type());
888 __ movq(kScratchRegister, bit_cast<int64_t>(src.ToFloat64()));
889 if (destination->IsDoubleRegister()) {
890 __ movq(g.ToDoubleRegister(destination), kScratchRegister);
892 DCHECK(destination->IsDoubleStackSlot());
893 __ movq(g.ToOperand(destination), kScratchRegister);
896 } else if (source->IsDoubleRegister()) {
897 XMMRegister src = g.ToDoubleRegister(source);
898 if (destination->IsDoubleRegister()) {
899 XMMRegister dst = g.ToDoubleRegister(destination);
902 DCHECK(destination->IsDoubleStackSlot());
903 Operand dst = g.ToOperand(destination);
906 } else if (source->IsDoubleStackSlot()) {
907 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
908 Operand src = g.ToOperand(source);
909 if (destination->IsDoubleRegister()) {
910 XMMRegister dst = g.ToDoubleRegister(destination);
913 // We rely on having xmm0 available as a fixed scratch register.
914 Operand dst = g.ToOperand(destination);
924 void CodeGenerator::AssembleSwap(InstructionOperand* source,
925 InstructionOperand* destination) {
926 X64OperandConverter g(this, NULL);
927 // Dispatch on the source and destination operand kinds. Not all
928 // combinations are possible.
929 if (source->IsRegister() && destination->IsRegister()) {
930 // Register-register.
931 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
932 } else if (source->IsRegister() && destination->IsStackSlot()) {
933 Register src = g.ToRegister(source);
934 Operand dst = g.ToOperand(destination);
936 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
937 (source->IsDoubleStackSlot() &&
938 destination->IsDoubleStackSlot())) {
940 Register tmp = kScratchRegister;
941 Operand src = g.ToOperand(source);
942 Operand dst = g.ToOperand(destination);
946 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
947 // XMM register-register swap. We rely on having xmm0
948 // available as a fixed scratch register.
949 XMMRegister src = g.ToDoubleRegister(source);
950 XMMRegister dst = g.ToDoubleRegister(destination);
954 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
955 // XMM register-memory swap. We rely on having xmm0
956 // available as a fixed scratch register.
957 XMMRegister src = g.ToDoubleRegister(source);
958 Operand dst = g.ToOperand(destination);
963 // No other combinations are possible.
969 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
972 void CodeGenerator::EnsureSpaceForLazyDeopt() {
973 int space_needed = Deoptimizer::patch_size();
974 if (!linkage()->info()->IsStub()) {
975 // Ensure that we have enough space after the previous lazy-bailout
976 // instruction for patching the code here.
977 int current_pc = masm()->pc_offset();
978 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
979 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
980 __ Nop(padding_size);
988 } // namespace internal
989 } // namespace compiler