Skip to content

Commit

Permalink
simplify opRO
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Nov 8, 2023
1 parent 24b71a1 commit b220be9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 83 deletions.
18 changes: 9 additions & 9 deletions gen/gen_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ void put()
const char *msg = "//-V524"; // disable warning of PVS-Studio
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
const Tbl *p = &tbl[i];
printf("void cmov%s(const Reg& reg, const Operand& op) { opRO(reg, op, op.isREG(16 | i32e), op.isMEM(), T_0F, 0x40 | %d); }%s\n", p->name, p->ext, msg);
printf("void cmov%s(const Reg& reg, const Operand& op) { opRO(reg, op, op.isREG(16 | i32e), T_0F, 0x40 | %d); }%s\n", p->name, p->ext, msg);
printf("void j%s(std::string label, LabelType type = T_AUTO) { opJmp(label, type, 0x%02X, 0x%02X, 0x%02X); }%s\n", p->name, p->ext | 0x70, p->ext | 0x80, 0x0F, msg);
printf("void j%s(const Label& label, LabelType type = T_AUTO) { opJmp(label, type, 0x%02X, 0x%02X, 0x%02X); }%s\n", p->name, p->ext | 0x70, p->ext | 0x80, 0x0F, msg);
printf("void j%s(const char *label, LabelType type = T_AUTO) { j%s(std::string(label), type); }%s\n", p->name, p->name, msg);
Expand Down Expand Up @@ -856,7 +856,7 @@ void put()
};
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
const Tbl *p = &tbl[i];
printf("void %s(const Operand& op, const Reg& reg) { opRO(reg, op, op.isREG(16|32|64) && op.getBit() == reg.getBit(), op.isMEM(), T_0F, 0x%02X); }\n", p->name, p->code);
printf("void %s(const Operand& op, const Reg& reg) { opRO(reg, op, op.isREG(16|32|64) && op.getBit() == reg.getBit(), T_0F, 0x%02X); }\n", p->name, p->code);
printf("void %s(const Operand& op, uint8_t imm) { opRext(op, 16|32|64, %d, T_0F, 0xba, false, 1); db(imm); }\n", p->name, p->ext);
}
}
Expand Down Expand Up @@ -924,7 +924,7 @@ void put()
};
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
const Tbl *p = &tbl[i];
printf("void %s(const Reg&reg, const Operand& op) { opRO(reg, op, op.isREG(16 | i32e), op.isMEM(), T_0F, 0x%02X); }\n", p->name, p->code);
printf("void %s(const Reg&reg, const Operand& op) { opRO(reg, op, op.isREG(16 | i32e), T_0F, 0x%02X); }\n", p->name, p->code);
}
}
{
Expand Down Expand Up @@ -1073,7 +1073,7 @@ void put()
puts("void bndcn(const BoundsReg& bnd, const Operand& op) { opRext(op, i32e, bnd.getIdx(), T_F2 | T_0F, 0x1B, !op.isMEM()); }");
puts("void bndldx(const BoundsReg& bnd, const Address& addr) { opMIB(addr, bnd, T_0F, 0x1A); }");
puts("void bndmk(const BoundsReg& bnd, const Address& addr) { opMR(addr, bnd, T_F3 | T_0F, 0x1B); }");
puts("void bndmov(const BoundsReg& bnd, const Operand& op) { opRO(bnd, op, op.isBNDREG(), op.isMEM(), T_66 | T_0F, 0x1A); }");
puts("void bndmov(const BoundsReg& bnd, const Operand& op) { opRO(bnd, op, op.isBNDREG(), T_66 | T_0F, 0x1A); }");
puts("void bndmov(const Address& addr, const BoundsReg& bnd) { opMR(addr, bnd, T_66 | T_0F, 0x1B); }");
puts("void bndstx(const Address& addr, const BoundsReg& bnd) { opMIB(addr, bnd, T_0F, 0x1B); }");
}
Expand All @@ -1084,8 +1084,8 @@ void put()
puts("void ret(int imm = 0) { if (imm) { db(0xC2); dw(imm); } else { db(0xC3); } }");
puts("void retf(int imm = 0) { if (imm) { db(0xCA); dw(imm); } else { db(0xCB); } }");

