History

Addresses of non-weak symbols are non-zero

Use this fact in some foldings of comparisons. See testcase.

Michael Matz committed 10 years ago

Commit d7208651 parent be6d8ff2 files · +13 −1Browse files

Unified · Split

2 files
  1. tccgen.c +3 −1
  2. tests/tcctest.c +10 −0
tccgen.c+3−1
@@ -4979,7 +4979,9 @@ static void expr_cond(void) · 4978 unmodified lines
4979 expr_lor();4979 expr_lor();
4980 if (tok == '?') {4980 if (tok == '?') {
4981 next();4981 next();
4982 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {4982 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
4983 (!(vtop->r & VT_SYM) ||
4984 !(vtop->sym->type.t & VT_WEAK))) {
4983 int saved_nocode_wanted = nocode_wanted;4985 int saved_nocode_wanted = nocode_wanted;
4984 CType boolean;4986 CType boolean;
4985 int c;4987 int c;
tests/tcctest.c+10−0
@@ -1470,6 +1470,10 @@ typedef int arrtype2[3]; · 1469 unmodified lines
1470arrtype2 sinit21 = {4};1470arrtype2 sinit21 = {4};
1471arrtype2 sinit22 = {5,6,7};1471arrtype2 sinit22 = {5,6,7};
1472 1472
1473/* Address comparisons of non-weak symbols with zero can be const-folded */
1474int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1475 &sinit23 ? 42 : -1 };
1476
1473void init_test(void)1477void init_test(void)
1474{1478{
1475 int linit1 = 2;1479 int linit1 = 2;
@@ -1484,6 +1488,9 @@ void init_test(void) · 8 unmodified lines
1484 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };1488 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
1485 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };1489 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
1486 int linit17 = sizeof(linit17);1490 int linit17 = sizeof(linit17);
1491 int zero = 0;
1492 /* Addresses on non-weak symbols are non-zero, but not the access itself */
1493 int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 };
1487 1494
1488 printf("init_test:\n");1495 printf("init_test:\n");
1489 1496
@@ -1577,6 +1584,9 @@ void init_test(void) · 87 unmodified lines
1577 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);1584 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
1578 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));1585 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
1579 printf("arrtype6: %d\n", sizeof(arrtype2));1586 printf("arrtype6: %d\n", sizeof(arrtype2));
1587
1588 printf("sinit23= %d %d\n", sinit23[0], sinit23[1]);
1589 printf("linit18= %d %d\n", linit18[0], linit18[1]);
1580}1590}
1581 1591
1582 1592

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