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/scopes.h"
11 #include "src/x64/assembler-x64.h"
12 #include "src/x64/macro-assembler-x64.h"
21 #define kScratchDoubleReg xmm0
24 // Adds X64 specific methods for decoding operands.
25 class X64OperandConverter : public InstructionOperandConverter {
27 X64OperandConverter(CodeGenerator* gen, Instruction* instr)
28 : InstructionOperandConverter(gen, instr) {}
30 Immediate InputImmediate(size_t index) {
31 return ToImmediate(instr_->InputAt(index));
34 Operand InputOperand(size_t index, int extra = 0) {
35 return ToOperand(instr_->InputAt(index), extra);
38 Operand OutputOperand() { return ToOperand(instr_->Output()); }
40 Immediate ToImmediate(InstructionOperand* operand) {
41 return Immediate(ToConstant(operand).ToInt32());
44 Operand ToOperand(InstructionOperand* op, int extra = 0) {
45 DCHECK(op->IsStackSlot() || op->IsDoubleStackSlot());
46 // The linkage computes where all spill slots are located.
47 FrameOffset offset = linkage()->GetFrameOffset(op->index(), frame(), extra);
48 return Operand(offset.from_stack_pointer() ? rsp : rbp, offset.offset());
51 static size_t NextOffset(size_t* offset) {
57 static ScaleFactor ScaleFor(AddressingMode one, AddressingMode mode) {
58 STATIC_ASSERT(0 == static_cast<int>(times_1));
59 STATIC_ASSERT(1 == static_cast<int>(times_2));
60 STATIC_ASSERT(2 == static_cast<int>(times_4));
61 STATIC_ASSERT(3 == static_cast<int>(times_8));
62 int scale = static_cast<int>(mode - one);
63 DCHECK(scale >= 0 && scale < 4);
64 return static_cast<ScaleFactor>(scale);
67 Operand MemoryOperand(size_t* offset) {
68 AddressingMode mode = AddressingModeField::decode(instr_->opcode());
71 Register base = InputRegister(NextOffset(offset));
73 return Operand(base, disp);
76 Register base = InputRegister(NextOffset(offset));
77 int32_t disp = InputInt32(NextOffset(offset));
78 return Operand(base, disp);
84 Register base = InputRegister(NextOffset(offset));
85 Register index = InputRegister(NextOffset(offset));
86 ScaleFactor scale = ScaleFor(kMode_MR1, mode);
88 return Operand(base, index, scale, disp);
94 Register base = InputRegister(NextOffset(offset));
95 Register index = InputRegister(NextOffset(offset));
96 ScaleFactor scale = ScaleFor(kMode_MR1I, mode);
97 int32_t disp = InputInt32(NextOffset(offset));
98 return Operand(base, index, scale, disp);
101 Register base = InputRegister(NextOffset(offset));
103 return Operand(base, disp);
106 UNREACHABLE(); // Should use kModeMR with more compact encoding instead
107 return Operand(no_reg, 0);
110 Register index = InputRegister(NextOffset(offset));
111 ScaleFactor scale = ScaleFor(kMode_M1, mode);
113 return Operand(index, scale, disp);
119 Register index = InputRegister(NextOffset(offset));
120 ScaleFactor scale = ScaleFor(kMode_M1I, mode);
121 int32_t disp = InputInt32(NextOffset(offset));
122 return Operand(index, scale, disp);
126 return Operand(no_reg, 0);
129 return Operand(no_reg, 0);
132 Operand MemoryOperand(size_t first_input = 0) {
133 return MemoryOperand(&first_input);
140 bool HasImmediateInput(Instruction* instr, size_t index) {
141 return instr->InputAt(index)->IsImmediate();
145 class OutOfLineLoadZero FINAL : public OutOfLineCode {
147 OutOfLineLoadZero(CodeGenerator* gen, Register result)
148 : OutOfLineCode(gen), result_(result) {}
150 void Generate() FINAL { __ xorl(result_, result_); }
153 Register const result_;
157 class OutOfLineLoadNaN FINAL : public OutOfLineCode {
159 OutOfLineLoadNaN(CodeGenerator* gen, XMMRegister result)
160 : OutOfLineCode(gen), result_(result) {}
162 void Generate() FINAL { __ pcmpeqd(result_, result_); }
165 XMMRegister const result_;
169 class OutOfLineTruncateDoubleToI FINAL : public OutOfLineCode {
171 OutOfLineTruncateDoubleToI(CodeGenerator* gen, Register result,
173 : OutOfLineCode(gen), result_(result), input_(input) {}
175 void Generate() FINAL {
176 __ subp(rsp, Immediate(kDoubleSize));
177 __ movsd(MemOperand(rsp, 0), input_);
178 __ SlowTruncateToI(result_, rsp, 0);
179 __ addp(rsp, Immediate(kDoubleSize));
183 Register const result_;
184 XMMRegister const input_;
190 #define ASSEMBLE_UNOP(asm_instr) \
192 if (instr->Output()->IsRegister()) { \
193 __ asm_instr(i.OutputRegister()); \
195 __ asm_instr(i.OutputOperand()); \
200 #define ASSEMBLE_BINOP(asm_instr) \
202 if (HasImmediateInput(instr, 1)) { \
203 if (instr->InputAt(0)->IsRegister()) { \
204 __ asm_instr(i.InputRegister(0), i.InputImmediate(1)); \
206 __ asm_instr(i.InputOperand(0), i.InputImmediate(1)); \
209 if (instr->InputAt(1)->IsRegister()) { \
210 __ asm_instr(i.InputRegister(0), i.InputRegister(1)); \
212 __ asm_instr(i.InputRegister(0), i.InputOperand(1)); \
218 #define ASSEMBLE_MULT(asm_instr) \
220 if (HasImmediateInput(instr, 1)) { \
221 if (instr->InputAt(0)->IsRegister()) { \
222 __ asm_instr(i.OutputRegister(), i.InputRegister(0), \
223 i.InputImmediate(1)); \
225 __ asm_instr(i.OutputRegister(), i.InputOperand(0), \
226 i.InputImmediate(1)); \
229 if (instr->InputAt(1)->IsRegister()) { \
230 __ asm_instr(i.OutputRegister(), i.InputRegister(1)); \
232 __ asm_instr(i.OutputRegister(), i.InputOperand(1)); \
238 #define ASSEMBLE_SHIFT(asm_instr, width) \
240 if (HasImmediateInput(instr, 1)) { \
241 if (instr->Output()->IsRegister()) { \
242 __ asm_instr(i.OutputRegister(), Immediate(i.InputInt##width(1))); \
244 __ asm_instr(i.OutputOperand(), Immediate(i.InputInt##width(1))); \
247 if (instr->Output()->IsRegister()) { \
248 __ asm_instr##_cl(i.OutputRegister()); \
250 __ asm_instr##_cl(i.OutputOperand()); \
256 #define ASSEMBLE_MOVX(asm_instr) \
258 if (instr->addressing_mode() != kMode_None) { \
259 __ asm_instr(i.OutputRegister(), i.MemoryOperand()); \
260 } else if (instr->InputAt(0)->IsRegister()) { \
261 __ asm_instr(i.OutputRegister(), i.InputRegister(0)); \
263 __ asm_instr(i.OutputRegister(), i.InputOperand(0)); \
268 #define ASSEMBLE_DOUBLE_BINOP(asm_instr) \
270 if (instr->InputAt(1)->IsDoubleRegister()) { \
271 __ asm_instr(i.InputDoubleRegister(0), i.InputDoubleRegister(1)); \
273 __ asm_instr(i.InputDoubleRegister(0), i.InputOperand(1)); \
278 #define ASSEMBLE_AVX_DOUBLE_BINOP(asm_instr) \
280 CpuFeatureScope avx_scope(masm(), AVX); \
281 if (instr->InputAt(1)->IsDoubleRegister()) { \
282 __ asm_instr(i.OutputDoubleRegister(), i.InputDoubleRegister(0), \
283 i.InputDoubleRegister(1)); \
285 __ asm_instr(i.OutputDoubleRegister(), i.InputDoubleRegister(0), \
286 i.InputOperand(1)); \
291 #define ASSEMBLE_CHECKED_LOAD_FLOAT(asm_instr) \
293 auto result = i.OutputDoubleRegister(); \
294 auto buffer = i.InputRegister(0); \
295 auto index1 = i.InputRegister(1); \
296 auto index2 = i.InputInt32(2); \
297 OutOfLineCode* ool; \
298 if (instr->InputAt(3)->IsRegister()) { \
299 auto length = i.InputRegister(3); \
300 DCHECK_EQ(0, index2); \
301 __ cmpl(index1, length); \
302 ool = new (zone()) OutOfLineLoadNaN(this, result); \
304 auto length = i.InputInt32(3); \
305 DCHECK_LE(index2, length); \
306 __ cmpq(index1, Immediate(length - index2)); \
307 class OutOfLineLoadFloat FINAL : public OutOfLineCode { \
309 OutOfLineLoadFloat(CodeGenerator* gen, XMMRegister result, \
310 Register buffer, Register index1, int32_t index2, \
312 : OutOfLineCode(gen), \
319 void Generate() FINAL { \
320 __ leal(kScratchRegister, Operand(index1_, index2_)); \
321 __ pcmpeqd(result_, result_); \
322 __ cmpl(kScratchRegister, Immediate(length_)); \
323 __ j(above_equal, exit()); \
324 __ asm_instr(result_, \
325 Operand(buffer_, kScratchRegister, times_1, 0)); \
329 XMMRegister const result_; \
330 Register const buffer_; \
331 Register const index1_; \
332 int32_t const index2_; \
333 int32_t const length_; \
336 OutOfLineLoadFloat(this, result, buffer, index1, index2, length); \
338 __ j(above_equal, ool->entry()); \
339 __ asm_instr(result, Operand(buffer, index1, times_1, index2)); \
340 __ bind(ool->exit()); \
344 #define ASSEMBLE_CHECKED_LOAD_INTEGER(asm_instr) \
346 auto result = i.OutputRegister(); \
347 auto buffer = i.InputRegister(0); \
348 auto index1 = i.InputRegister(1); \
349 auto index2 = i.InputInt32(2); \
350 OutOfLineCode* ool; \
351 if (instr->InputAt(3)->IsRegister()) { \
352 auto length = i.InputRegister(3); \
353 DCHECK_EQ(0, index2); \
354 __ cmpl(index1, length); \
355 ool = new (zone()) OutOfLineLoadZero(this, result); \
357 auto length = i.InputInt32(3); \
358 DCHECK_LE(index2, length); \
359 __ cmpq(index1, Immediate(length - index2)); \
360 class OutOfLineLoadInteger FINAL : public OutOfLineCode { \
362 OutOfLineLoadInteger(CodeGenerator* gen, Register result, \
363 Register buffer, Register index1, int32_t index2, \
365 : OutOfLineCode(gen), \
372 void Generate() FINAL { \
374 __ leal(kScratchRegister, Operand(index1_, index2_)); \
375 __ cmpl(kScratchRegister, Immediate(length_)); \
376 __ j(above_equal, &oob, Label::kNear); \
377 __ asm_instr(result_, \
378 Operand(buffer_, kScratchRegister, times_1, 0)); \
381 __ xorl(result_, result_); \
385 Register const result_; \
386 Register const buffer_; \
387 Register const index1_; \
388 int32_t const index2_; \
389 int32_t const length_; \
392 OutOfLineLoadInteger(this, result, buffer, index1, index2, length); \
394 __ j(above_equal, ool->entry()); \
395 __ asm_instr(result, Operand(buffer, index1, times_1, index2)); \
396 __ bind(ool->exit()); \
400 #define ASSEMBLE_CHECKED_STORE_FLOAT(asm_instr) \
402 auto buffer = i.InputRegister(0); \
403 auto index1 = i.InputRegister(1); \
404 auto index2 = i.InputInt32(2); \
405 auto value = i.InputDoubleRegister(4); \
406 if (instr->InputAt(3)->IsRegister()) { \
407 auto length = i.InputRegister(3); \
408 DCHECK_EQ(0, index2); \
410 __ cmpl(index1, length); \
411 __ j(above_equal, &done, Label::kNear); \
412 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
415 auto length = i.InputInt32(3); \
416 DCHECK_LE(index2, length); \
417 __ cmpq(index1, Immediate(length - index2)); \
418 class OutOfLineStoreFloat FINAL : public OutOfLineCode { \
420 OutOfLineStoreFloat(CodeGenerator* gen, Register buffer, \
421 Register index1, int32_t index2, int32_t length, \
423 : OutOfLineCode(gen), \
430 void Generate() FINAL { \
431 __ leal(kScratchRegister, Operand(index1_, index2_)); \
432 __ cmpl(kScratchRegister, Immediate(length_)); \
433 __ j(above_equal, exit()); \
434 __ asm_instr(Operand(buffer_, kScratchRegister, times_1, 0), \
439 Register const buffer_; \
440 Register const index1_; \
441 int32_t const index2_; \
442 int32_t const length_; \
443 XMMRegister const value_; \
445 auto ool = new (zone()) \
446 OutOfLineStoreFloat(this, buffer, index1, index2, length, value); \
447 __ j(above_equal, ool->entry()); \
448 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
449 __ bind(ool->exit()); \
454 #define ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Value) \
456 auto buffer = i.InputRegister(0); \
457 auto index1 = i.InputRegister(1); \
458 auto index2 = i.InputInt32(2); \
459 if (instr->InputAt(3)->IsRegister()) { \
460 auto length = i.InputRegister(3); \
461 DCHECK_EQ(0, index2); \
463 __ cmpl(index1, length); \
464 __ j(above_equal, &done, Label::kNear); \
465 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
468 auto length = i.InputInt32(3); \
469 DCHECK_LE(index2, length); \
470 __ cmpq(index1, Immediate(length - index2)); \
471 class OutOfLineStoreInteger FINAL : public OutOfLineCode { \
473 OutOfLineStoreInteger(CodeGenerator* gen, Register buffer, \
474 Register index1, int32_t index2, int32_t length, \
476 : OutOfLineCode(gen), \
483 void Generate() FINAL { \
484 __ leal(kScratchRegister, Operand(index1_, index2_)); \
485 __ cmpl(kScratchRegister, Immediate(length_)); \
486 __ j(above_equal, exit()); \
487 __ asm_instr(Operand(buffer_, kScratchRegister, times_1, 0), \
492 Register const buffer_; \
493 Register const index1_; \
494 int32_t const index2_; \
495 int32_t const length_; \
496 Value const value_; \
498 auto ool = new (zone()) \
499 OutOfLineStoreInteger(this, buffer, index1, index2, length, value); \
500 __ j(above_equal, ool->entry()); \
501 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
502 __ bind(ool->exit()); \
507 #define ASSEMBLE_CHECKED_STORE_INTEGER(asm_instr) \
509 if (instr->InputAt(4)->IsRegister()) { \
510 Register value = i.InputRegister(4); \
511 ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Register); \
513 Immediate value = i.InputImmediate(4); \
514 ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Immediate); \
519 // Assembles an instruction after register allocation, producing machine code.
520 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
521 X64OperandConverter i(this, instr);
523 switch (ArchOpcodeField::decode(instr->opcode())) {
524 case kArchCallCodeObject: {
525 EnsureSpaceForLazyDeopt();
526 if (HasImmediateInput(instr, 0)) {
527 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
528 __ Call(code, RelocInfo::CODE_TARGET);
530 Register reg = i.InputRegister(0);
531 int entry = Code::kHeaderSize - kHeapObjectTag;
532 __ Call(Operand(reg, entry));
534 RecordCallPosition(instr);
537 case kArchCallJSFunction: {
538 EnsureSpaceForLazyDeopt();
539 Register func = i.InputRegister(0);
540 if (FLAG_debug_code) {
541 // Check the function's context matches the context argument.
542 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
543 __ Assert(equal, kWrongFunctionContext);
545 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
546 RecordCallPosition(instr);
550 AssembleArchJump(i.InputRpo(0));
552 case kArchLookupSwitch:
553 AssembleArchLookupSwitch(instr);
555 case kArchTableSwitch:
556 AssembleArchTableSwitch(instr);
559 // don't emit code for nops.
561 case kArchDeoptimize: {
563 BuildTranslation(instr, -1, 0, OutputFrameStateCombine::Ignore());
564 AssembleDeoptimizerCall(deopt_state_id, Deoptimizer::EAGER);
570 case kArchStackPointer:
571 __ movq(i.OutputRegister(), rsp);
573 case kArchTruncateDoubleToI: {
574 auto result = i.OutputRegister();
575 auto input = i.InputDoubleRegister(0);
576 auto ool = new (zone()) OutOfLineTruncateDoubleToI(this, result, input);
577 __ cvttsd2siq(result, input);
578 __ cmpq(result, Immediate(1));
579 __ j(overflow, ool->entry());
580 __ bind(ool->exit());
584 ASSEMBLE_BINOP(addl);
587 ASSEMBLE_BINOP(addq);
590 ASSEMBLE_BINOP(subl);
593 ASSEMBLE_BINOP(subq);
596 ASSEMBLE_BINOP(andl);
599 ASSEMBLE_BINOP(andq);
602 ASSEMBLE_BINOP(cmpl);
605 ASSEMBLE_BINOP(cmpq);
608 ASSEMBLE_BINOP(testl);
611 ASSEMBLE_BINOP(testq);
614 ASSEMBLE_MULT(imull);
617 ASSEMBLE_MULT(imulq);
620 if (instr->InputAt(1)->IsRegister()) {
621 __ imull(i.InputRegister(1));
623 __ imull(i.InputOperand(1));
627 if (instr->InputAt(1)->IsRegister()) {
628 __ mull(i.InputRegister(1));
630 __ mull(i.InputOperand(1));
635 __ idivl(i.InputRegister(1));
639 __ idivq(i.InputRegister(1));
643 __ divl(i.InputRegister(1));
647 __ divq(i.InputRegister(1));
668 ASSEMBLE_BINOP(xorl);
671 ASSEMBLE_BINOP(xorq);
674 ASSEMBLE_SHIFT(shll, 5);
677 ASSEMBLE_SHIFT(shlq, 6);
680 ASSEMBLE_SHIFT(shrl, 5);
683 ASSEMBLE_SHIFT(shrq, 6);
686 ASSEMBLE_SHIFT(sarl, 5);
689 ASSEMBLE_SHIFT(sarq, 6);
692 ASSEMBLE_SHIFT(rorl, 5);
695 ASSEMBLE_SHIFT(rorq, 6);
698 ASSEMBLE_DOUBLE_BINOP(ucomisd);
701 ASSEMBLE_DOUBLE_BINOP(addsd);
704 ASSEMBLE_DOUBLE_BINOP(subsd);
707 ASSEMBLE_DOUBLE_BINOP(mulsd);
710 ASSEMBLE_DOUBLE_BINOP(divsd);
712 case kSSEFloat64Mod: {
713 __ subq(rsp, Immediate(kDoubleSize));
714 // Move values to st(0) and st(1).
715 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
716 __ fld_d(Operand(rsp, 0));
717 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
718 __ fld_d(Operand(rsp, 0));
719 // Loop while fprem isn't done.
722 // This instructions traps on all kinds inputs, but we are assuming the
723 // floating point control word is set to ignore them all.
725 // The following 2 instruction implicitly use rax.
727 if (CpuFeatures::IsSupported(SAHF)) {
728 CpuFeatureScope sahf_scope(masm(), SAHF);
731 __ shrl(rax, Immediate(8));
732 __ andl(rax, Immediate(0xFF));
736 __ j(parity_even, &mod_loop);
737 // Move output to stack and clean up.
739 __ fstp_d(Operand(rsp, 0));
740 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
741 __ addq(rsp, Immediate(kDoubleSize));
744 case kSSEFloat64Sqrt:
745 if (instr->InputAt(0)->IsDoubleRegister()) {
746 __ sqrtsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
748 __ sqrtsd(i.OutputDoubleRegister(), i.InputOperand(0));
751 case kSSEFloat64Floor: {
752 CpuFeatureScope sse_scope(masm(), SSE4_1);
753 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
754 v8::internal::Assembler::kRoundDown);
757 case kSSEFloat64Ceil: {
758 CpuFeatureScope sse_scope(masm(), SSE4_1);
759 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
760 v8::internal::Assembler::kRoundUp);
763 case kSSEFloat64RoundTruncate: {
764 CpuFeatureScope sse_scope(masm(), SSE4_1);
765 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0),
766 v8::internal::Assembler::kRoundToZero);
770 if (instr->InputAt(0)->IsDoubleRegister()) {
771 __ cvtss2sd(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
773 __ cvtss2sd(i.OutputDoubleRegister(), i.InputOperand(0));
777 if (instr->InputAt(0)->IsDoubleRegister()) {
778 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputDoubleRegister(0));
780 __ cvtsd2ss(i.OutputDoubleRegister(), i.InputOperand(0));
783 case kSSEFloat64ToInt32:
784 if (instr->InputAt(0)->IsDoubleRegister()) {
785 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
787 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
790 case kSSEFloat64ToUint32: {
791 if (instr->InputAt(0)->IsDoubleRegister()) {
792 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
794 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
796 __ AssertZeroExtended(i.OutputRegister());
799 case kSSEInt32ToFloat64:
800 if (instr->InputAt(0)->IsRegister()) {
801 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
803 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
806 case kSSEUint32ToFloat64:
807 if (instr->InputAt(0)->IsRegister()) {
808 __ movl(kScratchRegister, i.InputRegister(0));
810 __ movl(kScratchRegister, i.InputOperand(0));
812 __ cvtqsi2sd(i.OutputDoubleRegister(), kScratchRegister);
814 case kSSEFloat64ExtractLowWord32:
815 if (instr->InputAt(0)->IsDoubleStackSlot()) {
816 __ movl(i.OutputRegister(), i.InputOperand(0));
818 __ movd(i.OutputRegister(), i.InputDoubleRegister(0));
821 case kSSEFloat64ExtractHighWord32:
822 if (instr->InputAt(0)->IsDoubleStackSlot()) {
823 __ movl(i.OutputRegister(), i.InputOperand(0, kDoubleSize / 2));
825 __ Pextrd(i.OutputRegister(), i.InputDoubleRegister(0), 1);
828 case kSSEFloat64InsertLowWord32:
829 if (instr->InputAt(1)->IsRegister()) {
830 __ Pinsrd(i.OutputDoubleRegister(), i.InputRegister(1), 0);
832 __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 0);
835 case kSSEFloat64InsertHighWord32:
836 if (instr->InputAt(1)->IsRegister()) {
837 __ Pinsrd(i.OutputDoubleRegister(), i.InputRegister(1), 1);
839 __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 1);
842 case kSSEFloat64LoadLowWord32:
843 if (instr->InputAt(0)->IsRegister()) {
844 __ movd(i.OutputDoubleRegister(), i.InputRegister(0));
846 __ movd(i.OutputDoubleRegister(), i.InputOperand(0));
850 ASSEMBLE_AVX_DOUBLE_BINOP(vaddsd);
853 ASSEMBLE_AVX_DOUBLE_BINOP(vsubsd);
856 ASSEMBLE_AVX_DOUBLE_BINOP(vmulsd);
859 ASSEMBLE_AVX_DOUBLE_BINOP(vdivsd);
862 ASSEMBLE_MOVX(movsxbl);
863 __ AssertZeroExtended(i.OutputRegister());
866 ASSEMBLE_MOVX(movzxbl);
867 __ AssertZeroExtended(i.OutputRegister());
871 Operand operand = i.MemoryOperand(&index);
872 if (HasImmediateInput(instr, index)) {
873 __ movb(operand, Immediate(i.InputInt8(index)));
875 __ movb(operand, i.InputRegister(index));
880 ASSEMBLE_MOVX(movsxwl);
881 __ AssertZeroExtended(i.OutputRegister());
884 ASSEMBLE_MOVX(movzxwl);
885 __ AssertZeroExtended(i.OutputRegister());
889 Operand operand = i.MemoryOperand(&index);
890 if (HasImmediateInput(instr, index)) {
891 __ movw(operand, Immediate(i.InputInt16(index)));
893 __ movw(operand, i.InputRegister(index));
898 if (instr->HasOutput()) {
899 if (instr->addressing_mode() == kMode_None) {
900 if (instr->InputAt(0)->IsRegister()) {
901 __ movl(i.OutputRegister(), i.InputRegister(0));
903 __ movl(i.OutputRegister(), i.InputOperand(0));
906 __ movl(i.OutputRegister(), i.MemoryOperand());
908 __ AssertZeroExtended(i.OutputRegister());
911 Operand operand = i.MemoryOperand(&index);
912 if (HasImmediateInput(instr, index)) {
913 __ movl(operand, i.InputImmediate(index));
915 __ movl(operand, i.InputRegister(index));
920 ASSEMBLE_MOVX(movsxlq);
923 if (instr->HasOutput()) {
924 __ movq(i.OutputRegister(), i.MemoryOperand());
927 Operand operand = i.MemoryOperand(&index);
928 if (HasImmediateInput(instr, index)) {
929 __ movq(operand, i.InputImmediate(index));
931 __ movq(operand, i.InputRegister(index));
936 if (instr->HasOutput()) {
937 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
940 Operand operand = i.MemoryOperand(&index);
941 __ movss(operand, i.InputDoubleRegister(index));
945 if (instr->HasOutput()) {
946 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
949 Operand operand = i.MemoryOperand(&index);
950 __ movsd(operand, i.InputDoubleRegister(index));
954 AddressingMode mode = AddressingModeField::decode(instr->opcode());
955 // Shorten "leal" to "addl", "subl" or "shll" if the register allocation
956 // and addressing mode just happens to work out. The "addl"/"subl" forms
957 // in these cases are faster based on measurements.
958 if (i.InputRegister(0).is(i.OutputRegister())) {
959 if (mode == kMode_MRI) {
960 int32_t constant_summand = i.InputInt32(1);
961 if (constant_summand > 0) {
962 __ addl(i.OutputRegister(), Immediate(constant_summand));
963 } else if (constant_summand < 0) {
964 __ subl(i.OutputRegister(), Immediate(-constant_summand));
966 } else if (mode == kMode_MR1) {
967 if (i.InputRegister(1).is(i.OutputRegister())) {
968 __ shll(i.OutputRegister(), Immediate(1));
970 __ leal(i.OutputRegister(), i.MemoryOperand());
972 } else if (mode == kMode_M2) {
973 __ shll(i.OutputRegister(), Immediate(1));
974 } else if (mode == kMode_M4) {
975 __ shll(i.OutputRegister(), Immediate(2));
976 } else if (mode == kMode_M8) {
977 __ shll(i.OutputRegister(), Immediate(3));
979 __ leal(i.OutputRegister(), i.MemoryOperand());
982 __ leal(i.OutputRegister(), i.MemoryOperand());
984 __ AssertZeroExtended(i.OutputRegister());
988 __ leaq(i.OutputRegister(), i.MemoryOperand());
991 __ decl(i.OutputRegister());
994 __ incl(i.OutputRegister());
997 if (HasImmediateInput(instr, 0)) {
998 __ pushq(i.InputImmediate(0));
1000 if (instr->InputAt(0)->IsRegister()) {
1001 __ pushq(i.InputRegister(0));
1003 __ pushq(i.InputOperand(0));
1007 case kX64StoreWriteBarrier: {
1008 Register object = i.InputRegister(0);
1009 Register index = i.InputRegister(1);
1010 Register value = i.InputRegister(2);
1011 __ movq(Operand(object, index, times_1, 0), value);
1012 __ leaq(index, Operand(object, index, times_1, 0));
1013 SaveFPRegsMode mode =
1014 frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
1015 __ RecordWrite(object, index, value, mode);
1018 case kCheckedLoadInt8:
1019 ASSEMBLE_CHECKED_LOAD_INTEGER(movsxbl);
1021 case kCheckedLoadUint8:
1022 ASSEMBLE_CHECKED_LOAD_INTEGER(movzxbl);
1024 case kCheckedLoadInt16:
1025 ASSEMBLE_CHECKED_LOAD_INTEGER(movsxwl);
1027 case kCheckedLoadUint16:
1028 ASSEMBLE_CHECKED_LOAD_INTEGER(movzxwl);
1030 case kCheckedLoadWord32:
1031 ASSEMBLE_CHECKED_LOAD_INTEGER(movl);
1033 case kCheckedLoadFloat32:
1034 ASSEMBLE_CHECKED_LOAD_FLOAT(movss);
1036 case kCheckedLoadFloat64:
1037 ASSEMBLE_CHECKED_LOAD_FLOAT(movsd);
1039 case kCheckedStoreWord8:
1040 ASSEMBLE_CHECKED_STORE_INTEGER(movb);
1042 case kCheckedStoreWord16:
1043 ASSEMBLE_CHECKED_STORE_INTEGER(movw);
1045 case kCheckedStoreWord32:
1046 ASSEMBLE_CHECKED_STORE_INTEGER(movl);
1048 case kCheckedStoreFloat32:
1049 ASSEMBLE_CHECKED_STORE_FLOAT(movss);
1051 case kCheckedStoreFloat64:
1052 ASSEMBLE_CHECKED_STORE_FLOAT(movsd);
1055 } // NOLINT(readability/fn_size)
1058 // Assembles branches after this instruction.
1059 void CodeGenerator::AssembleArchBranch(Instruction* instr, BranchInfo* branch) {
1060 X64OperandConverter i(this, instr);
1061 Label::Distance flabel_distance =
1062 branch->fallthru ? Label::kNear : Label::kFar;
1063 Label* tlabel = branch->true_label;
1064 Label* flabel = branch->false_label;
1065 switch (branch->condition) {
1066 case kUnorderedEqual:
1067 __ j(parity_even, flabel, flabel_distance);
1070 __ j(equal, tlabel);
1072 case kUnorderedNotEqual:
1073 __ j(parity_even, tlabel);
1076 __ j(not_equal, tlabel);
1078 case kSignedLessThan:
1081 case kSignedGreaterThanOrEqual:
1082 __ j(greater_equal, tlabel);
1084 case kSignedLessThanOrEqual:
1085 __ j(less_equal, tlabel);
1087 case kSignedGreaterThan:
1088 __ j(greater, tlabel);
1090 case kUnsignedLessThan:
1091 __ j(below, tlabel);
1093 case kUnsignedGreaterThanOrEqual:
1094 __ j(above_equal, tlabel);
1096 case kUnsignedLessThanOrEqual:
1097 __ j(below_equal, tlabel);
1099 case kUnsignedGreaterThan:
1100 __ j(above, tlabel);
1103 __ j(overflow, tlabel);
1106 __ j(no_overflow, tlabel);
1109 if (!branch->fallthru) __ jmp(flabel, flabel_distance);
1113 void CodeGenerator::AssembleArchJump(RpoNumber target) {
1114 if (!IsNextInAssemblyOrder(target)) __ jmp(GetLabel(target));
1118 // Assembles boolean materializations after this instruction.
1119 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
1120 FlagsCondition condition) {
1121 X64OperandConverter i(this, instr);
1124 // Materialize a full 64-bit 1 or 0 value. The result register is always the
1125 // last output of the instruction.
1127 DCHECK_NE(0u, instr->OutputCount());
1128 Register reg = i.OutputRegister(instr->OutputCount() - 1);
1129 Condition cc = no_condition;
1130 switch (condition) {
1131 case kUnorderedEqual:
1132 __ j(parity_odd, &check, Label::kNear);
1133 __ movl(reg, Immediate(0));
1134 __ jmp(&done, Label::kNear);
1139 case kUnorderedNotEqual:
1140 __ j(parity_odd, &check, Label::kNear);
1141 __ movl(reg, Immediate(1));
1142 __ jmp(&done, Label::kNear);
1147 case kSignedLessThan:
1150 case kSignedGreaterThanOrEqual:
1153 case kSignedLessThanOrEqual:
1156 case kSignedGreaterThan:
1159 case kUnsignedLessThan:
1162 case kUnsignedGreaterThanOrEqual:
1165 case kUnsignedLessThanOrEqual:
1168 case kUnsignedGreaterThan:
1180 __ movzxbl(reg, reg);
1185 void CodeGenerator::AssembleArchLookupSwitch(Instruction* instr) {
1186 X64OperandConverter i(this, instr);
1187 Register input = i.InputRegister(0);
1188 for (size_t index = 2; index < instr->InputCount(); index += 2) {
1189 __ cmpl(input, Immediate(i.InputInt32(index + 0)));
1190 __ j(equal, GetLabel(i.InputRpo(index + 1)));
1192 AssembleArchJump(i.InputRpo(1));
1196 void CodeGenerator::AssembleArchTableSwitch(Instruction* instr) {
1197 X64OperandConverter i(this, instr);
1198 Register input = i.InputRegister(0);
1199 int32_t const case_count = static_cast<int32_t>(instr->InputCount() - 2);
1200 Label** cases = zone()->NewArray<Label*>(case_count);
1201 for (int32_t index = 0; index < case_count; ++index) {
1202 cases[index] = GetLabel(i.InputRpo(index + 2));
1204 Label* const table = AddJumpTable(cases, case_count);
1205 __ cmpl(input, Immediate(case_count));
1206 __ j(above_equal, GetLabel(i.InputRpo(1)));
1207 __ leaq(kScratchRegister, Operand(table));
1208 __ jmp(Operand(kScratchRegister, input, times_8, 0));
1212 void CodeGenerator::AssembleDeoptimizerCall(
1213 int deoptimization_id, Deoptimizer::BailoutType bailout_type) {
1214 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
1215 isolate(), deoptimization_id, bailout_type);
1216 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
1220 void CodeGenerator::AssemblePrologue() {
1221 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1222 int stack_slots = frame()->GetSpillSlotCount();
1223 if (descriptor->kind() == CallDescriptor::kCallAddress) {
1226 const RegList saves = descriptor->CalleeSavedRegisters();
1227 if (saves != 0) { // Save callee-saved registers.
1228 int register_save_area_size = 0;
1229 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
1230 if (!((1 << i) & saves)) continue;
1231 __ pushq(Register::from_code(i));
1232 register_save_area_size += kPointerSize;
1234 frame()->SetRegisterSaveAreaSize(register_save_area_size);
1236 } else if (descriptor->IsJSFunctionCall()) {
1237 CompilationInfo* info = this->info();
1238 __ Prologue(info->IsCodePreAgingActive());
1239 frame()->SetRegisterSaveAreaSize(
1240 StandardFrameConstants::kFixedFrameSizeFromFp);
1241 } else if (stack_slots > 0) {
1243 frame()->SetRegisterSaveAreaSize(
1244 StandardFrameConstants::kFixedFrameSizeFromFp);
1247 if (info()->is_osr()) {
1248 // TurboFan OSR-compiled functions cannot be entered directly.
1249 __ Abort(kShouldNotDirectlyEnterOsrFunction);
1251 // Unoptimized code jumps directly to this entrypoint while the unoptimized
1252 // frame is still on the stack. Optimized code uses OSR values directly from
1253 // the unoptimized frame. Thus, all that needs to be done is to allocate the
1254 // remaining stack slots.
1255 if (FLAG_code_comments) __ RecordComment("-- OSR entrypoint --");
1256 osr_pc_offset_ = __ pc_offset();
1257 DCHECK(stack_slots >= frame()->GetOsrStackSlotCount());
1258 stack_slots -= frame()->GetOsrStackSlotCount();
1261 if (stack_slots > 0) {
1262 __ subq(rsp, Immediate(stack_slots * kPointerSize));
1267 void CodeGenerator::AssembleReturn() {
1268 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1269 int stack_slots = frame()->GetSpillSlotCount();
1270 if (descriptor->kind() == CallDescriptor::kCallAddress) {
1271 if (frame()->GetRegisterSaveAreaSize() > 0) {
1272 // Remove this frame's spill slots first.
1273 if (stack_slots > 0) {
1274 __ addq(rsp, Immediate(stack_slots * kPointerSize));
1276 const RegList saves = descriptor->CalleeSavedRegisters();
1277 // Restore registers.
1279 for (int i = 0; i < Register::kNumRegisters; i++) {
1280 if (!((1 << i) & saves)) continue;
1281 __ popq(Register::from_code(i));
1284 __ popq(rbp); // Pop caller's frame pointer.
1287 // No saved registers.
1288 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
1289 __ popq(rbp); // Pop caller's frame pointer.
1292 } else if (descriptor->IsJSFunctionCall() || stack_slots > 0) {
1293 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
1294 __ popq(rbp); // Pop caller's frame pointer.
1295 int pop_count = descriptor->IsJSFunctionCall()
1296 ? static_cast<int>(descriptor->JSParameterCount())
1298 __ ret(pop_count * kPointerSize);
1305 void CodeGenerator::AssembleMove(InstructionOperand* source,
1306 InstructionOperand* destination) {
1307 X64OperandConverter g(this, NULL);
1308 // Dispatch on the source and destination operand kinds. Not all
1309 // combinations are possible.
1310 if (source->IsRegister()) {
1311 DCHECK(destination->IsRegister() || destination->IsStackSlot());
1312 Register src = g.ToRegister(source);
1313 if (destination->IsRegister()) {
1314 __ movq(g.ToRegister(destination), src);
1316 __ movq(g.ToOperand(destination), src);
1318 } else if (source->IsStackSlot()) {
1319 DCHECK(destination->IsRegister() || destination->IsStackSlot());
1320 Operand src = g.ToOperand(source);
1321 if (destination->IsRegister()) {
1322 Register dst = g.ToRegister(destination);
1325 // Spill on demand to use a temporary register for memory-to-memory
1327 Register tmp = kScratchRegister;
1328 Operand dst = g.ToOperand(destination);
1332 } else if (source->IsConstant()) {
1333 ConstantOperand* constant_source = ConstantOperand::cast(source);
1334 Constant src = g.ToConstant(constant_source);
1335 if (destination->IsRegister() || destination->IsStackSlot()) {
1336 Register dst = destination->IsRegister() ? g.ToRegister(destination)
1338 switch (src.type()) {
1339 case Constant::kInt32:
1340 // TODO(dcarney): don't need scratch in this case.
1341 __ Set(dst, src.ToInt32());
1343 case Constant::kInt64:
1344 __ Set(dst, src.ToInt64());
1346 case Constant::kFloat32:
1348 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
1350 case Constant::kFloat64:
1352 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
1354 case Constant::kExternalReference:
1355 __ Move(dst, src.ToExternalReference());
1357 case Constant::kHeapObject: {
1358 Handle<HeapObject> src_object = src.ToHeapObject();
1359 if (info()->IsOptimizing() &&
1360 src_object.is_identical_to(info()->context())) {
1361 // Loading the context from the frame is way cheaper than
1362 // materializing the actual context heap object address.
1363 __ movp(dst, Operand(rbp, StandardFrameConstants::kContextOffset));
1365 __ Move(dst, src_object);
1369 case Constant::kRpoNumber:
1370 UNREACHABLE(); // TODO(dcarney): load of labels on x64.
1373 if (destination->IsStackSlot()) {
1374 __ movq(g.ToOperand(destination), kScratchRegister);
1376 } else if (src.type() == Constant::kFloat32) {
1377 // TODO(turbofan): Can we do better here?
1378 uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
1379 if (destination->IsDoubleRegister()) {
1380 __ Move(g.ToDoubleRegister(destination), src_const);
1382 DCHECK(destination->IsDoubleStackSlot());
1383 Operand dst = g.ToOperand(destination);
1384 __ movl(dst, Immediate(src_const));
1387 DCHECK_EQ(Constant::kFloat64, src.type());
1388 uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
1389 if (destination->IsDoubleRegister()) {
1390 __ Move(g.ToDoubleRegister(destination), src_const);
1392 DCHECK(destination->IsDoubleStackSlot());
1393 __ movq(kScratchRegister, src_const);
1394 __ movq(g.ToOperand(destination), kScratchRegister);
1397 } else if (source->IsDoubleRegister()) {
1398 XMMRegister src = g.ToDoubleRegister(source);
1399 if (destination->IsDoubleRegister()) {
1400 XMMRegister dst = g.ToDoubleRegister(destination);
1401 __ movaps(dst, src);
1403 DCHECK(destination->IsDoubleStackSlot());
1404 Operand dst = g.ToOperand(destination);
1407 } else if (source->IsDoubleStackSlot()) {
1408 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
1409 Operand src = g.ToOperand(source);
1410 if (destination->IsDoubleRegister()) {
1411 XMMRegister dst = g.ToDoubleRegister(destination);
1414 // We rely on having xmm0 available as a fixed scratch register.
1415 Operand dst = g.ToOperand(destination);
1416 __ movsd(xmm0, src);
1417 __ movsd(dst, xmm0);
1425 void CodeGenerator::AssembleSwap(InstructionOperand* source,
1426 InstructionOperand* destination) {
1427 X64OperandConverter g(this, NULL);
1428 // Dispatch on the source and destination operand kinds. Not all
1429 // combinations are possible.
1430 if (source->IsRegister() && destination->IsRegister()) {
1431 // Register-register.
1432 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
1433 } else if (source->IsRegister() && destination->IsStackSlot()) {
1434 Register src = g.ToRegister(source);
1435 Operand dst = g.ToOperand(destination);
1437 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
1438 (source->IsDoubleStackSlot() &&
1439 destination->IsDoubleStackSlot())) {
1441 Register tmp = kScratchRegister;
1442 Operand src = g.ToOperand(source);
1443 Operand dst = g.ToOperand(destination);
1447 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
1448 // XMM register-register swap. We rely on having xmm0
1449 // available as a fixed scratch register.
1450 XMMRegister src = g.ToDoubleRegister(source);
1451 XMMRegister dst = g.ToDoubleRegister(destination);
1452 __ movaps(xmm0, src);
1453 __ movaps(src, dst);
1454 __ movaps(dst, xmm0);
1455 } else if (source->IsDoubleRegister() && destination->IsDoubleStackSlot()) {
1456 // XMM register-memory swap. We rely on having xmm0
1457 // available as a fixed scratch register.
1458 XMMRegister src = g.ToDoubleRegister(source);
1459 Operand dst = g.ToOperand(destination);
1460 __ movsd(xmm0, src);
1462 __ movsd(dst, xmm0);
1464 // No other combinations are possible.
1470 void CodeGenerator::AssembleJumpTable(Label** targets, size_t target_count) {
1471 for (size_t index = 0; index < target_count; ++index) {
1472 __ dq(targets[index]);
1477 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1480 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1481 int space_needed = Deoptimizer::patch_size();
1482 if (!info()->IsStub()) {
1483 // Ensure that we have enough space after the previous lazy-bailout
1484 // instruction for patching the code here.
1485 int current_pc = masm()->pc_offset();
1486 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1487 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1488 __ Nop(padding_size);
1491 MarkLazyDeoptSite();
1496 } // namespace internal
1497 } // namespace compiler