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(code_->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 __ imull(i.InputRegister(1));
296 __ idivl(i.InputRegister(1));
300 __ idivq(i.InputRegister(1));
304 __ divl(i.InputRegister(1));
308 __ divq(i.InputRegister(1));
329 ASSEMBLE_BINOP(xorl);
332 ASSEMBLE_BINOP(xorq);
335 ASSEMBLE_SHIFT(shll, 5);
338 ASSEMBLE_SHIFT(shlq, 6);
341 ASSEMBLE_SHIFT(shrl, 5);
344 ASSEMBLE_SHIFT(shrq, 6);
347 ASSEMBLE_SHIFT(sarl, 5);
350 ASSEMBLE_SHIFT(sarq, 6);
353 ASSEMBLE_SHIFT(rorl, 5);
356 ASSEMBLE_SHIFT(rorq, 6);
359 ASSEMBLE_DOUBLE_BINOP(ucomisd);
362 ASSEMBLE_DOUBLE_BINOP(addsd);
365 ASSEMBLE_DOUBLE_BINOP(subsd);
368 ASSEMBLE_DOUBLE_BINOP(mulsd);
371 ASSEMBLE_DOUBLE_BINOP(divsd);
373 case kSSEFloat64Mod: {
374 __ subq(rsp, Immediate(kDoubleSize));
375 // Move values to st(0) and st(1).
376 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
377 __ fld_d(Operand(rsp, 0));
378 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
379 __ fld_d(Operand(rsp, 0));
380 // Loop while fprem isn't done.
383 // This instructions traps on all kinds inputs, but we are assuming the
384 // floating point control word is set to ignore them all.
386 // The following 2 instruction implicitly use rax.
388 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
391 __ shrl(rax, Immediate(8));
392 __ andl(rax, Immediate(0xFF));
396 __ j(parity_even, &mod_loop);
397 // Move output to stack and clean up.
399 __ fstp_d(Operand(rsp, 0));
400 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
401 __ addq(rsp, Immediate(kDoubleSize));
404 case kSSEFloat64Sqrt:
405 if (instr->InputAt(0)->IsDoubleRegister()) {
406 __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
408 __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
411 case kSSEFloat64Floor: {
412 CpuFeatureScope sse_scope(masm(), SSE4_1);
413 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
414 v8::internal::Assembler::kRoundDown);
417 case kSSEFloat64Ceil: {
418 CpuFeatureScope sse_scope(masm(), SSE4_1);
419 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
420 v8::internal::Assembler::kRoundUp);
423 case kSSEFloat64RoundTruncate: {
424 CpuFeatureScope sse_scope(masm(), SSE4_1);
425 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
426 v8::internal::Assembler::kRoundToZero);
430 if (instr->InputAt(0)->IsDoubleRegister()) {
431 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
433 __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
437 if (instr->InputAt(0)->IsDoubleRegister()) {
438 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
440 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
443 case kSSEFloat64ToInt32:
444 if (instr->InputAt(0)->IsDoubleRegister()) {
445 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
447 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
450 case kSSEFloat64ToUint32: {
451 if (instr->InputAt(0)->IsDoubleRegister()) {
452 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
454 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
456 __ AssertZeroExtended(i.OutputRegister());
459 case kSSEInt32ToFloat64:
460 if (instr->InputAt(0)->IsRegister()) {
461 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
463 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
466 case kSSEUint32ToFloat64:
467 if (instr->InputAt(0)->IsRegister()) {
468 __ movl(kScratchRegister, i.InputRegister(0));
470 __ movl(kScratchRegister, i.InputOperand(0));
472 __ cvtqsi2sd(i.OutputDoubleRegister(), kScratchRegister);
475 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
478 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
482 Operand operand = i.MemoryOperand(&index);
483 if (HasImmediateInput(instr, index)) {
484 __ movb(operand, Immediate(i.InputInt8(index)));
486 __ movb(operand, i.InputRegister(index));
491 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
494 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
498 Operand operand = i.MemoryOperand(&index);
499 if (HasImmediateInput(instr, index)) {
500 __ movw(operand, Immediate(i.InputInt16(index)));
502 __ movw(operand, i.InputRegister(index));
507 if (instr->HasOutput()) {
508 if (instr->addressing_mode() == kMode_None) {
509 if (instr->InputAt(0)->IsRegister()) {
510 __ movl(i.OutputRegister(), i.InputRegister(0));
512 __ movl(i.OutputRegister(), i.InputOperand(0));
515 __ movl(i.OutputRegister(), i.MemoryOperand());
519 Operand operand = i.MemoryOperand(&index);
520 if (HasImmediateInput(instr, index)) {
521 __ movl(operand, i.InputImmediate(index));
523 __ movl(operand, i.InputRegister(index));
528 if (instr->InputAt(0)->IsRegister()) {
529 __ movsxlq(i.OutputRegister(), i.InputRegister(0));
531 __ movsxlq(i.OutputRegister(), i.InputOperand(0));
536 if (instr->HasOutput()) {
537 __ movq(i.OutputRegister(), i.MemoryOperand());
540 Operand operand = i.MemoryOperand(&index);
541 if (HasImmediateInput(instr, index)) {
542 __ movq(operand, i.InputImmediate(index));
544 __ movq(operand, i.InputRegister(index));
549 if (instr->HasOutput()) {
550 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
553 Operand operand = i.MemoryOperand(&index);
554 __ movss(operand, i.InputDoubleRegister(index));
558 if (instr->HasOutput()) {
559 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
562 Operand operand = i.MemoryOperand(&index);
563 __ movsd(operand, i.InputDoubleRegister(index));
567 __ leal(i.OutputRegister(), i.MemoryOperand());
570 __ leaq(i.OutputRegister(), i.MemoryOperand());
573 if (HasImmediateInput(instr, 0)) {
574 __ pushq(i.InputImmediate(0));
576 if (instr->InputAt(0)->IsRegister()) {
577 __ pushq(i.InputRegister(0));
579 __ pushq(i.InputOperand(0));
583 case kX64StoreWriteBarrier: {
584 Register object = i.InputRegister(0);
585 Register index = i.InputRegister(1);
586 Register value = i.InputRegister(2);
587 __ movsxlq(index, index);
588 __ movq(Operand(object, index, times_1, 0), value);
589 __ leaq(index, Operand(object, index, times_1, 0));
590 SaveFPRegsMode mode =
591 frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
592 __ RecordWrite(object, index, value, mode);
599 // Assembles branches after this instruction.
600 void CodeGenerator::AssembleArchBranch(Instruction* instr,
601 FlagsCondition condition) {
602 X64OperandConverter i(this, instr);
605 // Emit a branch. The true and false targets are always the last two inputs
606 // to the instruction.
607 BasicBlock::RpoNumber tblock =
608 i.InputRpo(static_cast<int>(instr->InputCount()) - 2);
609 BasicBlock::RpoNumber fblock =
610 i.InputRpo(static_cast<int>(instr->InputCount()) - 1);
611 bool fallthru = IsNextInAssemblyOrder(fblock);
612 Label* tlabel = code()->GetLabel(tblock);
613 Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
614 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
616 case kUnorderedEqual:
617 __ j(parity_even, flabel, flabel_distance);
622 case kUnorderedNotEqual:
623 __ j(parity_even, tlabel);
626 __ j(not_equal, tlabel);
628 case kSignedLessThan:
631 case kSignedGreaterThanOrEqual:
632 __ j(greater_equal, tlabel);
634 case kSignedLessThanOrEqual:
635 __ j(less_equal, tlabel);
637 case kSignedGreaterThan:
638 __ j(greater, tlabel);
640 case kUnorderedLessThan:
641 __ j(parity_even, flabel, flabel_distance);
643 case kUnsignedLessThan:
646 case kUnorderedGreaterThanOrEqual:
647 __ j(parity_even, tlabel);
649 case kUnsignedGreaterThanOrEqual:
650 __ j(above_equal, tlabel);
652 case kUnorderedLessThanOrEqual:
653 __ j(parity_even, flabel, flabel_distance);
655 case kUnsignedLessThanOrEqual:
656 __ j(below_equal, tlabel);
658 case kUnorderedGreaterThan:
659 __ j(parity_even, tlabel);
661 case kUnsignedGreaterThan:
665 __ j(overflow, tlabel);
668 __ j(no_overflow, tlabel);
671 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
676 // Assembles boolean materializations after this instruction.
677 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
678 FlagsCondition condition) {
679 X64OperandConverter i(this, instr);
682 // Materialize a full 64-bit 1 or 0 value. The result register is always the
683 // last output of the instruction.
685 DCHECK_NE(0, static_cast<int>(instr->OutputCount()));
686 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
687 Condition cc = no_condition;
689 case kUnorderedEqual:
690 __ j(parity_odd, &check, Label::kNear);
691 __ movl(reg, Immediate(0));
692 __ jmp(&done, Label::kNear);
697 case kUnorderedNotEqual:
698 __ j(parity_odd, &check, Label::kNear);
699 __ movl(reg, Immediate(1));
700 __ jmp(&done, Label::kNear);
705 case kSignedLessThan:
708 case kSignedGreaterThanOrEqual:
711 case kSignedLessThanOrEqual:
714 case kSignedGreaterThan:
717 case kUnorderedLessThan:
718 __ j(parity_odd, &check, Label::kNear);
719 __ movl(reg, Immediate(0));
720 __ jmp(&done, Label::kNear);
722 case kUnsignedLessThan:
725 case kUnorderedGreaterThanOrEqual:
726 __ j(parity_odd, &check, Label::kNear);
727 __ movl(reg, Immediate(1));
728 __ jmp(&done, Label::kNear);
730 case kUnsignedGreaterThanOrEqual:
733 case kUnorderedLessThanOrEqual:
734 __ j(parity_odd, &check, Label::kNear);
735 __ movl(reg, Immediate(0));
736 __ jmp(&done, Label::kNear);
738 case kUnsignedLessThanOrEqual:
741 case kUnorderedGreaterThan:
742 __ j(parity_odd, &check, Label::kNear);
743 __ movl(reg, Immediate(1));
744 __ jmp(&done, Label::kNear);
746 case kUnsignedGreaterThan:
758 __ movzxbl(reg, reg);
763 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
764 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
765 isolate(), deoptimization_id, Deoptimizer::LAZY);
766 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
770 void CodeGenerator::AssemblePrologue() {
771 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
772 int stack_slots = frame()->GetSpillSlotCount();
773 if (descriptor->kind() == CallDescriptor::kCallAddress) {
776 const RegList saves = descriptor->CalleeSavedRegisters();
777 if (saves != 0) { // Save callee-saved registers.
778 int register_save_area_size = 0;
779 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
780 if (!((1 << i) & saves)) continue;
781 __ pushq(Register::from_code(i));
782 register_save_area_size += kPointerSize;
784 frame()->SetRegisterSaveAreaSize(register_save_area_size);
786 } else if (descriptor->IsJSFunctionCall()) {
787 CompilationInfo* info = this->info();
788 __ Prologue(info->IsCodePreAgingActive());
789 frame()->SetRegisterSaveAreaSize(
790 StandardFrameConstants::kFixedFrameSizeFromFp);
792 // Sloppy mode functions and builtins need to replace the receiver with the
793 // global proxy when called as functions (without an explicit receiver
795 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
796 if (info->strict_mode() == SLOPPY && !info->is_native()) {
798 StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
799 __ movp(rcx, args.GetReceiverOperand());
800 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
801 __ j(not_equal, &ok, Label::kNear);
802 __ movp(rcx, GlobalObjectOperand());
803 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
804 __ movp(args.GetReceiverOperand(), rcx);
810 frame()->SetRegisterSaveAreaSize(
811 StandardFrameConstants::kFixedFrameSizeFromFp);
813 if (stack_slots > 0) {
814 __ subq(rsp, Immediate(stack_slots * kPointerSize));
819 void CodeGenerator::AssembleReturn() {
820 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
821 if (descriptor->kind() == CallDescriptor::kCallAddress) {
822 if (frame()->GetRegisterSaveAreaSize() > 0) {
823 // Remove this frame's spill slots first.
824 int stack_slots = frame()->GetSpillSlotCount();
825 if (stack_slots > 0) {
826 __ addq(rsp, Immediate(stack_slots * kPointerSize));
828 const RegList saves = descriptor->CalleeSavedRegisters();
829 // Restore registers.
831 for (int i = 0; i < Register::kNumRegisters; i++) {
832 if (!((1 << i) & saves)) continue;
833 __ popq(Register::from_code(i));
836 __ popq(rbp); // Pop caller's frame pointer.
839 // No saved registers.
840 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
841 __ popq(rbp); // Pop caller's frame pointer.
845 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
846 __ popq(rbp); // Pop caller's frame pointer.
847 int pop_count = descriptor->IsJSFunctionCall()
848 ? static_cast<int>(descriptor->JSParameterCount())
850 __ ret(pop_count * kPointerSize);
855 void CodeGenerator::AssembleMove(InstructionOperand* source,
856 InstructionOperand* destination) {
857 X64OperandConverter g(this, NULL);
858 // Dispatch on the source and destination operand kinds. Not all
859 // combinations are possible.
860 if (source->IsRegister()) {
861 DCHECK(destination->IsRegister() || destination->IsStackSlot());
862 Register src = g.ToRegister(source);
863 if (destination->IsRegister()) {
864 __ movq(g.ToRegister(destination), src);
866 __ movq(g.ToOperand(destination), src);
868 } else if (source->IsStackSlot()) {
869 DCHECK(destination->IsRegister() || destination->IsStackSlot());
870 Operand src = g.ToOperand(source);
871 if (destination->IsRegister()) {
872 Register dst = g.ToRegister(destination);
875 // Spill on demand to use a temporary register for memory-to-memory
877 Register tmp = kScratchRegister;
878 Operand dst = g.ToOperand(destination);
882 } else if (source->IsConstant()) {
883 ConstantOperand* constant_source = ConstantOperand::cast(source);
884 Constant src = g.ToConstant(constant_source);
885 if (destination->IsRegister() || destination->IsStackSlot()) {
886 Register dst = destination->IsRegister() ? g.ToRegister(destination)
888 switch (src.type()) {
889 case Constant::kInt32:
890 // TODO(dcarney): don't need scratch in this case.
891 __ Set(dst, src.ToInt32());
893 case Constant::kInt64:
894 __ Set(dst, src.ToInt64());
896 case Constant::kFloat32:
898 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
900 case Constant::kFloat64:
902 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
904 case Constant::kExternalReference:
905 __ Move(dst, src.ToExternalReference());
907 case Constant::kHeapObject:
908 __ Move(dst, src.ToHeapObject());
911 if (destination->IsStackSlot()) {
912 __ movq(g.ToOperand(destination), kScratchRegister);
914 } else if (src.type() == Constant::kFloat32) {
915 // TODO(turbofan): Can we do better here?
916 uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
917 if (destination->IsDoubleRegister()) {
918 __ Move(g.ToDoubleRegister(destination), src_const);
920 DCHECK(destination->IsDoubleStackSlot());
921 Operand dst = g.ToOperand(destination);
922 __ movl(dst, Immediate(src_const));
925 DCHECK_EQ(Constant::kFloat64, src.type());
926 uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
927 if (destination->IsDoubleRegister()) {
928 __ Move(g.ToDoubleRegister(destination), src_const);
930 DCHECK(destination->IsDoubleStackSlot());
931 __ movq(kScratchRegister, src_const);
932 __ movq(g.ToOperand(destination), kScratchRegister);
935 } else if (source->IsDoubleRegister()) {
936 XMMRegister src = g.ToDoubleRegister(source);
937 if (destination->IsDoubleRegister()) {
938 XMMRegister dst = g.ToDoubleRegister(destination);
941 DCHECK(destination->IsDoubleStackSlot());
942 Operand dst = g.ToOperand(destination);
945 } else if (source->IsDoubleStackSlot()) {
946 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
947 Operand src = g.ToOperand(source);
948 if (destination->IsDoubleRegister()) {
949 XMMRegister dst = g.ToDoubleRegister(destination);
952 // We rely on having xmm0 available as a fixed scratch register.
953 Operand dst = g.ToOperand(destination);
963 void CodeGenerator::AssembleSwap(InstructionOperand* source,
964 InstructionOperand* destination) {
965 X64OperandConverter g(this, NULL);
966 // Dispatch on the source and destination operand kinds. Not all
967 // combinations are possible.
968 if (source->IsRegister() && destination->IsRegister()) {
969 // Register-register.
970 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
971 } else if (source->IsRegister() && destination->IsStackSlot()) {
972 Register src = g.ToRegister(source);
973 Operand dst = g.ToOperand(destination);
975 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
976 (source->IsDoubleStackSlot() &&
977 destination->IsDoubleStackSlot())) {
979 Register tmp = kScratchRegister;
980 Operand src = g.ToOperand(source);
981 Operand dst = g.ToOperand(destination);
985 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
986 // XMM register-register swap. We rely on having xmm0
987 // available as a fixed scratch register.
988 XMMRegister src = g.ToDoubleRegister(source);
989 XMMRegister dst = g.ToDoubleRegister(destination);
993 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
994 // XMM register-memory swap. We rely on having xmm0
995 // available as a fixed scratch register.
996 XMMRegister src = g.ToDoubleRegister(source);
997 Operand dst = g.ToOperand(destination);
1000 __ movsd(dst, xmm0);
1002 // No other combinations are possible.
1008 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1011 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1012 int space_needed = Deoptimizer::patch_size();
1013 if (!info()->IsStub()) {
1014 // Ensure that we have enough space after the previous lazy-bailout
1015 // instruction for patching the code here.
1016 int current_pc = masm()->pc_offset();
1017 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1018 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1019 __ Nop(padding_size);
1022 MarkLazyDeoptSite();
1027 } // namespace internal
1028 } // namespace compiler