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
25502550 }
25512551 }
25522552 } else {
2553− unsigned long long n, n1;
2553+ unsigned long long n = 0, n1 = 0;
25542554 int lcount, ucount, ov = 0;
25552555 const char *p1;
25562556
@@ -2561,7 +2561,6 @@ static void parse_number(const char *p) · 4 unmodified lines
25612561 b = 8;
25622562 q++;
25632563 }
2564− n = 0;
25652564 while(1) {
25662565 t = *q++;
25672566 /* no need for checks except for base 10 / 8 errors */
@@ -2575,13 +2574,22 @@ static void parse_number(const char *p) · 7 unmodified lines
25752574 t = t - '0';
25762575 if (t >= b)
25772576 tcc_error("invalid digit");
2578− n1 = n;
25792577 n = n * b + t;
2580− /* detect overflow */
2581− if (n1 >= 0x1000000000000000ULL && n / b != n1)
2582− ov = 1;
2578+ if (!ov)
2579+ /* detect overflow */
2580+ if (n1 >= 0x1000000000000000ULL && n / b != n1)
2581+ ov = 1;
2582+ else
2583+ n1 = n;
25832584 }
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
25852593 /* Determine the characteristics (unsigned and/or 64bit) the type of
25862594 the constant must have according to the constant suffix(es) */
25872595 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