[v3,015/206] Split out eval_op_binary

Message ID 20210220201609.838264-16-tom@tromey.com
State New
Headers show
Series
  • Refactor expressions
Related show

Commit Message

Tom Tromey Feb. 20, 2021, 8:12 p.m.
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-02-20  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(-)

-- 
2.26.2

Patch

diff --git a/gdb/eval.c b/gdb/eval.c
index 3c4980b45cf..e0c80035e2b 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1508,6 +1508,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,
@@ -2214,44 +2261,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);