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 if (instr->Output()->IsRegister()) { \
191 __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
193 __ asm_instr(i.OutputOperand(), Immediate(i.InputInt##width(1))); \
196 if (instr->Output()->IsRegister()) { \
197 __ asm_instr##_cl(i.OutputRegister()); \
199 __ asm_instr##_cl(i.OutputOperand()); \
205 // Assembles an instruction after register allocation, producing machine code.
206 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
207 X64OperandConverter i(this, instr);
209 switch (ArchOpcodeField::decode(instr->opcode())) {
210 case kArchCallCodeObject: {
211 EnsureSpaceForLazyDeopt();
212 if (HasImmediateInput(instr, 0)) {
213 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
214 __ Call(code, RelocInfo::CODE_TARGET);
216 Register reg = i.InputRegister(0);
217 int entry = Code::kHeaderSize - kHeapObjectTag;
218 __ Call(Operand(reg, entry));
220 AddSafepointAndDeopt(instr);
223 case kArchCallJSFunction: {
224 EnsureSpaceForLazyDeopt();
225 Register func = i.InputRegister(0);
226 if (FLAG_debug_code) {
227 // Check the function's context matches the context argument.
228 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
229 __ Assert(equal, kWrongFunctionContext);
231 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
232 AddSafepointAndDeopt(instr);
236 __ jmp(code_->GetLabel(i.InputRpo(0)));
239 // don't emit code for nops.
244 case kArchStackPointer:
245 __ movq(i.OutputRegister(), rsp);
247 case kArchTruncateDoubleToI:
248 __ TruncateDoubleToI(i.OutputRegister(), i.InputDoubleRegister(0));
251 ASSEMBLE_BINOP(addl);
254 ASSEMBLE_BINOP(addq);
257 ASSEMBLE_BINOP(subl);
260 ASSEMBLE_BINOP(subq);
263 ASSEMBLE_BINOP(andl);
266 ASSEMBLE_BINOP(andq);
269 ASSEMBLE_BINOP(cmpl);
272 ASSEMBLE_BINOP(cmpq);
275 ASSEMBLE_BINOP(testl);
278 ASSEMBLE_BINOP(testq);
281 ASSEMBLE_MULT(imull);
284 ASSEMBLE_MULT(imulq);
287 __ imull(i.InputRegister(1));
291 __ idivl(i.InputRegister(1));
295 __ idivq(i.InputRegister(1));
299 __ divl(i.InputRegister(1));
303 __ divq(i.InputRegister(1));
324 ASSEMBLE_BINOP(xorl);
327 ASSEMBLE_BINOP(xorq);
330 ASSEMBLE_SHIFT(shll, 5);
333 ASSEMBLE_SHIFT(shlq, 6);
336 ASSEMBLE_SHIFT(shrl, 5);
339 ASSEMBLE_SHIFT(shrq, 6);
342 ASSEMBLE_SHIFT(sarl, 5);
345 ASSEMBLE_SHIFT(sarq, 6);
348 ASSEMBLE_SHIFT(rorl, 5);
351 ASSEMBLE_SHIFT(rorq, 6);
354 if (instr->InputAt(1)->IsDoubleRegister()) {
355 __ ucomisd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
357 __ ucomisd(i.InputDoubleRegister(0), i.InputOperand(1));
361 __ addsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
364 __ subsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
367 __ mulsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
370 __ divsd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
372 case kSSEFloat64Mod: {
373 __ subq(rsp, Immediate(kDoubleSize));
374 // Move values to st(0) and st(1).
375 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
376 __ fld_d(Operand(rsp, 0));
377 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
378 __ fld_d(Operand(rsp, 0));
379 // Loop while fprem isn't done.
382 // This instructions traps on all kinds inputs, but we are assuming the
383 // floating point control word is set to ignore them all.
385 // The following 2 instruction implicitly use rax.
387 if (CpuFeatures::IsSupported(SAHF) && masm()->IsEnabled(SAHF)) {
390 __ shrl(rax, Immediate(8));
391 __ andl(rax, Immediate(0xFF));
395 __ j(parity_even, &mod_loop);
396 // Move output to stack and clean up.
398 __ fstp_d(Operand(rsp, 0));
399 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
400 __ addq(rsp, Immediate(kDoubleSize));
403 case kSSEFloat64Sqrt:
404 if (instr->InputAt(0)->IsDoubleRegister()) {
405 __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
407 __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
411 if (instr->InputAt(0)->IsDoubleRegister()) {
412 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
414 __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
418 if (instr->InputAt(0)->IsDoubleRegister()) {
419 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
421 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
424 case kSSEFloat64ToInt32:
425 if (instr->InputAt(0)->IsDoubleRegister()) {
426 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
428 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
431 case kSSEFloat64ToUint32:
432 if (instr->InputAt(0)->IsDoubleRegister()) {
433 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
435 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
437 __ andl(i.OutputRegister(), i.OutputRegister()); // clear upper bits.
438 // TODO(turbofan): generated code should not look at the upper 32 bits
439 // of the result, but those bits could escape to the outside world.
441 case kSSEInt32ToFloat64:
442 if (instr->InputAt(0)->IsRegister()) {
443 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
445 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
448 case kSSEUint32ToFloat64:
449 if (instr->InputAt(0)->IsRegister()) {
450 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
452 __ cvtqsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
456 __ movsxbl(i.OutputRegister(), i.MemoryOperand());
459 __ movzxbl(i.OutputRegister(), i.MemoryOperand());
463 Operand operand = i.MemoryOperand(&index);
464 if (HasImmediateInput(instr, index)) {
465 __ movb(operand, Immediate(i.InputInt8(index)));
467 __ movb(operand, i.InputRegister(index));
472 __ movsxwl(i.OutputRegister(), i.MemoryOperand());
475 __ movzxwl(i.OutputRegister(), i.MemoryOperand());
479 Operand operand = i.MemoryOperand(&index);
480 if (HasImmediateInput(instr, index)) {
481 __ movw(operand, Immediate(i.InputInt16(index)));
483 __ movw(operand, i.InputRegister(index));
488 if (instr->HasOutput()) {
489 if (instr->addressing_mode() == kMode_None) {
490 if (instr->InputAt(0)->IsRegister()) {
491 __ movl(i.OutputRegister(), i.InputRegister(0));
493 __ movl(i.OutputRegister(), i.InputOperand(0));
496 __ movl(i.OutputRegister(), i.MemoryOperand());
500 Operand operand = i.MemoryOperand(&index);
501 if (HasImmediateInput(instr, index)) {
502 __ movl(operand, i.InputImmediate(index));
504 __ movl(operand, i.InputRegister(index));
509 if (instr->InputAt(0)->IsRegister()) {
510 __ movsxlq(i.OutputRegister(), i.InputRegister(0));
512 __ movsxlq(i.OutputRegister(), i.InputOperand(0));
517 if (instr->HasOutput()) {
518 __ movq(i.OutputRegister(), i.MemoryOperand());
521 Operand operand = i.MemoryOperand(&index);
522 if (HasImmediateInput(instr, index)) {
523 __ movq(operand, i.InputImmediate(index));
525 __ movq(operand, i.InputRegister(index));
530 if (instr->HasOutput()) {
531 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
534 Operand operand = i.MemoryOperand(&index);
535 __ movss(operand, i.InputDoubleRegister(index));
539 if (instr->HasOutput()) {
540 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
543 Operand operand = i.MemoryOperand(&index);
544 __ movsd(operand, i.InputDoubleRegister(index));
548 __ leal(i.OutputRegister(), i.MemoryOperand());
551 __ leaq(i.OutputRegister(), i.MemoryOperand());
554 if (HasImmediateInput(instr, 0)) {
555 __ pushq(i.InputImmediate(0));
557 if (instr->InputAt(0)->IsRegister()) {
558 __ pushq(i.InputRegister(0));
560 __ pushq(i.InputOperand(0));
564 case kX64StoreWriteBarrier: {
565 Register object = i.InputRegister(0);
566 Register index = i.InputRegister(1);
567 Register value = i.InputRegister(2);
568 __ movsxlq(index, index);
569 __ movq(Operand(object, index, times_1, 0), value);
570 __ leaq(index, Operand(object, index, times_1, 0));
571 SaveFPRegsMode mode = code_->frame()->DidAllocateDoubleRegisters()
574 __ RecordWrite(object, index, value, mode);
581 // Assembles branches after this instruction.
582 void CodeGenerator::AssembleArchBranch(Instruction* instr,
583 FlagsCondition condition) {
584 X64OperandConverter i(this, instr);
587 // Emit a branch. The true and false targets are always the last two inputs
588 // to the instruction.
589 BasicBlock::RpoNumber tblock =
590 i.InputRpo(static_cast<int>(instr->InputCount()) - 2);
591 BasicBlock::RpoNumber fblock =
592 i.InputRpo(static_cast<int>(instr->InputCount()) - 1);
593 bool fallthru = IsNextInAssemblyOrder(fblock);
594 Label* tlabel = code()->GetLabel(tblock);
595 Label* flabel = fallthru ? &done : code()->GetLabel(fblock);
596 Label::Distance flabel_distance = fallthru ? Label::kNear : Label::kFar;
598 case kUnorderedEqual:
599 __ j(parity_even, flabel, flabel_distance);
604 case kUnorderedNotEqual:
605 __ j(parity_even, tlabel);
608 __ j(not_equal, tlabel);
610 case kSignedLessThan:
613 case kSignedGreaterThanOrEqual:
614 __ j(greater_equal, tlabel);
616 case kSignedLessThanOrEqual:
617 __ j(less_equal, tlabel);
619 case kSignedGreaterThan:
620 __ j(greater, tlabel);
622 case kUnorderedLessThan:
623 __ j(parity_even, flabel, flabel_distance);
625 case kUnsignedLessThan:
628 case kUnorderedGreaterThanOrEqual:
629 __ j(parity_even, tlabel);
631 case kUnsignedGreaterThanOrEqual:
632 __ j(above_equal, tlabel);
634 case kUnorderedLessThanOrEqual:
635 __ j(parity_even, flabel, flabel_distance);
637 case kUnsignedLessThanOrEqual:
638 __ j(below_equal, tlabel);
640 case kUnorderedGreaterThan:
641 __ j(parity_even, tlabel);
643 case kUnsignedGreaterThan:
647 __ j(overflow, tlabel);
650 __ j(no_overflow, tlabel);
653 if (!fallthru) __ jmp(flabel, flabel_distance); // no fallthru to flabel.
658 // Assembles boolean materializations after this instruction.
659 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
660 FlagsCondition condition) {
661 X64OperandConverter i(this, instr);
664 // Materialize a full 64-bit 1 or 0 value. The result register is always the
665 // last output of the instruction.
667 DCHECK_NE(0, static_cast<int>(instr->OutputCount()));
668 Register reg = i.OutputRegister(static_cast<int>(instr->OutputCount() - 1));
669 Condition cc = no_condition;
671 case kUnorderedEqual:
672 __ j(parity_odd, &check, Label::kNear);
673 __ movl(reg, Immediate(0));
674 __ jmp(&done, Label::kNear);
679 case kUnorderedNotEqual:
680 __ j(parity_odd, &check, Label::kNear);
681 __ movl(reg, Immediate(1));
682 __ jmp(&done, Label::kNear);
687 case kSignedLessThan:
690 case kSignedGreaterThanOrEqual:
693 case kSignedLessThanOrEqual:
696 case kSignedGreaterThan:
699 case kUnorderedLessThan:
700 __ j(parity_odd, &check, Label::kNear);
701 __ movl(reg, Immediate(0));
702 __ jmp(&done, Label::kNear);
704 case kUnsignedLessThan:
707 case kUnorderedGreaterThanOrEqual:
708 __ j(parity_odd, &check, Label::kNear);
709 __ movl(reg, Immediate(1));
710 __ jmp(&done, Label::kNear);
712 case kUnsignedGreaterThanOrEqual:
715 case kUnorderedLessThanOrEqual:
716 __ j(parity_odd, &check, Label::kNear);
717 __ movl(reg, Immediate(0));
718 __ jmp(&done, Label::kNear);
720 case kUnsignedLessThanOrEqual:
723 case kUnorderedGreaterThan:
724 __ j(parity_odd, &check, Label::kNear);
725 __ movl(reg, Immediate(1));
726 __ jmp(&done, Label::kNear);
728 case kUnsignedGreaterThan:
740 __ movzxbl(reg, reg);
745 void CodeGenerator::AssembleDeoptimizerCall(int deoptimization_id) {
746 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
747 isolate(), deoptimization_id, Deoptimizer::LAZY);
748 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
752 void CodeGenerator::AssemblePrologue() {
753 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
754 int stack_slots = frame()->GetSpillSlotCount();
755 if (descriptor->kind() == CallDescriptor::kCallAddress) {
758 const RegList saves = descriptor->CalleeSavedRegisters();
759 if (saves != 0) { // Save callee-saved registers.
760 int register_save_area_size = 0;
761 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
762 if (!((1 << i) & saves)) continue;
763 __ pushq(Register::from_code(i));
764 register_save_area_size += kPointerSize;
766 frame()->SetRegisterSaveAreaSize(register_save_area_size);
768 } else if (descriptor->IsJSFunctionCall()) {
769 CompilationInfo* info = linkage()->info();
770 __ Prologue(info->IsCodePreAgingActive());
771 frame()->SetRegisterSaveAreaSize(
772 StandardFrameConstants::kFixedFrameSizeFromFp);
774 // Sloppy mode functions and builtins need to replace the receiver with the
775 // global proxy when called as functions (without an explicit receiver
777 // TODO(mstarzinger/verwaest): Should this be moved back into the CallIC?
778 if (info->strict_mode() == SLOPPY && !info->is_native()) {
780 StackArgumentsAccessor args(rbp, info->scope()->num_parameters());
781 __ movp(rcx, args.GetReceiverOperand());
782 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
783 __ j(not_equal, &ok, Label::kNear);
784 __ movp(rcx, GlobalObjectOperand());
785 __ movp(rcx, FieldOperand(rcx, GlobalObject::kGlobalProxyOffset));
786 __ movp(args.GetReceiverOperand(), rcx);
792 frame()->SetRegisterSaveAreaSize(
793 StandardFrameConstants::kFixedFrameSizeFromFp);
795 if (stack_slots > 0) {
796 __ subq(rsp, Immediate(stack_slots * kPointerSize));
801 void CodeGenerator::AssembleReturn() {
802 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
803 if (descriptor->kind() == CallDescriptor::kCallAddress) {
804 if (frame()->GetRegisterSaveAreaSize() > 0) {
805 // Remove this frame's spill slots first.
806 int stack_slots = frame()->GetSpillSlotCount();
807 if (stack_slots > 0) {
808 __ addq(rsp, Immediate(stack_slots * kPointerSize));
810 const RegList saves = descriptor->CalleeSavedRegisters();
811 // Restore registers.
813 for (int i = 0; i < Register::kNumRegisters; i++) {
814 if (!((1 << i) & saves)) continue;
815 __ popq(Register::from_code(i));
818 __ popq(rbp); // Pop caller's frame pointer.
821 // No saved registers.
822 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
823 __ popq(rbp); // Pop caller's frame pointer.
827 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
828 __ popq(rbp); // Pop caller's frame pointer.
829 int pop_count = descriptor->IsJSFunctionCall()
830 ? static_cast<int>(descriptor->JSParameterCount())
832 __ ret(pop_count * kPointerSize);
837 void CodeGenerator::AssembleMove(InstructionOperand* source,
838 InstructionOperand* destination) {
839 X64OperandConverter g(this, NULL);
840 // Dispatch on the source and destination operand kinds. Not all
841 // combinations are possible.
842 if (source->IsRegister()) {
843 DCHECK(destination->IsRegister() || destination->IsStackSlot());
844 Register src = g.ToRegister(source);
845 if (destination->IsRegister()) {
846 __ movq(g.ToRegister(destination), src);
848 __ movq(g.ToOperand(destination), src);
850 } else if (source->IsStackSlot()) {
851 DCHECK(destination->IsRegister() || destination->IsStackSlot());
852 Operand src = g.ToOperand(source);
853 if (destination->IsRegister()) {
854 Register dst = g.ToRegister(destination);
857 // Spill on demand to use a temporary register for memory-to-memory
859 Register tmp = kScratchRegister;
860 Operand dst = g.ToOperand(destination);
864 } else if (source->IsConstant()) {
865 ConstantOperand* constant_source = ConstantOperand::cast(source);
866 Constant src = g.ToConstant(constant_source);
867 if (destination->IsRegister() || destination->IsStackSlot()) {
868 Register dst = destination->IsRegister() ? g.ToRegister(destination)
870 switch (src.type()) {
871 case Constant::kInt32:
872 // TODO(dcarney): don't need scratch in this case.
873 __ movq(dst, Immediate(src.ToInt32()));
875 case Constant::kInt64:
876 __ Set(dst, src.ToInt64());
878 case Constant::kFloat32:
880 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
882 case Constant::kFloat64:
884 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
886 case Constant::kExternalReference:
887 __ Move(dst, src.ToExternalReference());
889 case Constant::kHeapObject:
890 __ Move(dst, src.ToHeapObject());
893 if (destination->IsStackSlot()) {
894 __ movq(g.ToOperand(destination), kScratchRegister);
896 } else if (src.type() == Constant::kFloat32) {
897 // TODO(turbofan): Can we do better here?
898 __ movl(kScratchRegister, Immediate(bit_cast<int32_t>(src.ToFloat32())));
899 if (destination->IsDoubleRegister()) {
900 XMMRegister dst = g.ToDoubleRegister(destination);
901 __ movq(dst, kScratchRegister);
903 DCHECK(destination->IsDoubleStackSlot());
904 Operand dst = g.ToOperand(destination);
905 __ movl(dst, kScratchRegister);
908 DCHECK_EQ(Constant::kFloat64, src.type());
909 __ movq(kScratchRegister, bit_cast<int64_t>(src.ToFloat64()));
910 if (destination->IsDoubleRegister()) {
911 __ movq(g.ToDoubleRegister(destination), kScratchRegister);
913 DCHECK(destination->IsDoubleStackSlot());
914 __ movq(g.ToOperand(destination), kScratchRegister);
917 } else if (source->IsDoubleRegister()) {
918 XMMRegister src = g.ToDoubleRegister(source);
919 if (destination->IsDoubleRegister()) {
920 XMMRegister dst = g.ToDoubleRegister(destination);
923 DCHECK(destination->IsDoubleStackSlot());
924 Operand dst = g.ToOperand(destination);
927 } else if (source->IsDoubleStackSlot()) {
928 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
929 Operand src = g.ToOperand(source);
930 if (destination->IsDoubleRegister()) {
931 XMMRegister dst = g.ToDoubleRegister(destination);
934 // We rely on having xmm0 available as a fixed scratch register.
935 Operand dst = g.ToOperand(destination);
945 void CodeGenerator::AssembleSwap(InstructionOperand* source,
946 InstructionOperand* destination) {
947 X64OperandConverter g(this, NULL);
948 // Dispatch on the source and destination operand kinds. Not all
949 // combinations are possible.
950 if (source->IsRegister() && destination->IsRegister()) {
951 // Register-register.
952 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
953 } else if (source->IsRegister() && destination->IsStackSlot()) {
954 Register src = g.ToRegister(source);
955 Operand dst = g.ToOperand(destination);
957 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
958 (source->IsDoubleStackSlot() &&
959 destination->IsDoubleStackSlot())) {
961 Register tmp = kScratchRegister;
962 Operand src = g.ToOperand(source);
963 Operand dst = g.ToOperand(destination);
967 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
968 // XMM register-register swap. We rely on having xmm0
969 // available as a fixed scratch register.
970 XMMRegister src = g.ToDoubleRegister(source);
971 XMMRegister dst = g.ToDoubleRegister(destination);
975 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
976 // XMM register-memory swap. We rely on having xmm0
977 // available as a fixed scratch register.
978 XMMRegister src = g.ToDoubleRegister(source);
979 Operand dst = g.ToOperand(destination);
984 // No other combinations are possible.
990 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
993 void CodeGenerator::EnsureSpaceForLazyDeopt() {
994 int space_needed = Deoptimizer::patch_size();
995 if (!linkage()->info()->IsStub()) {
996 // Ensure that we have enough space after the previous lazy-bailout
997 // instruction for patching the code here.
998 int current_pc = masm()->pc_offset();
999 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1000 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1001 __ Nop(padding_size);
1004 MarkLazyDeoptSite();
1009 } // namespace internal
1010 } // namespace compiler