Don't clobber flags, fix asserts a bit

This commit is contained in:
offtkp 2024-09-02 00:40:01 +03:00
commit d9f7d8a09d

View file

@ -742,13 +742,15 @@ static void GenerateINSERTQ(const ZydisDecodedOperand* operands, Xbyak::CodeGene
bool immediateForm = operands[2].type == ZYDIS_OPERAND_TYPE_IMMEDIATE && bool immediateForm = operands[2].type == ZYDIS_OPERAND_TYPE_IMMEDIATE &&
operands[3].type == ZYDIS_OPERAND_TYPE_IMMEDIATE; operands[3].type == ZYDIS_OPERAND_TYPE_IMMEDIATE;
if (operands[0].type != ZYDIS_OPERAND_TYPE_REGISTER || ASSERT_MSG(operands[0].type == ZYDIS_OPERAND_TYPE_REGISTER &&
operands[1].type != ZYDIS_OPERAND_TYPE_REGISTER) { operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER,
ASSERT_MSG("operands 0 and 1 must be registers."); "operands 0 and 1 must be registers.");
}
const auto dst = ZydisToXbyakRegisterOperand(operands[0]); const auto dst = ZydisToXbyakRegisterOperand(operands[0]);
const auto src = ZydisToXbyakRegisterOperand(operands[1]); const auto src = ZydisToXbyakRegisterOperand(operands[1]);
ASSERT_MSG(dst.isXMM() && src.isXMM(), "operands 0 and 1 must be xmm registers.");
Xbyak::Xmm xmm_dst = *reinterpret_cast<const Xbyak::Xmm*>(&dst); Xbyak::Xmm xmm_dst = *reinterpret_cast<const Xbyak::Xmm*>(&dst);
Xbyak::Xmm xmm_src = *reinterpret_cast<const Xbyak::Xmm*>(&src); Xbyak::Xmm xmm_src = *reinterpret_cast<const Xbyak::Xmm*>(&src);
@ -759,16 +761,15 @@ static void GenerateINSERTQ(const ZydisDecodedOperand* operands, Xbyak::CodeGene
length = 64; length = 64;
} }
if (length + index > 64) { ASSERT_MSG(length + index <= 64, "length + index must be less than or equal to 64.");
ASSERT_MSG("length + index must be less than or equal to 64.");
}
const Xbyak::Reg64 scratch1 = rax; const Xbyak::Reg64 scratch1 = rax;
const Xbyak::Reg64 scratch2 = rcx; const Xbyak::Reg64 scratch2 = rcx;
const Xbyak::Reg64 mask = rdx; const Xbyak::Reg64 mask = rdx;
// Set rsp to before red zone and save scratch registers // Set rsp to before red zone and save scratch registers
c.sub(rsp, 128); c.lea(rsp, ptr[rsp - 128]);
c.pushfq();
c.push(scratch1); c.push(scratch1);
c.push(scratch2); c.push(scratch2);
c.push(mask); c.push(mask);
@ -804,19 +805,20 @@ static void GenerateINSERTQ(const ZydisDecodedOperand* operands, Xbyak::CodeGene
c.pop(mask); c.pop(mask);
c.pop(scratch2); c.pop(scratch2);
c.pop(scratch1); c.pop(scratch1);
c.add(rsp, 128); c.popfq();
c.lea(rsp, ptr[rsp + 128]);
} else { } else {
if (operands[2].type != ZYDIS_OPERAND_TYPE_UNUSED || ASSERT_MSG(operands[2].type == ZYDIS_OPERAND_TYPE_UNUSED &&
operands[3].type != ZYDIS_OPERAND_TYPE_UNUSED) { operands[3].type == ZYDIS_OPERAND_TYPE_UNUSED,
ASSERT_MSG("operands 2 and 3 must be unused for register form."); "operands 2 and 3 must be unused for register form.");
}
const Xbyak::Reg64 scratch1 = rax; const Xbyak::Reg64 scratch1 = rax;
const Xbyak::Reg64 scratch2 = rcx; const Xbyak::Reg64 scratch2 = rcx;
const Xbyak::Reg64 index = rdx; const Xbyak::Reg64 index = rdx;
const Xbyak::Reg64 mask = rbx; const Xbyak::Reg64 mask = rbx;
c.sub(rsp, 128); c.lea(rsp, ptr[rsp - 128]);
c.pushfq();
c.push(scratch1); c.push(scratch1);
c.push(scratch2); c.push(scratch2);
c.push(index); c.push(index);
@ -869,7 +871,8 @@ static void GenerateINSERTQ(const ZydisDecodedOperand* operands, Xbyak::CodeGene
c.pop(index); c.pop(index);
c.pop(scratch2); c.pop(scratch2);
c.pop(scratch1); c.pop(scratch1);
c.add(rsp, 128); c.popfq();
c.lea(rsp, ptr[rsp + 128]);
} }
} }