[v3,104/206] Introduce comma_operation

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

Commit Message

Tom Tromey Feb. 20, 2021, 8:14 p.m.
This adds class comma_operation, which implements BINOP_COMMA.

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

	* ax-gdb.c (comma_operation::do_generate_ax): New method.
---
 gdb/ChangeLog |  4 ++++
 gdb/ax-gdb.c  | 22 ++++++++++++++++++++++
 gdb/expop.h   | 31 +++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)

-- 
2.26.2

Patch

diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index 332840456fe..8d5e4fff903 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -2469,6 +2469,28 @@  repeat_operation::do_generate_ax (struct expression *exp,
   value->type = array;
 }
 
+void
+comma_operation::do_generate_ax (struct expression *exp,
+				 struct agent_expr *ax,
+				 struct axs_value *value,
+				 struct type *cast_type)
+{
+  /* Note that we need to be a little subtle about generating code
+     for comma.  In C, we can do some optimizations here because
+     we know the left operand is only being evaluated for effect.
+     However, if the tracing kludge is in effect, then we always
+     need to evaluate the left hand side fully, so that all the
+     variables it mentions get traced.  */
+  struct axs_value value1;
+  std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
+  /* Don't just dispose of the left operand.  We might be tracing,
+     in which case we want to emit code to trace it if it's an
+     lvalue.  */
+  gen_traced_pop (ax, &value1);
+  std::get<1> (m_storage)->generate_ax (exp, ax, value);
+  /* It's the consumer's responsibility to trace the right operand.  */
+}
+
 }
 
 /* This handles the middle-to-right-side of code generation for binary
diff --git a/gdb/expop.h b/gdb/expop.h
index 4bf32f538e1..2fa5b2f2055 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -1208,6 +1208,37 @@  class repeat_operation
     override;
 };
 
+/* C-style comma operator.  */
+class comma_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
+  {
+    /* The left-hand-side is only evaluated for side effects, so don't
+       bother in other modes.  */
+    if (noside == EVAL_NORMAL)
+      std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
+    return std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
+  }
+
+  enum exp_opcode opcode () const override
+  { return BINOP_COMMA; }
+
+protected:
+
+  void do_generate_ax (struct expression *exp,
+		       struct agent_expr *ax,
+		       struct axs_value *value,
+		       struct type *cast_type)
+    override;
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */