History

tccpp.c: Configurable integer literal overflow handling

Add the define TCC_CUT_ON_INTEGER_LITERAL_OVERFLOW to use the most significant digits of an integer literal before its parsing led to an overflow. When compiling tcc with another compiler, which is not able to handle 64 bit arithmetic, it is beneficial to use the last value before an integer literal overflows. Parsing 0x1000000000000000 then results in 0x10000000. The mescc from GNU Mes is able to compile a first tcc on a 32 bit system without 64 bit arithmetic. This change allows this first tcc to compile a second tcc with complete 64 bit arithmetic.

Stefan committed 11 months ago

Commit 829c8481 parent cb41cbf1 file · +15 −7Browse files

Unified · Split

tccpp.c+15−7
@@ -2550,7 +2550,7 @@ static void parse_number(const char *p) · 2549 unmodified lines
2550 }2550 }
2551 }2551 }
2552 } else {2552 } else {
2553 unsigned long long n, n1;2553 unsigned long long n = 0, n1 = 0;
2554 int lcount, ucount, ov = 0;2554 int lcount, ucount, ov = 0;
2555 const char *p1;2555 const char *p1;
2556 2556
@@ -2561,7 +2561,6 @@ static void parse_number(const char *p) · 4 unmodified lines
2561 b = 8;2561 b = 8;
2562 q++;2562 q++;
2563 }2563 }
2564 n = 0;
2565 while(1) {2564 while(1) {
2566 t = *q++;2565 t = *q++;
2567 /* no need for checks except for base 10 / 8 errors */2566 /* no need for checks except for base 10 / 8 errors */
@@ -2575,13 +2574,22 @@ static void parse_number(const char *p) · 7 unmodified lines
2575 t = t - '0';2574 t = t - '0';
2576 if (t >= b)2575 if (t >= b)
2577 tcc_error("invalid digit");2576 tcc_error("invalid digit");
2578 n1 = n;
2579 n = n * b + t;2577 n = n * b + t;
2580 /* detect overflow */2578 if (!ov)
2581 if (n1 >= 0x1000000000000000ULL && n / b != n1)2579 /* detect overflow */
2582 ov = 1;2580 if (n1 >= 0x1000000000000000ULL && n / b != n1)
2581 ov = 1;
2582 else
2583 n1 = n;
2583 }2584 }
2584 2585#ifdef TCC_CUT_ON_INTEGER_LITERAL_OVERFLOW
2586 /* On integer literal overflow use the most significant digits before
2587 the overflow happened. Effectively this cuts the 0x1000000000000000
2588 from above down to 0x10000000 and allows to bootstrap tcc with 32 bit
2589 arithmetic. */
2590 if (ov)
2591 n = n1;
2592#endif
2585 /* Determine the characteristics (unsigned and/or 64bit) the type of2593 /* Determine the characteristics (unsigned and/or 64bit) the type of
2586 the constant must have according to the constant suffix(es) */2594 the constant must have according to the constant suffix(es) */
2587 lcount = ucount = 0;2595 lcount = ucount = 0;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t