Re: [PATCH 8/9] Add support for fixed-point type arithmetic

Simon Marchi
2020-11-10T23:18:42Z

On 2020-11-08 1:30 a.m., Joel Brobecker wrote:
> diff --git a/gdb/valarith.c b/gdb/valarith.c
> index f6caf3d..65a6f13 100644
> --- a/gdb/valarith.c
> +++ b/gdb/valarith.c
> @@ -881,6 +881,84 @@ value_args_as_target_float (struct value *arg1, struct value *arg2,
> type2->name ());
> }
>
> +/* Assuming at last one of ARG1 or ARG2 is a fixed point value,
> + perform the binary operation OP on these two operands, and return
> + the resulting value (also as a fixed point). */
> +
> +static struct value *
> +fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
> +{
> + struct type *type1 = check_typedef (value_type (arg1));
> + struct type *type2 = check_typedef (value_type (arg2));
> +
> + struct value *val;
> +
> + gdb_assert (is_fixed_point_type (type1) || is_fixed_point_type (type2));
> + if (!is_fixed_point_type (type1))
> + {
> + arg1 = value_cast (type2, arg1);
> + type1 = type2;
> + }
> + if (!is_fixed_point_type (type2))
> + {
> + arg2 = value_cast (type1, arg2);
> + type2 = type1;
> + }
> +
> + gdb_mpq v1, v2, res;
> + v1.read_fixed_point (value_contents (arg1), TYPE_LENGTH (type1),
> + type_byte_order (type1), type1->is_unsigned (),
> + fixed_point_scaling_factor (type1));
> + v2.read_fixed_point (value_contents (arg2), TYPE_LENGTH (type2),
> + type_byte_order (type2), type2->is_unsigned (),
> + fixed_point_scaling_factor (type2));
> +
> +#define INIT_VAL_WITH_FIXED_POINT_VAL(RESULT) \
> + do { \
> + val = allocate_value (type1); \
> + (RESULT).write_fixed_point \
> + (value_contents_raw (val), TYPE_LENGTH (type1), \
> + type_byte_order (type1), type1->is_unsigned (), \
> + fixed_point_scaling_factor (type1)); \
> + } while (0)

Can you use a lambda function instead of a macro? It's easier to
debug and edit. Something like

auto fixed_point_to_value = [type1] (const gdb_mpq &fp)
{
value *val_ = allocate_value (type1);

fp.write_fixed_point (value_contents_raw (val_),
TYPE_LENGTH (type1),
type_byte_order (type1),
type1->is_unsigned (),
fixed_point_scaling_factor (type1));

return val_;
};

and

val = fixed_point_to_value (res);

Simon