2018-01-23 14:21:10 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "backend_x64/block_of_code.h"
|
|
|
|
#include "backend_x64/emit_x64.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "frontend/ir/basic_block.h"
|
|
|
|
#include "frontend/ir/microinstruction.h"
|
|
|
|
#include "frontend/ir/opcodes.h"
|
|
|
|
|
2018-01-26 14:51:48 +01:00
|
|
|
namespace Dynarmic::BackendX64 {
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
using namespace Xbyak::util;
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitPack2x32To1x64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 lo = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
Xbyak::Reg64 hi = ctx.reg_alloc.UseScratchGpr(args[1]);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shl(hi, 32);
|
|
|
|
code.mov(lo.cvt32(), lo.cvt32()); // Zero extend to 64-bits
|
|
|
|
code.or_(lo, hi);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, lo);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLeastSignificantWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitMostSignificantWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, 32);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
if (carry_inst) {
|
|
|
|
Xbyak::Reg64 carry = ctx.reg_alloc.ScratchGpr();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.setc(carry.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLeastSignificantHalf(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLeastSignificantByte(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, args[0]);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitMostSignificantBit(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
// TODO: Flag optimization
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, 31);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitIsZero32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
// TODO: Flag optimization
|
2018-02-03 15:28:57 +01:00
|
|
|
code.test(result, result);
|
|
|
|
code.sete(result.cvt8());
|
|
|
|
code.movzx(result, result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitIsZero64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
// TODO: Flag optimization
|
2018-02-03 15:28:57 +01:00
|
|
|
code.test(result, result);
|
|
|
|
code.sete(result.cvt8());
|
|
|
|
code.movzx(result, result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitTestBit(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
ASSERT(args[1].IsImmediate());
|
|
|
|
// TODO: Flag optimization
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(result, args[1].GetImmediateU8());
|
|
|
|
code.setc(result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
static void EmitConditionalSelect(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, int bitsize) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 nzcv = ctx.reg_alloc.ScratchGpr({HostLoc::RAX}).cvt32();
|
|
|
|
Xbyak::Reg then_ = ctx.reg_alloc.UseGpr(args[1]).changeBit(bitsize);
|
|
|
|
Xbyak::Reg else_ = ctx.reg_alloc.UseScratchGpr(args[2]).changeBit(bitsize);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(nzcv, dword[r15 + code.GetJitStateInfo().offsetof_CPSR_nzcv]);
|
2018-01-23 14:21:10 +01:00
|
|
|
// TODO: Flag optimization
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(nzcv, 28);
|
|
|
|
code.imul(nzcv, nzcv, 0b00010000'10000001);
|
|
|
|
code.and_(nzcv.cvt8(), 1);
|
|
|
|
code.add(nzcv.cvt8(), 0x7F); // restore OF
|
|
|
|
code.sahf(); // restore SF, ZF, CF
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
switch (args[0].GetImmediateCond()) {
|
|
|
|
case IR::Cond::EQ: //z
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovz(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::NE: //!z
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovnz(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::CS: //c
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovc(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::CC: //!c
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovnc(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::MI: //n
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovs(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::PL: //!n
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovns(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::VS: //v
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovo(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::VC: //!v
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovno(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::HI: //c & !z
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmc();
|
|
|
|
code.cmova(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::LS: //!c | z
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmc();
|
|
|
|
code.cmovna(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::GE: // n == v
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovge(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::LT: // n != v
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovl(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::GT: // !z & (n == v)
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovg(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::LE: // z | (n != v)
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmovle(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
case IR::Cond::AL:
|
|
|
|
case IR::Cond::NV:
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(else_, then_);
|
2018-01-23 14:21:10 +01:00
|
|
|
break;
|
|
|
|
default:
|
2018-01-28 00:42:30 +01:00
|
|
|
ASSERT_MSG(false, "Invalid cond {}", static_cast<size_t>(args[0].GetImmediateCond()));
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, else_);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitConditionalSelect32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
EmitConditionalSelect(code, ctx, inst, 32);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitConditionalSelect64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
EmitConditionalSelect(code, ctx, inst, 64);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 00:07:51 +01:00
|
|
|
void EmitX64::EmitConditionalSelectNZCV(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
EmitConditionalSelect(code, ctx, inst, 32);
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:49:08 +01:00
|
|
|
static void EmitExtractRegister(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, int bit_size) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
const Xbyak::Reg result = ctx.reg_alloc.UseScratchGpr(args[0]).changeBit(bit_size);
|
|
|
|
const Xbyak::Reg operand = ctx.reg_alloc.UseScratchGpr(args[1]).changeBit(bit_size);
|
|
|
|
const u8 lsb = args[2].GetImmediateU8();
|
|
|
|
|
|
|
|
code.shrd(result, operand, lsb);
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitExtractRegister32(Dynarmic::BackendX64::EmitContext& ctx, IR::Inst* inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
EmitExtractRegister(code, ctx, inst, 32);
|
2018-01-26 17:49:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitExtractRegister64(Dynarmic::BackendX64::EmitContext& ctx, IR::Inst* inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
EmitExtractRegister(code, ctx, inst, 64);
|
2018-01-26 17:49:08 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLogicalShiftLeft32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
|
|
|
// TODO: Consider using BMI2 instructions like SHLX when arm-in-host flags is implemented.
|
|
|
|
|
|
|
|
if (!carry_inst) {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
|
|
|
|
if (shift <= 31) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shl(result, shift);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, result);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 zero = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
|
|
|
|
// The 32-bit x64 SHL instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shl(result, code.cl);
|
|
|
|
code.xor_(zero, zero);
|
|
|
|
code.cmp(code.cl, 32);
|
|
|
|
code.cmovnb(result, zero);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift < 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.shl(result, shift);
|
|
|
|
code.setc(carry.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
} else if (shift > 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, result);
|
|
|
|
code.xor_(carry, carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(carry, result);
|
|
|
|
code.xor_(result, result);
|
|
|
|
code.and_(carry, 1);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.inLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmp(code.cl, 32);
|
|
|
|
code.ja(".Rs_gt32");
|
|
|
|
code.je(".Rs_eq32");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0xFF < 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0); // Set the carry flag for correct behaviour in the case when Rs & 0xFF == 0
|
|
|
|
code.shl(result, code.cl);
|
|
|
|
code.setc(carry.cvt8());
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else if (Rs & 0xFF > 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".Rs_gt32");
|
|
|
|
code.xor_(result, result);
|
|
|
|
code.xor_(carry, carry);
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else if (Rs & 0xFF == 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".Rs_eq32");
|
|
|
|
code.mov(carry, result);
|
|
|
|
code.and_(carry, 1);
|
|
|
|
code.xor_(result, result);
|
2018-01-23 14:21:10 +01:00
|
|
|
// }
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.outLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLogicalShiftLeft64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
|
|
|
|
if (shift < 64) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shl(result, shift);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result.cvt32(), result.cvt32());
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
Xbyak::Reg64 zero = ctx.reg_alloc.ScratchGpr();
|
|
|
|
|
|
|
|
// The x64 SHL instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shl(result, code.cl);
|
|
|
|
code.xor_(zero.cvt32(), zero.cvt32());
|
|
|
|
code.cmp(code.cl, 64);
|
|
|
|
code.cmovnb(result, zero);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLogicalShiftRight32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
|
|
|
if (!carry_inst) {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
|
|
|
|
if (shift <= 31) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, shift);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, result);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 zero = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
|
|
|
|
// The 32-bit x64 SHR instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, code.cl);
|
|
|
|
code.xor_(zero, zero);
|
|
|
|
code.cmp(code.cl, 32);
|
|
|
|
code.cmovnb(result, zero);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift < 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, shift);
|
|
|
|
code.setc(carry.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
} else if (shift == 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(result, 31);
|
|
|
|
code.setc(carry.cvt8());
|
|
|
|
code.mov(result, 0);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, result);
|
|
|
|
code.xor_(carry, carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt32();
|
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.inLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmp(code.cl, 32);
|
|
|
|
code.ja(".Rs_gt32");
|
|
|
|
code.je(".Rs_eq32");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2018-02-03 15:28:57 +01:00
|
|
|
code.test(code.cl, code.cl);
|
|
|
|
code.jz(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0xFF < 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, code.cl);
|
|
|
|
code.setc(carry.cvt8());
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else if (Rs & 0xFF > 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".Rs_gt32");
|
|
|
|
code.xor_(result, result);
|
|
|
|
code.xor_(carry, carry);
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else if (Rs & 0xFF == 32) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".Rs_eq32");
|
|
|
|
code.bt(result, 31);
|
|
|
|
code.setc(carry.cvt8());
|
|
|
|
code.xor_(result, result);
|
2018-01-23 14:21:10 +01:00
|
|
|
// }
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.outLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitLogicalShiftRight64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
|
|
|
|
if (shift < 64) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, shift);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result.cvt32(), result.cvt32());
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
Xbyak::Reg64 zero = ctx.reg_alloc.ScratchGpr();
|
|
|
|
|
|
|
|
// The x64 SHR instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count, so shifts above 31 result in zeros.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.shr(result, code.cl);
|
|
|
|
code.xor_(zero.cvt32(), zero.cvt32());
|
|
|
|
code.cmp(code.cl, 64);
|
|
|
|
code.cmovnb(result, zero);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitArithmeticShiftRight32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
|
|
|
if (!carry_inst) {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sar(result, u8(shift < 31 ? shift : 31));
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.UseScratch(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg32 const31 = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
|
|
|
|
// The 32-bit x64 SAR instruction masks the shift count by 0x1F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count.
|
|
|
|
|
|
|
|
// We note that all shift values above 31 have the same behaviour as 31 does, so we saturate `shift` to 31.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(const31, 31);
|
|
|
|
code.movzx(code.ecx, code.cl);
|
|
|
|
code.cmp(code.ecx, u32(31));
|
|
|
|
code.cmovg(code.ecx, const31);
|
|
|
|
code.sar(result, code.cl);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if (shift <= 31) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sar(result, shift);
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sar(result, 31);
|
|
|
|
code.bt(result, 31);
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
|
|
|
|
|
|
|
// TODO: Optimize this.
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.inLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmp(code.cl, u32(31));
|
|
|
|
code.ja(".Rs_gt31");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2018-02-03 15:28:57 +01:00
|
|
|
code.test(code.cl, code.cl);
|
|
|
|
code.jz(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0xFF <= 31) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sar(result, code.cl);
|
|
|
|
code.setc(carry);
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else if (Rs & 0xFF > 31) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".Rs_gt31");
|
|
|
|
code.sar(result, 31); // 31 produces the same results as anything above 31
|
|
|
|
code.bt(result, 31);
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
// }
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.outLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitArithmeticShiftRight64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sar(result, u8(shift < 63 ? shift : 63));
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.UseScratch(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
Xbyak::Reg64 const63 = ctx.reg_alloc.ScratchGpr();
|
|
|
|
|
|
|
|
// The 64-bit x64 SAR instruction masks the shift count by 0x3F before performing the shift.
|
|
|
|
// ARM differs from the behaviour: It does not mask the count.
|
|
|
|
|
|
|
|
// We note that all shift values above 63 have the same behaviour as 63 does, so we saturate `shift` to 63.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(const63, 63);
|
|
|
|
code.movzx(code.ecx, code.cl);
|
|
|
|
code.cmp(code.ecx, u32(63));
|
|
|
|
code.cmovg(code.ecx, const63);
|
|
|
|
code.sar(result, code.cl);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitRotateRight32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
auto& carry_arg = args[2];
|
|
|
|
|
|
|
|
if (!carry_inst) {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, u8(shift & 0x1F));
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
|
|
|
|
// x64 ROR instruction does (shift & 0x1F) for us.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, code.cl);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
|
|
|
|
|
|
|
if (shift == 0) {
|
|
|
|
// There is nothing more to do.
|
|
|
|
} else if ((shift & 0x1F) == 0) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(result, u8(31));
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, shift);
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.UseScratch(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(operand_arg).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(carry_arg).cvt8();
|
|
|
|
|
|
|
|
// TODO: Optimize
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.inLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
// if (Rs & 0xFF == 0) goto end;
|
2018-02-03 15:28:57 +01:00
|
|
|
code.test(code.cl, code.cl);
|
|
|
|
code.jz(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.and_(code.ecx, u32(0x1F));
|
|
|
|
code.jz(".zero_1F");
|
2018-01-23 14:21:10 +01:00
|
|
|
// if (Rs & 0x1F != 0) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, code.cl);
|
|
|
|
code.setc(carry);
|
|
|
|
code.jmp(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
// } else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".zero_1F");
|
|
|
|
code.bt(result, u8(31));
|
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
// }
|
2018-02-03 15:28:57 +01:00
|
|
|
code.L(".end");
|
2018-01-23 14:21:10 +01:00
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.outLocalLabel();
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitRotateRight64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& operand_arg = args[0];
|
|
|
|
auto& shift_arg = args[1];
|
|
|
|
|
|
|
|
if (shift_arg.IsImmediate()) {
|
|
|
|
u8 shift = shift_arg.GetImmediateU8();
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, u8(shift & 0x3F));
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
ctx.reg_alloc.Use(shift_arg, HostLoc::RCX);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(operand_arg);
|
|
|
|
|
|
|
|
// x64 ROR instruction does (shift & 0x3F) for us.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.ror(result, code.cl);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitRotateRightExtended(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg8 carry = ctx.reg_alloc.UseScratchGpr(args[1]).cvt8();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.rcr(result, 1);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
if (carry_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Xbyak::Reg64 INVALID_REG = Xbyak::Reg64(-1);
|
|
|
|
|
|
|
|
static Xbyak::Reg8 DoCarry(RegAlloc& reg_alloc, Argument& carry_in, IR::Inst* carry_out) {
|
|
|
|
if (carry_in.IsImmediate()) {
|
|
|
|
return carry_out ? reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
|
|
|
} else {
|
|
|
|
return carry_out ? reg_alloc.UseScratchGpr(carry_in).cvt8() : reg_alloc.UseGpr(carry_in).cvt8();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
static Xbyak::Reg64 DoNZCV(BlockOfCode& code, RegAlloc& reg_alloc, IR::Inst* nzcv_out) {
|
2018-01-23 14:21:10 +01:00
|
|
|
if (!nzcv_out)
|
|
|
|
return INVALID_REG;
|
|
|
|
|
|
|
|
Xbyak::Reg64 nzcv = reg_alloc.ScratchGpr({HostLoc::RAX});
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(nzcv.cvt32(), nzcv.cvt32());
|
2018-01-23 14:21:10 +01:00
|
|
|
return nzcv;
|
|
|
|
}
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
static void EmitAdd(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, int bitsize) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
auto nzcv_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetNZCVFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& carry_in = args[2];
|
|
|
|
|
|
|
|
Xbyak::Reg64 nzcv = DoNZCV(code, ctx.reg_alloc, nzcv_inst);
|
|
|
|
Xbyak::Reg result = ctx.reg_alloc.UseScratchGpr(args[0]).changeBit(bitsize);
|
|
|
|
Xbyak::Reg8 carry = DoCarry(ctx.reg_alloc, carry_in, carry_inst);
|
|
|
|
Xbyak::Reg8 overflow = overflow_inst ? ctx.reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
|
|
|
|
|
|
|
// TODO: Consider using LEA.
|
|
|
|
|
|
|
|
if (args[1].IsImmediate() && args[1].GetType() == IR::Type::U32) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
|
|
|
if (carry_in.IsImmediate()) {
|
|
|
|
if (carry_in.GetImmediateU1()) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.stc();
|
|
|
|
code.adc(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.add(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.adc(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(bitsize);
|
|
|
|
if (carry_in.IsImmediate()) {
|
|
|
|
if (carry_in.GetImmediateU1()) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.stc();
|
|
|
|
code.adc(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.add(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.adc(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nzcv_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.lahf();
|
|
|
|
code.seto(code.al);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(nzcv_inst, nzcv);
|
|
|
|
ctx.EraseInstruction(nzcv_inst);
|
|
|
|
}
|
|
|
|
if (carry_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.setc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
}
|
|
|
|
if (overflow_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.seto(overflow);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
|
|
|
ctx.EraseInstruction(overflow_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitAdd32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
EmitAdd(code, ctx, inst, 32);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitAdd64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
EmitAdd(code, ctx, inst, 64);
|
|
|
|
}
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
static void EmitSub(BlockOfCode& code, EmitContext& ctx, IR::Inst* inst, int bitsize) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto carry_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetCarryFromOp);
|
|
|
|
auto overflow_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetOverflowFromOp);
|
|
|
|
auto nzcv_inst = inst->GetAssociatedPseudoOperation(IR::Opcode::GetNZCVFromOp);
|
|
|
|
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
auto& carry_in = args[2];
|
|
|
|
|
|
|
|
Xbyak::Reg64 nzcv = DoNZCV(code, ctx.reg_alloc, nzcv_inst);
|
|
|
|
Xbyak::Reg result = ctx.reg_alloc.UseScratchGpr(args[0]).changeBit(bitsize);
|
|
|
|
Xbyak::Reg8 carry = DoCarry(ctx.reg_alloc, carry_in, carry_inst);
|
|
|
|
Xbyak::Reg8 overflow = overflow_inst ? ctx.reg_alloc.ScratchGpr().cvt8() : INVALID_REG.cvt8();
|
|
|
|
|
|
|
|
// TODO: Consider using LEA.
|
|
|
|
// TODO: Optimize CMP case.
|
|
|
|
// Note that x64 CF is inverse of what the ARM carry flag is here.
|
|
|
|
|
|
|
|
if (args[1].IsImmediate() && args[1].GetType() == IR::Type::U32) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
|
|
|
if (carry_in.IsImmediate()) {
|
|
|
|
if (carry_in.GetImmediateU1()) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sub(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.stc();
|
|
|
|
code.sbb(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.cmc();
|
|
|
|
code.sbb(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(bitsize);
|
|
|
|
if (carry_in.IsImmediate()) {
|
|
|
|
if (carry_in.GetImmediateU1()) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.sub(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.stc();
|
|
|
|
code.sbb(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bt(carry.cvt32(), 0);
|
|
|
|
code.cmc();
|
|
|
|
code.sbb(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nzcv_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.cmc();
|
|
|
|
code.lahf();
|
|
|
|
code.seto(code.al);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(nzcv_inst, nzcv);
|
|
|
|
ctx.EraseInstruction(nzcv_inst);
|
|
|
|
}
|
|
|
|
if (carry_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.setnc(carry);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(carry_inst, carry);
|
|
|
|
ctx.EraseInstruction(carry_inst);
|
|
|
|
}
|
|
|
|
if (overflow_inst) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.seto(overflow);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(overflow_inst, overflow);
|
|
|
|
ctx.EraseInstruction(overflow_inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSub32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
EmitSub(code, ctx, inst, 32);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSub64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
EmitSub(code, ctx, inst, 64);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitMul32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
if (args[1].IsImmediate()) {
|
2018-02-03 15:28:57 +01:00
|
|
|
code.imul(result, result, args[1].GetImmediateU32());
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(32);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.imul(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitMul64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.imul(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:32:07 +01:00
|
|
|
void EmitX64::EmitUnsignedMultiplyHigh64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
ctx.reg_alloc.UseScratch(args[0], HostLoc::RAX);
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
code.mul(*op_arg);
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, rdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitSignedMultiplyHigh64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
ctx.reg_alloc.UseScratch(args[0], HostLoc::RAX);
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
code.imul(*op_arg);
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, rdx);
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:36:39 +01:00
|
|
|
void EmitX64::EmitUnsignedDiv32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RAX});
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
Xbyak::Reg32 dividend = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 divisor = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(eax, eax);
|
|
|
|
code.test(divisor, divisor);
|
|
|
|
code.jz(end);
|
|
|
|
code.mov(eax, dividend);
|
|
|
|
code.xor_(edx, edx);
|
|
|
|
code.div(divisor);
|
|
|
|
code.L(end);
|
2018-01-24 13:36:39 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, eax);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitUnsignedDiv64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RAX});
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
Xbyak::Reg64 dividend = ctx.reg_alloc.UseGpr(args[0]);
|
|
|
|
Xbyak::Reg64 divisor = ctx.reg_alloc.UseGpr(args[1]);
|
|
|
|
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(eax, eax);
|
|
|
|
code.test(divisor, divisor);
|
|
|
|
code.jz(end);
|
|
|
|
code.mov(rax, dividend);
|
|
|
|
code.xor_(edx, edx);
|
|
|
|
code.div(divisor);
|
|
|
|
code.L(end);
|
2018-01-24 13:36:39 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, rax);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitSignedDiv32(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RAX});
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
Xbyak::Reg32 dividend = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 divisor = ctx.reg_alloc.UseGpr(args[1]).cvt32();
|
|
|
|
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(eax, eax);
|
|
|
|
code.test(divisor, divisor);
|
|
|
|
code.jz(end);
|
|
|
|
code.mov(eax, dividend);
|
|
|
|
code.cdq();
|
|
|
|
code.idiv(divisor);
|
|
|
|
code.L(end);
|
2018-01-24 13:36:39 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, eax);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitX64::EmitSignedDiv64(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RAX});
|
|
|
|
ctx.reg_alloc.ScratchGpr({HostLoc::RDX});
|
|
|
|
Xbyak::Reg64 dividend = ctx.reg_alloc.UseGpr(args[0]);
|
|
|
|
Xbyak::Reg64 divisor = ctx.reg_alloc.UseGpr(args[1]);
|
|
|
|
|
|
|
|
Xbyak::Label end;
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(eax, eax);
|
|
|
|
code.test(divisor, divisor);
|
|
|
|
code.jz(end);
|
|
|
|
code.mov(rax, dividend);
|
|
|
|
code.cqo();
|
|
|
|
code.idiv(divisor);
|
|
|
|
code.L(end);
|
2018-01-24 13:36:39 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, rax);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitAnd32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
|
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.and_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(32);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.and_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitAnd64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
|
|
|
|
if (args[1].FitsInImmediateS32()) {
|
|
|
|
u32 op_arg = u32(args[1].GetImmediateS32());
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.and_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(64);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.and_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitEor32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
|
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(32);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitEor64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
|
|
|
|
if (args[1].FitsInImmediateS32()) {
|
|
|
|
u32 op_arg = u32(args[1].GetImmediateS32());
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(64);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.xor_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitOr32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
|
|
|
|
if (args[1].IsImmediate()) {
|
|
|
|
u32 op_arg = args[1].GetImmediateU32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.or_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(32);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.or_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitOr64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
|
|
|
|
|
|
|
if (args[1].FitsInImmediateS32()) {
|
|
|
|
u32 op_arg = u32(args[1].GetImmediateS32());
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.or_(result, op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
OpArg op_arg = ctx.reg_alloc.UseOpArg(args[1]);
|
|
|
|
op_arg.setBit(64);
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.or_(result, *op_arg);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitNot32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg32 result;
|
|
|
|
if (args[0].IsImmediate()) {
|
|
|
|
result = ctx.reg_alloc.ScratchGpr().cvt32();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(result, u32(~args[0].GetImmediateU32()));
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.not_(result);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitNot64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
|
|
|
Xbyak::Reg64 result;
|
|
|
|
if (args[0].IsImmediate()) {
|
|
|
|
result = ctx.reg_alloc.ScratchGpr();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(result, ~args[0].GetImmediateU64());
|
2018-01-23 14:21:10 +01:00
|
|
|
} else {
|
|
|
|
result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.not_(result);
|
2018-01-23 14:21:10 +01:00
|
|
|
}
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSignExtendByteToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movsx(result.cvt32(), result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSignExtendHalfToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movsx(result.cvt32(), result.cvt16());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSignExtendByteToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movsx(result.cvt64(), result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSignExtendHalfToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movsx(result.cvt64(), result.cvt16());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitSignExtendWordToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movsxd(result.cvt64(), result.cvt32());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitZeroExtendByteToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movzx(result.cvt32(), result.cvt8());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitZeroExtendHalfToWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movzx(result.cvt32(), result.cvt16());
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitZeroExtendByteToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movzx(result.cvt32(), result.cvt8()); // x64 zeros upper 32 bits on a 32-bit move
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitZeroExtendHalfToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movzx(result.cvt32(), result.cvt16()); // x64 zeros upper 32 bits on a 32-bit move
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitZeroExtendWordToLong(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.mov(result.cvt32(), result.cvt32()); // x64 zeros upper 32 bits on a 32-bit move
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-24 16:54:11 +01:00
|
|
|
void EmitX64::EmitZeroExtendLongToQuad(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
if (args[0].IsInGpr()) {
|
2018-01-27 02:59:14 +01:00
|
|
|
Xbyak::Reg64 source = ctx.reg_alloc.UseGpr(args[0]);
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.ScratchXmm();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movq(result, source);
|
2018-01-27 02:59:14 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
2018-01-24 16:54:11 +01:00
|
|
|
} else {
|
|
|
|
Xbyak::Xmm result = ctx.reg_alloc.UseScratchXmm(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.movq(result, result);
|
2018-01-24 16:54:11 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitByteReverseWord(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bswap(result);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitByteReverseHalf(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg16 result = ctx.reg_alloc.UseScratchGpr(args[0]).cvt16();
|
2018-02-03 15:28:57 +01:00
|
|
|
code.rol(result, 8);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitByteReverseDual(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.UseScratchGpr(args[0]);
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bswap(result);
|
2018-01-23 14:21:10 +01:00
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitCountLeadingZeros32(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2018-02-03 15:28:57 +01:00
|
|
|
if (code.DoesCpuSupport(Xbyak::util::Cpu::tLZCNT)) {
|
2018-01-23 14:21:10 +01:00
|
|
|
Xbyak::Reg32 source = ctx.reg_alloc.UseGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.lzcnt(result, source);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
Xbyak::Reg32 source = ctx.reg_alloc.UseScratchGpr(args[0]).cvt32();
|
|
|
|
Xbyak::Reg32 result = ctx.reg_alloc.ScratchGpr().cvt32();
|
|
|
|
|
|
|
|
// The result of a bsr of zero is undefined, but zf is set after it.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bsr(result, source);
|
|
|
|
code.mov(source, 0xFFFFFFFF);
|
|
|
|
code.cmovz(result, source);
|
|
|
|
code.neg(result);
|
|
|
|
code.add(result, 31);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 20:16:39 +01:00
|
|
|
void EmitX64::EmitCountLeadingZeros64(EmitContext& ctx, IR::Inst* inst) {
|
2018-01-23 14:21:10 +01:00
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
2018-02-03 15:28:57 +01:00
|
|
|
if (code.DoesCpuSupport(Xbyak::util::Cpu::tLZCNT)) {
|
2018-01-23 14:21:10 +01:00
|
|
|
Xbyak::Reg64 source = ctx.reg_alloc.UseGpr(args[0]).cvt64();
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.ScratchGpr().cvt64();
|
|
|
|
|
2018-02-03 15:28:57 +01:00
|
|
|
code.lzcnt(result, source);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
|
|
|
Xbyak::Reg64 source = ctx.reg_alloc.UseScratchGpr(args[0]).cvt64();
|
|
|
|
Xbyak::Reg64 result = ctx.reg_alloc.ScratchGpr().cvt64();
|
|
|
|
|
|
|
|
// The result of a bsr of zero is undefined, but zf is set after it.
|
2018-02-03 15:28:57 +01:00
|
|
|
code.bsr(result, source);
|
|
|
|
code.mov(source.cvt32(), 0xFFFFFFFF);
|
|
|
|
code.cmovz(result.cvt32(), source.cvt32());
|
|
|
|
code.neg(result.cvt32());
|
|
|
|
code.add(result.cvt32(), 63);
|
2018-01-23 14:21:10 +01:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 14:51:48 +01:00
|
|
|
} // namespace Dynarmic::BackendX64
|