From 9307d17b7a97bd49baf0d877785c43cf203b7cee Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 8 Mar 2021 07:27:57 -0700 Subject: Implement some unary operations This introduces a couple of new template classes and then uses them to implement some simple unary operations. gdb/ChangeLog 2021-03-08 Tom Tromey * expop.h (unary_ftype): New typedef. (unop_operation, usual_ax_binop_operation): New templates. (unary_plus_operation, unary_neg_operation) (unary_complement_operation, unary_logical_not_operation): New typedefs. * eval.c (eval_op_plus, eval_op_neg, eval_op_complement) (eval_op_lognot): No longer static. * ax-gdb.c (gen_expr_unop): New function. --- gdb/ax-gdb.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'gdb/ax-gdb.c') diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c index 8d5e4ff..c87b290 100644 --- a/gdb/ax-gdb.c +++ b/gdb/ax-gdb.c @@ -2694,6 +2694,53 @@ gen_expr_structop (struct expression *exp, internal_error (__FILE__, __LINE__, _("gen_expr: unhandled struct case")); } + +/* A helper function that emits a unary operation. */ + +void +gen_expr_unop (struct expression *exp, + enum exp_opcode op, + expr::operation *lhs, + struct agent_expr *ax, struct axs_value *value) +{ + struct axs_value value1, value2; + + switch (op) + { + case UNOP_NEG: + gen_int_literal (ax, &value1, 0, + builtin_type (ax->gdbarch)->builtin_int); + gen_usual_unary (ax, &value1); /* shouldn't do much */ + lhs->generate_ax (exp, ax, &value2); + gen_usual_unary (ax, &value2); + gen_usual_arithmetic (ax, &value1, &value2); + gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation"); + break; + + case UNOP_PLUS: + /* + FOO is equivalent to 0 + FOO, which can be optimized. */ + lhs->generate_ax (exp, ax, value); + gen_usual_unary (ax, value); + break; + + case UNOP_LOGICAL_NOT: + lhs->generate_ax (exp, ax, value); + gen_usual_unary (ax, value); + gen_logical_not (ax, value, builtin_type (ax->gdbarch)->builtin_int); + break; + + case UNOP_COMPLEMENT: + lhs->generate_ax (exp, ax, value); + gen_usual_unary (ax, value); + gen_integral_promotions (ax, value); + gen_complement (ax, value); + break; + + default: + gdb_assert_not_reached ("invalid case in gen_expr_unop"); + } +} + /* Given a single variable and a scope, generate bytecodes to trace -- cgit v1.1