History

separated type and value on stack

bellard committed 24 years ago

Commit f7181901 parent f50d0b42 files · +295 −257Browse files

Unified · Split

2 files
  1. i386-gen.c +50 −42
  2. tcc.c +245 −215
i386-gen.c+50−42
@@ -155,10 +155,10 @@ int oad(int c, int s) · 154 unmodified lines
155 return s;155 return s;
156}156}
157 157
158/* output constant with relocation if 't & VT_FORWARD' is true */158/* output constant with relocation if 'r & VT_FORWARD' is true */
159void gen_addr32(int c, int t)159void gen_addr32(int r, int c)
160{160{
161 if (!(t & VT_FORWARD)) {161 if (!(r & VT_FORWARD)) {
162 gen_le32(c);162 gen_le32(c);
163 } else {163 } else {
164 greloc((Sym *)c, ind, RELOC_ADDR32);164 greloc((Sym *)c, ind, RELOC_ADDR32);
@@ -166,16 +166,23 @@ void gen_addr32(int c, int t) · 1 unmodified line
166 }166 }
167}167}
168 168
169/* XXX: generate correct pointer for forward references to functions */169/* load 'r' from value 'sv' */
170/* r = (ft, fc) */170void load(int r, SValue *sv)
171void load(int r, int ft, int fc)
172{171{
173 int v, t;172 int v, t, ft, fc, fr;
173 SValue v1;
174 174
175 v = ft & VT_VALMASK;175 fr = sv->r;
176 if (ft & VT_LVAL) {176 ft = sv->t;
177 fc = sv->c.ul;
178
179 v = fr & VT_VALMASK;
180 if (fr & VT_LVAL) {
177 if (v == VT_LLOCAL) {181 if (v == VT_LLOCAL) {
178 load(r, VT_LOCAL | VT_LVAL, fc);182 v1.t = VT_INT;
183 v1.r = VT_LOCAL | VT_LVAL;
184 v1.c.ul = fc;
185 load(r, &v1);
179 v = r;186 v = r;
180 }187 }
181 if ((ft & VT_BTYPE) == VT_FLOAT) {188 if ((ft & VT_BTYPE) == VT_FLOAT) {
@@ -200,7 +207,7 @@ void load(int r, int ft, int fc) · 18 unmodified lines
200 207
201 if (v == VT_CONST) {208 if (v == VT_CONST) {
202 o(0x05 + r * 8); /* 0xXX, r */209 o(0x05 + r * 8); /* 0xXX, r */
203 gen_addr32(fc, ft);210 gen_addr32(fr, fc);
204 } else if (v == VT_LOCAL) {211 } else if (v == VT_LOCAL) {
205 oad(0x85 + r * 8, fc); /* xx(%ebp), r */212 oad(0x85 + r * 8, fc); /* xx(%ebp), r */
206 } else {213 } else {
@@ -209,7 +216,7 @@ void load(int r, int ft, int fc) · 2 unmodified lines
209 } else {216 } else {
210 if (v == VT_CONST) {217 if (v == VT_CONST) {
211 o(0xb8 + r); /* mov $xx, r */218 o(0xb8 + r); /* mov $xx, r */
212 gen_addr32(fc, ft);219 gen_addr32(fr, fc);
213 } else if (v == VT_LOCAL) {220 } else if (v == VT_LOCAL) {
214 o(0x8d);221 o(0x8d);
215 oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */222 oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */
@@ -231,13 +238,14 @@ void load(int r, int ft, int fc) · 15 unmodified lines
231 }238 }
232}239}
233 240
234/* (ft, fc) = r */241/* store register 'r' in lvalue 'v' */
235/* WARNING: r must not be allocated on the stack */242void store(int r, SValue *v)
236void store(r, ft, fc)
237{243{
238 int fr, bt;244 int fr, bt, ft, fc;
239 245
240 fr = ft & VT_VALMASK;246 ft = v->t;
247 fc = v->c.ul;
248 fr = v->r & VT_VALMASK;
241 bt = ft & VT_BTYPE;249 bt = ft & VT_BTYPE;
242 /* XXX: incorrect if reg to reg */250 /* XXX: incorrect if reg to reg */
243 /* XXX: should not flush float stack */251 /* XXX: should not flush float stack */
@@ -261,10 +269,10 @@ void store(r, ft, fc) · 17 unmodified lines
261 }269 }
262 if (fr == VT_CONST) {270 if (fr == VT_CONST) {
263 o(0x05 + r * 8); /* mov r,xxx */271 o(0x05 + r * 8); /* mov r,xxx */
264 gen_addr32(fc, ft);272 gen_addr32(v->r, fc);
265 } else if (fr == VT_LOCAL) {273 } else if (fr == VT_LOCAL) {
266 oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */274 oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */
267 } else if (ft & VT_LVAL) {275 } else if (v->r & VT_LVAL) {
268 g(fr + r * 8); /* mov r, (fr) */276 g(fr + r * 8); /* mov r, (fr) */
269 } else if (fr != r) {277 } else if (fr != r) {
270 o(0xc0 + fr + r * 8); /* mov r, fr */278 o(0xc0 + fr + r * 8); /* mov r, fr */
@@ -283,7 +291,7 @@ void gfunc_param(GFuncContext *c) · 12 unmodified lines
283{291{
284 int size, align, r;292 int size, align, r;
285 293
286 if ((vtop->t & (VT_BTYPE | VT_LVAL)) == (VT_STRUCT | VT_LVAL)) {294 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
287 size = type_size(vtop->t, &align);295 size = type_size(vtop->t, &align);
288 /* align to stack align size */296 /* align to stack align size */
289 size = (size + 3) & ~3;297 size = (size + 3) & ~3;
@@ -293,7 +301,7 @@ void gfunc_param(GFuncContext *c) · 3 unmodified lines
293 r = get_reg(REG_CLASS_INT);301 r = get_reg(REG_CLASS_INT);
294 o(0x89); /* mov %esp, r */302 o(0x89); /* mov %esp, r */
295 o(0xe0 + r);303 o(0xe0 + r);
296 vset(VT_INT | r, 0);304 vset(VT_INT, r, 0);
297 vswap();305 vswap();
298 vstore();306 vstore();
299 c->args_size += size;307 c->args_size += size;
@@ -328,10 +336,10 @@ void gfunc_param(GFuncContext *c) · 28 unmodified lines
328void gfunc_call(GFuncContext *c)336void gfunc_call(GFuncContext *c)
329{337{
330 int r;338 int r;
331 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {339 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
332 /* constant case */340 /* constant case */
333 /* forward reference */341 /* forward reference */
334 if (vtop->t & VT_FORWARD) {342 if (vtop->r & VT_FORWARD) {
335 greloc(vtop->c.sym, ind + 1, RELOC_REL32);343 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
336 oad(0xe8, 0);344 oad(0xe8, 0);
337 } else {345 } else {
@@ -357,7 +365,7 @@ int gjmp(int t) · 19 unmodified lines
357int gtst(int inv, int t)365int gtst(int inv, int t)
358{366{
359 int v, *p;367 int v, *p;
360 v = vtop->t & VT_VALMASK;368 v = vtop->r & VT_VALMASK;
361 if (v == VT_CMP) {369 if (v == VT_CMP) {
362 /* fast case : can jump directly since flags are set */370 /* fast case : can jump directly since flags are set */
363 g(0x0f);371 g(0x0f);
@@ -375,7 +383,7 @@ int gtst(int inv, int t) · 11 unmodified lines
375 t = gjmp(t);383 t = gjmp(t);
376 gsym(vtop->c.i);384 gsym(vtop->c.i);
377 }385 }
378 } else if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {386 } else if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
379 /* constant jmp optimization */387 /* constant jmp optimization */
380 if ((vtop->c.i != 0) != inv) 388 if ((vtop->c.i != 0) != inv)
381 t = gjmp(t);389 t = gjmp(t);
@@ -438,7 +446,7 @@ void gen_opi(int op) · 56 unmodified lines
438 o(0xe8 + r);446 o(0xe8 + r);
439 else447 else
440 o(0xf8 + r);448 o(0xf8 + r);
441 vtop->t = (vtop->t & VT_TYPE) | r;449 vtop->r = r;
442 } else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV | 450 } else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV |
443 op == '%' | op == TOK_UMOD) {451 op == '%' | op == TOK_UMOD) {
444 save_reg(2); /* save edx */452 save_reg(2); /* save edx */
@@ -455,12 +463,12 @@ void gen_opi(int op) · 10 unmodified lines
455 r = 2;463 r = 2;
456 else464 else
457 r = 0;465 r = 0;
458 vtop->t = (vtop->t & VT_TYPE) | r;466 vtop->r = r;
459 } else {467 } else {
460 vtop--;468 vtop--;
461 o(0x39);469 o(0x39);
462 o(0xc0 + r + fr * 8); /* cmp fr, r */470 o(0xc0 + r + fr * 8); /* cmp fr, r */
463 vset(VT_CMP, op);471 vset(VT_INT, VT_CMP, op);
464 }472 }
465}473}
466 474
@@ -472,24 +480,24 @@ void gen_opf(int op) · 5 unmodified lines
472 int a, ft, fc, swapped, r;480 int a, ft, fc, swapped, r;
473 481
474 /* convert constants to memory references */482 /* convert constants to memory references */
475 if ((vtop[-1].t & (VT_CONST | VT_LVAL)) == VT_CONST) {483 if ((vtop[-1].r & (VT_CONST | VT_LVAL)) == VT_CONST) {
476 vswap();484 vswap();
477 gv();485 gv();
478 vswap();486 vswap();
479 }487 }
480 if ((vtop[0].t & (VT_CONST | VT_LVAL)) == VT_CONST)488 if ((vtop[0].r & (VT_CONST | VT_LVAL)) == VT_CONST)
481 gv();489 gv();
482 490
483 /* must put at least one value in the floating point register */491 /* must put at least one value in the floating point register */
484 if ((vtop[-1].t & VT_LVAL) &&492 if ((vtop[-1].r & VT_LVAL) &&
485 (vtop[0].t & VT_LVAL)) {493 (vtop[0].r & VT_LVAL)) {
486 vswap();494 vswap();
487 gv();495 gv();
488 vswap();496 vswap();
489 }497 }
490 if (op >= TOK_EQ && op <= TOK_GT) {498 if (op >= TOK_EQ && op <= TOK_GT) {
491 /* load on stack second operand */499 /* load on stack second operand */
492 load(REG_ST0, vtop->t, vtop->c.ul);500 load(REG_ST0, vtop);
493 if (op == TOK_GE || op == TOK_GT)501 if (op == TOK_GE || op == TOK_GT)
494 o(0xc9d9); /* fxch %st(1) */502 o(0xc9d9); /* fxch %st(1) */
495 o(0xe9da); /* fucompp */503 o(0xe9da); /* fucompp */
@@ -509,13 +517,13 @@ void gen_opf(int op) · 13 unmodified lines
509 op = TOK_EQ;517 op = TOK_EQ;
510 }518 }
511 vtop--;519 vtop--;
512 vtop->t = (vtop->t & VT_TYPE) | VT_CMP;520 vtop->r = VT_CMP;
513 vtop->c.i = op;521 vtop->c.i = op;
514 } else {522 } else {
515 /* swap the stack if needed so that t1 is the register and t2 is523 /* swap the stack if needed so that t1 is the register and t2 is
516 the memory reference */524 the memory reference */
517 swapped = 0;525 swapped = 0;
518 if (vtop[-1].t & VT_LVAL) {526 if (vtop[-1].r & VT_LVAL) {
519 vswap();527 vswap();
520 swapped = 1;528 swapped = 1;
521 }529 }
@@ -546,10 +554,10 @@ void gen_opf(int op) · 24 unmodified lines
546 else554 else
547 o(0xd8);555 o(0xd8);
548 556
549 r = ft & VT_VALMASK;557 r = vtop->r & VT_VALMASK;
550 if (r == VT_CONST) {558 if (r == VT_CONST) {
551 o(0x05 + a);559 o(0x05 + a);
552 gen_addr32(fc, ft);560 gen_addr32(vtop->r, fc);
553 } else if (r == VT_LOCAL) {561 } else if (r == VT_LOCAL) {
554 oad(0x85 + a, fc);562 oad(0x85 + a, fc);
555 } else {563 } else {
@@ -567,16 +575,16 @@ void gen_cvt_itof(int t) · 11 unmodified lines
567 /* unsigned int to float/double/long double */575 /* unsigned int to float/double/long double */
568 o(0x6a); /* push $0 */576 o(0x6a); /* push $0 */
569 g(0x00);577 g(0x00);
570 o(0x50 + (vtop->t & VT_VALMASK)); /* push r */578 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
571 o(0x242cdf); /* fildll (%esp) */579 o(0x242cdf); /* fildll (%esp) */
572 o(0x08c483); /* add $8, %esp */580 o(0x08c483); /* add $8, %esp */
573 } else {581 } else {
574 /* int to float/double/long double */582 /* int to float/double/long double */
575 o(0x50 + (vtop->t & VT_VALMASK)); /* push r */583 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
576 o(0x2404db); /* fildl (%esp) */584 o(0x2404db); /* fildl (%esp) */
577 o(0x04c483); /* add $4, %esp */585 o(0x04c483); /* add $4, %esp */
578 }586 }
579 vtop->t = t | REG_ST0;587 vtop->r = REG_ST0;
580}588}
581 589
582/* FPU control word for rounding to nearest mode */590/* FPU control word for rounding to nearest mode */
@@ -611,7 +619,7 @@ void gen_cvt_ftoi(int t) · 28 unmodified lines
611 o(0x58 + r); /* pop r */619 o(0x58 + r); /* pop r */
612 if (size == 8) 620 if (size == 8)
613 o(0x04c483); /* add $4, %esp */621 o(0x04c483); /* add $4, %esp */
614 vtop->t = t | r;622 vtop->r = r;
615}623}
616 624
617/* convert from one floating point type to another */625/* convert from one floating point type to another */
@@ -625,7 +633,7 @@ void gen_cvt_ftof(int t) · 7 unmodified lines
625void vpop(void)633void vpop(void)
626{634{
627 /* for x86, we need to pop the FP stack */635 /* for x86, we need to pop the FP stack */
628 if ((vtop->t & VT_VALMASK) == REG_ST0) {636 if ((vtop->r & VT_VALMASK) == REG_ST0) {
629 o(0xd9dd); /* fstp %st(1) */637 o(0xd9dd); /* fstp %st(1) */
630 }638 }
631 vtop--;639 vtop--;
tcc.c+245−215
@@ -64,14 +64,15 @@ typedef union CValue { · 63 unmodified lines
64 long long ll;64 long long ll;
65 unsigned long long ull;65 unsigned long long ull;
66 struct TokenSym *ts;66 struct TokenSym *ts;
67 int tab[1];
68 struct Sym *sym;67 struct Sym *sym;
68 int tab[1];
69} CValue;69} CValue;
70 70
71/* value on stack */71/* value on stack */
72typedef struct SValue {72typedef struct SValue {
73 int t;73 int t; /* type */
74 CValue c;74 int r; /* register + flags */
75 CValue c; /* constant */
75} SValue;76} SValue;
76 77
77/* symbol management */78/* symbol management */
@@ -360,7 +361,7 @@ int pointed_size(int t); · 282 unmodified lines
360int ist(void);361int ist(void);
361int type_decl(int *v, int t, int td);362int type_decl(int *v, int t, int td);
362void error(const char *fmt, ...);363void error(const char *fmt, ...);
363void vset(int t, int v);364void vset(int t, int r, int v);
364 365
365/* true if float/double/long double type */366/* true if float/double/long double type */
366static inline int is_float(int t)367static inline int is_float(int t)
@@ -485,7 +486,7 @@ void skip(int c) · 118 unmodified lines
485 486
486void test_lvalue(void)487void test_lvalue(void)
487{488{
488 if (!(vtop->t & VT_LVAL))489 if (!(vtop->r & VT_LVAL))
489 expect("lvalue");490 expect("lvalue");
490}491}
491 492
@@ -1874,25 +1875,34 @@ void swap(int *p, int *q) · 1382 unmodified lines
1874 *q = t;1875 *q = t;
1875}1876}
1876 1877
1877void vsetc(int t, CValue *vc)1878void vsetc(int t, int r, CValue *vc)
1878{1879{
1879 if (vtop >= vstack + VSTACK_SIZE)1880 if (vtop >= vstack + VSTACK_SIZE)
1880 error("memory full");1881 error("memory full");
1881 /* cannot let cpu flags if other instruction are generated */1882 /* cannot let cpu flags if other instruction are generated */
1882 /* XXX: VT_JMP test too ? */1883 /* XXX: VT_JMP test too ? */
1883 if ((vtop->t & VT_VALMASK) == VT_CMP)1884 if ((vtop->r & VT_VALMASK) == VT_CMP)
1884 gv();1885 gv();
1885 vtop++;1886 vtop++;
1886 vtop->t = t;1887 vtop->t = t;
1888 vtop->r = r;
1887 vtop->c = *vc;1889 vtop->c = *vc;
1888}1890}
1889 1891
1890void vset(int t, int v)1892/* push integer constant */
1893void vpushi(int v)
1894{
1895 CValue cval;
1896 cval.i = v;
1897 vsetc(VT_INT, VT_CONST, &cval);
1898}
1899
1900void vset(int t, int r, int v)
1891{1901{
1892 CValue cval;1902 CValue cval;
1893 1903
1894 cval.i = v;1904 cval.i = v;
1895 vsetc(t, &cval);1905 vsetc(t, r, &cval);
1896}1906}
1897 1907
1898void vswap(void)1908void vswap(void)
@@ -1915,22 +1925,25 @@ void vdup(void) · 16 unmodified lines
1915int save_reg_forced(int r)1925int save_reg_forced(int r)
1916{1926{
1917 int i, l, t;1927 int i, l, t;
1918 SValue *p;1928 SValue *p, sv;
1919 1929
1920 /* store register */1930 /* store register */
1921 loc = (loc - 4) & -3;1931 loc = (loc - 4) & -3;
1922 store(r, VT_LOCAL, loc);1932 sv.t = VT_INT;
1933 sv.r = VT_LOCAL | VT_LVAL;
1934 sv.c.ul = loc;
1935 store(r, &sv);
1923 l = loc;1936 l = loc;
1924 1937
1925 /* modify all stack values */1938 /* modify all stack values */
1926 for(p=vstack;p<=vtop;p++) {1939 for(p=vstack;p<=vtop;p++) {
1927 i = p->t & VT_VALMASK;1940 i = p->r & VT_VALMASK;
1928 if (i == r) {1941 if (i == r) {
1929 if (p->t & VT_LVAL)1942 if (p->r & VT_LVAL)
1930 t = VT_LLOCAL;1943 t = VT_LLOCAL;
1931 else1944 else
1932 t = VT_LOCAL;1945 t = VT_LOCAL;
1933 p->t = (p->t & VT_TYPE) | VT_LVAL | t;1946 p->r = VT_LVAL | t;
1934 p->c.ul = l;1947 p->c.ul = l;
1935 }1948 }
1936 }1949 }
@@ -1945,7 +1958,7 @@ void save_reg(int r) · 8 unmodified lines
1945 1958
1946 /* modify all stack values */1959 /* modify all stack values */
1947 for(p=vstack;p<=vtop;p++) {1960 for(p=vstack;p<=vtop;p++) {
1948 i = p->t & VT_VALMASK;1961 i = p->r & VT_VALMASK;
1949 if (i == r) {1962 if (i == r) {
1950 save_reg_forced(r);1963 save_reg_forced(r);
1951 break;1964 break;
@@ -1963,7 +1976,7 @@ int get_reg(int rc) · 11 unmodified lines
1963 for(r=0;r<NB_REGS;r++) {1976 for(r=0;r<NB_REGS;r++) {
1964 if (reg_classes[r] & rc) {1977 if (reg_classes[r] & rc) {
1965 for(p=vstack;p<=vtop;p++) {1978 for(p=vstack;p<=vtop;p++) {
1966 i = p->t & VT_VALMASK;1979 i = p->r & VT_VALMASK;
1967 if (i == r)1980 if (i == r)
1968 goto notfound;1981 goto notfound;
1969 }1982 }
@@ -1976,7 +1989,7 @@ int get_reg(int rc) · 6 unmodified lines
1976 important to start from the bottom to ensure that we don't1989 important to start from the bottom to ensure that we don't
1977 spill registers used in gen_op()) */1990 spill registers used in gen_op()) */
1978 for(p=vstack;p<=vtop;p++) {1991 for(p=vstack;p<=vtop;p++) {
1979 r = p->t & VT_VALMASK;1992 r = p->r & VT_VALMASK;
1980 if (r < VT_CONST && (reg_classes[r] & rc)) {1993 if (r < VT_CONST && (reg_classes[r] & rc)) {
1981 save_reg(r);1994 save_reg(r);
1982 break;1995 break;
@@ -1985,13 +1998,13 @@ int get_reg(int rc) · 2 unmodified lines
1985 return r;1998 return r;
1986}1999}
1987 2000
1988void save_regs()2001void save_regs(void)
1989{2002{
1990 int r;2003 int r;
1991 SValue *p;2004 SValue *p;
1992 2005
1993 for(p=vstack;p<=vtop;p++) {2006 for(p=vstack;p<=vtop;p++) {
1994 r = p->t & VT_VALMASK;2007 r = p->r & VT_VALMASK;
1995 if (r < VT_CONST) {2008 if (r < VT_CONST) {
1996 save_reg(r);2009 save_reg(r);
1997 }2010 }
@@ -2002,9 +2015,14 @@ void save_regs() · 4 unmodified lines
2002 if needed */2015 if needed */
2003void move_reg(int r, int s)2016void move_reg(int r, int s)
2004{2017{
2018 SValue sv;
2019
2005 if (r != s) {2020 if (r != s) {
2006 save_reg(r);2021 save_reg(r);
2007 load(r, s, 0);2022 sv.t = VT_INT;
2023 sv.r = s;
2024 sv.c.ul = 0;
2025 load(r, &sv);
2008 }2026 }
2009}2027}
2010 2028
@@ -2022,14 +2040,15 @@ int gv(void) · 11 unmodified lines
2022 /* remove bit field info to avoid loops */2040 /* remove bit field info to avoid loops */
2023 vtop->t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));2041 vtop->t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2024 /* generate shifts */2042 /* generate shifts */
2025 vset(VT_CONST, 32 - (bit_pos + bit_size));2043 vpushi(32 - (bit_pos + bit_size));
2026 gen_op(TOK_SHL);2044 gen_op(TOK_SHL);
2027 vset(VT_CONST, 32 - bit_size);2045 vpushi(32 - bit_size);
2028 /* NOTE: transformed to SHR if unsigned */2046 /* NOTE: transformed to SHR if unsigned */
2029 gen_op(TOK_SAR);2047 gen_op(TOK_SAR);
2030 r = gv();2048 r = gv();
2031 } else {2049 } else {
2032 if (is_float(vtop->t) && (vtop->t & (VT_CONST | VT_LVAL)) == VT_CONST) {2050 if (is_float(vtop->t) &&
2051 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
2033 /* CPUs usually cannot use float constants, so we store them2052 /* CPUs usually cannot use float constants, so we store them
2034 generically in data segment */2053 generically in data segment */
2035 size = type_size(vtop->t, &align);2054 size = type_size(vtop->t, &align);
@@ -2038,20 +2057,20 @@ int gv(void) · 2 unmodified lines
2038 size = size >> 2;2057 size = size >> 2;
2039 for(i=0;i<size;i++)2058 for(i=0;i<size;i++)
2040 ((int *)glo)[i] = vtop->c.tab[i];2059 ((int *)glo)[i] = vtop->c.tab[i];
2041 vtop->t |= VT_LVAL;2060 vtop->r |= VT_LVAL;
2042 vtop->c.ul = glo;2061 vtop->c.ul = glo;
2043 glo += size << 2;2062 glo += size << 2;
2044 }2063 }
2045 r = vtop->t & VT_VALMASK;2064 r = vtop->r & VT_VALMASK;
2046 if (r >= VT_CONST || (vtop->t & VT_LVAL)) {2065 if (r >= VT_CONST || (vtop->r & VT_LVAL)) {
2047 if (is_float(vtop->t))2066 if (is_float(vtop->t))
2048 rc = REG_CLASS_FLOAT;2067 rc = REG_CLASS_FLOAT;
2049 else2068 else
2050 rc = REG_CLASS_INT;2069 rc = REG_CLASS_INT;
2051 r = get_reg(rc);2070 r = get_reg(rc);
2052 }2071 }
2053 load(r, vtop->t, vtop->c.ul);2072 load(r, vtop);
2054 vtop->t = (vtop->t & VT_TYPE) | r;2073 vtop->r = r;
2055 }2074 }
2056 return r;2075 return r;
2057}2076}
@@ -2065,8 +2084,8 @@ void gen_opc(int op) · 7 unmodified lines
2065 v1 = vtop - 1;2084 v1 = vtop - 1;
2066 v2 = vtop;2085 v2 = vtop;
2067 /* currently, we cannot do computations with forward symbols */2086 /* currently, we cannot do computations with forward symbols */
2068 c1 = (v1->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;2087 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2069 c2 = (v2->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;2088 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2070 if (c1 && c2) {2089 if (c1 && c2) {
2071 fc = v2->c.i;2090 fc = v2->c.i;
2072 switch(op) {2091 switch(op) {
@@ -2184,9 +2203,9 @@ void gen_op(int op) · 111 unmodified lines
2184 gen_opf(op);2203 gen_opf(op);
2185 if (op >= TOK_EQ && op <= TOK_GT) {2204 if (op >= TOK_EQ && op <= TOK_GT) {
2186 /* the result is an int */2205 /* the result is an int */
2187 vtop->t = (vtop->t & ~VT_TYPE) | VT_INT;2206 vtop->t = VT_INT;
2188 } else {2207 } else {
2189 vtop->t = (vtop->t & ~VT_TYPE) | t;2208 vtop->t = t;
2190 }2209 }
2191 } else if (op == '+' || op == '-') {2210 } else if (op == '+' || op == '-') {
2192 if ((t1 & VT_BTYPE) == VT_PTR && 2211 if ((t1 & VT_BTYPE) == VT_PTR &&
@@ -2197,8 +2216,8 @@ void gen_op(int op) · 4 unmodified lines
2197 u = pointed_size(t1);2216 u = pointed_size(t1);
2198 gen_opc(op);2217 gen_opc(op);
2199 /* set to integer type */2218 /* set to integer type */
2200 vtop->t = (vtop->t & ~VT_TYPE) | VT_INT; 2219 vtop->t = VT_INT;
2201 vset(VT_CONST, u);2220 vpushi(u);
2202 gen_op(TOK_PDIV);2221 gen_op(TOK_PDIV);
2203 } else if ((t1 & VT_BTYPE) == VT_PTR ||2222 } else if ((t1 & VT_BTYPE) == VT_PTR ||
2204 (t2 & VT_BTYPE) == VT_PTR) {2223 (t2 & VT_BTYPE) == VT_PTR) {
@@ -2207,11 +2226,11 @@ void gen_op(int op) · 2 unmodified lines
2207 swap(&t1, &t2);2226 swap(&t1, &t2);
2208 }2227 }
2209 /* stack-4 contains pointer, stack-2 value to add */2228 /* stack-4 contains pointer, stack-2 value to add */
2210 vset(VT_CONST, pointed_size(vtop[-1].t));2229 vpushi(pointed_size(vtop[-1].t));
2211 gen_op('*');2230 gen_op('*');
2212 gen_opc(op);2231 gen_opc(op);
2213 /* put again type if gen_opc() swaped operands */2232 /* put again type if gen_opc() swaped operands */
2214 vtop->t = (vtop->t & ~VT_TYPE) | (t1 & VT_TYPE);2233 vtop->t = t1;
2215 } else {2234 } else {
2216 gen_opc(op);2235 gen_opc(op);
2217 }2236 }
@@ -2242,128 +2261,128 @@ void gen_op(int op) · 24 unmodified lines
2242/* cast 'vtop' to 't' type */2261/* cast 'vtop' to 't' type */
2243void gen_cast(int t)2262void gen_cast(int t)
2244{2263{
2245 int r, bits, sbt, dbt, sf, df, c, st1, dt1;2264 int bits, sbt, dbt, sf, df, c, st1, dt1;
2246 2265
2247 r = vtop->t & VT_VALMASK;2266 /* if not lvalue, then we convert now */
2248 if (!(t & VT_LVAL)) {2267 dbt = t & VT_BTYPE;
2249 /* if not lvalue, then we convert now */2268 sbt = vtop->t & VT_BTYPE;
2250 dbt = t & VT_BTYPE;2269 if (sbt != dbt) {
2251 sbt = vtop->t & VT_BTYPE;2270 sf = is_float(sbt);
2252 if (sbt != dbt) {2271 df = is_float(dbt);
2253 sf = is_float(sbt);2272 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2254 df = is_float(dbt);2273 if (sf && df) {
2255 c = (vtop->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;2274 /* convert from fp to fp */
2256 if (sf && df) {2275 if (c) {
2257 /* convert from fp to fp */2276 /* constant case: we can do it now */
2258 if (c) {2277 /* XXX: in ISOC, cannot do it if error in convert */
2259 /* constant case: we can do it now */2278 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
2260 /* XXX: in ISOC, cannot do it if error in convert */2279 vtop->c.f = (float)vtop->c.d;
2261 if (dbt == VT_FLOAT && sbt == VT_DOUBLE) 2280 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
2262 vtop->c.f = (float)vtop->c.d;2281 vtop->c.f = (float)vtop->c.ld;
2263 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE) 2282 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
2264 vtop->c.f = (float)vtop->c.ld;2283 vtop->c.d = (double)vtop->c.f;
2265 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT) 2284 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
2266 vtop->c.d = (double)vtop->c.f;2285 vtop->c.d = (double)vtop->c.ld;
2267 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE) 2286 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
2268 vtop->c.d = (double)vtop->c.ld;2287 vtop->c.ld = (long double)vtop->c.f;
2269 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT) 2288 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
2270 vtop->c.ld = (long double)vtop->c.f;2289 vtop->c.ld = (long double)vtop->c.d;
2271 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE) 2290 } else {
2272 vtop->c.ld = (long double)vtop->c.d;2291 /* non constant case: generate code */
2273 } else {2292 gen_cvt_ftof(dbt);
2274 /* non constant case: generate code */2293 }
2275 gen_cvt_ftof(dbt);2294 } else if (df) {
2276 }2295 /* convert int to fp */
2277 } else if (df) {2296 /* XXX: add const cases */
2278 /* convert int to fp */2297 st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED);
2279 /* XXX: add const cases */2298 if (c) {
2280 st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED);2299 switch(st1) {
2281 if (c) {2300 case VT_LLONG | VT_UNSIGNED:
2282 switch(st1) {2301 case VT_LLONG:
2283 case VT_LLONG | VT_UNSIGNED:2302 /* well, currently not needed */
2284 case VT_LLONG:2303 goto do_itof;
2285 /* well, currently not needed */2304 case VT_INT | VT_UNSIGNED:
2286 goto do_itof;2305 switch(dbt) {
2287 case VT_INT | VT_UNSIGNED:2306 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
2288 switch(dbt) {2307 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
2289 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;2308 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
2290 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
2291 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
2292 }
2293 break;
2294 default:
2295 switch(dbt) {
2296 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
2297 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
2298 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
2299 }
2300 break;
2301 }2309 }
2302 } else {2310 break;
2303 do_itof:2311 default:
2304 gen_cvt_itof(dbt);2312 switch(dbt) {
2305 }2313 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
2306 } else if (sf) {2314 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
2307 /* convert fp to int */2315 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
2308 dt1 = t & (VT_BTYPE | VT_UNSIGNED);
2309 /* we handle char/short/etc... with generic code */
2310 if (dt1 != VT_INT | VT_UNSIGNED &&
2311 dt1 != VT_LLONG | VT_UNSIGNED &&
2312 dt1 != VT_LLONG)
2313 dt1 = VT_INT;
2314 if (c) {
2315 switch(dt1) {
2316 case VT_LLONG | VT_UNSIGNED:
2317 case VT_LLONG:
2318 /* well, currently not needed */
2319 goto do_ftoi;
2320 case VT_INT | VT_UNSIGNED:
2321 switch(sbt) {
2322 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
2323 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2324 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2325 }
2326 break;
2327 default:
2328 /* int case */
2329 switch(sbt) {
2330 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
2331 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
2332 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
2333 }
2334 break;
2335 }2316 }
2336 } else {2317 break;
2337 do_ftoi:
2338 gen_cvt_ftoi(dt1);
2339 }
2340 if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) {
2341 /* additionnal cast for char/short/bool... */
2342 vtop->t = (vtop->t & ~VT_TYPE) | dt1;
2343 gen_cast(t);
2344 }2318 }
2345 } else if (dbt == VT_BOOL) {2319 } else {
2346 vset(VT_CONST, 0);2320 do_itof:
2347 gen_op(TOK_NE);2321 gen_cvt_itof(dbt);
2348 } else if (dbt == VT_BYTE || dbt == VT_SHORT) {2322 }
2349 if (dbt == VT_BYTE)2323 } else if (sf) {
2350 bits = 8;2324 /* convert fp to int */
2351 else2325 dt1 = t & (VT_BTYPE | VT_UNSIGNED);
2352 bits = 16;2326 /* we handle char/short/etc... with generic code */
2353 if (t & VT_UNSIGNED) {2327 if (dt1 != VT_INT | VT_UNSIGNED &&
2354 vset(VT_CONST, (1 << bits) - 1);2328 dt1 != VT_LLONG | VT_UNSIGNED &&
2355 gen_op('&');2329 dt1 != VT_LLONG)
2356 } else {2330 dt1 = VT_INT;
2357 bits = 32 - bits;2331 if (c) {
2358 vset(VT_CONST, bits);2332 switch(dt1) {
2359 gen_op(TOK_SHL);2333 case VT_LLONG | VT_UNSIGNED:
2360 vset(VT_CONST, bits);2334 case VT_LLONG:
2361 gen_op(TOK_SAR);2335 /* well, currently not needed */
2336 goto do_ftoi;
2337 case VT_INT | VT_UNSIGNED:
2338 switch(sbt) {
2339 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
2340 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2341 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2342 }
2343 break;
2344 default:
2345 /* int case */
2346 switch(sbt) {
2347 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
2348 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
2349 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
2350 }
2351 break;
2362 }2352 }
2353 } else {
2354 do_ftoi:
2355 gen_cvt_ftoi(dt1);
2356 }
2357 if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) {
2358 /* additionnal cast for char/short/bool... */
2359 vtop->t = (vtop->t & ~VT_TYPE) | dt1;
2360 gen_cast(t);
2361 }
2362 } else if (dbt == VT_BOOL) {
2363 vpushi(0);
2364 gen_op(TOK_NE);
2365 } else if ((dbt == VT_BYTE || dbt == VT_SHORT) &&
2366 !(vtop->r & VT_LVAL)) {
2367 /* no need to apply if lvalue because we do it while
2368 loading the value */
2369 if (dbt == VT_BYTE)
2370 bits = 8;
2371 else
2372 bits = 16;
2373 if (t & VT_UNSIGNED) {
2374 vpushi((1 << bits) - 1);
2375 gen_op('&');
2376 } else {
2377 bits = 32 - bits;
2378 vpushi(bits);
2379 gen_op(TOK_SHL);
2380 vpushi(bits);
2381 gen_op(TOK_SAR);
2363 }2382 }
2364 }2383 }
2365 }2384 }
2366 vtop->t = (vtop->t & ~VT_TYPE) | t;2385 vtop->t = t;
2367}2386}
2368 2387
2369/* return type size. Put alignment at 'a' */2388/* return type size. Put alignment at 'a' */
@@ -2599,7 +2618,7 @@ void gen_assign_cast(int dt) · 229 unmodified lines
2599/* store vtop in lvalue pushed on stack */2618/* store vtop in lvalue pushed on stack */
2600void vstore(void)2619void vstore(void)
2601{2620{
2602 int ft, fc, r, t, size, align, bit_size, bit_pos;2621 int ft, r, t, size, align, bit_size, bit_pos;
2603 GFuncContext gf;2622 GFuncContext gf;
2604 2623
2605 ft = vtop[-1].t;2624 ft = vtop[-1].t;
@@ -2614,18 +2633,20 @@ void vstore(void) · 8 unmodified lines
2614 gfunc_start(&gf);2633 gfunc_start(&gf);
2615 /* type size */2634 /* type size */
2616 size = type_size(vtop->t, &align);2635 size = type_size(vtop->t, &align);
2617 vset(VT_CONST, size);2636 vpushi(size);
2618 gfunc_param(&gf);2637 gfunc_param(&gf);
2619 /* source */2638 /* source */
2620 vtop->t &= ~VT_LVAL;2639 vtop->t = VT_INT;
2640 vtop->r &= ~VT_LVAL;
2621 gfunc_param(&gf);2641 gfunc_param(&gf);
2622 /* destination */2642 /* destination */
2623 vswap();2643 vswap();
2624 vtop->t &= ~VT_LVAL;2644 vtop->t = VT_INT;
2645 vtop->r &= ~VT_LVAL;
2625 gfunc_param(&gf);2646 gfunc_param(&gf);
2626 2647
2627 save_regs();2648 save_regs();
2628 vset(VT_CONST, (int)&memcpy);2649 vpushi((int)&memcpy);
2629 gfunc_call(&gf);2650 gfunc_call(&gf);
2630 /* leave source on stack */2651 /* leave source on stack */
2631 } else if (ft & VT_BITFIELD) {2652 } else if (ft & VT_BITFIELD) {
@@ -2640,30 +2661,32 @@ void vstore(void) · 8 unmodified lines
2640 vtop[-1] = vtop[-2];2661 vtop[-1] = vtop[-2];
2641 2662
2642 /* mask and shift source */2663 /* mask and shift source */
2643 vset(VT_CONST, (1 << bit_size) - 1);2664 vpushi((1 << bit_size) - 1);
2644 gen_op('&');2665 gen_op('&');
2645 vset(VT_CONST, bit_pos);2666 vpushi(bit_pos);
2646 gen_op(TOK_SHL);2667 gen_op(TOK_SHL);
2647 /* load destination, mask and or with source */2668 /* load destination, mask and or with source */
2648 vswap();2669 vswap();
2649 vset(VT_CONST, ~(((1 << bit_size) - 1) << bit_pos));2670 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2650 gen_op('&');2671 gen_op('&');
2651 gen_op('|');2672 gen_op('|');
2652 /* store result */2673 /* store result */
2653 vstore();2674 vstore();
2654 } else {2675 } else {
2655 r = gv(); /* generate value */2676 r = gv(); /* generate value */
2656 ft = vtop[-1].t;
2657 fc = vtop[-1].c.i;
2658 /* if lvalue was saved on stack, must read it */2677 /* if lvalue was saved on stack, must read it */
2659 if ((ft & VT_VALMASK) == VT_LLOCAL) {2678 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2679 SValue sv;
2660 t = get_reg(REG_CLASS_INT);2680 t = get_reg(REG_CLASS_INT);
2661 load(t, VT_LOCAL | VT_LVAL, fc);2681 sv.t = VT_INT;
2662 ft = (ft & ~VT_VALMASK) | t;2682 sv.r = VT_LOCAL | VT_LVAL;
2683 sv.c.ul = vtop[-1].c.ul;
2684 load(t, &sv);
2685 vtop[-1].r = t | VT_LVAL;
2663 }2686 }
2664 store(r, ft, fc);2687 store(r, vtop - 1);
2665 vtop--;2688 vtop--;
2666 vtop->t = (ft & VT_TYPE) | r;2689 vtop->r = r;
2667 vtop->c.i = 0;2690 vtop->c.i = 0;
2668 }2691 }
2669}2692}
@@ -2672,6 +2695,7 @@ void vstore(void) · 2 unmodified lines
2672void inc(int post, int c)2695void inc(int post, int c)
2673{2696{
2674 int r, r1;2697 int r, r1;
2698 SValue sv;
2675 2699
2676 test_lvalue();2700 test_lvalue();
2677 if (post)2701 if (post)
@@ -2682,13 +2706,16 @@ void inc(int post, int c) · 4 unmodified lines
2682 /* duplicate value */2706 /* duplicate value */
2683 /* XXX: handle floats */2707 /* XXX: handle floats */
2684 r1 = get_reg(REG_CLASS_INT);2708 r1 = get_reg(REG_CLASS_INT);
2685 load(r1, r, 0); /* move r to r1 */2709 sv.t = VT_INT;
2710 sv.r = r;
2711 sv.c.ul = 0;
2712 load(r1, &sv); /* move r to r1 */
2686 /* duplicates value */2713 /* duplicates value */
2687 vtop[-2].t = (vtop->t & VT_TYPE) | r1;2714 vtop[-2].r = r1;
2688 vtop[-2].c.i = 0;2715 vtop[-2].c.i = 0;
2689 }2716 }
2690 /* add constant */2717 /* add constant */
2691 vset(VT_CONST, c - TOK_MID); 2718 vpushi(c - TOK_MID);
2692 gen_op('+');2719 gen_op('+');
2693 vstore(); /* store value */2720 vstore(); /* store value */
2694 if (post)2721 if (post)
@@ -3107,13 +3134,13 @@ Sym *external_sym(int v, int u) · 412 unmodified lines
3107 3134
3108void indir(void)3135void indir(void)
3109{3136{
3110 if (vtop->t & VT_LVAL)3137 if (vtop->r & VT_LVAL)
3111 gv();3138 gv();
3112 if ((vtop->t & VT_BTYPE) != VT_PTR)3139 if ((vtop->t & VT_BTYPE) != VT_PTR)
3113 expect("pointer");3140 expect("pointer");
3114 vtop->t = pointed_type(vtop->t);3141 vtop->t = pointed_type(vtop->t);
3115 if (!(vtop->t & VT_ARRAY)) /* an array is never an lvalue */3142 if (!(vtop->t & VT_ARRAY)) /* an array is never an lvalue */
3116 vtop->t |= VT_LVAL;3143 vtop->r |= VT_LVAL;
3117}3144}
3118 3145
3119/* pass a parameter to a function and do type checking and casting */3146/* pass a parameter to a function and do type checking and casting */
@@ -3141,21 +3168,21 @@ void unary(void) · 21 unmodified lines
3141 GFuncContext gf;3168 GFuncContext gf;
3142 3169
3143 if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {3170 if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {
3144 vset(VT_CONST | VT_INT, tokc.i);3171 vpushi(tokc.i);
3145 next();3172 next();
3146 } else if (tok == TOK_CFLOAT) {3173 } else if (tok == TOK_CFLOAT) {
3147 vsetc(VT_CONST | VT_FLOAT, &tokc);3174 vsetc(VT_FLOAT, VT_CONST, &tokc);
3148 next();3175 next();
3149 } else if (tok == TOK_CDOUBLE) {3176 } else if (tok == TOK_CDOUBLE) {
3150 vsetc(VT_CONST | VT_DOUBLE, &tokc);3177 vsetc(VT_DOUBLE, VT_CONST, &tokc);
3151 next();3178 next();
3152 } else if (tok == TOK_CLDOUBLE) {3179 } else if (tok == TOK_CLDOUBLE) {
3153 vsetc(VT_CONST | VT_LDOUBLE, &tokc);3180 vsetc(VT_LDOUBLE, VT_CONST, &tokc);
3154 next();3181 next();
3155 } else if (tok == TOK___FUNC__) {3182 } else if (tok == TOK___FUNC__) {
3156 /* special function name identifier */3183 /* special function name identifier */
3157 /* generate (char *) type */3184 /* generate (char *) type */
3158 vset(VT_CONST | mk_pointer(VT_BYTE), glo);3185 vset(mk_pointer(VT_BYTE), VT_CONST, glo);
3159 strcpy((void *)glo, funcname);3186 strcpy((void *)glo, funcname);
3160 glo += strlen(funcname) + 1;3187 glo += strlen(funcname) + 1;
3161 next();3188 next();
@@ -3174,7 +3201,7 @@ void unary(void) · 12 unmodified lines
3174 decl_initializer(t, glo, 1, 0);3201 decl_initializer(t, glo, 1, 0);
3175 glo += type_size(t, &align);3202 glo += type_size(t, &align);
3176 /* put it as pointer */3203 /* put it as pointer */
3177 vset(t & ~VT_ARRAY, fc);3204 vset(t & ~VT_ARRAY, VT_CONST, fc);
3178 } else {3205 } else {
3179 t = tok;3206 t = tok;
3180 next();3207 next();
@@ -3194,7 +3221,7 @@ void unary(void) · 13 unmodified lines
3194 if (!(ft & VT_ARRAY))3221 if (!(ft & VT_ARRAY))
3195 ft |= VT_LVAL;3222 ft |= VT_LVAL;
3196 fc = decl_initializer_alloc(ft, 1);3223 fc = decl_initializer_alloc(ft, 1);
3197 vset(ft, fc);3224 vset(ft & VT_TYPE, ft & (VT_VALMASK | VT_LVAL), fc);
3198 } else {3225 } else {
3199 unary();3226 unary();
3200 gen_cast(ft);3227 gen_cast(ft);
@@ -3214,20 +3241,21 @@ void unary(void) · 13 unmodified lines
3214 there and in function calls. */3241 there and in function calls. */
3215 if ((vtop->t & VT_BTYPE) != VT_FUNC)3242 if ((vtop->t & VT_BTYPE) != VT_FUNC)
3216 test_lvalue();3243 test_lvalue();
3217 vtop->t = mk_pointer(vtop->t & VT_LVALN);3244 vtop->t = mk_pointer(vtop->t);
3245 vtop->r &= ~VT_LVAL;
3218 } else3246 } else
3219 if (t == '!') {3247 if (t == '!') {
3220 unary();3248 unary();
3221 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) 3249 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
3222 vtop->c.i = !vtop->c.i;3250 vtop->c.i = !vtop->c.i;
3223 else if ((vtop->t & VT_VALMASK) == VT_CMP)3251 else if ((vtop->r & VT_VALMASK) == VT_CMP)
3224 vtop->c.i = vtop->c.i ^ 1;3252 vtop->c.i = vtop->c.i ^ 1;
3225 else3253 else
3226 vset(VT_JMP, gtst(1, 0));3254 vset(VT_INT, VT_JMP, gtst(1, 0));
3227 } else3255 } else
3228 if (t == '~') {3256 if (t == '~') {
3229 unary();3257 unary();
3230 vset(VT_CONST, -1);3258 vpushi(-1);
3231 gen_op('^');3259 gen_op('^');
3232 } else 3260 } else
3233 if (t == '+') {3261 if (t == '+') {
@@ -3251,13 +3279,13 @@ void unary(void) · 17 unmodified lines
3251 t = vtop->t;3279 t = vtop->t;
3252 vpop();3280 vpop();
3253 }3281 }
3254 vset(VT_CONST, type_size(t, &t));3282 vpushi(type_size(t, &t));
3255 } else3283 } else
3256 if (t == TOK_INC || t == TOK_DEC) {3284 if (t == TOK_INC || t == TOK_DEC) {
3257 unary();3285 unary();
3258 inc(0, t);3286 inc(0, t);
3259 } else if (t == '-') {3287 } else if (t == '-') {
3260 vset(VT_CONST, 0);3288 vpushi(0);
3261 unary();3289 unary();
3262 gen_op('-');3290 gen_op('-');
3263 } else 3291 } else
@@ -3273,9 +3301,10 @@ void unary(void) · 9 unmodified lines
3273 /* int() function */3301 /* int() function */
3274 s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT)); 3302 s = external_sym(t, VT_FUNC | (p << VT_STRUCT_SHIFT));
3275 }3303 }
3276 vset(s->t, s->c);3304 vset(s->t & VT_TYPE,
3305 s->t & (VT_VALMASK | VT_LVAL | VT_FORWARD), s->c);
3277 /* if forward reference, we must point to s */3306 /* if forward reference, we must point to s */
3278 if (vtop->t & VT_FORWARD)3307 if (vtop->r & VT_FORWARD)
3279 vtop->c.sym = s;3308 vtop->c.sym = s;
3280 }3309 }
3281 }3310 }
@@ -3290,7 +3319,7 @@ void unary(void) · 8 unmodified lines
3290 if (tok == TOK_ARROW) 3319 if (tok == TOK_ARROW)
3291 indir();3320 indir();
3292 test_lvalue();3321 test_lvalue();
3293 vtop->t &= VT_LVALN;3322 vtop->r &= ~VT_LVAL;
3294 next();3323 next();
3295 /* expect pointer on structure */3324 /* expect pointer on structure */
3296 if ((vtop->t & VT_BTYPE) != VT_STRUCT)3325 if ((vtop->t & VT_BTYPE) != VT_STRUCT)
@@ -3305,14 +3334,14 @@ void unary(void) · 8 unmodified lines
3305 if (!s)3334 if (!s)
3306 error("field not found");3335 error("field not found");
3307 /* add field offset to pointer */3336 /* add field offset to pointer */
3308 vtop->t = (vtop->t & ~VT_TYPE) | VT_INT; /* change type to int */3337 vtop->t = VT_INT; /* change type to int */
3309 vset(VT_CONST, s->c);3338 vpushi(s->c);
3310 gen_op('+');3339 gen_op('+');
3311 /* change type to field type, and set to lvalue */3340 /* change type to field type, and set to lvalue */
3312 vtop->t = (vtop->t & ~VT_TYPE) | s->t;3341 vtop->t = s->t;
3313 /* an array is never an lvalue */3342 /* an array is never an lvalue */
3314 if (!(vtop->t & VT_ARRAY))3343 if (!(vtop->t & VT_ARRAY))
3315 vtop->t |= VT_LVAL;3344 vtop->r |= VT_LVAL;
3316 next();3345 next();
3317 } else if (tok == '[') {3346 } else if (tok == '[') {
3318 next();3347 next();
@@ -3321,8 +3350,7 @@ void unary(void) · 2 unmodified lines
3321 indir();3350 indir();
3322 skip(']');3351 skip(']');
3323 } else if (tok == '(') {3352 } else if (tok == '(') {
3324 int rett;3353 SValue ret;
3325 CValue retc;
3326 Sym *sa;3354 Sym *sa;
3327 3355
3328 /* function call */3356 /* function call */
@@ -3337,7 +3365,7 @@ void unary(void) · 8 unmodified lines
3337 expect("function pointer");3365 expect("function pointer");
3338 }3366 }
3339 } else {3367 } else {
3340 vtop->t &= ~VT_LVAL; /* no lvalue */3368 vtop->r &= ~VT_LVAL; /* no lvalue */
3341 }3369 }
3342 /* get return type */3370 /* get return type */
3343 s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);3371 s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);
@@ -3403,15 +3431,17 @@ void unary(void) · 59 unmodified lines
3403 /* get some space for the returned structure */3431 /* get some space for the returned structure */
3404 size = type_size(s->t, &align);3432 size = type_size(s->t, &align);
3405 loc = (loc - size) & -align;3433 loc = (loc - size) & -align;
3406 rett = s->t | VT_LOCAL | VT_LVAL;3434 ret.t = s->t;
3435 ret.r = VT_LOCAL | VT_LVAL;
3407 /* pass it as 'int' to avoid structure arg passing3436 /* pass it as 'int' to avoid structure arg passing
3408 problems */3437 problems */
3409 vset(VT_INT | VT_LOCAL, loc);3438 vset(VT_INT, VT_LOCAL, loc);
3410 retc = vtop->c;3439 ret.c = vtop->c;
3411 gfunc_param(&gf);3440 gfunc_param(&gf);
3412 } else {3441 } else {
3413 rett = s->t | FUNC_RET_REG; /* return in register */3442 ret.t = s->t;
3414 retc.i = 0;3443 ret.r = FUNC_RET_REG; /* return in register */
3444 ret.c.i = 0;
3415 }3445 }
3416#ifndef INVERT_FUNC_PARAMS3446#ifndef INVERT_FUNC_PARAMS
3417 while (tok != ')') {3447 while (tok != ')') {
@@ -3428,7 +3458,7 @@ void unary(void) · 10 unmodified lines
3428 skip(')');3458 skip(')');
3429 gfunc_call(&gf);3459 gfunc_call(&gf);
3430 /* return value */3460 /* return value */
3431 vsetc(rett, &retc);3461 vsetc(ret.t, ret.r, &ret.c);
3432 } else {3462 } else {
3433 break;3463 break;
3434 }3464 }
@@ -3496,7 +3526,7 @@ void eand(void) · 61 unmodified lines
3496 if (tok != TOK_LAND) {3526 if (tok != TOK_LAND) {
3497 if (t) {3527 if (t) {
3498 t = gtst(1, t);3528 t = gtst(1, t);
3499 vset(VT_JMPI, t);3529 vset(VT_INT, VT_JMPI, t);
3500 }3530 }
3501 break;3531 break;
3502 }3532 }
@@ -3516,7 +3546,7 @@ void eor(void) · 13 unmodified lines
3516 if (tok != TOK_LOR) {3546 if (tok != TOK_LOR) {
3517 if (t) {3547 if (t) {
3518 t = gtst(0, t);3548 t = gtst(0, t);
3519 vset(VT_JMP, t);3549 vset(VT_INT, VT_JMP, t);
3520 }3550 }
3521 break;3551 break;
3522 }3552 }
@@ -3561,7 +3591,7 @@ void expr_eq(void) · 38 unmodified lines
3561 expr_eq();3591 expr_eq();
3562 r2 = gv();3592 r2 = gv();
3563 move_reg(r1, r2);3593 move_reg(r1, r2);
3564 vtop->t = (vtop->t & VT_TYPE) | r1;3594 vtop->r = r1;
3565 gsym(u);3595 gsym(u);
3566 }3596 }
3567 }3597 }
@@ -3585,7 +3615,7 @@ void expr_const1(void) · 17 unmodified lines
3585 a = const_wanted;3615 a = const_wanted;
3586 const_wanted = 1;3616 const_wanted = 1;
3587 expr_eq();3617 expr_eq();
3588 if ((vtop->t & (VT_CONST | VT_LVAL)) != VT_CONST)3618 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
3589 expect("constant");3619 expect("constant");
3590 const_wanted = a;3620 const_wanted = a;
3591}3621}
@@ -3680,7 +3710,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 88 unmodified lines
3680 if ((func_vt & VT_BTYPE) == VT_STRUCT) {3710 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
3681 /* if returning structure, must copy it to implicit3711 /* if returning structure, must copy it to implicit
3682 first pointer arg location */3712 first pointer arg location */
3683 vset(mk_pointer(func_vt) | VT_LOCAL | VT_LVAL, func_vc);3713 vset(mk_pointer(func_vt), VT_LOCAL | VT_LVAL, func_vc);
3684 indir();3714 indir();
3685 vswap();3715 vswap();
3686 /* copy structure value to pointer */3716 /* copy structure value to pointer */
@@ -3785,8 +3815,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 98 unmodified lines
3785 /* since a case is like a label, we must skip it with a jmp */3815 /* since a case is like a label, we must skip it with a jmp */
3786 b = gjmp(0);3816 b = gjmp(0);
3787 gsym(*case_sym);3817 gsym(*case_sym);
3788 vset(case_reg, 0);3818 vset(VT_INT, case_reg, 0);
3789 vset(VT_CONST, a);3819 vpushi(a);
3790 gen_op(TOK_EQ);3820 gen_op(TOK_EQ);
3791 *case_sym = gtst(1, 0);3821 *case_sym = gtst(1, 0);
3792 gsym(b);3822 gsym(b);
@@ -3936,7 +3966,7 @@ void init_putv(int t, int c, int v, int is_expr) · 143 unmodified lines
3936 expr_const1();3966 expr_const1();
3937 global_expr = saved_global_expr;3967 global_expr = saved_global_expr;
3938 } else {3968 } else {
3939 vset(VT_CONST | VT_INT, v);3969 vpushi(v);
3940 }3970 }
3941 /* XXX: do casting */3971 /* XXX: do casting */
3942 /* XXX: not portable */3972 /* XXX: not portable */
@@ -3965,11 +3995,11 @@ void init_putv(int t, int c, int v, int is_expr) · 22 unmodified lines
3965 }3995 }
3966 vpop();3996 vpop();
3967 } else {3997 } else {
3968 vset(t, c);3998 vset(t & VT_TYPE, t & (VT_VALMASK | VT_LVAL | VT_FORWARD), c);
3969 if (is_expr)3999 if (is_expr)
3970 expr_eq();4000 expr_eq();
3971 else4001 else
3972 vset(VT_CONST | VT_INT, v);4002 vpushi(v);
3973 vstore();4003 vstore();
3974 vpop();4004 vpop();
3975 }4005 }
@@ -3984,13 +4014,13 @@ void init_putz(int t, int c, int size) · 8 unmodified lines
3984 /* nothing to do because global are already set to zero */4014 /* nothing to do because global are already set to zero */
3985 } else {4015 } else {
3986 gfunc_start(&gf);4016 gfunc_start(&gf);
3987 vset(VT_CONST, size);4017 vpushi(size);
3988 gfunc_param(&gf);4018 gfunc_param(&gf);
3989 vset(VT_CONST, 0);4019 vpushi(0);
3990 gfunc_param(&gf);4020 gfunc_param(&gf);
3991 vset(VT_LOCAL, c);4021 vset(VT_INT, VT_LOCAL, c);
3992 gfunc_param(&gf);4022 gfunc_param(&gf);
3993 vset(VT_CONST, (int)&memset);4023 vpushi((int)&memset);
3994 gfunc_call(&gf);4024 gfunc_call(&gf);
3995 }4025 }
3996}4026}

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