[PATCH 016/203] Split out eval_op_binary

Tom Tromey
2021-01-01T21:47:41Z

This splits out a new eval_op_binary helper function. This function
can handle several different binary operations:

case BINOP_EXP:
case BINOP_MUL:
case BINOP_DIV:
case BINOP_INTDIV:
case BINOP_REM:
case BINOP_MOD:
case BINOP_LSH:
case BINOP_RSH:
case BINOP_BITWISE_AND:
case BINOP_BITWISE_IOR:
case BINOP_BITWISE_XOR:

gdb/ChangeLog
2021-01-01 Tom Tromey <tom@tromey.com>

* eval.c (eval_op_binary): New function.
(evaluate_subexp_standard): Use it.
---
gdb/ChangeLog | 5 +++
gdb/eval.c | 86 ++++++++++++++++++++++++++++-----------------------
2 files changed, 53 insertions(+), 38 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index a94f61b0cec..5f9d5413498 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1493,6 +1493,53 @@ eval_op_sub (struct type *expect_type, struct expression *exp,
}
}

+/* Helper function for several different binary operations. */
+
+static struct value *
+eval_op_binary (struct type *expect_type, struct expression *exp,
+ enum noside noside, enum exp_opcode op,
+ struct value *arg1, struct value *arg2)
+{
+ if (noside == EVAL_SKIP)
+ return eval_skip_value (exp);
+ if (binop_user_defined_p (op, arg1, arg2))
+ return value_x_binop (arg1, arg2, op, OP_NULL, noside);
+ else
+ {
+ /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
+ fudge arg2 to avoid division-by-zero, the caller is
+ (theoretically) only looking for the type of the result. */
+ if (noside == EVAL_AVOID_SIDE_EFFECTS
+ /* ??? Do we really want to test for BINOP_MOD here?
+ The implementation of value_binop gives it a well-defined
+ value. */
+ && (op == BINOP_DIV
+ || op == BINOP_INTDIV
+ || op == BINOP_REM
+ || op == BINOP_MOD)
+ && value_logical_not (arg2))
+ {
+ struct value *v_one;
+
+ v_one = value_one (value_type (arg2));
+ binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
+ return value_binop (arg1, v_one, op);
+ }
+ else
+ {
+ /* For shift and integer exponentiation operations,
+ only promote the first argument. */
+ if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
+ && is_integral_type (value_type (arg2)))
+ unop_promote (exp->language_defn, exp->gdbarch, &arg1);
+ else
+ binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
+
+ return value_binop (arg1, arg2, op);
+ }
+ }
+}
+
struct value *
evaluate_subexp_standard (struct type *expect_type,
struct expression *exp, int *pos,
@@ -2199,44 +2246,7 @@ evaluate_subexp_standard (struct type *expect_type,
case BINOP_BITWISE_XOR:
arg1 = evaluate_subexp (nullptr, exp, pos, noside);
arg2 = evaluate_subexp (nullptr, exp, pos, noside);
- if (noside == EVAL_SKIP)
- return eval_skip_value (exp);
- if (binop_user_defined_p (op, arg1, arg2))
- return value_x_binop (arg1, arg2, op, OP_NULL, noside);
- else
- {
- /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
- fudge arg2 to avoid division-by-zero, the caller is
- (theoretically) only looking for the type of the result. */
- if (noside == EVAL_AVOID_SIDE_EFFECTS
- /* ??? Do we really want to test for BINOP_MOD here?
- The implementation of value_binop gives it a well-defined
- value. */
- && (op == BINOP_DIV
- || op == BINOP_INTDIV
- || op == BINOP_REM
- || op == BINOP_MOD)
- && value_logical_not (arg2))
- {
- struct value *v_one;
-
- v_one = value_one (value_type (arg2));
- binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
- return value_binop (arg1, v_one, op);
- }
- else
- {
- /* For shift and integer exponentiation operations,
- only promote the first argument. */
- if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
- && is_integral_type (value_type (arg2)))
- unop_promote (exp->language_defn, exp->gdbarch, &arg1);
- else
- binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
-
- return value_binop (arg1, arg2, op);
- }
- }
+ return eval_op_binary (expect_type, exp, noside, op, arg1, arg2);

case BINOP_SUBSCRIPT:
arg1 = evaluate_subexp (nullptr, exp, pos, noside);
--
2.26.2