Hi - I think I've found a bug in tinycc, and help (esp. a patch!!) would be really appreciated. Below is a really simple (repeatable!) test program, along with expected & actual results.
The basic problem is that if "num" is an unsigned (e.g., uint32_t), I believe an expression like this: num == (int8_t) num should be true if num is 1 or 0xffffffdc, and NOT true if num is 0xfe or such. gcc does this. tinycc (at least 0.9.22) also does this if you insert an extra (unnecessary) temporary variable as a workaround. But tcc does NOT compute the same result without the temp variable. Semantics about conversions are complex anyway, but I think it's clear that inserting a temp variable should NOT change an answer, and I believe tcc is doing the wrong thing here without the temp variable.
There appears to be some bug in the tcc conversion code.
Can anyone suggest a patch to tcc for this?
--- David A. Wheeler
/// TEST CODE /* Demo of tcc bug */
#include <stdio.h> #include <inttypes.h>
main() {
// This shows what the current tcc code does.
// 1,0,1 is the expected answer
// and is what gcc produces; but tcc produces 1,1,0.
// tcc 0.9.22 doesn't properly do sign-extension from
// a value cast to int8_t
uint32_t num;
int8_t tmp;printf("Testing tcc and gcc\n");
num = 1;
printf("0x%x: result=%d (correct=1)\n", (int) num, num == (int8_t) num);
num = 0xfe;
printf("0x%x: result=%d (correct=0)\n", (int) num, num == (int8_t) num);
num = 0xffffffdc;
printf("0x%x: result=%d (correct=1)\n", (int) num, num == (int8_t) num);
// By inserting an extra temporary variable,
// the tcc bug is worked around
printf("\nTesting workaround for tcc (excess temporary var)\n");
num = 1;
tmp = (int8_t) num;
printf("0x%x: result=%d (correct=1)\n", (int) num, num == tmp);
num = 0xfe;
tmp = (int8_t) num;
printf("0x%x: result=%d (correct=0)\n", (int) num, num == tmp);
num = 0xffffffdc;
tmp = (int8_t) num;
printf("0x%x: result=%d (correct=1)\n", (int) num, num == tmp);
}
/// END OF TEST CODE
*** Here's what gcc does ***
Testing tcc and gcc 0x1: result=1 (correct=1) 0xfe: result=0 (correct=0) 0xffffffdc: result=1 (correct=1)
Testing workaround for tcc (excess temporary var) 0x1: result=1 (correct=1) 0xfe: result=0 (correct=0) 0xffffffdc: result=1 (correct=1)
*** Here's what tcc 0.9.22 does *** Testing tcc and gcc 0x1: result=1 (correct=1) 0xfe: result=1 (correct=0) 0xffffffdc: result=0 (correct=1)
Testing workaround for tcc (excess temporary var) 0x1: result=1 (correct=1) 0xfe: result=0 (correct=0) 0xffffffdc: result=1 (correct=1)
_______________________________________________ Tinycc-devel mailing list [email protected] http://lists.nongnu.org/mailman/listinfo/tinycc-devel
