[PATCH 041/203] Split out eval_op_f_modulo

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

This splits BINOP_FORTRAN_MODULO into a new function for future use.

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

* f-lang.c (eval_op_f_modulo): New function.
(evaluate_subexp_f): Use it.
---
gdb/ChangeLog | 5 ++++
gdb/f-lang.c | 80 ++++++++++++++++++++++++++++-----------------------
2 files changed, 49 insertions(+), 36 deletions(-)

diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index df77a852fbc..b127f6edf05 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -807,6 +807,47 @@ eval_op_f_floor (struct type *expect_type, struct expression *exp,
return value_from_host_double (type, val);
}

+/* A helper function for BINOP_FORTRAN_MODULO. */
+
+static struct value *
+eval_op_f_modulo (struct type *expect_type, struct expression *exp,
+ enum noside noside,
+ struct value *arg1, struct value *arg2)
+{
+ if (noside == EVAL_SKIP)
+ return eval_skip_value (exp);
+ struct type *type = value_type (arg1);
+ if (type->code () != value_type (arg2)->code ())
+ error (_("non-matching types for parameters to MODULO ()"));
+ /* MODULO(A, P) = A - FLOOR (A / P) * P */
+ switch (type->code ())
+ {
+ case TYPE_CODE_INT:
+ {
+ LONGEST a = value_as_long (arg1);
+ LONGEST p = value_as_long (arg2);
+ LONGEST result = a - (a / p) * p;
+ if (result != 0 && (a < 0) != (p < 0))
+ result += p;
+ return value_from_longest (value_type (arg1), result);
+ }
+ case TYPE_CODE_FLT:
+ {
+ double a
+ = target_float_to_host_double (value_contents (arg1),
+ value_type (arg1));
+ double p
+ = target_float_to_host_double (value_contents (arg2),
+ value_type (arg2));
+ double result = fmod (a, p);
+ if (result != 0 && (a < 0.0) != (p < 0.0))
+ result += p;
+ return value_from_host_double (type, result);
+ }
+ }
+ error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
+}
+
/* Special expression evaluation cases for Fortran. */

static struct value *
@@ -846,42 +887,9 @@ evaluate_subexp_f (struct type *expect_type, struct expression *exp,
return eval_op_f_floor (expect_type, exp, noside, arg1);

case BINOP_FORTRAN_MODULO:
- {
- arg1 = evaluate_subexp (nullptr, exp, pos, noside);
- arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
- if (noside == EVAL_SKIP)
- return eval_skip_value (exp);
- type = value_type (arg1);
- if (type->code () != value_type (arg2)->code ())
- error (_("non-matching types for parameters to MODULO ()"));
- /* MODULO(A, P) = A - FLOOR (A / P) * P */
- switch (type->code ())
- {
- case TYPE_CODE_INT:
- {
- LONGEST a = value_as_long (arg1);
- LONGEST p = value_as_long (arg2);
- LONGEST result = a - (a / p) * p;
- if (result != 0 && (a < 0) != (p < 0))
- result += p;
- return value_from_longest (value_type (arg1), result);
- }
- case TYPE_CODE_FLT:
- {
- double a
- = target_float_to_host_double (value_contents (arg1),
- value_type (arg1));
- double p
- = target_float_to_host_double (value_contents (arg2),
- value_type (arg2));
- double result = fmod (a, p);
- if (result != 0 && (a < 0.0) != (p < 0.0))
- result += p;
- return value_from_host_double (type, result);
- }
- }
- error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type));
- }
+ arg1 = evaluate_subexp (nullptr, exp, pos, noside);
+ arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
+ return eval_op_f_modulo (expect_type, exp, noside, arg1, arg2);

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