From: Jonathan Wakely <jwakely@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Implement floordiv operator for gdb.Value
Date: Tue, 20 Sep 2016 13:34:00 -0000 [thread overview]
Message-ID: <20160920132633.GA897@redhat.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 961 bytes --]
This is my attempt to implement the // operator on gdb.Value objects.
There is already BINOP_INTDIV which works fine for integral types, but
for floats I use BINOP_DIV and then call floor() on the result. This
doesn't support decimal floats though.
Is this a reasonable solution? Is the test sufficient?
I have a follow-up patch which changes the meaning of the / operator
for gdb.Value when built against Python 3, to be consistent with
Python (see comment 1 in the Bugzilla PR) but I expect that to be more
controversial :-)
gdb/ChangeLog:
2016-09-20 Jonathan Wakely <jwakely@redhat.com>
PR python/20624
* python/py-value.c (VALPY_FLOORDIV): Define new enumerator.
(valpy_binop_throw): Handle VALPY_FLOORDIV.
(valpy_floordiv): New function.
(value_object_as_number): Set valpy_floordiv in relevant slot.
gdb/testsuite/ChangeLog:
2016-09-20 Jonathan Wakely <jwakely@redhat.com>
PR python/20624
* gdb.python/py-value.exp: Test floor division.
[-- Attachment #2: floordiv.txt --]
[-- Type: text/plain, Size: 4343 bytes --]
commit 3a019f3ba61bde5320419f5782d8f2554ac55ace
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Sep 20 10:38:35 2016 +0100
gdb: Implement floordiv operator for gdb.Value
gdb/ChangeLog:
2016-09-20 Jonathan Wakely <jwakely@redhat.com>
PR python/20624
* python/py-value.c (VALPY_FLOORDIV): Define new enumerator.
(valpy_binop_throw): Handle VALPY_FLOORDIV.
(valpy_floordiv): New function.
(value_object_as_number): Set valpy_floordiv in relevant slot.
gdb/testsuite/ChangeLog:
2016-09-20 Jonathan Wakely <jwakely@redhat.com>
PR python/20624
* gdb.python/py-value.exp: Test floor division.
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 21e9247..f6a6c11 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1011,7 +1011,8 @@ enum valpy_opcode
VALPY_RSH,
VALPY_BITAND,
VALPY_BITOR,
- VALPY_BITXOR
+ VALPY_BITXOR,
+ VALPY_FLOORDIV
};
/* If TYPE is a reference, return the target; otherwise return TYPE. */
@@ -1032,6 +1033,7 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
struct value *res_val = NULL;
enum exp_opcode op = OP_NULL;
int handled = 0;
+ bool floor_it = false;
/* If the gdb.Value object is the second operand, then it will be
passed to us as the OTHER argument, and SELF will be an entirely
@@ -1113,6 +1115,22 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
case VALPY_REM:
op = BINOP_REM;
break;
+ case VALPY_FLOORDIV:
+ {
+ struct type *ltype = value_type (arg1);
+ struct type *rtype = value_type (arg2);
+ ltype = check_typedef (ltype);
+ rtype = check_typedef (rtype);
+ if (TYPE_CODE (ltype) == TYPE_CODE_FLT
+ || TYPE_CODE (rtype) == TYPE_CODE_FLT)
+ {
+ op = BINOP_DIV;
+ floor_it = true;
+ }
+ else
+ op = BINOP_INTDIV;
+ }
+ break;
case VALPY_POW:
op = BINOP_EXP;
break;
@@ -1142,7 +1160,15 @@ valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
}
if (res_val)
- result = value_to_value_object (res_val);
+ {
+ if (floor_it)
+ {
+ double d = value_as_double (res_val);
+ d = floor (d);
+ res_val = value_from_double (value_type (res_val), d);
+ }
+ result = value_to_value_object (res_val);
+ }
do_cleanups (cleanup);
return result;
@@ -1200,6 +1226,12 @@ valpy_remainder (PyObject *self, PyObject *other)
}
static PyObject *
+valpy_floordiv (PyObject *self, PyObject *other)
+{
+ return valpy_binop (VALPY_FLOORDIV, self, other);
+}
+
+static PyObject *
valpy_power (PyObject *self, PyObject *other, PyObject *unused)
{
/* We don't support the ternary form of pow. I don't know how to express
@@ -1837,7 +1869,7 @@ static PyNumberMethods value_object_as_number = {
NULL, /* nb_inplace_and */
NULL, /* nb_inplace_xor */
NULL, /* nb_inplace_or */
- NULL, /* nb_floor_divide */
+ valpy_floordiv, /* nb_floor_divide */
valpy_divide, /* nb_true_divide */
NULL, /* nb_inplace_floor_divide */
NULL, /* nb_inplace_true_divide */
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 57a9ba1..81837e9 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -87,6 +87,8 @@ proc test_value_numeric_ops {} {
gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values"
gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values"
# Remainder of float is implemented in Python but not in GDB's value system.
+ gdb_test "python print ('result = ' + str(i//j))" " = 2" "floor-divide two integer values"
+ gdb_test "python print ('result = ' + str(f//g))" " = 0" "floor-divide two double values"
gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value"
gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value"
next reply other threads:[~2016-09-20 13:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-20 13:34 Jonathan Wakely [this message]
2016-09-20 14:46 ` Jonathan Wakely
2016-09-20 15:41 ` Pedro Alves
2016-09-20 16:41 ` Jonathan Wakely
2016-09-20 16:42 ` Jonathan Wakely
2016-09-20 17:01 ` Pedro Alves
2016-09-20 17:11 ` Jonathan Wakely
2016-09-20 17:08 ` Paul.Koning
2016-09-20 18:20 ` Jonathan Wakely
2016-09-20 19:06 ` Paul.Koning
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160920132633.GA897@redhat.com \
--to=jwakely@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox