History

tccgen: 32bits: fix PTR +/- long long

Previously in order to perform a ll+ll operation tcc was trying to 'lexpand' PTR in gen_opl which did not work well. The case: int printf(const char *, ...); char t[] = "012345678"; int main(void) { char *data = t; unsigned long long r = 4; unsigned a = 5; unsigned long long b = 12; *(unsigned*)(data + r) += a - b; printf("data %s\n", data); return 0; }

grischka committed 10 years ago

Commit ed15cdd1 parent 8986bc83 files · +38 −0Browse files

Unified · Split

3 files
  1. tccgen.c +22 −0
  2. tests/tests2/87_ptr_longlong_arith32.c +15 −0
  3. tests/tests2/87_ptr_longlong_arith32.expect +1 −0
tccgen.c+22−0
@@ -133,6 +133,21 @@ ST_FUNC void check_vstack(void) · 132 unmodified lines
133 tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);133 tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
134}134}
135 135
136/* ------------------------------------------------------------------------- */
137/* vstack debugging aid */
138
139#if 0
140void pv (const char *lbl, int a, int b)
141{
142 int i;
143 for (i = a; i < a + b; ++i) {
144 SValue *p = &vtop[-i];
145 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
146 lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
147 }
148}
149#endif
150
136/* ------------------------------------------------------------------------- */151/* ------------------------------------------------------------------------- */
137/* symbol allocator */152/* symbol allocator */
138static Sym *__sym_malloc(void)153static Sym *__sym_malloc(void)
@@ -1212,6 +1227,7 @@ static void gen_opl(int op) · 1073 unmodified lines
1212 case '*':1227 case '*':
1213 case '+':1228 case '+':
1214 case '-':1229 case '-':
1230 //pv("gen_opl A",0,2);
1215 t = vtop->type.t;1231 t = vtop->type.t;
1216 vswap();1232 vswap();
1217 lexpand();1233 lexpand();
@@ -1226,6 +1242,7 @@ static void gen_opl(int op) · 8 unmodified lines
1226 vtop[-3] = tmp;1242 vtop[-3] = tmp;
1227 vswap();1243 vswap();
1228 /* stack: H1 H2 L1 L2 */1244 /* stack: H1 H2 L1 L2 */
1245 //pv("gen_opl B",0,4);
1229 if (op == '*') {1246 if (op == '*') {
1230 vpushv(vtop - 1);1247 vpushv(vtop - 1);
1231 vpushv(vtop - 1);1248 vpushv(vtop - 1);
@@ -1760,6 +1777,11 @@ ST_FUNC void gen_op(int op) · 528 unmodified lines
1760 vswap();1777 vswap();
1761 swap(&t1, &t2);1778 swap(&t1, &t2);
1762 }1779 }
1780#if PTR_SIZE == 4
1781 if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
1782 /* XXX: truncate here because gen_opl can't handle ptr + long long */
1783 gen_cast(&int_type);
1784#endif
1763 type1 = vtop[-1].type;1785 type1 = vtop[-1].type;
1764 type1.t &= ~VT_ARRAY;1786 type1.t &= ~VT_ARRAY;
1765 if (vtop[-1].type.t & VT_VLA)1787 if (vtop[-1].type.t & VT_VLA)
tests/tests2/87_ptr_longlong_arith32.c+15−0
@@ -0,0 +1,15 @@
1int printf(const char *, ...);
2char t[] = "012345678";
3
4int main(void)
5{
6 char *data = t;
7 unsigned long long r = 4;
8 unsigned a = 5;
9 unsigned long long b = 12;
10
11 *(unsigned*)(data + r) += a - b;
12
13 printf("data = \"%s\"\n", data);
14 return 0;
15}
tests/tests2/87_ptr_longlong_arith32.expect+1−0
@@ -0,0 +1 @@
1data = "0123-5678"

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