public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sim: warnings: disable -Wshift-negative-value
@ 2023-12-24  8:26 Mike Frysinger
  2023-12-29  0:52 ` Joseph Myers
  2024-01-07  4:45 ` [PATCH] sim: cgen: rework DI macros to avoid signed left shifts Mike Frysinger
  0 siblings, 2 replies; 4+ messages in thread
From: Mike Frysinger @ 2023-12-24  8:26 UTC (permalink / raw)
  To: gdb-patches

The sim expects left shift operations on negative values to have two's
compliment behavior, and right shift operations to sign extend.  In C89,
this was not explicitly mentioned.  In C90, this was changed to undefined
behavior.  In C23, this was settled as the behavior we want in N2412 [1].
One C23 proposal documented that GCC, LLVM, and MSVC already behaved this
way [2], as did every known hardware, and GCC guarantees it behaves this
way in all C standards as an extension.  So disable the warning in case a
compiler automatically turns it on when running in C11 mode (which is our
required minimum version).

From the GCC manual (4.5 Integers):
> https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
> The results of some bitwise operations on signed integers (C90 6.3, C99 and C11 6.5).
>
> Bitwise operators act on the representation of the value including
> both the sign and value bits, where the sign bit is considered
> immediately above the highest-value value bit. Signed ‘>>’ acts on
> negative numbers by sign extension.
>
> As an extension to the C language, GCC does not use the latitude given
> in C99 and C11 only to treat certain aspects of signed ‘<<’ as undefined.
> However, -fsanitize=shift (and -fsanitize=undefined) will diagnose such
> cases. They are also diagnosed where constant expressions are required.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2412.pdf
[2] https://wg21.link/P0907R4 (not adopted, but has relevant research)
---
 sim/configure                    | 1 +
 sim/m4/sim_ac_option_warnings.m4 | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/sim/m4/sim_ac_option_warnings.m4 b/sim/m4/sim_ac_option_warnings.m4
index 8a54e563bad2..41a7c5c8c0d6 100644
--- a/sim/m4/sim_ac_option_warnings.m4
+++ b/sim/m4/sim_ac_option_warnings.m4
@@ -74,6 +74,10 @@ build_warnings="$build_warnings
 dnl The cgen virtual insn logic involves enum conversions.
 dnl Disable until we can figure out how to make this work.
 -Wno-enum-conversion
+dnl The sim expects left shift operations on negative values to have two's
+dnl compliment behavior, and right shift operations to sign extend.  In
+dnl practice, all compilers do this, and C23 settled it, so disable the warning.
+-Wno-shift-negative-value
 "
 
 case "${host}" in
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-07  4:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH] sim: cgen: rework DI macros to avoid signed left shifts Mike Frysinger

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).