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_SSE_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_SSE_UNOP(asm_instr) \
280 if (instr->InputAt(0)->IsDoubleRegister()) { \
281 __ asm_instr(i.OutputDoubleRegister(), i.InputDoubleRegister(0)); \
283 __ asm_instr(i.OutputDoubleRegister(), i.InputOperand(0)); \
288 #define ASSEMBLE_AVX_BINOP(asm_instr) \
290 CpuFeatureScope avx_scope(masm(), AVX); \
291 if (instr->InputAt(1)->IsDoubleRegister()) { \
292 __ asm_instr(i.OutputDoubleRegister(), i.InputDoubleRegister(0), \
293 i.InputDoubleRegister(1)); \
295 __ asm_instr(i.OutputDoubleRegister(), i.InputDoubleRegister(0), \
296 i.InputOperand(1)); \
301 #define ASSEMBLE_CHECKED_LOAD_FLOAT(asm_instr) \
303 auto result = i.OutputDoubleRegister(); \
304 auto buffer = i.InputRegister(0); \
305 auto index1 = i.InputRegister(1); \
306 auto index2 = i.InputInt32(2); \
307 OutOfLineCode* ool; \
308 if (instr->InputAt(3)->IsRegister()) { \
309 auto length = i.InputRegister(3); \
310 DCHECK_EQ(0, index2); \
311 __ cmpl(index1, length); \
312 ool = new (zone()) OutOfLineLoadNaN(this, result); \
314 auto length = i.InputInt32(3); \
315 DCHECK_LE(index2, length); \
316 __ cmpq(index1, Immediate(length - index2)); \
317 class OutOfLineLoadFloat FINAL : public OutOfLineCode { \
319 OutOfLineLoadFloat(CodeGenerator* gen, XMMRegister result, \
320 Register buffer, Register index1, int32_t index2, \
322 : OutOfLineCode(gen), \
329 void Generate() FINAL { \
330 __ leal(kScratchRegister, Operand(index1_, index2_)); \
331 __ pcmpeqd(result_, result_); \
332 __ cmpl(kScratchRegister, Immediate(length_)); \
333 __ j(above_equal, exit()); \
334 __ asm_instr(result_, \
335 Operand(buffer_, kScratchRegister, times_1, 0)); \
339 XMMRegister const result_; \
340 Register const buffer_; \
341 Register const index1_; \
342 int32_t const index2_; \
343 int32_t const length_; \
346 OutOfLineLoadFloat(this, result, buffer, index1, index2, length); \
348 __ j(above_equal, ool->entry()); \
349 __ asm_instr(result, Operand(buffer, index1, times_1, index2)); \
350 __ bind(ool->exit()); \
354 #define ASSEMBLE_CHECKED_LOAD_INTEGER(asm_instr) \
356 auto result = i.OutputRegister(); \
357 auto buffer = i.InputRegister(0); \
358 auto index1 = i.InputRegister(1); \
359 auto index2 = i.InputInt32(2); \
360 OutOfLineCode* ool; \
361 if (instr->InputAt(3)->IsRegister()) { \
362 auto length = i.InputRegister(3); \
363 DCHECK_EQ(0, index2); \
364 __ cmpl(index1, length); \
365 ool = new (zone()) OutOfLineLoadZero(this, result); \
367 auto length = i.InputInt32(3); \
368 DCHECK_LE(index2, length); \
369 __ cmpq(index1, Immediate(length - index2)); \
370 class OutOfLineLoadInteger FINAL : public OutOfLineCode { \
372 OutOfLineLoadInteger(CodeGenerator* gen, Register result, \
373 Register buffer, Register index1, int32_t index2, \
375 : OutOfLineCode(gen), \
382 void Generate() FINAL { \
384 __ leal(kScratchRegister, Operand(index1_, index2_)); \
385 __ cmpl(kScratchRegister, Immediate(length_)); \
386 __ j(above_equal, &oob, Label::kNear); \
387 __ asm_instr(result_, \
388 Operand(buffer_, kScratchRegister, times_1, 0)); \
391 __ xorl(result_, result_); \
395 Register const result_; \
396 Register const buffer_; \
397 Register const index1_; \
398 int32_t const index2_; \
399 int32_t const length_; \
402 OutOfLineLoadInteger(this, result, buffer, index1, index2, length); \
404 __ j(above_equal, ool->entry()); \
405 __ asm_instr(result, Operand(buffer, index1, times_1, index2)); \
406 __ bind(ool->exit()); \
410 #define ASSEMBLE_CHECKED_STORE_FLOAT(asm_instr) \
412 auto buffer = i.InputRegister(0); \
413 auto index1 = i.InputRegister(1); \
414 auto index2 = i.InputInt32(2); \
415 auto value = i.InputDoubleRegister(4); \
416 if (instr->InputAt(3)->IsRegister()) { \
417 auto length = i.InputRegister(3); \
418 DCHECK_EQ(0, index2); \
420 __ cmpl(index1, length); \
421 __ j(above_equal, &done, Label::kNear); \
422 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
425 auto length = i.InputInt32(3); \
426 DCHECK_LE(index2, length); \
427 __ cmpq(index1, Immediate(length - index2)); \
428 class OutOfLineStoreFloat FINAL : public OutOfLineCode { \
430 OutOfLineStoreFloat(CodeGenerator* gen, Register buffer, \
431 Register index1, int32_t index2, int32_t length, \
433 : OutOfLineCode(gen), \
440 void Generate() FINAL { \
441 __ leal(kScratchRegister, Operand(index1_, index2_)); \
442 __ cmpl(kScratchRegister, Immediate(length_)); \
443 __ j(above_equal, exit()); \
444 __ asm_instr(Operand(buffer_, kScratchRegister, times_1, 0), \
449 Register const buffer_; \
450 Register const index1_; \
451 int32_t const index2_; \
452 int32_t const length_; \
453 XMMRegister const value_; \
455 auto ool = new (zone()) \
456 OutOfLineStoreFloat(this, buffer, index1, index2, length, value); \
457 __ j(above_equal, ool->entry()); \
458 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
459 __ bind(ool->exit()); \
464 #define ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Value) \
466 auto buffer = i.InputRegister(0); \
467 auto index1 = i.InputRegister(1); \
468 auto index2 = i.InputInt32(2); \
469 if (instr->InputAt(3)->IsRegister()) { \
470 auto length = i.InputRegister(3); \
471 DCHECK_EQ(0, index2); \
473 __ cmpl(index1, length); \
474 __ j(above_equal, &done, Label::kNear); \
475 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
478 auto length = i.InputInt32(3); \
479 DCHECK_LE(index2, length); \
480 __ cmpq(index1, Immediate(length - index2)); \
481 class OutOfLineStoreInteger FINAL : public OutOfLineCode { \
483 OutOfLineStoreInteger(CodeGenerator* gen, Register buffer, \
484 Register index1, int32_t index2, int32_t length, \
486 : OutOfLineCode(gen), \
493 void Generate() FINAL { \
494 __ leal(kScratchRegister, Operand(index1_, index2_)); \
495 __ cmpl(kScratchRegister, Immediate(length_)); \
496 __ j(above_equal, exit()); \
497 __ asm_instr(Operand(buffer_, kScratchRegister, times_1, 0), \
502 Register const buffer_; \
503 Register const index1_; \
504 int32_t const index2_; \
505 int32_t const length_; \
506 Value const value_; \
508 auto ool = new (zone()) \
509 OutOfLineStoreInteger(this, buffer, index1, index2, length, value); \
510 __ j(above_equal, ool->entry()); \
511 __ asm_instr(Operand(buffer, index1, times_1, index2), value); \
512 __ bind(ool->exit()); \
517 #define ASSEMBLE_CHECKED_STORE_INTEGER(asm_instr) \
519 if (instr->InputAt(4)->IsRegister()) { \
520 Register value = i.InputRegister(4); \
521 ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Register); \
523 Immediate value = i.InputImmediate(4); \
524 ASSEMBLE_CHECKED_STORE_INTEGER_IMPL(asm_instr, Immediate); \
529 // Assembles an instruction after register allocation, producing machine code.
530 void CodeGenerator::AssembleArchInstruction(Instruction* instr) {
531 X64OperandConverter i(this, instr);
533 switch (ArchOpcodeField::decode(instr->opcode())) {
534 case kArchCallCodeObject: {
535 EnsureSpaceForLazyDeopt();
536 if (HasImmediateInput(instr, 0)) {
537 Handle<Code> code = Handle<Code>::cast(i.InputHeapObject(0));
538 __ Call(code, RelocInfo::CODE_TARGET);
540 Register reg = i.InputRegister(0);
541 int entry = Code::kHeaderSize - kHeapObjectTag;
542 __ Call(Operand(reg, entry));
544 RecordCallPosition(instr);
547 case kArchCallJSFunction: {
548 EnsureSpaceForLazyDeopt();
549 Register func = i.InputRegister(0);
550 if (FLAG_debug_code) {
551 // Check the function's context matches the context argument.
552 __ cmpp(rsi, FieldOperand(func, JSFunction::kContextOffset));
553 __ Assert(equal, kWrongFunctionContext);
555 __ Call(FieldOperand(func, JSFunction::kCodeEntryOffset));
556 RecordCallPosition(instr);
560 AssembleArchJump(i.InputRpo(0));
562 case kArchLookupSwitch:
563 AssembleArchLookupSwitch(instr);
565 case kArchTableSwitch:
566 AssembleArchTableSwitch(instr);
569 // don't emit code for nops.
571 case kArchDeoptimize: {
573 BuildTranslation(instr, -1, 0, OutputFrameStateCombine::Ignore());
574 AssembleDeoptimizerCall(deopt_state_id, Deoptimizer::EAGER);
580 case kArchStackPointer:
581 __ movq(i.OutputRegister(), rsp);
583 case kArchTruncateDoubleToI: {
584 auto result = i.OutputRegister();
585 auto input = i.InputDoubleRegister(0);
586 auto ool = new (zone()) OutOfLineTruncateDoubleToI(this, result, input);
587 __ cvttsd2siq(result, input);
588 __ cmpq(result, Immediate(1));
589 __ j(overflow, ool->entry());
590 __ bind(ool->exit());
594 ASSEMBLE_BINOP(addl);
597 ASSEMBLE_BINOP(addq);
600 ASSEMBLE_BINOP(subl);
603 ASSEMBLE_BINOP(subq);
606 ASSEMBLE_BINOP(andl);
609 ASSEMBLE_BINOP(andq);
612 ASSEMBLE_BINOP(cmpl);
615 ASSEMBLE_BINOP(cmpq);
618 ASSEMBLE_BINOP(testl);
621 ASSEMBLE_BINOP(testq);
624 ASSEMBLE_MULT(imull);
627 ASSEMBLE_MULT(imulq);
630 if (instr->InputAt(1)->IsRegister()) {
631 __ imull(i.InputRegister(1));
633 __ imull(i.InputOperand(1));
637 if (instr->InputAt(1)->IsRegister()) {
638 __ mull(i.InputRegister(1));
640 __ mull(i.InputOperand(1));
645 __ idivl(i.InputRegister(1));
649 __ idivq(i.InputRegister(1));
653 __ divl(i.InputRegister(1));
657 __ divq(i.InputRegister(1));
678 ASSEMBLE_BINOP(xorl);
681 ASSEMBLE_BINOP(xorq);
684 ASSEMBLE_SHIFT(shll, 5);
687 ASSEMBLE_SHIFT(shlq, 6);
690 ASSEMBLE_SHIFT(shrl, 5);
693 ASSEMBLE_SHIFT(shrq, 6);
696 ASSEMBLE_SHIFT(sarl, 5);
699 ASSEMBLE_SHIFT(sarq, 6);
702 ASSEMBLE_SHIFT(rorl, 5);
705 ASSEMBLE_SHIFT(rorq, 6);
708 if (instr->InputAt(0)->IsRegister()) {
709 __ Lzcntl(i.OutputRegister(), i.InputRegister(0));
711 __ Lzcntl(i.OutputRegister(), i.InputOperand(0));
715 ASSEMBLE_SSE_BINOP(ucomiss);
718 ASSEMBLE_SSE_BINOP(addss);
721 ASSEMBLE_SSE_BINOP(subss);
724 ASSEMBLE_SSE_BINOP(mulss);
727 ASSEMBLE_SSE_BINOP(divss);
729 case kSSEFloat32Neg: {
730 // TODO(bmeurer): Use RIP relative 128-bit constants.
731 // TODO(turbofan): Add AVX version with relaxed register constraints.
732 __ pcmpeqd(kScratchDoubleReg, kScratchDoubleReg);
733 __ psllq(kScratchDoubleReg, 31);
734 __ xorps(i.OutputDoubleRegister(), kScratchDoubleReg);
737 case kSSEFloat32Sqrt:
738 ASSEMBLE_SSE_UNOP(sqrtss);
741 ASSEMBLE_SSE_BINOP(maxss);
744 ASSEMBLE_SSE_BINOP(minss);
746 case kSSEFloat32ToFloat64:
747 ASSEMBLE_SSE_UNOP(cvtss2sd);
750 ASSEMBLE_SSE_BINOP(ucomisd);
753 ASSEMBLE_SSE_BINOP(addsd);
756 ASSEMBLE_SSE_BINOP(subsd);
759 ASSEMBLE_SSE_BINOP(mulsd);
762 ASSEMBLE_SSE_BINOP(divsd);
764 case kSSEFloat64Mod: {
765 __ subq(rsp, Immediate(kDoubleSize));
766 // Move values to st(0) and st(1).
767 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(1));
768 __ fld_d(Operand(rsp, 0));
769 __ movsd(Operand(rsp, 0), i.InputDoubleRegister(0));
770 __ fld_d(Operand(rsp, 0));
771 // Loop while fprem isn't done.
774 // This instructions traps on all kinds inputs, but we are assuming the
775 // floating point control word is set to ignore them all.
777 // The following 2 instruction implicitly use rax.
779 if (CpuFeatures::IsSupported(SAHF)) {
780 CpuFeatureScope sahf_scope(masm(), SAHF);
783 __ shrl(rax, Immediate(8));
784 __ andl(rax, Immediate(0xFF));
788 __ j(parity_even, &mod_loop);
789 // Move output to stack and clean up.
791 __ fstp_d(Operand(rsp, 0));
792 __ movsd(i.OutputDoubleRegister(), Operand(rsp, 0));
793 __ addq(rsp, Immediate(kDoubleSize));
797 ASSEMBLE_SSE_BINOP(maxsd);
800 ASSEMBLE_SSE_BINOP(minsd);
802 case kSSEFloat64Neg: {
803 // TODO(bmeurer): Use RIP relative 128-bit constants.
804 // TODO(turbofan): Add AVX version with relaxed register constraints.
805 __ pcmpeqd(kScratchDoubleReg, kScratchDoubleReg);
806 __ psllq(kScratchDoubleReg, 63);
807 __ xorpd(i.OutputDoubleRegister(), kScratchDoubleReg);
810 case kSSEFloat64Sqrt:
811 ASSEMBLE_SSE_UNOP(sqrtsd);
813 case kSSEFloat64Round: {
814 CpuFeatureScope sse_scope(masm(), SSE4_1);
815 RoundingMode const mode =
816 static_cast<RoundingMode>(MiscField::decode(instr->opcode()));
817 __ roundsd(i.OutputDoubleRegister(), i.InputDoubleRegister(0), mode);
820 case kSSEFloat64ToFloat32:
821 ASSEMBLE_SSE_UNOP(cvtsd2ss);
823 case kSSEFloat64ToInt32:
824 if (instr->InputAt(0)->IsDoubleRegister()) {
825 __ cvttsd2si(i.OutputRegister(), i.InputDoubleRegister(0));
827 __ cvttsd2si(i.OutputRegister(), i.InputOperand(0));
830 case kSSEFloat64ToUint32: {
831 if (instr->InputAt(0)->IsDoubleRegister()) {
832 __ cvttsd2siq(i.OutputRegister(), i.InputDoubleRegister(0));
834 __ cvttsd2siq(i.OutputRegister(), i.InputOperand(0));
836 __ AssertZeroExtended(i.OutputRegister());
839 case kSSEInt32ToFloat64:
840 if (instr->InputAt(0)->IsRegister()) {
841 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputRegister(0));
843 __ cvtlsi2sd(i.OutputDoubleRegister(), i.InputOperand(0));
846 case kSSEUint32ToFloat64:
847 if (instr->InputAt(0)->IsRegister()) {
848 __ movl(kScratchRegister, i.InputRegister(0));
850 __ movl(kScratchRegister, i.InputOperand(0));
852 __ cvtqsi2sd(i.OutputDoubleRegister(), kScratchRegister);
854 case kSSEFloat64ExtractLowWord32:
855 if (instr->InputAt(0)->IsDoubleStackSlot()) {
856 __ movl(i.OutputRegister(), i.InputOperand(0));
858 __ movd(i.OutputRegister(), i.InputDoubleRegister(0));
861 case kSSEFloat64ExtractHighWord32:
862 if (instr->InputAt(0)->IsDoubleStackSlot()) {
863 __ movl(i.OutputRegister(), i.InputOperand(0, kDoubleSize / 2));
865 __ Pextrd(i.OutputRegister(), i.InputDoubleRegister(0), 1);
868 case kSSEFloat64InsertLowWord32:
869 if (instr->InputAt(1)->IsRegister()) {
870 __ Pinsrd(i.OutputDoubleRegister(), i.InputRegister(1), 0);
872 __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 0);
875 case kSSEFloat64InsertHighWord32:
876 if (instr->InputAt(1)->IsRegister()) {
877 __ Pinsrd(i.OutputDoubleRegister(), i.InputRegister(1), 1);
879 __ Pinsrd(i.OutputDoubleRegister(), i.InputOperand(1), 1);
882 case kSSEFloat64LoadLowWord32:
883 if (instr->InputAt(0)->IsRegister()) {
884 __ movd(i.OutputDoubleRegister(), i.InputRegister(0));
886 __ movd(i.OutputDoubleRegister(), i.InputOperand(0));
889 case kAVXFloat32Cmp: {
890 CpuFeatureScope avx_scope(masm(), AVX);
891 if (instr->InputAt(1)->IsDoubleRegister()) {
892 __ vucomiss(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
894 __ vucomiss(i.InputDoubleRegister(0), i.InputOperand(1));
899 ASSEMBLE_AVX_BINOP(vaddss);
902 ASSEMBLE_AVX_BINOP(vsubss);
905 ASSEMBLE_AVX_BINOP(vmulss);
908 ASSEMBLE_AVX_BINOP(vdivss);
911 ASSEMBLE_AVX_BINOP(vmaxss);
914 ASSEMBLE_AVX_BINOP(vminss);
916 case kAVXFloat64Cmp: {
917 CpuFeatureScope avx_scope(masm(), AVX);
918 if (instr->InputAt(1)->IsDoubleRegister()) {
919 __ vucomisd(i.InputDoubleRegister(0), i.InputDoubleRegister(1));
921 __ vucomisd(i.InputDoubleRegister(0), i.InputOperand(1));
926 ASSEMBLE_AVX_BINOP(vaddsd);
929 ASSEMBLE_AVX_BINOP(vsubsd);
932 ASSEMBLE_AVX_BINOP(vmulsd);
935 ASSEMBLE_AVX_BINOP(vdivsd);
938 ASSEMBLE_AVX_BINOP(vmaxsd);
941 ASSEMBLE_AVX_BINOP(vminsd);
944 ASSEMBLE_MOVX(movsxbl);
945 __ AssertZeroExtended(i.OutputRegister());
948 ASSEMBLE_MOVX(movzxbl);
949 __ AssertZeroExtended(i.OutputRegister());
953 Operand operand = i.MemoryOperand(&index);
954 if (HasImmediateInput(instr, index)) {
955 __ movb(operand, Immediate(i.InputInt8(index)));
957 __ movb(operand, i.InputRegister(index));
962 ASSEMBLE_MOVX(movsxwl);
963 __ AssertZeroExtended(i.OutputRegister());
966 ASSEMBLE_MOVX(movzxwl);
967 __ AssertZeroExtended(i.OutputRegister());
971 Operand operand = i.MemoryOperand(&index);
972 if (HasImmediateInput(instr, index)) {
973 __ movw(operand, Immediate(i.InputInt16(index)));
975 __ movw(operand, i.InputRegister(index));
980 if (instr->HasOutput()) {
981 if (instr->addressing_mode() == kMode_None) {
982 if (instr->InputAt(0)->IsRegister()) {
983 __ movl(i.OutputRegister(), i.InputRegister(0));
985 __ movl(i.OutputRegister(), i.InputOperand(0));
988 __ movl(i.OutputRegister(), i.MemoryOperand());
990 __ AssertZeroExtended(i.OutputRegister());
993 Operand operand = i.MemoryOperand(&index);
994 if (HasImmediateInput(instr, index)) {
995 __ movl(operand, i.InputImmediate(index));
997 __ movl(operand, i.InputRegister(index));
1002 ASSEMBLE_MOVX(movsxlq);
1005 if (instr->HasOutput()) {
1006 __ movq(i.OutputRegister(), i.MemoryOperand());
1009 Operand operand = i.MemoryOperand(&index);
1010 if (HasImmediateInput(instr, index)) {
1011 __ movq(operand, i.InputImmediate(index));
1013 __ movq(operand, i.InputRegister(index));
1018 if (instr->HasOutput()) {
1019 __ movss(i.OutputDoubleRegister(), i.MemoryOperand());
1022 Operand operand = i.MemoryOperand(&index);
1023 __ movss(operand, i.InputDoubleRegister(index));
1027 if (instr->HasOutput()) {
1028 __ movsd(i.OutputDoubleRegister(), i.MemoryOperand());
1031 Operand operand = i.MemoryOperand(&index);
1032 __ movsd(operand, i.InputDoubleRegister(index));
1036 AddressingMode mode = AddressingModeField::decode(instr->opcode());
1037 // Shorten "leal" to "addl", "subl" or "shll" if the register allocation
1038 // and addressing mode just happens to work out. The "addl"/"subl" forms
1039 // in these cases are faster based on measurements.
1040 if (i.InputRegister(0).is(i.OutputRegister())) {
1041 if (mode == kMode_MRI) {
1042 int32_t constant_summand = i.InputInt32(1);
1043 if (constant_summand > 0) {
1044 __ addl(i.OutputRegister(), Immediate(constant_summand));
1045 } else if (constant_summand < 0) {
1046 __ subl(i.OutputRegister(), Immediate(-constant_summand));
1048 } else if (mode == kMode_MR1) {
1049 if (i.InputRegister(1).is(i.OutputRegister())) {
1050 __ shll(i.OutputRegister(), Immediate(1));
1052 __ leal(i.OutputRegister(), i.MemoryOperand());
1054 } else if (mode == kMode_M2) {
1055 __ shll(i.OutputRegister(), Immediate(1));
1056 } else if (mode == kMode_M4) {
1057 __ shll(i.OutputRegister(), Immediate(2));
1058 } else if (mode == kMode_M8) {
1059 __ shll(i.OutputRegister(), Immediate(3));
1061 __ leal(i.OutputRegister(), i.MemoryOperand());
1064 __ leal(i.OutputRegister(), i.MemoryOperand());
1066 __ AssertZeroExtended(i.OutputRegister());
1070 __ leaq(i.OutputRegister(), i.MemoryOperand());
1073 __ decl(i.OutputRegister());
1076 __ incl(i.OutputRegister());
1079 if (HasImmediateInput(instr, 0)) {
1080 __ pushq(i.InputImmediate(0));
1082 if (instr->InputAt(0)->IsRegister()) {
1083 __ pushq(i.InputRegister(0));
1085 __ pushq(i.InputOperand(0));
1089 case kX64StoreWriteBarrier: {
1090 Register object = i.InputRegister(0);
1091 Register index = i.InputRegister(1);
1092 Register value = i.InputRegister(2);
1093 __ movq(Operand(object, index, times_1, 0), value);
1094 __ leaq(index, Operand(object, index, times_1, 0));
1095 SaveFPRegsMode mode =
1096 frame()->DidAllocateDoubleRegisters() ? kSaveFPRegs : kDontSaveFPRegs;
1097 __ RecordWrite(object, index, value, mode);
1100 case kCheckedLoadInt8:
1101 ASSEMBLE_CHECKED_LOAD_INTEGER(movsxbl);
1103 case kCheckedLoadUint8:
1104 ASSEMBLE_CHECKED_LOAD_INTEGER(movzxbl);
1106 case kCheckedLoadInt16:
1107 ASSEMBLE_CHECKED_LOAD_INTEGER(movsxwl);
1109 case kCheckedLoadUint16:
1110 ASSEMBLE_CHECKED_LOAD_INTEGER(movzxwl);
1112 case kCheckedLoadWord32:
1113 ASSEMBLE_CHECKED_LOAD_INTEGER(movl);
1115 case kCheckedLoadFloat32:
1116 ASSEMBLE_CHECKED_LOAD_FLOAT(movss);
1118 case kCheckedLoadFloat64:
1119 ASSEMBLE_CHECKED_LOAD_FLOAT(movsd);
1121 case kCheckedStoreWord8:
1122 ASSEMBLE_CHECKED_STORE_INTEGER(movb);
1124 case kCheckedStoreWord16:
1125 ASSEMBLE_CHECKED_STORE_INTEGER(movw);
1127 case kCheckedStoreWord32:
1128 ASSEMBLE_CHECKED_STORE_INTEGER(movl);
1130 case kCheckedStoreFloat32:
1131 ASSEMBLE_CHECKED_STORE_FLOAT(movss);
1133 case kCheckedStoreFloat64:
1134 ASSEMBLE_CHECKED_STORE_FLOAT(movsd);
1136 case kX64StackCheck:
1137 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
1140 } // NOLINT(readability/fn_size)
1143 // Assembles branches after this instruction.
1144 void CodeGenerator::AssembleArchBranch(Instruction* instr, BranchInfo* branch) {
1145 X64OperandConverter i(this, instr);
1146 Label::Distance flabel_distance =
1147 branch->fallthru ? Label::kNear : Label::kFar;
1148 Label* tlabel = branch->true_label;
1149 Label* flabel = branch->false_label;
1150 switch (branch->condition) {
1151 case kUnorderedEqual:
1152 __ j(parity_even, flabel, flabel_distance);
1155 __ j(equal, tlabel);
1157 case kUnorderedNotEqual:
1158 __ j(parity_even, tlabel);
1161 __ j(not_equal, tlabel);
1163 case kSignedLessThan:
1166 case kSignedGreaterThanOrEqual:
1167 __ j(greater_equal, tlabel);
1169 case kSignedLessThanOrEqual:
1170 __ j(less_equal, tlabel);
1172 case kSignedGreaterThan:
1173 __ j(greater, tlabel);
1175 case kUnsignedLessThan:
1176 __ j(below, tlabel);
1178 case kUnsignedGreaterThanOrEqual:
1179 __ j(above_equal, tlabel);
1181 case kUnsignedLessThanOrEqual:
1182 __ j(below_equal, tlabel);
1184 case kUnsignedGreaterThan:
1185 __ j(above, tlabel);
1188 __ j(overflow, tlabel);
1191 __ j(no_overflow, tlabel);
1194 if (!branch->fallthru) __ jmp(flabel, flabel_distance);
1198 void CodeGenerator::AssembleArchJump(RpoNumber target) {
1199 if (!IsNextInAssemblyOrder(target)) __ jmp(GetLabel(target));
1203 // Assembles boolean materializations after this instruction.
1204 void CodeGenerator::AssembleArchBoolean(Instruction* instr,
1205 FlagsCondition condition) {
1206 X64OperandConverter i(this, instr);
1209 // Materialize a full 64-bit 1 or 0 value. The result register is always the
1210 // last output of the instruction.
1212 DCHECK_NE(0u, instr->OutputCount());
1213 Register reg = i.OutputRegister(instr->OutputCount() - 1);
1214 Condition cc = no_condition;
1215 switch (condition) {
1216 case kUnorderedEqual:
1217 __ j(parity_odd, &check, Label::kNear);
1218 __ movl(reg, Immediate(0));
1219 __ jmp(&done, Label::kNear);
1224 case kUnorderedNotEqual:
1225 __ j(parity_odd, &check, Label::kNear);
1226 __ movl(reg, Immediate(1));
1227 __ jmp(&done, Label::kNear);
1232 case kSignedLessThan:
1235 case kSignedGreaterThanOrEqual:
1238 case kSignedLessThanOrEqual:
1241 case kSignedGreaterThan:
1244 case kUnsignedLessThan:
1247 case kUnsignedGreaterThanOrEqual:
1250 case kUnsignedLessThanOrEqual:
1253 case kUnsignedGreaterThan:
1265 __ movzxbl(reg, reg);
1270 void CodeGenerator::AssembleArchLookupSwitch(Instruction* instr) {
1271 X64OperandConverter i(this, instr);
1272 Register input = i.InputRegister(0);
1273 for (size_t index = 2; index < instr->InputCount(); index += 2) {
1274 __ cmpl(input, Immediate(i.InputInt32(index + 0)));
1275 __ j(equal, GetLabel(i.InputRpo(index + 1)));
1277 AssembleArchJump(i.InputRpo(1));
1281 void CodeGenerator::AssembleArchTableSwitch(Instruction* instr) {
1282 X64OperandConverter i(this, instr);
1283 Register input = i.InputRegister(0);
1284 int32_t const case_count = static_cast<int32_t>(instr->InputCount() - 2);
1285 Label** cases = zone()->NewArray<Label*>(case_count);
1286 for (int32_t index = 0; index < case_count; ++index) {
1287 cases[index] = GetLabel(i.InputRpo(index + 2));
1289 Label* const table = AddJumpTable(cases, case_count);
1290 __ cmpl(input, Immediate(case_count));
1291 __ j(above_equal, GetLabel(i.InputRpo(1)));
1292 __ leaq(kScratchRegister, Operand(table));
1293 __ jmp(Operand(kScratchRegister, input, times_8, 0));
1297 void CodeGenerator::AssembleDeoptimizerCall(
1298 int deoptimization_id, Deoptimizer::BailoutType bailout_type) {
1299 Address deopt_entry = Deoptimizer::GetDeoptimizationEntry(
1300 isolate(), deoptimization_id, bailout_type);
1301 __ call(deopt_entry, RelocInfo::RUNTIME_ENTRY);
1305 void CodeGenerator::AssemblePrologue() {
1306 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1307 int stack_slots = frame()->GetSpillSlotCount();
1308 if (descriptor->kind() == CallDescriptor::kCallAddress) {
1311 const RegList saves = descriptor->CalleeSavedRegisters();
1312 if (saves != 0) { // Save callee-saved registers.
1313 int register_save_area_size = 0;
1314 for (int i = Register::kNumRegisters - 1; i >= 0; i--) {
1315 if (!((1 << i) & saves)) continue;
1316 __ pushq(Register::from_code(i));
1317 register_save_area_size += kPointerSize;
1319 frame()->SetRegisterSaveAreaSize(register_save_area_size);
1321 } else if (descriptor->IsJSFunctionCall()) {
1322 CompilationInfo* info = this->info();
1323 __ Prologue(info->IsCodePreAgingActive());
1324 frame()->SetRegisterSaveAreaSize(
1325 StandardFrameConstants::kFixedFrameSizeFromFp);
1326 } else if (stack_slots > 0) {
1328 frame()->SetRegisterSaveAreaSize(
1329 StandardFrameConstants::kFixedFrameSizeFromFp);
1332 if (info()->is_osr()) {
1333 // TurboFan OSR-compiled functions cannot be entered directly.
1334 __ Abort(kShouldNotDirectlyEnterOsrFunction);
1336 // Unoptimized code jumps directly to this entrypoint while the unoptimized
1337 // frame is still on the stack. Optimized code uses OSR values directly from
1338 // the unoptimized frame. Thus, all that needs to be done is to allocate the
1339 // remaining stack slots.
1340 if (FLAG_code_comments) __ RecordComment("-- OSR entrypoint --");
1341 osr_pc_offset_ = __ pc_offset();
1342 // TODO(titzer): cannot address target function == local #-1
1343 __ movq(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1344 DCHECK(stack_slots >= frame()->GetOsrStackSlotCount());
1345 stack_slots -= frame()->GetOsrStackSlotCount();
1348 if (stack_slots > 0) {
1349 __ subq(rsp, Immediate(stack_slots * kPointerSize));
1354 void CodeGenerator::AssembleReturn() {
1355 CallDescriptor* descriptor = linkage()->GetIncomingDescriptor();
1356 int stack_slots = frame()->GetSpillSlotCount();
1357 if (descriptor->kind() == CallDescriptor::kCallAddress) {
1358 if (frame()->GetRegisterSaveAreaSize() > 0) {
1359 // Remove this frame's spill slots first.
1360 if (stack_slots > 0) {
1361 __ addq(rsp, Immediate(stack_slots * kPointerSize));
1363 const RegList saves = descriptor->CalleeSavedRegisters();
1364 // Restore registers.
1366 for (int i = 0; i < Register::kNumRegisters; i++) {
1367 if (!((1 << i) & saves)) continue;
1368 __ popq(Register::from_code(i));
1371 __ popq(rbp); // Pop caller's frame pointer.
1374 // No saved registers.
1375 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
1376 __ popq(rbp); // Pop caller's frame pointer.
1379 } else if (descriptor->IsJSFunctionCall() || stack_slots > 0) {
1380 __ movq(rsp, rbp); // Move stack pointer back to frame pointer.
1381 __ popq(rbp); // Pop caller's frame pointer.
1382 int pop_count = descriptor->IsJSFunctionCall()
1383 ? static_cast<int>(descriptor->JSParameterCount())
1385 __ ret(pop_count * kPointerSize);
1392 void CodeGenerator::AssembleMove(InstructionOperand* source,
1393 InstructionOperand* destination) {
1394 X64OperandConverter g(this, NULL);
1395 // Dispatch on the source and destination operand kinds. Not all
1396 // combinations are possible.
1397 if (source->IsRegister()) {
1398 DCHECK(destination->IsRegister() || destination->IsStackSlot());
1399 Register src = g.ToRegister(source);
1400 if (destination->IsRegister()) {
1401 __ movq(g.ToRegister(destination), src);
1403 __ movq(g.ToOperand(destination), src);
1405 } else if (source->IsStackSlot()) {
1406 DCHECK(destination->IsRegister() || destination->IsStackSlot());
1407 Operand src = g.ToOperand(source);
1408 if (destination->IsRegister()) {
1409 Register dst = g.ToRegister(destination);
1412 // Spill on demand to use a temporary register for memory-to-memory
1414 Register tmp = kScratchRegister;
1415 Operand dst = g.ToOperand(destination);
1419 } else if (source->IsConstant()) {
1420 ConstantOperand* constant_source = ConstantOperand::cast(source);
1421 Constant src = g.ToConstant(constant_source);
1422 if (destination->IsRegister() || destination->IsStackSlot()) {
1423 Register dst = destination->IsRegister() ? g.ToRegister(destination)
1425 switch (src.type()) {
1426 case Constant::kInt32:
1427 // TODO(dcarney): don't need scratch in this case.
1428 __ Set(dst, src.ToInt32());
1430 case Constant::kInt64:
1431 __ Set(dst, src.ToInt64());
1433 case Constant::kFloat32:
1435 isolate()->factory()->NewNumber(src.ToFloat32(), TENURED));
1437 case Constant::kFloat64:
1439 isolate()->factory()->NewNumber(src.ToFloat64(), TENURED));
1441 case Constant::kExternalReference:
1442 __ Move(dst, src.ToExternalReference());
1444 case Constant::kHeapObject: {
1445 Handle<HeapObject> src_object = src.ToHeapObject();
1446 if (info()->IsOptimizing() &&
1447 src_object.is_identical_to(info()->context())) {
1448 // Loading the context from the frame is way cheaper than
1449 // materializing the actual context heap object address.
1450 __ movp(dst, Operand(rbp, StandardFrameConstants::kContextOffset));
1452 __ Move(dst, src_object);
1456 case Constant::kRpoNumber:
1457 UNREACHABLE(); // TODO(dcarney): load of labels on x64.
1460 if (destination->IsStackSlot()) {
1461 __ movq(g.ToOperand(destination), kScratchRegister);
1463 } else if (src.type() == Constant::kFloat32) {
1464 // TODO(turbofan): Can we do better here?
1465 uint32_t src_const = bit_cast<uint32_t>(src.ToFloat32());
1466 if (destination->IsDoubleRegister()) {
1467 __ Move(g.ToDoubleRegister(destination), src_const);
1469 DCHECK(destination->IsDoubleStackSlot());
1470 Operand dst = g.ToOperand(destination);
1471 __ movl(dst, Immediate(src_const));
1474 DCHECK_EQ(Constant::kFloat64, src.type());
1475 uint64_t src_const = bit_cast<uint64_t>(src.ToFloat64());
1476 if (destination->IsDoubleRegister()) {
1477 __ Move(g.ToDoubleRegister(destination), src_const);
1479 DCHECK(destination->IsDoubleStackSlot());
1480 __ movq(kScratchRegister, src_const);
1481 __ movq(g.ToOperand(destination), kScratchRegister);
1484 } else if (source->IsDoubleRegister()) {
1485 XMMRegister src = g.ToDoubleRegister(source);
1486 if (destination->IsDoubleRegister()) {
1487 XMMRegister dst = g.ToDoubleRegister(destination);
1488 __ movaps(dst, src);
1490 DCHECK(destination->IsDoubleStackSlot());
1491 Operand dst = g.ToOperand(destination);
1494 } else if (source->IsDoubleStackSlot()) {
1495 DCHECK(destination->IsDoubleRegister() || destination->IsDoubleStackSlot());
1496 Operand src = g.ToOperand(source);
1497 if (destination->IsDoubleRegister()) {
1498 XMMRegister dst = g.ToDoubleRegister(destination);
1501 // We rely on having xmm0 available as a fixed scratch register.
1502 Operand dst = g.ToOperand(destination);
1503 __ movsd(xmm0, src);
1504 __ movsd(dst, xmm0);
1512 void CodeGenerator::AssembleSwap(InstructionOperand* source,
1513 InstructionOperand* destination) {
1514 X64OperandConverter g(this, NULL);
1515 // Dispatch on the source and destination operand kinds. Not all
1516 // combinations are possible.
1517 if (source->IsRegister() && destination->IsRegister()) {
1518 // Register-register.
1519 __ xchgq(g.ToRegister(source), g.ToRegister(destination));
1520 } else if (source->IsRegister() && destination->IsStackSlot()) {
1521 Register src = g.ToRegister(source);
1522 Operand dst = g.ToOperand(destination);
1524 } else if ((source->IsStackSlot() && destination->IsStackSlot()) ||
1525 (source->IsDoubleStackSlot() &&
1526 destination->IsDoubleStackSlot())) {
1528 Register tmp = kScratchRegister;
1529 Operand src = g.ToOperand(source);
1530 Operand dst = g.ToOperand(destination);
1534 } else if (source->IsDoubleRegister() && destination->IsDoubleRegister()) {
1535 // XMM register-register swap. We rely on having xmm0
1536 // available as a fixed scratch register.
1537 XMMRegister src = g.ToDoubleRegister(source);
1538 XMMRegister dst = g.ToDoubleRegister(destination);
1539 __ movaps(xmm0, src);
1540 __ movaps(src, dst);
1541 __ movaps(dst, xmm0);
1542 } else if (source->IsDoubleRegister() && destination->IsDoubleStackSlot()) {
1543 // XMM register-memory swap. We rely on having xmm0
1544 // available as a fixed scratch register.
1545 XMMRegister src = g.ToDoubleRegister(source);
1546 Operand dst = g.ToOperand(destination);
1547 __ movsd(xmm0, src);
1549 __ movsd(dst, xmm0);
1551 // No other combinations are possible.
1557 void CodeGenerator::AssembleJumpTable(Label** targets, size_t target_count) {
1558 for (size_t index = 0; index < target_count; ++index) {
1559 __ dq(targets[index]);
1564 void CodeGenerator::AddNopForSmiCodeInlining() { __ nop(); }
1567 void CodeGenerator::EnsureSpaceForLazyDeopt() {
1568 int space_needed = Deoptimizer::patch_size();
1569 if (!info()->IsStub()) {
1570 // Ensure that we have enough space after the previous lazy-bailout
1571 // instruction for patching the code here.
1572 int current_pc = masm()->pc_offset();
1573 if (current_pc < last_lazy_deopt_pc_ + space_needed) {
1574 int padding_size = last_lazy_deopt_pc_ + space_needed - current_pc;
1575 __ Nop(padding_size);
1578 MarkLazyDeoptSite();
1583 } // namespace internal
1584 } // namespace compiler