From 5133d78b7bf19f350865c9515839865503cacfa9 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tom@tromey.com>
Date: Mon, 8 Mar 2021 07:27:57 -0700
Subject: [PATCH] Introduce sub_operation

This adds class sub_operation, which implements BINOP_SUB.

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

	* expop.h (class sub_operation): New.
	* eval.c (eval_op_sub): No longer static.  Remove "op" parameter.
	(evaluate_subexp_standard): Update.
---
 gdb/ChangeLog |  6 ++++++
 gdb/eval.c    | 10 +++++-----
 gdb/expop.h   | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 5fece77c5ea..50a933a3a28 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-08  Tom Tromey  <tom@tromey.com>
+
+	* expop.h (class sub_operation): New.
+	* eval.c (eval_op_sub): No longer static.  Remove "op" parameter.
+	(evaluate_subexp_standard): Update.
+
 2021-03-08  Tom Tromey  <tom@tromey.com>
 
 	* expop.h (class add_operation): New.
diff --git a/gdb/eval.c b/gdb/eval.c
index cba5bcce332..87c8b0d69e0 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1483,15 +1483,15 @@ eval_op_add (struct type *expect_type, struct expression *exp,
 
 /* A helper function for BINOP_SUB.  */
 
-static struct value *
+struct value *
 eval_op_sub (struct type *expect_type, struct expression *exp,
-	     enum noside noside, enum exp_opcode op,
+	     enum noside noside,
 	     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);
+  if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
+    return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
   else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
 	   && ptrmath_type_p (exp->language_defn, value_type (arg2)))
     {
@@ -2827,7 +2827,7 @@ evaluate_subexp_standard (struct type *expect_type,
     case BINOP_SUB:
       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
-      return eval_op_sub (expect_type, exp, noside, op, arg1, arg2);
+      return eval_op_sub (expect_type, exp, noside, arg1, arg2);
 
     case BINOP_EXP:
     case BINOP_MUL:
diff --git a/gdb/expop.h b/gdb/expop.h
index 492d7babd49..c996d520809 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -92,6 +92,10 @@ extern struct value *eval_op_add (struct type *expect_type,
 				  struct expression *exp,
 				  enum noside noside,
 				  struct value *arg1, struct value *arg2);
+extern struct value *eval_op_sub (struct type *expect_type,
+				  struct expression *exp,
+				  enum noside noside,
+				  struct value *arg1, struct value *arg2);
 
 namespace expr
 {
@@ -1001,6 +1005,42 @@ protected:
   }
 };
 
+class sub_operation
+  : public maybe_constant_operation<operation_up, operation_up>
+{
+public:
+
+  using maybe_constant_operation::maybe_constant_operation;
+
+  value *evaluate (struct type *expect_type,
+		   struct expression *exp,
+		   enum noside noside) override
+  {
+    value *lhs
+      = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
+    value *rhs
+      = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
+    return eval_op_sub (expect_type, exp, noside, lhs, rhs);
+  }
+
+  enum exp_opcode opcode () const override
+  { return BINOP_SUB; }
+
+protected:
+
+  void do_generate_ax (struct expression *exp,
+		       struct agent_expr *ax,
+		       struct axs_value *value,
+		       struct type *cast_type)
+    override
+  {
+    gen_expr_binop (exp, BINOP_SUB,
+		    std::get<0> (this->m_storage).get (),
+		    std::get<1> (this->m_storage).get (),
+		    ax, value);
+  }
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */