Skip to content

Commit

Permalink
Remove x86_bool_to_bv and use the standard BOOL_TO_BV
Browse files Browse the repository at this point in the history
  • Loading branch information
DMaroo committed Oct 25, 2023
1 parent e08db58 commit e8fcecd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
36 changes: 13 additions & 23 deletions librz/analysis/arch/x86/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,16 +884,6 @@ RzILOpBool *x86_il_is_sub_underflow(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x
return or ;
}

/**
* \brief Convert a bool \p b to a bitvector of length \p bits
*
* \param b
* \param bits
*/
RzILOpBitVector *x86_bool_to_bv(RzILOpBool *b, unsigned int bits) {
return ITE(b, UN(bits, 1), UN(bits, 0));
}

struct x86_parity_helper_t {
RzILOpBool *val; ///< value of parity
RzILOpEffect *eff; ///< RzILOpEffect used to find the parity
Expand All @@ -913,7 +903,7 @@ struct x86_parity_helper_t x86_il_get_parity(RZ_OWN RzILOpPure *val) {
since the value of "_popcnt" wouldn't change any further */
RzILOpBool *condition = NON_ZERO(VARL("_val"));

RzILOpEffect *popcnt = SETL("_popcnt", ADD(VARL("_popcnt"), x86_bool_to_bv(LSB(VARL("_val")), 8)));
RzILOpEffect *popcnt = SETL("_popcnt", ADD(VARL("_popcnt"), BOOL_TO_BV(LSB(VARL("_val")), 8)));
popcnt = SEQ2(popcnt, SETL("_val", SHIFTR0(VARL("_val"), U8(1))));

RzILOpEffect *repeat_eff = REPEAT(condition, popcnt);
Expand Down Expand Up @@ -1009,30 +999,30 @@ RzILOpPure *x86_il_get_flags(unsigned int size) {
always 1 on 8086 and 186,
always 0 on later models
Assuming 0 */
val = x86_bool_to_bv(IL_FALSE, size);
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(NT)), size));
val = BOOL_TO_BV(IL_FALSE, size);
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(NT)), size));

/** Bit 12-13: IOPL,
I/O privilege level (286+ only),
always 1 on 8086 and 186
Assuming all 1 */
val = LOGOR(SHIFTL0(val, UN(size, 2)), UN(size, 0x3));
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(OF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(DF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(IF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(TF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(OF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(DF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(IF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(TF)), size));

lower_half:
if (size == 8) {
val = x86_bool_to_bv(VARG(EFLAGS(SF)), size);
val = BOOL_TO_BV(VARG(EFLAGS(SF)), size);
} else {
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(ZF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(ZF)), size));
}
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(ZF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 2)), x86_bool_to_bv(VARG(EFLAGS(AF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 2)), x86_bool_to_bv(VARG(EFLAGS(PF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(ZF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 2)), BOOL_TO_BV(VARG(EFLAGS(AF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 2)), BOOL_TO_BV(VARG(EFLAGS(PF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), UN(size, 1));
val = LOGOR(SHIFTL0(val, UN(size, 1)), x86_bool_to_bv(VARG(EFLAGS(CF)), size));
val = LOGOR(SHIFTL0(val, UN(size, 1)), BOOL_TO_BV(VARG(EFLAGS(CF)), size));

return val;
}
Expand Down
4 changes: 3 additions & 1 deletion librz/analysis/arch/x86/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ RzILOpPure *x86_il_get_memaddr_bits(X86Mem mem, int bits, ut64 pc);
RzILOpPure *x86_il_get_memaddr_segment_bits(X86Mem mem, X86Reg segment, int bits, ut64 pc);
RzILOpEffect *x86_il_set_mem_bits(X86Mem mem, RzILOpPure *val, int bits, ut64 pc);

RzILOpBool *x86_il_is_add_carry(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x, RZ_OWN RzILOpPure *y);
RzILOpBool *x86_il_is_sub_borrow(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x, RZ_OWN RzILOpPure *y);
RzILOpBitVector *x86_bool_to_bv(RzILOpBool *b, unsigned int bits);
RzILOpBool *x86_il_is_add_overflow(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x, RZ_OWN RzILOpPure *y);
RzILOpBool *x86_il_is_sub_underflow(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x, RZ_OWN RzILOpPure *y);

RzILOpEffect *x86_il_set_result_flags_bits(RZ_OWN RzILOpPure *result, int bits);
RzILOpEffect *x86_il_set_arithmetic_flags_bits(RZ_OWN RzILOpPure *res, RZ_OWN RzILOpPure *x, RZ_OWN RzILOpPure *y, bool addition, int bits);
Expand Down
12 changes: 6 additions & 6 deletions librz/analysis/arch/x86/il_ops.inc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ IL_LIFTER(adc) {
RzILOpEffect *op2 = SETL("op2", x86_il_get_op(1));
RzILOpPure *cf = VARG(EFLAGS(CF));

RzILOpEffect *sum = SETL("sum", ADD(ADD(VARL("op1"), VARL("op2")), x86_bool_to_bv(cf, ins->structure->operands[0].size * BITS_PER_BYTE)));
RzILOpEffect *sum = SETL("sum", ADD(ADD(VARL("op1"), VARL("op2")), BOOL_TO_BV(cf, ins->structure->operands[0].size * BITS_PER_BYTE)));
RzILOpEffect *set_dest = x86_il_set_op(0, VARL("sum"));
RzILOpEffect *set_res_flags = x86_il_set_result_flags(VARL("sum"));
RzILOpEffect *set_arith_flags = x86_il_set_arithmetic_flags(VARL("sum"), VARL("op1"), VARL("op2"), true);
Expand Down Expand Up @@ -1800,7 +1800,7 @@ IL_LIFTER(rcl) {

RzILOpBool *cond = NON_ZERO(VARL("_tmp_cnt"));
RzILOpEffect *repeat = SETL("_tmp_cf", MSB(VARL("_dest")));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTL0(VARL("_dest"), U8(1)), x86_bool_to_bv(VARG(EFLAGS(CF)), BITS_PER_BYTE * size))));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTL0(VARL("_dest"), U8(1)), BOOL_TO_BV(VARG(EFLAGS(CF)), BITS_PER_BYTE * size))));
repeat = SEQ2(repeat, SETG(EFLAGS(CF), VARL("_tmp_cf")));
repeat = SEQ2(repeat, SETL("_tmp_cnt", SUB(VARL("_tmp_cnt"), UN(tmp_count_size, 1))));

Expand All @@ -1826,7 +1826,7 @@ IL_LIFTER(rcr) {

RzILOpBool *cond = NON_ZERO(VARL("_tmp_cnt"));
RzILOpEffect *repeat = SETL("_tmp_cf", LSB(VARL("_dest")));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTR0(VARL("_dest"), U8(1)), SHIFTL0(x86_bool_to_bv(VARG(EFLAGS(CF)), BITS_PER_BYTE * size), U8(size)))));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTR0(VARL("_dest"), U8(1)), SHIFTL0(BOOL_TO_BV(VARG(EFLAGS(CF)), BITS_PER_BYTE * size), U8(size)))));
repeat = SEQ2(repeat, SETG(EFLAGS(CF), VARL("_tmp_cf")));
repeat = SEQ2(repeat, SETL("_tmp_cnt", SUB(VARL("_tmp_cnt"), UN(tmp_count_size, 1))));

