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
49794979 expr_lor();
49804980 if (tok == '?') {
49814981 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))) {
49834985 int saved_nocode_wanted = nocode_wanted;
49844986 CType boolean;
49854987 int c;
tests/tcctest.c+10−0
@@ -1470,6 +1470,10 @@ typedef int arrtype2[3]; · 1469 unmodified lines
14701470 arrtype2 sinit21 = {4};
14711471 arrtype2 sinit22 = {5,6,7};
14721472
1473+/* Address comparisons of non-weak symbols with zero can be const-folded */
1474+int sinit23[2] = { "astring" ? sizeof("astring") : -1,
1475+ &sinit23 ? 42 : -1 };
1476+
14731477 void init_test(void)
14741478 {
14751479 int linit1 = 2;
@@ -1484,6 +1488,9 @@ void init_test(void) · 8 unmodified lines
14841488 int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, };
14851489 struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 };
14861490 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 };
14871494
14881495 printf("init_test:\n");
14891496
@@ -1577,6 +1584,9 @@ void init_test(void) · 87 unmodified lines
15771584 printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]);
15781585 printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22));
15791586 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]);
15801590 }
15811591
15821592

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