From: Mike Frysinger <vapier@gentoo.org>
To: gdb-patches@sourceware.org
Subject: [PATCH] sim: cgen: rework DI macros to avoid signed left shifts
Date: Sat, 6 Jan 2024 23:45:56 -0500 [thread overview]
Message-ID: <20240107044556.4626-1-vapier@gentoo.org> (raw)
In-Reply-To: <20231224082639.18038-1-vapier@gentoo.org>
The cgen code uses DI as int64_t and UDI as uint64_t. The DI macros
are used to construct 64-bit values from 32-bit values (for the low
and high parts). The MAKEDI macro casts the high 32-bit value to a
signed 32-bit value before shifting. If this created a negative
value, this would be undefined behavior according to the C standard.
All we care about is shifting the 32-bits as they are to the high
32-bits, not caring about sign extension (since there's nothing left
to shift into), and the low 32-bits being empty. This is what we
get from shifting an unsigned value, so cast it to unsigned 32-bit
to avoid undefined behavior.
While we're here, change the SETLODI macro to truncate the lower
value to 32-bits before we set it. If it was passing in a 64-bit
value, those high bits would get included too, and that's not what
we want.
Similarly, tweak the SETHIDI macro to cast the value to an unsigned
64-bit instead of a signed 64-bit. If the value was only 32-bits,
the behavior would be the same. If it happened to be signed 64-bit,
it would trigger the undefined behavior too.
---
sim/common/cgen-types.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/sim/common/cgen-types.h b/sim/common/cgen-types.h
index 01a3ee9be584..24c2b89216fc 100644
--- a/sim/common/cgen-types.h
+++ b/sim/common/cgen-types.h
@@ -72,9 +72,9 @@ typedef int64_t DI;
typedef uint64_t UDI;
#define GETLODI(di) ((SI) (di))
#define GETHIDI(di) ((SI) ((UDI) (di) >> 32))
-#define SETLODI(di, val) ((di) = (((di) & 0xffffffff00000000LL) | (val)))
-#define SETHIDI(di, val) ((di) = (((di) & 0xffffffffLL) | (((DI) (val)) << 32)))
-#define MAKEDI(hi, lo) ((((DI) (SI) (hi)) << 32) | ((UDI) (USI) (lo)))
+#define SETLODI(di, val) ((di) = (((di) & 0xffffffff00000000LL) | (USI) (val)))
+#define SETHIDI(di, val) ((di) = (((di) & 0xffffffffLL) | (((UDI) (val)) << 32)))
+#define MAKEDI(hi, lo) ((DI) (((UDI) (hi) << 32) | (UDI) (USI) (lo)))
/* These are used to record extracted raw data from an instruction, among other
things. It must be a host data type, and not a target one. */
--
2.43.0
prev parent reply other threads:[~2024-01-07 4:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-24 8:26 [PATCH] sim: warnings: disable -Wshift-negative-value Mike Frysinger
2023-12-29 0:52 ` Joseph Myers
2024-01-05 7:40 ` Mike Frysinger
2024-01-07 4:45 ` Mike Frysinger [this message]
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=20240107044556.4626-1-vapier@gentoo.org \
--to=vapier@gentoo.org \
--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).