From 96b31ba67044d76e2374da09e8e43b0ff1d87812 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Fri, 10 Mar 2023 16:49:27 +0100 Subject: [PATCH] Fix casted boolean expressions the casted type was lost when a delayed bool was finally converted to a value. See testcase, in the wrong case the '(unsigned int)' cast was ignored, and hence the division was signed reulting in -1 instead of the proper 0x7fffffff. --- tccgen.c | 2 ++ tests/tcctest.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tccgen.c b/tccgen.c index 86ced00c..863d6a4d 100644 --- a/tccgen.c +++ b/tccgen.c @@ -930,9 +930,11 @@ static void vset_VT_JMP(void) int op = vtop->cmp_op; if (vtop->jtrue || vtop->jfalse) { + int origt = vtop->type.t; /* we need to jump to 'mov $0,%R' or 'mov $1,%R' */ int inv = op & (op < 2); /* small optimization */ vseti(VT_JMP+inv, gvtst(inv, 0)); + vtop->type.t = origt; } else { /* otherwise convert flags (rsp. 0/1) to register */ vtop->c.i = op; diff --git a/tests/tcctest.c b/tests/tcctest.c index 5f5783de..f9df38ab 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -1265,6 +1265,10 @@ void bool_test() printf("exp=%d\n", f == (32 <= a && a <= 3)); printf("r=%d\n", (t || f) + (t && f)); + /* check that types of casted &&/|| are preserved (here the unsignedness) */ + t = 1; + printf("type of bool: %d\n", (int) ( (~ ((unsigned int) (t && 1))) / 2) ); + /* test ? : cast */ { int aspect_on;