Expand Down Expand Up @@ -1862,7 +1862,7 @@ IL_LIFTER(rol) {

RzILOpBool *cond = NON_ZERO(VARL("_tmp_cnt"));
RzILOpEffect *repeat = SETL("_tmp_cf", MSB(VARL("_dest")));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTL0(VARL("_dest"), U8(1)), x86_bool_to_bv(VARL("_tmp_cf"), BITS_PER_BYTE * size))));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTL0(VARL("_dest"), U8(1)), BOOL_TO_BV(VARL("_tmp_cf"), BITS_PER_BYTE * size))));
repeat = SEQ2(repeat, SETL("_tmp_cnt", SUB(VARL("_tmp_cnt"), UN(cnt_size, 1))));

RzILOpBool *if_cond1 = NON_ZERO(VARL("_masked"));
Expand All @@ -1887,7 +1887,7 @@ IL_LIFTER(ror) {

RzILOpBool *cond = NON_ZERO(VARL("_tmp_cnt"));
RzILOpEffect *repeat = SETL("_tmp_cf", LSB(VARL("_dest")));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTR0(VARL("_dest"), U8(1)), SHIFTL0(x86_bool_to_bv(VARL("_tmp_cf"), BITS_PER_BYTE * size), U8(size)))));
repeat = SEQ2(repeat, SETL("_dest", ADD(SHIFTR0(VARL("_dest"), U8(1)), SHIFTL0(BOOL_TO_BV(VARL("_tmp_cf"), BITS_PER_BYTE * size), U8(size)))));
repeat = SEQ2(repeat, SETL("_tmp_cnt", SUB(VARL("_tmp_cnt"), UN(cnt_size, 1))));

RzILOpBool *if_cond1 = NON_ZERO(VARL("_masked"));
Expand Down Expand Up @@ -2066,7 +2066,7 @@ IL_LIFTER(sbb) {
RzILOpEffect *op2 = SETL("_op2", x86_il_get_op(1));
RzILOpPure *cf = VARG(EFLAGS(CF));

RzILOpEffect *diff = SETL("_diff", SUB(SUB(VARL("_op1"), VARL("_op2")), x86_bool_to_bv(cf, ins->structure->operands[0].size * BITS_PER_BYTE)));
RzILOpEffect *diff = SETL("_diff", SUB(SUB(VARL("_op1"), VARL("_op2")), BOOL_TO_BV(cf, ins->structure->operands[0].size * BITS_PER_BYTE)));
RzILOpEffect *set_dest = x86_il_set_op(0, VARL("_diff"));
RzILOpEffect *set_res_flags = x86_il_set_result_flags(VARL("_diff"));
RzILOpEffect *set_arith_flags = x86_il_set_arithmetic_flags(VARL("_diff"), VARL("_op1"), VARL("_op2"), false);
Expand Down

0 comments on commit e8fcecd

Please sign in to comment.