puts("void xadd(const Operand& op, const Reg& reg) { opRO(reg, op, (op.isREG() && reg.isREG() && op.getBit() == reg.getBit()), op.isMEM(), T_0F, 0xC0 | (reg.isBit(8) ? 0 : 1)); }");
puts("void cmpxchg(const Operand& op, const Reg& reg) { opRO(reg, op, (op.isREG() && reg.isREG() && op.getBit() == reg.getBit()), op.isMEM(), T_0F, 0xB0 | (reg.isBit(8) ? 0 : 1)); }");
puts("void xadd(const Operand& op, const Reg& reg) { opRO(reg, op, (op.isREG() && reg.isREG() && op.getBit() == reg.getBit()), T_0F, 0xC0 | (reg.isBit(8) ? 0 : 1)); }");
puts("void cmpxchg(const Operand& op, const Reg& reg) { opRO(reg, op, (op.isREG() && reg.isREG() && op.getBit() == reg.getBit()), T_0F, 0xB0 | (reg.isBit(8) ? 0 : 1)); }");
puts("void movbe(const Reg& reg, const Address& addr) { opMR(addr, reg, T_0F38, 0xF0); }");
puts("void movbe(const Address& addr, const Reg& reg) { opMR(addr, reg, T_0F38, 0xF1); }");
puts("void movdiri(const Address& addr, const Reg32e& reg) { opMR(addr, reg, T_0F38, 0xF9); }");
Expand Down Expand Up @@ -1117,11 +1117,11 @@ void put()
puts("void movd(const Mmx& mmx, const Reg32& reg) { if (mmx.isXMM()) db(0x66); opRR(mmx, reg, T_0F, 0x6E); }");
puts("void movq2dq(const Xmm& xmm, const Mmx& mmx) { opRR(xmm, mmx, T_F3 | T_0F, 0xD6); }");
puts("void movdq2q(const Mmx& mmx, const Xmm& xmm) { opRR(mmx, xmm, T_F2 | T_0F, 0xD6); }");
puts("void movq(const Mmx& mmx, const Operand& op) { if (mmx.isXMM()) db(0xF3); opRO(mmx, op, (mmx.getKind() == op.getKind()), op.isMEM(), T_0F, mmx.isXMM() ? 0x7E : 0x6F); }");
puts("void movq(const Mmx& mmx, const Operand& op) { if (mmx.isXMM()) db(0xF3); opRO(mmx, op, (mmx.getKind() == op.getKind()), T_0F, mmx.isXMM() ? 0x7E : 0x6F); }");
puts("void movq(const Address& addr, const Mmx& mmx) { if (mmx.isXMM()) db(0x66); opMR(addr, mmx, T_0F, mmx.isXMM() ? 0xD6 : 0x7F); }");
puts("void rdrand(const Reg& r) { if (r.isBit(8)) XBYAK_THROW(ERR_BAD_SIZE_OF_REGISTER) opRR(Reg(6, Operand::REG, r.getBit()), r, T_0F, 0xC7); }");
puts("void rdseed(const Reg& r) { if (r.isBit(8)) XBYAK_THROW(ERR_BAD_SIZE_OF_REGISTER) opRR(Reg(7, Operand::REG, r.getBit()), r, T_0F, 0xC7); }");
puts("void crc32(const Reg32e& reg, const Operand& op) { if (reg.isBit(32) && op.isBit(16)) db(0x66); opRO(reg, op, op.isREG(), op.isMEM(), T_F2 | T_0F38, 0xF0 | (op.isBit(8) ? 0 : 1)); }");
puts("void crc32(const Reg32e& reg, const Operand& op) { if (reg.isBit(32) && op.isBit(16)) db(0x66); opRO(reg, op, op.isREG(), T_F2 | T_0F38, 0xF0 | (op.isBit(8) ? 0 : 1)); }");
puts("void tpause(const Reg32& r) { int idx = r.getIdx(); if (idx > 7) XBYAK_THROW(ERR_BAD_PARAMETER) db(0x66); db(0x0F); db(0xAE); setModRM(3, 6, idx); }");
puts("void umonitor(const Reg& r) { int idx = r.getIdx(); if (idx > 7) XBYAK_THROW(ERR_BAD_PARAMETER) int bit = r.getBit(); if (BIT != bit) { if ((BIT == 32 && bit == 16) || (BIT == 64 && bit == 32)) { db(0x67); } else { XBYAK_THROW(ERR_BAD_SIZE_OF_REGISTER) } } db(0xF3); db(0x0F); db(0xAE); setModRM(3, 6, idx); }");
puts("void umwait(const Reg32& r) { int idx = r.getIdx(); if (idx > 7) XBYAK_THROW(ERR_BAD_PARAMETER) db(0xF2); db(0x0F); db(0xAE); setModRM(3, 6, idx); }");
Expand Down Expand Up @@ -1902,7 +1902,7 @@ void put64()
putMemOp("fxrstor64", "T_0F", 1, 0xAE, 64);
puts("void movq(const Reg64& reg, const Mmx& mmx) { if (mmx.isXMM()) db(0x66); opRR(mmx, reg, T_0F, 0x7E); }");
puts("void movq(const Mmx& mmx, const Reg64& reg) { if (mmx.isXMM()) db(0x66); opRR(mmx, reg, T_0F, 0x6E); }");
puts("void movsxd(const Reg64& reg, const Operand& op) { if (!op.isBit(32)) XBYAK_THROW(ERR_BAD_COMBINATION) opRO(reg, op, op.isREG(), op.isMEM(), 0, 0x63); }");
puts("void movsxd(const Reg64& reg, const Operand& op) { if (!op.isBit(32)) XBYAK_THROW(ERR_BAD_COMBINATION) opRO(reg, op, op.isREG(), 0, 0x63); }");
puts("void pextrq(const Operand& op, const Xmm& xmm, uint8_t imm) { if (!op.isREG(64) && !op.isMEM()) XBYAK_THROW(ERR_BAD_COMBINATION) opSSE(Reg64(xmm.getIdx()), op, T_66 | T_0F3A, 0x16, 0, imm); }");
puts("void pinsrq(const Xmm& xmm, const Operand& op, uint8_t imm) { if (!op.isREG(64) && !op.isMEM()) XBYAK_THROW(ERR_BAD_COMBINATION) opSSE(Reg64(xmm.getIdx()), op, T_66 | T_0F3A, 0x22, 0, imm); }");
puts("void senduipi(const Reg64& r) { opRR(Reg32(6), r.cvt32(), T_F3 | T_0F, 0xC7); }");
Expand Down
57 changes: 25 additions & 32 deletions xbyak/xbyak.h
Original file line number Diff line number Diff line change
Expand Up @@ -2175,38 +2175,31 @@ class CodeGenerator : public CodeArray {
if (_cl.getIdx() != Operand::CL) XBYAK_THROW(ERR_BAD_COMBINATION)
opRext(op, 0, ext, 0, 0xD2);
}
void opRO(const Reg& r, const Operand& op, bool condR, bool condM, int type, int code, int immSize = 0)
void opRO(const Reg& r, const Operand& op, bool condR, int type, int code, int immSize = 0)
{
#if 0
if (condM) {
if (op.isMEM()) {
opMR(op.getAddress(), r, type, code, immSize);
} else if (condR) {
opRR(r, op.getReg(), type, code);
#else
if (condR) {
opRR(r, op.getReg(), type, code);
} else if (condM) {
opMR(op.getAddress(), r, type, code, immSize);
#endif
} else {
XBYAK_THROW(ERR_BAD_COMBINATION)
}
}
void opShxd(const Operand& op, const Reg& reg, uint8_t imm, int code, const Reg8 *_cl = 0)
{
if (_cl && _cl->getIdx() != Operand::CL) XBYAK_THROW(ERR_BAD_COMBINATION)
opRO(reg, op, (op.isREG(16 | i32e) && op.getBit() == reg.getBit()), op.isMEM() && (reg.isREG(16 | i32e)), T_0F, code | (_cl ? 1 : 0), _cl ? 0 : 1);
if (!reg.isREG(16 | i32e)) XBYAK_THROW(ERR_BAD_SIZE_OF_REGISTER)
opRO(reg, op, op.isREG(), T_0F, code | (_cl ? 1 : 0), _cl ? 0 : 1);
if (!_cl) db(imm);
}
// (REG, REG|MEM), (MEM, REG)
void opRO_MR(const Operand& op1, const Operand& op2, int code)
{
if (op1.isREG() && op2.isMEM()) {
if (op2.isMEM()) {
if (!op1.isREG()) XBYAK_THROW(ERR_BAD_COMBINATION)
opMR(op2.getAddress(), op1.getReg(), 0, code | 2);
} else if (op2.isREG()) {
opRO(static_cast<const Reg&>(op2), op1, op1.isREG() && op1.getKind() == op2.getKind(), op1.isMEM() && op2.isREG(), 0, code);
} else {
XBYAK_THROW(ERR_BAD_COMBINATION)
opRO(static_cast<const Reg&>(op2), op1, op1.isREG() && op1.getKind() == op2.getKind(), 0, code);
}
}
uint32_t getImmBit(const Operand& op, uint32_t imm)
Expand Down Expand Up @@ -2323,8 +2316,8 @@ class CodeGenerator : public CodeArray {
{
if (op.isBit(32)) XBYAK_THROW(ERR_BAD_COMBINATION)
int w = op.isBit(16);
bool cond = reg.isREG() && (reg.getBit() > op.getBit());
opRO(reg, op, cond && op.isREG(), cond && op.isMEM(), T_0F, code | w);
if (!(reg.isREG() && (reg.getBit() > op.getBit()))) XBYAK_THROW(ERR_BAD_COMBINATION)
opRO(reg, op, op.isREG(), T_0F, code | w);
}
void opFpuMem(const Address& addr, uint8_t m16, uint8_t m32, uint8_t m64, uint8_t ext, uint8_t m64ext)
{
Expand Down Expand Up @@ -2468,7 +2461,7 @@ class CodeGenerator : public CodeArray {
bool is16bit = reg.isREG(16) && (op.isREG(16) || op.isMEM());
if (!is16bit && !(reg.isREG(i32e) && (op.isREG(reg.getBit()) || op.isMEM()))) XBYAK_THROW(ERR_BAD_COMBINATION)
if (is16bit) db(0x66);
opRO(reg.changeBit(i32e == 32 ? 32 : reg.getBit()), op, op.isREG(), true, T_F3 | T_0F, code);
opRO(reg.changeBit(i32e == 32 ? 32 : reg.getBit()), op, op.isREG(), T_F3 | T_0F, code);
}
void opGather(const Xmm& x1, const Address& addr, const Xmm& x2, int type, uint8_t code, int mode)
{
Expand Down Expand Up @@ -2691,7 +2684,7 @@ class CodeGenerator : public CodeArray {

void test(const Operand& op, const Reg& reg)
{
opRO(reg, op, op.isREG() && (op.getKind() == reg.getKind()), op.isMEM(), 0, 0x84);
opRO(reg, op, op.isREG() && (op.getKind() == reg.getKind()), 0, 0x84);
}
void test(const Operand& op, uint32_t imm)
{
Expand All @@ -2707,13 +2700,13 @@ class CodeGenerator : public CodeArray {
}
void imul(const Reg& reg, const Operand& op)
{
opRO(reg, op, op.isREG() && (reg.getKind() == op.getKind()), op.isMEM(), T_0F, 0xAF);
opRO(reg, op, op.isREG() && (reg.getKind() == op.getKind()), T_0F, 0xAF);
}
void imul(const Reg& reg, const Operand& op, int imm)
{
int s = inner::IsInDisp8(imm) ? 1 : 0;
int immSize = s ? 1 : reg.isREG(16) ? 2 : 4;
opRO(reg, op, op.isREG() && (reg.getKind() == op.getKind()), op.isMEM(), 0, 0x69 | (s << 1), immSize);
opRO(reg, op, op.isREG() && (reg.getKind() == op.getKind()), 0, 0x69 | (s << 1), immSize);
db(imm, immSize);
}
void push(const Operand& op) { opPushPop(op, 0xFF, 6, 0x50); }
Expand All @@ -2737,26 +2730,26 @@ class CodeGenerator : public CodeArray {
push(dword, imm);
}
}
void mov(const Operand& reg1, const Operand& reg2)
void mov(const Operand& op1, const Operand& op2)
{
const Reg *reg = 0;
const Address *addr = 0;
uint8_t code = 0;
if (reg1.isREG() && reg1.getIdx() == 0 && reg2.isMEM()) { // mov eax|ax|al, [disp]
reg = &reg1.getReg();
addr= &reg2.getAddress();
if (op1.isREG() && op1.getIdx() == 0 && op2.isMEM()) { // mov eax|ax|al, [disp]
reg = &op1.getReg();
addr= &op2.getAddress();
code = 0xA0;
} else
if (reg1.isMEM() && reg2.isREG() && reg2.getIdx() == 0) { // mov [disp], eax|ax|al
reg = &reg2.getReg();
addr= &reg1.getAddress();
if (op1.isMEM() && op2.isREG() && op2.getIdx() == 0) { // mov [disp], eax|ax|al
reg = &op2.getReg();
addr= &op1.getAddress();
code = 0xA2;
}
#ifdef XBYAK64
if (addr && addr->is64bitDisp()) {
if (code) {
rex(*reg);
db(reg1.isREG(8) ? 0xA0 : reg1.isREG() ? 0xA1 : reg2.isREG(8) ? 0xA2 : 0xA3);
db(op1.isREG(8) ? 0xA0 : op1.isREG() ? 0xA1 : op2.isREG(8) ? 0xA2 : 0xA3);
db(addr->getDisp(), 8);
} else {
XBYAK_THROW(ERR_BAD_COMBINATION)
Expand All @@ -2770,7 +2763,7 @@ class CodeGenerator : public CodeArray {
} else
#endif
{
opRO_MR(reg1, reg2, 0x88);
opRO_MR(op1, op2, 0x88);
}
}
void mov(const Operand& op, uint64_t imm)
Expand Down Expand Up @@ -2820,7 +2813,7 @@ class CodeGenerator : public CodeArray {
rex(*p2, *p1); db(0x90 | (p2->getIdx() & 7));
return;
}
opRO(static_cast<const Reg&>(*p1), *p2, (p1->isREG() && p2->isREG() && (p1->getBit() == p2->getBit())), p2->isMEM(), 0, 0x86 | (p1->isBit(8) ? 0 : 1));
opRO(static_cast<const Reg&>(*p1), *p2, (p1->isREG() && p2->isREG() && (p1->getBit() == p2->getBit())), 0, 0x86 | (p1->isBit(8) ? 0 : 1));
}

#ifndef XBYAK_DISABLE_SEGMENT
Expand Down Expand Up @@ -2865,11 +2858,11 @@ class CodeGenerator : public CodeArray {
}
void mov(const Operand& op, const Segment& seg)
{
opRO(Reg8(seg.getIdx()), op, op.isREG(16|i32e), op.isMEM(), 0, 0x8C);
opRO(Reg8(seg.getIdx()), op, op.isREG(16|i32e), 0, 0x8C);
}
void mov(const Segment& seg, const Operand& op)
{
opRO(Reg8(seg.getIdx()), op.isREG(16|i32e) ? static_cast<const Operand&>(op.getReg().cvt32()) : op, op.isREG(16|i32e), op.isMEM(), 0, 0x8E);
opRO(Reg8(seg.getIdx()), op.isREG(16|i32e) ? static_cast<const Operand&>(op.getReg().cvt32()) : op, op.isREG(16|i32e), 0, 0x8E);
}

// (r, r, m) or (r, m, r)
Expand Down
Loading

0 comments on commit b220be9

Please sign in to comment.