[PATCH] Fix SH sim build failures due to -Werror with GCC 7.5.x.

Luis Machado via Gdb-patches
2021-11-12T14:57:54Z

Adjust the statements to be mindful of signed overflows and initialize the res
variable. This fixes the strict-overflow and maybe-uninitialized warnings seen
below:

binutils-gdb/sim/sh/interp.c: In function "ppi_insn":
./ppi.c:875:21: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Werror=strict-overflow]
carry = res < Sy;
~~~~^~~~
./ppi.c:849:21: error: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Werror=strict-overflow]
carry = res > Sy;
~~~~^~~~
./ppi.c:823:21: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false [-Werror=strict-overflow]
carry = res < Sx;
~~~~^~~~
./ppi.c:797:21: error: assuming signed overflow does not occur when assuming that (X - c) > X is always false [-Werror=strict-overflow]
carry = res > Sx;
~~~~^~~~
binutils-gdb-arm64-bionic/sim/../../../repos/binutils-gdb/sim/sh/interp.c: In function "sim_resume":
./ppi.c:1178:28: warning: "res" may be used uninitialized in this function [-Wmaybe-uninitialized]
MACL = DSP_R (z) = res;
./ppi.c:44:7: note: "res" was declared here
int res, res_grd;
^~~
---
sim/sh/gencode.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/sim/sh/gencode.c b/sim/sh/gencode.c
index 80eecfdf1d3..80cf573de0c 100644
--- a/sim/sh/gencode.c
+++ b/sim/sh/gencode.c
@@ -2266,7 +2266,7 @@ op ppi_tab[] =
"int Sx_grd = GET_DSP_GRD (x);",
"",
"res = Sx - 0x10000;",
- "carry = res > Sx;",
+ "carry = (Sx < 0 && Sx < INT_MIN + 0x10000);",
"res_grd = Sx_grd - carry;",
"COMPUTE_OVERFLOW;",
"ADD_SUB_GE;",
@@ -2277,7 +2277,7 @@ op ppi_tab[] =
"int Sx_grd = GET_DSP_GRD (x);",
"",
"res = Sx + 0x10000;",
- "carry = res < Sx;",
+ "carry = (Sx > 0 && Sx > INT_MAX - 0x10000);",
"res_grd = Sx_grd + carry;",
"COMPUTE_OVERFLOW;",
"ADD_SUB_GE;",
@@ -2288,7 +2288,7 @@ op ppi_tab[] =
"int Sy_grd = SIGN32 (Sy);",
"",
"res = Sy - 0x10000;",
- "carry = res > Sy;",
+ "carry = (Sy < 0 && Sy < INT_MIN + 0x10000);",
"res_grd = Sy_grd - carry;",
"COMPUTE_OVERFLOW;",
"ADD_SUB_GE;",
@@ -2299,7 +2299,7 @@ op ppi_tab[] =
"int Sy_grd = SIGN32 (Sy);",
"",
"res = Sy + 0x10000;",
- "carry = res < Sy;",
+ "carry = (Sy > 0 && Sy > INT_MAX - 0x10000);",
"res_grd = Sy_grd + carry;",
"COMPUTE_OVERFLOW;",
"ADD_SUB_GE;",
@@ -3257,7 +3257,7 @@ ppi_gensim (void)
printf (" static char const u_tab[] = { 8, 10, 7, 5};\n");
printf ("\n");
printf (" int z;\n");
- printf (" int res, res_grd;\n");
+ printf (" int res = 0, res_grd;\n");
printf (" int carry, overflow, greater_equal;\n");
printf ("\n");
printf (" switch (ppi_table[iword >> 4]) {\n");
--
2.25.1