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
133133 tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
134134 }
135135
136+/* ------------------------------------------------------------------------- */
137+/* vstack debugging aid */
138+
139+#if 0
140+void 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+
136151 /* ------------------------------------------------------------------------- */
137152 /* symbol allocator */
138153 static Sym *__sym_malloc(void)
@@ -1212,6 +1227,7 @@ static void gen_opl(int op) · 1073 unmodified lines
12121227 case '*':
12131228 case '+':
12141229 case '-':
1230+ //pv("gen_opl A",0,2);
12151231 t = vtop->type.t;
12161232 vswap();
12171233 lexpand();
@@ -1226,6 +1242,7 @@ static void gen_opl(int op) · 8 unmodified lines
12261242 vtop[-3] = tmp;
12271243 vswap();
12281244 /* stack: H1 H2 L1 L2 */
1245+ //pv("gen_opl B",0,4);
12291246 if (op == '*') {
12301247 vpushv(vtop - 1);
12311248 vpushv(vtop - 1);
@@ -1760,6 +1777,11 @@ ST_FUNC void gen_op(int op) · 528 unmodified lines
17601777 vswap();
17611778 swap(&t1, &t2);
17621779 }
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
17631785 type1 = vtop[-1].type;
17641786 type1.t &= ~VT_ARRAY;
17651787 if (vtop[-1].type.t & VT_VLA)
tests/tests2/87_ptr_longlong_arith32.c+15−0
@@ -0,0 +1,15 @@
1+int printf(const char *, ...);
2+char t[] = "012345678";
3+
4+int 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 @@
1+data = "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