public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 8/8] Fix 128-bit integer bug in Ada
Date: Fri,  3 Mar 2023 14:12:07 -0700	[thread overview]
Message-ID: <20230303211207.1053037-9-tromey@adacore.com> (raw)
In-Reply-To: <20230303211207.1053037-1-tromey@adacore.com>

While working on 128-bit integer support, I found one spot in Ada that
needed a fix as well.
---
 gdb/ada-lang.c                          | 27 ++++--------------
 gdb/testsuite/gdb.ada/verylong.exp      | 37 +++++++++++++++++++++++++
 gdb/testsuite/gdb.ada/verylong/prog.adb | 20 +++++++++++++
 3 files changed, 63 insertions(+), 21 deletions(-)
 create mode 100644 gdb/testsuite/gdb.ada/verylong.exp
 create mode 100644 gdb/testsuite/gdb.ada/verylong/prog.adb

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 7b07c4f9473..d1afb1a27b1 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -62,14 +62,6 @@
 #include "charset.h"
 #include "ax-gdb.h"
 
-/* Define whether or not the C operator '/' truncates towards zero for
-   differently signed operands (truncation direction is undefined in C).
-   Copied from valarith.c.  */
-
-#ifndef TRUNCATION_TOWARDS_ZERO
-#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
-#endif
-
 static struct type *desc_base_type (struct type *);
 
 static struct type *desc_bounds_type (struct type *);
@@ -9351,9 +9343,7 @@ coerce_for_assign (struct type *type, struct value *val)
 static struct value *
 ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
 {
-  struct value *val;
   struct type *type1, *type2;
-  LONGEST v, v1, v2;
 
   arg1 = coerce_ref (arg1);
   arg2 = coerce_ref (arg2);
@@ -9374,8 +9364,8 @@ ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
       return value_binop (arg1, arg2, op);
     }
 
-  v2 = value_as_long (arg2);
-  if (v2 == 0)
+  gdb_mpz v2 = value_as_mpz (arg2);
+  if (v2.sgn () == 0)
     {
       const char *name;
       if (op == BINOP_MOD)
@@ -9394,13 +9384,12 @@ ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
   if (type1->is_unsigned () || op == BINOP_MOD)
     return value_binop (arg1, arg2, op);
 
-  v1 = value_as_long (arg1);
+  gdb_mpz v1 = value_as_mpz (arg1);
+  gdb_mpz v;
   switch (op)
     {
     case BINOP_DIV:
       v = v1 / v2;
-      if (!TRUNCATION_TOWARDS_ZERO && v1 * (v1 % v2) < 0)
-	v += v > 0 ? -1 : 1;
       break;
     case BINOP_REM:
       v = v1 % v2;
@@ -9409,14 +9398,10 @@ ada_value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
       break;
     default:
       /* Should not reach this point.  */
-      v = 0;
+      gdb_assert_not_reached ("invalid operator");
     }
 
-  val = value::allocate (type1);
-  store_unsigned_integer (val->contents_raw ().data (),
-			  val->type ()->length (),
-			  type_byte_order (type1), v);
-  return val;
+  return value_from_mpz (type1, v);
 }
 
 static int
diff --git a/gdb/testsuite/gdb.ada/verylong.exp b/gdb/testsuite/gdb.ada/verylong.exp
new file mode 100644
index 00000000000..93df785862b
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/verylong.exp
@@ -0,0 +1,37 @@
+# Copyright 2023 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+load_lib "ada.exp"
+
+require allow_ada_tests
+
+standard_ada_testfile prog
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable {debug}] != ""} {
+    return -1
+}
+
+clean_restart ${testfile}
+
+set bp_location [gdb_get_line_number "START" ${testdir}/prog.adb]
+runto "prog.adb:$bp_location"
+
+gdb_test "print x" " = 170141183460469231731687303715884105727"
+gdb_test "print x / 2" " = 85070591730234615865843651857942052863"
+gdb_test "print (x / 4) * 2" " = 85070591730234615865843651857942052862"
+gdb_test "print x - x" " = 0"
+gdb_test "print x - 99 + 1" " = 170141183460469231731687303715884105629"
+gdb_test "print -x" " = -170141183460469231731687303715884105727"
+gdb_test "print +x" " = 170141183460469231731687303715884105727"
diff --git a/gdb/testsuite/gdb.ada/verylong/prog.adb b/gdb/testsuite/gdb.ada/verylong/prog.adb
new file mode 100644
index 00000000000..766419d0a4f
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/verylong/prog.adb
@@ -0,0 +1,20 @@
+--  Copyright 2023 Free Software Foundation, Inc.
+--
+--  This program is free software; you can redistribute it and/or modify
+--  it under the terms of the GNU General Public License as published by
+--  the Free Software Foundation; either version 3 of the License, or
+--  (at your option) any later version.
+--
+--  This program is distributed in the hope that it will be useful,
+--  but WITHOUT ANY WARRANTY; without even the implied warranty of
+--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--  GNU General Public License for more details.
+--
+--  You should have received a copy of the GNU General Public License
+--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+procedure Main is
+   X : Long_Long_Long_Integer := Long_Long_Long_Integer'Last;
+begin
+   null; -- START
+end Main;
-- 
2.39.1


  parent reply	other threads:[~2023-03-03 21:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03 21:11 [PATCH 0/8] Arithmetic for 128-bit types Tom Tromey
2023-03-03 21:12 ` [PATCH 1/8] Add many operators to gdb_mpz Tom Tromey
2023-03-07 13:38   ` Alexandra Petlanova Hajkova
2023-03-03 21:12 ` [PATCH 2/8] Avoid a copy in gdb_mpz::safe_export Tom Tromey
2023-03-03 21:12 ` [PATCH 3/8] Add truncation mode to gdb_mpz Tom Tromey
2023-03-03 21:12 ` [PATCH 4/8] Add value_as_mpz and value_from_mpz Tom Tromey
2023-03-08 10:47   ` Lancelot SIX
2023-03-08 15:48     ` Tom Tromey
2023-03-03 21:12 ` [PATCH 5/8] Simplify binop_promote Tom Tromey
2023-03-03 21:12 ` [PATCH 6/8] Use value_true in value_equal and value_less Tom Tromey
2023-03-03 21:12 ` [PATCH 7/8] Use gdb_gmp for scalar arithmetic Tom Tromey
2023-03-03 21:12 ` Tom Tromey [this message]
2023-03-04  7:04 ` [PATCH 0/8] Arithmetic for 128-bit types Eli Zaretskii
2023-03-08 16:17   ` Tom Tromey
2023-03-27 14:20 ` Tom Tromey

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=20230303211207.1053037-9-tromey@adacore.com \
    --to=tromey@adacore.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).