History

float update

bellard committed 24 years ago

Commit 2694c101 parent 5ee15842 files · +568 −232Browse files

Unified · Split

2 files
  1. i386-gen.c +82 −19
  2. tcc.c +486 −213
i386-gen.c+82−19
@@ -242,12 +242,13 @@ void store(r, ft, fc) · 241 unmodified lines
242 /* XXX: incorrect if reg to reg */242 /* XXX: incorrect if reg to reg */
243 /* XXX: should not flush float stack */243 /* XXX: should not flush float stack */
244 if (bt == VT_FLOAT) {244 if (bt == VT_FLOAT) {
245 o(0xd9); /* fstps */245 o(0xd9); /* fsts */
246 r = 3;246 r = 2;
247 } else if (bt == VT_DOUBLE) {247 } else if (bt == VT_DOUBLE) {
248 o(0xdd); /* fstpl */248 o(0xdd); /* fstpl */
249 r = 3;249 r = 2;
250 } else if (bt == VT_LDOUBLE) {250 } else if (bt == VT_LDOUBLE) {
251 o(0xc0d9); /* fld %st(0) */
251 o(0xdb); /* fstpt */252 o(0xdb); /* fstpt */
252 r = 7;253 r = 7;
253 } else {254 } else {
@@ -296,9 +297,7 @@ void gfunc_param(GFuncContext *c) · 42 unmodified lines
296 vswap();297 vswap();
297 vstore();298 vstore();
298 c->args_size += size;299 c->args_size += size;
299 } else if ((vtop->t & VT_BTYPE) == VT_LDOUBLE ||300 } else if (is_float(vtop->t)) {
300 (vtop->t & VT_BTYPE) == VT_DOUBLE ||
301 (vtop->t & VT_BTYPE) == VT_FLOAT) {
302 gv(); /* only one float register */301 gv(); /* only one float register */
303 if ((vtop->t & VT_BTYPE) == VT_FLOAT)302 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
304 size = 4;303 size = 4;
@@ -333,10 +332,10 @@ void gfunc_call(GFuncContext *c) · 28 unmodified lines
333 /* constant case */332 /* constant case */
334 /* forward reference */333 /* forward reference */
335 if (vtop->t & VT_FORWARD) {334 if (vtop->t & VT_FORWARD) {
336 greloc((Sym *)vtop->c, ind + 1, RELOC_REL32);335 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
337 oad(0xe8, 0);336 oad(0xe8, 0);
338 } else {337 } else {
339 oad(0xe8, vtop->c - ind - 5);338 oad(0xe8, vtop->c.ul - ind - 5);
340 }339 }
341 } else {340 } else {
342 /* otherwise, indirect call */341 /* otherwise, indirect call */
@@ -362,23 +361,23 @@ int gtst(int inv, int t) · 19 unmodified lines
362 if (v == VT_CMP) {361 if (v == VT_CMP) {
363 /* fast case : can jump directly since flags are set */362 /* fast case : can jump directly since flags are set */
364 g(0x0f);363 g(0x0f);
365 t = psym((vtop->c - 16) ^ inv, t);364 t = psym((vtop->c.i - 16) ^ inv, t);
366 } else if (v == VT_JMP || v == VT_JMPI) {365 } else if (v == VT_JMP || v == VT_JMPI) {
367 /* && or || optimization */366 /* && or || optimization */
368 if ((v & 1) == inv) {367 if ((v & 1) == inv) {
369 /* insert vtop->c jump list in t */368 /* insert vtop->c jump list in t */
370 p = &vtop->c;369 p = &vtop->c.i;
371 while (*p != 0)370 while (*p != 0)
372 p = (int *)*p;371 p = (int *)*p;
373 *p = t;372 *p = t;
374 t = vtop->c;373 t = vtop->c.i;
375 } else {374 } else {
376 t = gjmp(t);375 t = gjmp(t);
377 gsym(vtop->c);376 gsym(vtop->c.i);
378 }377 }
379 } else if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {378 } else if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
380 /* constant jmp optimization */379 /* constant jmp optimization */
381 if ((vtop->c != 0) != inv) 380 if ((vtop->c.i != 0) != inv)
382 t = gjmp(t);381 t = gjmp(t);
383 } else {382 } else {
384 /* XXX: floats */383 /* XXX: floats */
@@ -472,6 +471,15 @@ void gen_opf(int op) · 87 unmodified lines
472{471{
473 int a, ft, fc, swapped, r;472 int a, ft, fc, swapped, r;
474 473
474 /* convert constants to memory references */
475 if ((vtop[-1].t & (VT_CONST | VT_LVAL)) == VT_CONST) {
476 vswap();
477 gv();
478 vswap();
479 }
480 if ((vtop[0].t & (VT_CONST | VT_LVAL)) == VT_CONST)
481 gv();
482
475 /* must put at least one value in the floating point register */483 /* must put at least one value in the floating point register */
476 if ((vtop[-1].t & VT_LVAL) &&484 if ((vtop[-1].t & VT_LVAL) &&
477 (vtop[0].t & VT_LVAL)) {485 (vtop[0].t & VT_LVAL)) {
@@ -481,7 +489,7 @@ void gen_opf(int op) · 3 unmodified lines
481 }489 }
482 if (op >= TOK_EQ && op <= TOK_GT) {490 if (op >= TOK_EQ && op <= TOK_GT) {
483 /* load on stack second operand */491 /* load on stack second operand */
484 load(REG_ST0, vtop->t, vtop->c);492 load(REG_ST0, vtop->t, vtop->c.ul);
485 if (op == TOK_GE || op == TOK_GT)493 if (op == TOK_GE || op == TOK_GT)
486 o(0xc9d9); /* fxch %st(1) */494 o(0xc9d9); /* fxch %st(1) */
487 o(0xe9da); /* fucompp */495 o(0xe9da); /* fucompp */
@@ -502,7 +510,7 @@ void gen_opf(int op) · 14 unmodified lines
502 }510 }
503 vtop--;511 vtop--;
504 vtop->t = (vtop->t & VT_TYPE) | VT_CMP;512 vtop->t = (vtop->t & VT_TYPE) | VT_CMP;
505 vtop->c = op;513 vtop->c.i = op;
506 } else {514 } else {
507 /* swap the stack if needed so that t1 is the register and t2 is515 /* swap the stack if needed so that t1 is the register and t2 is
508 the memory reference */516 the memory reference */
@@ -532,7 +540,7 @@ void gen_opf(int op) · 23 unmodified lines
532 break;540 break;
533 }541 }
534 ft = vtop->t;542 ft = vtop->t;
535 fc = vtop->c;543 fc = vtop->c.ul;
536 if ((ft & VT_BTYPE) == VT_DOUBLE)544 if ((ft & VT_BTYPE) == VT_DOUBLE)
537 o(0xdc);545 o(0xdc);
538 else546 else
@@ -551,10 +559,10 @@ void gen_opf(int op) · 12 unmodified lines
551 }559 }
552}560}
553 561
554/* convert integers to floating point 't' type (float/double/long562/* convert integers to fp 't' type */
555 double) */563void gen_cvt_itof(int t)
556void gen_cvtf(int t)
557{564{
565 gv();
558 if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {566 if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {
559 /* unsigned int to float/double/long double */567 /* unsigned int to float/double/long double */
560 o(0x6a); /* push $0 */568 o(0x6a); /* push $0 */
@@ -568,8 +576,63 @@ void gen_cvtf(int t) · 7 unmodified lines
568 o(0x2404db); /* fildl (%esp) */576 o(0x2404db); /* fildl (%esp) */
569 o(0x04c483); /* add $4, %esp */577 o(0x04c483); /* add $4, %esp */
570 }578 }
579 vtop->t = t | REG_ST0;
580}
581
582/* FPU control word for rounding to nearest mode */
583/* XXX: should move that into tcc lib support code ! */
584static unsigned short __tcc_fpu_control = 0x137f;
585/* FPU control word for round to zero mode for int convertion */
586static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
587
588/* convert fp to int 't' type */
589/* XXX: handle long long case */
590void gen_cvt_ftoi(int t)
591{
592 int r, size;
593
594 gv();
595 if (t == VT_INT | VT_UNSIGNED &&
596 t == VT_LLONG | VT_UNSIGNED &&
597 t == VT_LLONG)
598 size = 8;
599 else
600 size = 4;
601
602 r = get_reg(REG_CLASS_INT);
603 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
604 oad(0xec81, size); /* sub $xxx, %esp */
605 if (size == 4)
606 o(0x1cdb); /* fistpl */
607 else
608 o(0x3cdb); /* fistpll */
609 o(0x24);
610 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
611 o(0x58 + r); /* pop r */
612 if (size == 8)
613 o(0x04c483); /* add $4, %esp */
614 vtop->t = t | r;
571}615}
572 616
617/* convert from one floating point type to another */
618void gen_cvt_ftof(int t)
619{
620 /* all we have to do on i386 is to put the float in a register */
621 gv();
622}
623
624/* pop stack value */
625void vpop(void)
626{
627 /* for x86, we need to pop the FP stack */
628 if ((vtop->t & VT_VALMASK) == REG_ST0) {
629 o(0xd9dd); /* fstp %st(1) */
630 }
631 vtop--;
632}
633
634
635
573/* end of X86 code generator */636/* end of X86 code generator */
574/*************************************************************/637/*************************************************************/
575 638
tcc.c+486−213
@@ -60,14 +60,18 @@ typedef union CValue { · 59 unmodified lines
60 float f;60 float f;
61 int i;61 int i;
62 unsigned int ui;62 unsigned int ui;
63 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
64 long long ll;
65 unsigned long long ull;
63 struct TokenSym *ts;66 struct TokenSym *ts;
64 int tab[1];67 int tab[1];
68 struct Sym *sym;
65} CValue;69} CValue;
66 70
67/* value on stack */71/* value on stack */
68typedef struct SValue {72typedef struct SValue {
69 int t;73 int t;
70 int c;74 CValue c;
71} SValue;75} SValue;
72 76
73/* symbol management */77/* symbol management */
@@ -332,7 +336,7 @@ void next(void); · 258 unmodified lines
332void next_nomacro(void);336void next_nomacro(void);
333int expr_const(void);337int expr_const(void);
334void expr_eq(void);338void expr_eq(void);
335void expr(void);339void gexpr(void);
336void decl(int l);340void decl(int l);
337void decl_initializer(int t, int c, int first, int size_only);341void decl_initializer(int t, int c, int first, int size_only);
338int decl_initializer_alloc(int t, int has_init);342int decl_initializer_alloc(int t, int has_init);
@@ -358,6 +362,14 @@ int type_decl(int *v, int t, int td); · 19 unmodified lines
358void error(const char *fmt, ...);362void error(const char *fmt, ...);
359void vset(int t, int v);363void vset(int t, int v);
360 364
365/* true if float/double/long double type */
366static inline int is_float(int t)
367{
368 int bt;
369 bt = t & VT_BTYPE;
370 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
371}
372
361#include "i386-gen.c"373#include "i386-gen.c"
362 374
363#ifdef CONFIG_TCC_STATIC375#ifdef CONFIG_TCC_STATIC
@@ -983,7 +995,7 @@ void preprocess(void) · 619 unmodified lines
983 }995 }
984 tok_add(&str, &len, 0);996 tok_add(&str, &len, 0);
985#ifdef PP_DEBUG997#ifdef PP_DEBUG
986 printf("define %s %d: ", get_tok_str(v, 0), t);998 printf("define %s %d: ", get_tok_str(v, NULL), t);
987 tok_print(str);999 tok_print(str);
988#endif1000#endif
989 s = sym_push1(&define_stack, v, t, (int)str);1001 s = sym_push1(&define_stack, v, t, (int)str);
@@ -1220,7 +1232,6 @@ void parse_number(void) · 230 unmodified lines
1220 char *q;1232 char *q;
1221 unsigned int n, n1;1233 unsigned int n, n1;
1222 unsigned int bn[BN_SIZE];1234 unsigned int bn[BN_SIZE];
1223 CValue cval;
1224 double d;1235 double d;
1225 1236
1226 /* number */1237 /* number */
@@ -1343,7 +1354,6 @@ void parse_number(void) · 116 unmodified lines
1343 cinp();1354 cinp();
1344 }1355 }
1345 exp_val = exp_val * s;1356 exp_val = exp_val * s;
1346 printf("num=%08x %08x %d %d\n", bn[1], bn[0], frac_bits, exp_val);
1347 1357
1348 /* now we can generate the number */1358 /* now we can generate the number */
1349 /* XXX: should patch directly float number */1359 /* XXX: should patch directly float number */
@@ -1354,15 +1364,15 @@ void parse_number(void) · 4 unmodified lines
1354 cinp();1364 cinp();
1355 tok = TOK_CFLOAT;1365 tok = TOK_CFLOAT;
1356 /* float : should handle overflow */1366 /* float : should handle overflow */
1357 cval.f = (float)d;1367 tokc.f = (float)d;
1358 } else if (t == 'L') {1368 } else if (t == 'L') {
1359 cinp();1369 cinp();
1360 tok = TOK_CLDOUBLE;1370 tok = TOK_CLDOUBLE;
1361 /* XXX: not large enough */1371 /* XXX: not large enough */
1362 cval.ld = (long double)d;1372 tokc.ld = (long double)d;
1363 } else {1373 } else {
1364 tok = TOK_CDOUBLE;1374 tok = TOK_CDOUBLE;
1365 cval.d = d;1375 tokc.d = d;
1366 }1376 }
1367 } else {1377 } else {
1368 /* decimal floats */1378 /* decimal floats */
@@ -1405,14 +1415,14 @@ void parse_number(void) · 36 unmodified lines
1405 if (t == 'F') {1415 if (t == 'F') {
1406 cinp();1416 cinp();
1407 tok = TOK_CFLOAT;1417 tok = TOK_CFLOAT;
1408 cval.f = strtof(token_buf, NULL);1418 tokc.f = strtof(token_buf, NULL);
1409 } else if (t == 'L') {1419 } else if (t == 'L') {
1410 cinp();1420 cinp();
1411 tok = TOK_CLDOUBLE;1421 tok = TOK_CLDOUBLE;
1412 cval.ld = strtold(token_buf, NULL);1422 tokc.ld = strtold(token_buf, NULL);
1413 } else {1423 } else {
1414 tok = TOK_CDOUBLE;1424 tok = TOK_CDOUBLE;
1415 cval.d = strtod(token_buf, NULL);1425 tokc.d = strtod(token_buf, NULL);
1416 }1426 }
1417 }1427 }
1418 } else {1428 } else {
@@ -1858,7 +1868,7 @@ void swap(int *p, int *q) · 439 unmodified lines
1858 *q = t;1868 *q = t;
1859}1869}
1860 1870
1861void vset(int t, int v)1871void vsetc(int t, CValue *vc)
1862{1872{
1863 if (vtop >= vstack + VSTACK_SIZE)1873 if (vtop >= vstack + VSTACK_SIZE)
1864 error("memory full");1874 error("memory full");
@@ -1868,12 +1878,15 @@ void vset(int t, int v) · 3 unmodified lines
1868 gv();1878 gv();
1869 vtop++;1879 vtop++;
1870 vtop->t = t;1880 vtop->t = t;
1871 vtop->c = v;1881 vtop->c = *vc;
1872}1882}
1873 1883
1874void vpop(void)1884void vset(int t, int v)
1875{1885{
1876 vtop--;1886 CValue cval;
1887
1888 cval.i = v;
1889 vsetc(t, &cval);
1877}1890}
1878 1891
1879void vswap(void)1892void vswap(void)
@@ -1912,7 +1925,7 @@ int save_reg_forced(int r) · 32 unmodified lines
1912 else1925 else
1913 t = VT_LOCAL;1926 t = VT_LOCAL;
1914 p->t = (p->t & VT_TYPE) | VT_LVAL | t;1927 p->t = (p->t & VT_TYPE) | VT_LVAL | t;
1915 p->c = l;1928 p->c.ul = l;
1916 }1929 }
1917 }1930 }
1918 return l;1931 return l;
@@ -1994,7 +2007,7 @@ void move_reg(int r, int s) · 75 unmodified lines
1994 (such as structures). */2007 (such as structures). */
1995int gv(void)2008int gv(void)
1996{2009{
1997 int r, bit_pos, bit_size, rc;2010 int r, bit_pos, bit_size, rc, size, align, i;
1998 2011
1999 /* NOTE: get_reg can modify vstack[] */2012 /* NOTE: get_reg can modify vstack[] */
2000 if (vtop->t & VT_BITFIELD) {2013 if (vtop->t & VT_BITFIELD) {
@@ -2010,16 +2023,28 @@ int gv(void) · 9 unmodified lines
2010 gen_op(TOK_SAR);2023 gen_op(TOK_SAR);
2011 r = gv();2024 r = gv();
2012 } else {2025 } else {
2026 if (is_float(vtop->t) && (vtop->t & (VT_CONST | VT_LVAL)) == VT_CONST) {
2027 /* CPUs usually cannot use float constants, so we store them
2028 generically in data segment */
2029 size = type_size(vtop->t, &align);
2030 glo = (glo + align - 1) & -align;
2031 /* XXX: not portable yet */
2032 size = size >> 2;
2033 for(i=0;i<size;i++)
2034 ((int *)glo)[i] = vtop->c.tab[i];
2035 vtop->t |= VT_LVAL;
2036 vtop->c.ul = glo;
2037 glo += size << 2;
2038 }
2013 r = vtop->t & VT_VALMASK;2039 r = vtop->t & VT_VALMASK;
2014 if (r >= VT_CONST || (vtop->t & VT_LVAL)) {2040 if (r >= VT_CONST || (vtop->t & VT_LVAL)) {
2015 if ((vtop->t & VT_BTYPE) == VT_FLOAT ||2041 if (is_float(vtop->t))
2016 (vtop->t & VT_BTYPE) == VT_DOUBLE)
2017 rc = REG_CLASS_FLOAT;2042 rc = REG_CLASS_FLOAT;
2018 else2043 else
2019 rc = REG_CLASS_INT;2044 rc = REG_CLASS_INT;
2020 r = get_reg(rc);2045 r = get_reg(rc);
2021 }2046 }
2022 load(r, vtop->t, vtop->c);2047 load(r, vtop->t, vtop->c.ul);
2023 vtop->t = (vtop->t & VT_TYPE) | r;2048 vtop->t = (vtop->t & VT_TYPE) | r;
2024 }2049 }
2025 return r;2050 return r;
@@ -2037,36 +2062,36 @@ void gen_opc(int op) · 11 unmodified lines
2037 c1 = (v1->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;2062 c1 = (v1->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2038 c2 = (v2->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;2063 c2 = (v2->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2039 if (c1 && c2) {2064 if (c1 && c2) {
2040 fc = v2->c;2065 fc = v2->c.i;
2041 switch(op) {2066 switch(op) {
2042 case '+': v1->c += fc; break;2067 case '+': v1->c.i += fc; break;
2043 case '-': v1->c -= fc; break;2068 case '-': v1->c.i -= fc; break;
2044 case '&': v1->c &= fc; break;2069 case '&': v1->c.i &= fc; break;
2045 case '^': v1->c ^= fc; break;2070 case '^': v1->c.i ^= fc; break;
2046 case '|': v1->c |= fc; break;2071 case '|': v1->c.i |= fc; break;
2047 case '*': v1->c *= fc; break;2072 case '*': v1->c.i *= fc; break;
2048 case TOK_PDIV:2073 case TOK_PDIV:
2049 case '/': v1->c /= fc; break; /* XXX: zero case ? */2074 case '/': v1->c.i /= fc; break; /* XXX: zero case ? */
2050 case '%': v1->c %= fc; break; /* XXX: zero case ? */2075 case '%': v1->c.i %= fc; break; /* XXX: zero case ? */
2051 case TOK_UDIV: v1->c = (unsigned)v1->c / fc; break; /* XXX: zero case ? */2076 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break; /* XXX: zero case ? */
2052 case TOK_UMOD: v1->c = (unsigned)v1->c % fc; break; /* XXX: zero case ? */2077 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break; /* XXX: zero case ? */
2053 case TOK_SHL: v1->c <<= fc; break;2078 case TOK_SHL: v1->c.i <<= fc; break;
2054 case TOK_SHR: v1->c = (unsigned)v1->c >> fc; break;2079 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
2055 case TOK_SAR: v1->c >>= fc; break;2080 case TOK_SAR: v1->c.i >>= fc; break;
2056 /* tests */2081 /* tests */
2057 case TOK_ULT: v1->c = (unsigned)v1->c < (unsigned)fc; break;2082 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
2058 case TOK_UGE: v1->c = (unsigned)v1->c >= (unsigned)fc; break;2083 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
2059 case TOK_EQ: v1->c = v1->c == fc; break;2084 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
2060 case TOK_NE: v1->c = v1->c != fc; break;2085 case TOK_NE: v1->c.i = v1->c.i != fc; break;
2061 case TOK_ULE: v1->c = (unsigned)v1->c <= (unsigned)fc; break;2086 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
2062 case TOK_UGT: v1->c = (unsigned)v1->c > (unsigned)fc; break;2087 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
2063 case TOK_LT: v1->c = v1->c < fc; break;2088 case TOK_LT: v1->c.i = v1->c.i < fc; break;
2064 case TOK_GE: v1->c = v1->c >= fc; break;2089 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
2065 case TOK_LE: v1->c = v1->c <= fc; break;2090 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
2066 case TOK_GT: v1->c = v1->c > fc; break;2091 case TOK_GT: v1->c.i = v1->c.i > fc; break;
2067 /* logical */2092 /* logical */
2068 case TOK_LAND: v1->c = v1->c && fc; break;2093 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
2069 case TOK_LOR: v1->c = v1->c || fc; break;2094 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
2070 default:2095 default:
2071 goto general_case;2096 goto general_case;
2072 }2097 }
@@ -2078,7 +2103,7 @@ void gen_opc(int op) · 5 unmodified lines
2078 vswap();2103 vswap();
2079 swap(&c1, &c2);2104 swap(&c1, &c2);
2080 }2105 }
2081 fc = vtop->c;2106 fc = vtop->c.i;
2082 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV || 2107 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
2083 op == TOK_PDIV) && 2108 op == TOK_PDIV) &&
2084 fc == 1) ||2109 fc == 1) ||
@@ -2097,7 +2122,7 @@ void gen_opc(int op) · 12 unmodified lines
2097 fc >>= 1;2122 fc >>= 1;
2098 n++;2123 n++;
2099 }2124 }
2100 vtop->c = n;2125 vtop->c.i = n;
2101 if (op == '*')2126 if (op == '*')
2102 op = TOK_SHL;2127 op = TOK_SHL;
2103 else if (op == TOK_PDIV)2128 else if (op == TOK_PDIV)
@@ -2120,14 +2145,6 @@ int pointed_size(int t) · 16 unmodified lines
2120 return type_size(pointed_type(t), &t);2145 return type_size(pointed_type(t), &t);
2121}2146}
2122 2147
2123/* true if float/double/long double type */
2124static inline int is_float(int t)
2125{
2126 int bt;
2127 bt = t & VT_BTYPE;
2128 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
2129}
2130
2131/* generic gen_op: handles types problems */2148/* generic gen_op: handles types problems */
2132void gen_op(int op)2149void gen_op(int op)
2133{2150{
@@ -2219,23 +2236,111 @@ void gen_op(int op) · 85 unmodified lines
2219/* cast 'vtop' to 't' type */2236/* cast 'vtop' to 't' type */
2220void gen_cast(int t)2237void gen_cast(int t)
2221{2238{
2222 int r, bits, bt, vbt;2239 int r, bits, sbt, dbt, sf, df, c, st1, dt1;
2223 2240
2224 r = vtop->t & VT_VALMASK;2241 r = vtop->t & VT_VALMASK;
2225 if (!(t & VT_LVAL)) {2242 if (!(t & VT_LVAL)) {
2226 /* if not lvalue, then we convert now */2243 /* if not lvalue, then we convert now */
2227 bt = t & VT_BTYPE;2244 dbt = t & VT_BTYPE;
2228 vbt = vtop->t & VT_BTYPE;2245 sbt = vtop->t & VT_BTYPE;
2229 if (bt != vbt) {2246 if (sbt != dbt) {
2230 if (is_float(bt)) {2247 sf = is_float(sbt);
2231 /* need to generate value and do explicit cast */2248 df = is_float(dbt);
2232 gv();2249 c = (vtop->t & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST;
2233 gen_cvtf(bt);2250 if (sf && df) {
2234 } else if (bt == VT_BOOL) {2251 /* convert from fp to fp */
2252 if (c) {
2253 /* constant case: we can do it now */
2254 /* XXX: in ISOC, cannot do it if error in convert */
2255 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
2256 vtop->c.f = (float)vtop->c.d;
2257 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
2258 vtop->c.f = (float)vtop->c.ld;
2259 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
2260 vtop->c.d = (double)vtop->c.f;
2261 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
2262 vtop->c.d = (double)vtop->c.ld;
2263 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
2264 vtop->c.ld = (long double)vtop->c.f;
2265 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
2266 vtop->c.ld = (long double)vtop->c.d;
2267 } else {
2268 /* non constant case: generate code */
2269 gen_cvt_ftof(dbt);
2270 }
2271 } else if (df) {
2272 /* convert int to fp */
2273 /* XXX: add const cases */
2274 st1 = vtop->t & (VT_BTYPE | VT_UNSIGNED);
2275 if (c) {
2276 switch(st1) {
2277 case VT_LLONG | VT_UNSIGNED:
2278 case VT_LLONG:
2279 /* well, currently not needed */
2280 goto do_itof;
2281 case VT_INT | VT_UNSIGNED:
2282 switch(dbt) {
2283 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
2284 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
2285 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
2286 }
2287 break;
2288 default:
2289 switch(dbt) {
2290 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
2291 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
2292 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
2293 }
2294 break;
2295 }
2296 } else {
2297 do_itof:
2298 gen_cvt_itof(dbt);
2299 }
2300 } else if (sf) {
2301 /* convert fp to int */
2302 dt1 = t & (VT_BTYPE | VT_UNSIGNED);
2303 /* we handle char/short/etc... with generic code */
2304 if (dt1 != VT_INT | VT_UNSIGNED &&
2305 dt1 != VT_LLONG | VT_UNSIGNED &&
2306 dt1 != VT_LLONG)
2307 dt1 = VT_INT;
2308 if (c) {
2309 switch(dt1) {
2310 case VT_LLONG | VT_UNSIGNED:
2311 case VT_LLONG:
2312 /* well, currently not needed */
2313 goto do_ftoi;
2314 case VT_INT | VT_UNSIGNED:
2315 switch(sbt) {
2316 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
2317 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2318 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
2319 }
2320 break;
2321 default:
2322 /* int case */
2323 switch(sbt) {
2324 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
2325 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
2326 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
2327 }
2328 break;
2329 }
2330 } else {
2331 do_ftoi:
2332 gen_cvt_ftoi(dt1);
2333 }
2334 if (dt1 == VT_INT && (t & (VT_TYPE | VT_UNSIGNED)) != dt1) {
2335 /* additionnal cast for char/short/bool... */
2336 vtop->t = (vtop->t & ~VT_TYPE) | dt1;
2337 gen_cast(t);
2338 }
2339 } else if (dbt == VT_BOOL) {
2235 vset(VT_CONST, 0);2340 vset(VT_CONST, 0);
2236 gen_op(TOK_NE);2341 gen_op(TOK_NE);
2237 } else if (bt == VT_BYTE || bt == VT_SHORT) {2342 } else if (dbt == VT_BYTE || dbt == VT_SHORT) {
2238 if (bt == VT_BYTE)2343 if (dbt == VT_BYTE)
2239 bits = 8;2344 bits = 8;
2240 else2345 else
2241 bits = 16;2346 bits = 16;
@@ -2310,6 +2415,181 @@ int mk_pointer(int t) · 68 unmodified lines
2310 return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);2415 return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);
2311}2416}
2312 2417
2418int is_compatible_types(int t1, int t2)
2419{
2420 Sym *s1, *s2;
2421 int bt1, bt2;
2422
2423 t1 &= VT_TYPE;
2424 t2 &= VT_TYPE;
2425 bt1 = t1 & VT_BTYPE;
2426 bt2 = t2 & VT_BTYPE;
2427 if (bt1 == VT_PTR) {
2428 t1 = pointed_type(t1);
2429 /* if function, then convert implicitely to function pointer */
2430 if (bt2 != VT_FUNC) {
2431 if (bt2 != VT_PTR)
2432 return 0;
2433 t2 = pointed_type(t2);
2434 }
2435 /* void matches everything */
2436 t1 &= VT_TYPE;
2437 t2 &= VT_TYPE;
2438 if (t1 == VT_VOID || t2 == VT_VOID)
2439 return 1;
2440 return is_compatible_types(t1, t2);
2441 } else if (bt1 == VT_STRUCT) {
2442 return (t2 == t1);
2443 } else if (bt1 == VT_FUNC) {
2444 if (bt2 != VT_FUNC)
2445 return 0;
2446 s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
2447 s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
2448 if (!is_compatible_types(s1->t, s2->t))
2449 return 0;
2450 /* XXX: not complete */
2451 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2452 return 1;
2453 if (s1->c != s2->c)
2454 return 0;
2455 while (s1 != NULL) {
2456 if (s2 == NULL)
2457 return 0;
2458 if (!is_compatible_types(s1->t, s2->t))
2459 return 0;
2460 s1 = s1->next;
2461 s2 = s2->next;
2462 }
2463 if (s2)
2464 return 0;
2465 return 1;
2466 } else {
2467 /* XXX: not complete */
2468 return 1;
2469 }
2470}
2471
2472int check_assign_types(int t1, int t2)
2473{
2474 t1 &= VT_TYPE;
2475 t2 &= VT_TYPE;
2476 if ((t1 & VT_BTYPE) == VT_PTR &&
2477 (t2 & VT_BTYPE) == VT_FUNC) {
2478 return is_compatible_types(pointed_type(t1), t2);
2479 } else {
2480 return is_compatible_types(t1, t2);
2481 }
2482}
2483
2484/* print a type. If 'varstr' is not NULL, then the variable is also
2485 printed in the type */
2486/* XXX: add array and function pointers */
2487/* XXX: buffer overflows */
2488void type_to_str(char *buf, int buf_size,
2489 int t, const char *varstr)
2490{
2491 int bt, v;
2492 Sym *s, *sa;
2493 char buf1[256];
2494
2495 t = t & VT_TYPE;
2496 bt = t & VT_BTYPE;
2497 buf[0] = '\0';
2498 if (t & VT_UNSIGNED)
2499 strcat(buf, "unsigned ");
2500 switch(bt) {
2501 case VT_VOID:
2502 strcat(buf, "void");
2503 break;
2504 case VT_BOOL:
2505 strcat(buf, "_Bool");
2506 break;
2507 case VT_BYTE:
2508 strcat(buf, "char");
2509 break;
2510 case VT_SHORT:
2511 strcat(buf, "short");
2512 break;
2513 case VT_INT:
2514 strcat(buf, "int");
2515 break;
2516 case VT_LONG:
2517 strcat(buf, "long");
2518 break;
2519 case VT_LLONG:
2520 strcat(buf, "long long");
2521 break;
2522 case VT_FLOAT:
2523 strcat(buf, "float");
2524 break;
2525 case VT_DOUBLE:
2526 strcat(buf, "double");
2527 break;
2528 case VT_LDOUBLE:
2529 strcat(buf, "long double");
2530 break;
2531 case VT_ENUM:
2532 case VT_STRUCT:
2533 if (bt == VT_STRUCT)
2534 strcat(buf, "struct ");
2535 else
2536 strcat(buf, "enum ");
2537 v = (unsigned)t >> VT_STRUCT_SHIFT;
2538 if (v >= SYM_FIRST_ANOM)
2539 strcat(buf, "<anonymous>");
2540 else
2541 strcat(buf, get_tok_str(v, NULL));
2542 break;
2543 case VT_FUNC:
2544 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
2545 type_to_str(buf, buf_size, s->t, varstr);
2546 strcat(buf, "(");
2547 sa = s->next;
2548 while (sa != NULL) {
2549 type_to_str(buf1, sizeof(buf1), sa->t, NULL);
2550 strcat(buf, buf1);
2551 sa = sa->next;
2552 if (sa)
2553 strcat(buf, ", ");
2554 }
2555 strcat(buf, ")");
2556 goto no_var;
2557 case VT_PTR:
2558 s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
2559 strcpy(buf1, "*");
2560 if (varstr)
2561 strcat(buf1, varstr);
2562 type_to_str(buf, buf_size, s->t, buf1);
2563 goto no_var;
2564 }
2565 if (varstr) {
2566 strcat(buf, " ");
2567 strcat(buf, varstr);
2568 }
2569 no_var: ;
2570}
2571
2572
2573
2574/* verify type compatibility to store vtop in 'st' type, and generate
2575 casts if needed */
2576void gen_assign_cast(int dt)
2577{
2578 int st;
2579 char buf1[256], buf2[256];
2580
2581 st = vtop->t; /* destination type */
2582 if (!check_assign_types(dt, st)) {
2583 type_to_str(buf1, sizeof(buf1), st, NULL);
2584 type_to_str(buf2, sizeof(buf2), dt, NULL);
2585 error("cannot cast '%s' to '%s'", buf1, buf2);
2586 }
2587 if ((dt & VT_BTYPE) == VT_BOOL || is_float(dt)) {
2588 gen_cast(dt & VT_BTYPE);
2589 }
2590}
2591
2592
2313/* store vtop in lvalue pushed on stack */2593/* store vtop in lvalue pushed on stack */
2314void vstore(void)2594void vstore(void)
2315{2595{
@@ -2317,6 +2597,8 @@ void vstore(void) · 1 unmodified line
2317 GFuncContext gf;2597 GFuncContext gf;
2318 2598
2319 ft = vtop[-1].t;2599 ft = vtop[-1].t;
2600 gen_assign_cast(ft & VT_TYPE);
2601
2320 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {2602 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
2321 /* if structure, only generate pointer */2603 /* if structure, only generate pointer */
2322 /* structure assignment : generate memcpy */2604 /* structure assignment : generate memcpy */
@@ -2364,13 +2646,9 @@ void vstore(void) · 41 unmodified lines
2364 /* store result */2646 /* store result */
2365 vstore();2647 vstore();
2366 } else {2648 } else {
2367 /* generate cast if needed implicit cast for bool */
2368 if ((ft & VT_BTYPE) == VT_BOOL) {
2369 gen_cast(VT_BOOL);
2370 }
2371 r = gv(); /* generate value */2649 r = gv(); /* generate value */
2372 ft = vtop[-1].t;2650 ft = vtop[-1].t;
2373 fc = vtop[-1].c;2651 fc = vtop[-1].c.i;
2374 /* if lvalue was saved on stack, must read it */2652 /* if lvalue was saved on stack, must read it */
2375 if ((ft & VT_VALMASK) == VT_LLOCAL) {2653 if ((ft & VT_VALMASK) == VT_LLOCAL) {
2376 t = get_reg(REG_CLASS_INT);2654 t = get_reg(REG_CLASS_INT);
@@ -2380,7 +2658,7 @@ void vstore(void) · 3 unmodified lines
2380 store(r, ft, fc);2658 store(r, ft, fc);
2381 vtop--;2659 vtop--;
2382 vtop->t = (ft & VT_TYPE) | r;2660 vtop->t = (ft & VT_TYPE) | r;
2383 vtop->c = 0;2661 vtop->c.i = 0;
2384 }2662 }
2385}2663}
2386 2664
@@ -2401,7 +2679,7 @@ void inc(int post, int c) · 14 unmodified lines
2401 load(r1, r, 0); /* move r to r1 */2679 load(r1, r, 0); /* move r to r1 */
2402 /* duplicates value */2680 /* duplicates value */
2403 vtop[-2].t = (vtop->t & VT_TYPE) | r1;2681 vtop[-2].t = (vtop->t & VT_TYPE) | r1;
2404 vtop[-2].c = 0;2682 vtop[-2].c.i = 0;
2405 }2683 }
2406 /* add constant */2684 /* add constant */
2407 vset(VT_CONST, c - TOK_MID); 2685 vset(VT_CONST, c - TOK_MID);
@@ -2470,7 +2748,8 @@ int struct_decl(int u) · 62 unmodified lines
2470 t = type_decl(&v, b, TYPE_DIRECT);2748 t = type_decl(&v, b, TYPE_DIRECT);
2471 if ((t & VT_BTYPE) == VT_FUNC ||2749 if ((t & VT_BTYPE) == VT_FUNC ||
2472 (t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))2750 (t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
2473 error("invalid type for '%s'", get_tok_str(v, 0));2751 error("invalid type for '%s'",
2752 get_tok_str(v, NULL));
2474 } else {2753 } else {
2475 t = b;2754 t = b;
2476 }2755 }
@@ -2480,10 +2759,10 @@ int struct_decl(int u) · 3 unmodified lines
2480 /* XXX: handle v = 0 case for messages */2759 /* XXX: handle v = 0 case for messages */
2481 if (bit_size < 0)2760 if (bit_size < 0)
2482 error("negative width in bit-field '%s'", 2761 error("negative width in bit-field '%s'",
2483 get_tok_str(v, 0));2762 get_tok_str(v, NULL));
2484 if (v && bit_size == 0)2763 if (v && bit_size == 0)
2485 error("zero width for bit-field '%s'", 2764 error("zero width for bit-field '%s'",
2486 get_tok_str(v, 0));2765 get_tok_str(v, NULL));
2487 }2766 }
2488 size = type_size(t, &align);2767 size = type_size(t, &align);
2489 lbit_pos = 0;2768 lbit_pos = 0;
@@ -2496,7 +2775,7 @@ int struct_decl(int u) · 6 unmodified lines
2496 bsize = size * 8;2775 bsize = size * 8;
2497 if (bit_size > bsize) {2776 if (bit_size > bsize) {
2498 error("width of '%s' exceeds its type",2777 error("width of '%s' exceeds its type",
2499 get_tok_str(v, 0));2778 get_tok_str(v, NULL));
2500 } else if (bit_size == bsize) {2779 } else if (bit_size == bsize) {
2501 /* no need for bit fields */2780 /* no need for bit fields */
2502 bit_pos = 0;2781 bit_pos = 0;
@@ -2538,7 +2817,7 @@ int struct_decl(int u) · 35 unmodified lines
2538 }2817 }
2539#if 02818#if 0
2540 printf("add field %s offset=%d", 2819 printf("add field %s offset=%d",
2541 get_tok_str(v, 0), offset);2820 get_tok_str(v, NULL), offset);
2542 if (t & VT_BITFIELD) {2821 if (t & VT_BITFIELD) {
2543 printf(" pos=%d size=%d", 2822 printf(" pos=%d size=%d",
2544 (t >> VT_STRUCT_SHIFT) & 0x3f,2823 (t >> VT_STRUCT_SHIFT) & 0x3f,
@@ -2698,10 +2977,12 @@ int post_type(int t) · 153 unmodified lines
2698 goto old_proto;2977 goto old_proto;
2699 }2978 }
2700 }2979 }
2980 l = FUNC_NEW;
2701 if ((pt & VT_BTYPE) == VT_VOID && tok == ')')2981 if ((pt & VT_BTYPE) == VT_VOID && tok == ')')
2702 break;2982 break;
2703 l = FUNC_NEW;
2704 pt = type_decl(&n, pt, TYPE_DIRECT | TYPE_ABSTRACT);2983 pt = type_decl(&n, pt, TYPE_DIRECT | TYPE_ABSTRACT);
2984 if ((pt & VT_BTYPE) == VT_VOID)
2985 error("parameter declared as void");
2705 } else {2986 } else {
2706 old_proto:2987 old_proto:
2707 n = tok;2988 n = tok;
@@ -2722,6 +3003,9 @@ int post_type(int t) · 14 unmodified lines
2722 }3003 }
2723 }3004 }
2724 }3005 }
3006 /* if no parameters, then old type prototype */
3007 if (l == 0)
3008 l = FUNC_OLD;
2725 skip(')');3009 skip(')');
2726 t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);3010 t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
2727 t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));3011 t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN));
@@ -2826,6 +3110,24 @@ void indir(void) · 98 unmodified lines
2826 vtop->t |= VT_LVAL;3110 vtop->t |= VT_LVAL;
2827}3111}
2828 3112
3113/* pass a parameter to a function and do type checking and casting */
3114void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
3115{
3116 int func_type;
3117 func_type = func->c;
3118 if (func_type == FUNC_OLD ||
3119 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3120 /* default casting : only need to convert float to double */
3121 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
3122 gen_cast(VT_DOUBLE);
3123 } else if (arg == NULL) {
3124 error("too many arguments to function");
3125 } else {
3126 gen_assign_cast(arg->t);
3127 }
3128 gfunc_param(gf);
3129}
3130
2829void unary(void)3131void unary(void)
2830{3132{
2831 int n, t, ft, fc, p, align, size;3133 int n, t, ft, fc, p, align, size;
@@ -2836,16 +3138,13 @@ void unary(void) · 4 unmodified lines
2836 vset(VT_CONST | VT_INT, tokc.i);3138 vset(VT_CONST | VT_INT, tokc.i);
2837 next();3139 next();
2838 } else if (tok == TOK_CFLOAT) {3140 } else if (tok == TOK_CFLOAT) {
2839 /* currently, cannot do more */3141 vsetc(VT_CONST | VT_FLOAT, &tokc);
2840 vset(VT_CONST | VT_FLOAT, 0);
2841 next();3142 next();
2842 } else if (tok == TOK_CDOUBLE) {3143 } else if (tok == TOK_CDOUBLE) {
2843 /* currently, cannot do more */3144 vsetc(VT_CONST | VT_DOUBLE, &tokc);
2844 vset(VT_CONST | VT_DOUBLE, 0);
2845 next();3145 next();
2846 } else if (tok == TOK_CLDOUBLE) {3146 } else if (tok == TOK_CLDOUBLE) {
2847 /* currently, cannot do more */3147 vsetc(VT_CONST | VT_LDOUBLE, &tokc);
2848 vset(VT_CONST | VT_LDOUBLE, 0);
2849 next();3148 next();
2850 } else if (tok == TOK___FUNC__) {3149 } else if (tok == TOK___FUNC__) {
2851 /* special function name identifier */3150 /* special function name identifier */
@@ -2895,7 +3194,7 @@ void unary(void) · 43 unmodified lines
2895 gen_cast(ft);3194 gen_cast(ft);
2896 }3195 }
2897 } else {3196 } else {
2898 expr();3197 gexpr();
2899 skip(')');3198 skip(')');
2900 }3199 }
2901 } else if (t == '*') {3200 } else if (t == '*') {
@@ -2914,9 +3213,9 @@ void unary(void) · 12 unmodified lines
2914 if (t == '!') {3213 if (t == '!') {
2915 unary();3214 unary();
2916 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) 3215 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST)
2917 vtop->c = !vtop->c;3216 vtop->c.i = !vtop->c.i;
2918 else if ((vtop->t & VT_VALMASK) == VT_CMP)3217 else if ((vtop->t & VT_VALMASK) == VT_CMP)
2919 vtop->c = vtop->c ^ 1;3218 vtop->c.i = vtop->c.i ^ 1;
2920 else3219 else
2921 vset(VT_JMP, gtst(1, 0));3220 vset(VT_JMP, gtst(1, 0));
2922 } else3221 } else
@@ -2936,7 +3235,7 @@ void unary(void) · 13 unmodified lines
2936 else {3235 else {
2937 /* XXX: some code could be generated: add eval3236 /* XXX: some code could be generated: add eval
2938 flag */3237 flag */
2939 expr();3238 gexpr();
2940 t = vtop->t;3239 t = vtop->t;
2941 vpop();3240 vpop();
2942 }3241 }
@@ -2960,7 +3259,7 @@ void unary(void) · 17 unmodified lines
2960 s = sym_find(t);3259 s = sym_find(t);
2961 if (!s) {3260 if (!s) {
2962 if (tok != '(')3261 if (tok != '(')
2963 error("'%s' undeclared", get_tok_str(t, 0));3262 error("'%s' undeclared", get_tok_str(t, NULL));
2964 /* for simple function calls, we tolerate undeclared3263 /* for simple function calls, we tolerate undeclared
2965 external reference */3264 external reference */
2966 p = anon_sym++; 3265 p = anon_sym++;
@@ -2971,7 +3270,7 @@ void unary(void) · 4 unmodified lines
2971 vset(s->t, s->c);3270 vset(s->t, s->c);
2972 /* if forward reference, we must point to s */3271 /* if forward reference, we must point to s */
2973 if (vtop->t & VT_FORWARD)3272 if (vtop->t & VT_FORWARD)
2974 vtop->c = (int)s;3273 vtop->c.sym = s;
2975 }3274 }
2976 }3275 }
2977 3276
@@ -3011,12 +3310,14 @@ void unary(void) · 33 unmodified lines
3011 next();3310 next();
3012 } else if (tok == '[') {3311 } else if (tok == '[') {
3013 next();3312 next();
3014 expr();3313 gexpr();
3015 gen_op('+');3314 gen_op('+');
3016 indir();3315 indir();
3017 skip(']');3316 skip(']');
3018 } else if (tok == '(') {3317 } else if (tok == '(') {
3019 int rett, retc;3318 int rett;
3319 CValue retc;
3320 Sym *sa;
3020 3321
3021 /* function call */3322 /* function call */
3022 if ((vtop->t & VT_BTYPE) != VT_FUNC) {3323 if ((vtop->t & VT_BTYPE) != VT_FUNC) {
@@ -3032,12 +3333,12 @@ void unary(void) · 9 unmodified lines
3032 } else {3333 } else {
3033 vtop->t &= ~VT_LVAL; /* no lvalue */3334 vtop->t &= ~VT_LVAL; /* no lvalue */
3034 }3335 }
3035
3036 /* get return type */3336 /* get return type */
3037 s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);3337 s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);
3038 save_regs(); /* save used temporary registers */3338 save_regs(); /* save used temporary registers */
3039 gfunc_start(&gf);3339 gfunc_start(&gf);
3040 next();3340 next();
3341 sa = s->next; /* first parameter */
3041#ifdef INVERT_FUNC_PARAMS3342#ifdef INVERT_FUNC_PARAMS
3042 {3343 {
3043 int *str, len, parlevel, *saved_macro_ptr;3344 int *str, len, parlevel, *saved_macro_ptr;
@@ -3061,7 +3362,10 @@ void unary(void) · 17 unmodified lines
3061 }3362 }
3062 tok_add(&str, &len, -1); /* end of file added */3363 tok_add(&str, &len, -1); /* end of file added */
3063 tok_add(&str, &len, 0);3364 tok_add(&str, &len, 0);
3064 sym_push2(&args, 0, 0, (int)str);3365 s1 = sym_push2(&args, 0, 0, (int)str);
3366 s1->next = sa; /* add reference to argument */
3367 if (sa)
3368 sa = sa->next;
3065 if (tok != ',')3369 if (tok != ',')
3066 break;3370 break;
3067 next();3371 next();
@@ -3077,7 +3381,7 @@ void unary(void) · 9 unmodified lines
3077 expr_eq();3381 expr_eq();
3078 if (tok != -1)3382 if (tok != -1)
3079 expect("',' or ')'");3383 expect("',' or ')'");
3080 gfunc_param(&gf);3384 gfunc_param_typed(&gf, s, args->next);
3081 s1 = args->prev;3385 s1 = args->prev;
3082 free((int *)args->c);3386 free((int *)args->c);
3083 free(args);3387 free(args);
@@ -3101,93 +3405,30 @@ void unary(void) · 17 unmodified lines
3101 gfunc_param(&gf);3405 gfunc_param(&gf);
3102 } else {3406 } else {
3103 rett = s->t | FUNC_RET_REG; /* return in register */3407 rett = s->t | FUNC_RET_REG; /* return in register */
3104 retc = 0;3408 retc.i = 0;
3105 }3409 }
3106#ifndef INVERT_FUNC_PARAMS3410#ifndef INVERT_FUNC_PARAMS
3107 while (tok != ')') {3411 while (tok != ')') {
3108 expr_eq();3412 expr_eq();
3109 gfunc_param(&gf);3413 gfunc_param_typed(&gf, s, sa);
3414 if (sa)
3415 sa = sa->next;
3110 if (tok == ',')3416 if (tok == ',')
3111 next();3417 next();
3112 }3418 }
3113#endif3419#endif
3420 if (sa)
3421 error("too few arguments to function %x", sa->t);
3114 skip(')');3422 skip(')');
3115 gfunc_call(&gf);3423 gfunc_call(&gf);
3116 /* return value */3424 /* return value */
3117 vset(rett, retc);3425 vsetc(rett, &retc);
3118 } else {3426 } else {
3119 break;3427 break;
3120 }3428 }
3121 }3429 }
3122}3430}
3123 3431
3124int is_compatible_types(int t1, int t2)
3125{
3126 Sym *s1, *s2;
3127 int bt1, bt2;
3128
3129 t1 &= VT_TYPE;
3130 t2 &= VT_TYPE;
3131 bt1 = t1 & VT_BTYPE;
3132 bt2 = t2 & VT_BTYPE;
3133 if (bt1 == VT_PTR) {
3134 t1 = pointed_type(t1);
3135 /* if function, then convert implicitely to function pointer */
3136 if (bt2 != VT_FUNC) {
3137 if (bt2 != VT_PTR)
3138 return 0;
3139 t2 = pointed_type(t2);
3140 }
3141 /* void matches everything */
3142 t1 &= VT_TYPE;
3143 t2 &= VT_TYPE;
3144 if (t1 == VT_VOID || t2 == VT_VOID)
3145 return 1;
3146 return is_compatible_types(t1, t2);
3147 } else if (bt1 == VT_STRUCT) {
3148 return (t2 == t1);
3149 } else if (bt1 == VT_FUNC) {
3150 if (bt2 != VT_FUNC)
3151 return 0;
3152 s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
3153 s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
3154 if (!is_compatible_types(s1->t, s2->t))
3155 return 0;
3156 /* XXX: not complete */
3157 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
3158 return 1;
3159 if (s1->c != s2->c)
3160 return 0;
3161 while (s1 != NULL) {
3162 if (s2 == NULL)
3163 return 0;
3164 if (!is_compatible_types(s1->t, s2->t))
3165 return 0;
3166 s1 = s1->next;
3167 s2 = s2->next;
3168 }
3169 if (s2)
3170 return 0;
3171 return 1;
3172 } else {
3173 /* XXX: not complete */
3174 return 1;
3175 }
3176}
3177
3178int check_assign_types(int t1, int t2)
3179{
3180 t1 &= VT_TYPE;
3181 t2 &= VT_TYPE;
3182 if ((t1 & VT_BTYPE) == VT_PTR &&
3183 (t2 & VT_BTYPE) == VT_FUNC) {
3184 return is_compatible_types(pointed_type(t1), t2);
3185 } else {
3186 return is_compatible_types(t1, t2);
3187 }
3188}
3189
3190
3191void uneq(void)3432void uneq(void)
3192{3433{
3193 int t;3434 int t;
@@ -3202,8 +3443,6 @@ void uneq(void) · 8 unmodified lines
3202 next();3443 next();
3203 if (t == '=') {3444 if (t == '=') {
3204 expr_eq();3445 expr_eq();
3205 if (!check_assign_types(vtop[-1].t, vtop[0].t))
3206 warning("incompatible types");
3207 } else {3446 } else {
3208 vdup();3447 vdup();
3209 expr_eq();3448 expr_eq();
@@ -3289,16 +3528,16 @@ void expr_eq(void) · 79 unmodified lines
3289 if (const_wanted) {3528 if (const_wanted) {
3290 sum(10);3529 sum(10);
3291 if (tok == '?') {3530 if (tok == '?') {
3292 c = vtop->c;3531 c = vtop->c.i;
3293 vpop();3532 vpop();
3294 next();3533 next();
3295 expr();3534 gexpr();
3296 t = vtop->c;3535 t = vtop->c.i;
3297 vpop();3536 vpop();
3298 skip(':');3537 skip(':');
3299 expr_eq();3538 expr_eq();
3300 if (c)3539 if (c)
3301 vtop->c = t;3540 vtop->c.i = t;
3302 }3541 }
3303 } else {3542 } else {
3304 eor();3543 eor();
@@ -3306,7 +3545,7 @@ void expr_eq(void) · 1 unmodified line
3306 next();3545 next();
3307 t = gtst(1, 0);3546 t = gtst(1, 0);
3308 3547
3309 expr();3548 gexpr();
3310 r1 = gv();3549 r1 = gv();
3311 vpop();3550 vpop();
3312 skip(':');3551 skip(':');
@@ -3322,7 +3561,7 @@ void expr_eq(void) · 9 unmodified lines
3322 }3561 }
3323}3562}
3324 3563
3325void expr(void)3564void gexpr(void)
3326{3565{
3327 while (1) {3566 while (1) {
3328 expr_eq();3567 expr_eq();
@@ -3333,16 +3572,24 @@ void expr(void) · 4 unmodified lines
3333 }3572 }
3334}3573}
3335 3574
3336int expr_const(void)3575/* parse a constant expression and return value in vtop */
3576void expr_const1(void)
3337{3577{
3338 int a, c;3578 int a;
3339 a = const_wanted;3579 a = const_wanted;
3340 const_wanted = 1;3580 const_wanted = 1;
3341 expr_eq();3581 expr_eq();
3342 if ((vtop->t & (VT_CONST | VT_LVAL)) != VT_CONST)3582 if ((vtop->t & (VT_CONST | VT_LVAL)) != VT_CONST)
3343 expect("constant");3583 expect("constant");
3344 const_wanted = a;3584 const_wanted = a;
3345 c = vtop->c;3585}
3586
3587/* parse an integer constant and return its value */
3588int expr_const(void)
3589{
3590 int c;
3591 expr_const1();
3592 c = vtop->c.i;
3346 vpop();3593 vpop();
3347 return c;3594 return c;
3348}3595}
@@ -3383,7 +3630,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 34 unmodified lines
3383 /* if test */3630 /* if test */
3384 next();3631 next();
3385 skip('(');3632 skip('(');
3386 expr();3633 gexpr();
3387 skip(')');3634 skip(')');
3388 a = gtst(1, 0);3635 a = gtst(1, 0);
3389 block(bsym, csym, case_sym, def_sym, case_reg);3636 block(bsym, csym, case_sym, def_sym, case_reg);
@@ -3400,7 +3647,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 10 unmodified lines
3400 next();3647 next();
3401 d = ind;3648 d = ind;
3402 skip('(');3649 skip('(');
3403 expr();3650 gexpr();
3404 skip(')');3651 skip(')');
3405 a = gtst(1, 0);3652 a = gtst(1, 0);
3406 b = 0;3653 b = 0;
@@ -3423,7 +3670,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 16 unmodified lines
3423 } else if (tok == TOK_RETURN) {3670 } else if (tok == TOK_RETURN) {
3424 next();3671 next();
3425 if (tok != ';') {3672 if (tok != ';') {
3426 expr();3673 gexpr();
3427 if ((func_vt & VT_BTYPE) == VT_STRUCT) {3674 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
3428 /* if returning structure, must copy it to implicit3675 /* if returning structure, must copy it to implicit
3429 first pointer arg location */3676 first pointer arg location */
@@ -3432,6 +3679,9 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines
3432 vswap();3679 vswap();
3433 /* copy structure value to pointer */3680 /* copy structure value to pointer */
3434 vstore();3681 vstore();
3682 } else if (is_float(func_vt)) {
3683 /* move return value to float return register */
3684 move_reg(FUNC_RET_FREG, gv());
3435 } else {3685 } else {
3436 /* move return value to standard return register */3686 /* move return value to standard return register */
3437 move_reg(FUNC_RET_REG, gv());3687 move_reg(FUNC_RET_REG, gv());
@@ -3459,7 +3709,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 21 unmodified lines
3459 next();3709 next();
3460 skip('(');3710 skip('(');
3461 if (tok != ';') {3711 if (tok != ';') {
3462 expr();3712 gexpr();
3463 vpop();3713 vpop();
3464 }3714 }
3465 skip(';');3715 skip(';');
@@ -3468,14 +3718,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 2 unmodified lines
3468 a = 0;3718 a = 0;
3469 b = 0;3719 b = 0;
3470 if (tok != ';') {3720 if (tok != ';') {
3471 expr();3721 gexpr();
3472 a = gtst(1, 0);3722 a = gtst(1, 0);
3473 }3723 }
3474 skip(';');3724 skip(';');
3475 if (tok != ')') {3725 if (tok != ')') {
3476 e = gjmp(0);3726 e = gjmp(0);
3477 c = ind;3727 c = ind;
3478 expr();3728 gexpr();
3479 vpop();3729 vpop();
3480 oad(0xe9, d - ind - 5); /* jmp */3730 oad(0xe9, d - ind - 5); /* jmp */
3481 gsym(e);3731 gsym(e);
@@ -3495,7 +3745,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 13 unmodified lines
3495 skip(TOK_WHILE);3745 skip(TOK_WHILE);
3496 skip('(');3746 skip('(');
3497 gsym(b);3747 gsym(b);
3498 expr();3748 gexpr();
3499 c = gtst(0, 0);3749 c = gtst(0, 0);
3500 gsym_addr(c, d);3750 gsym_addr(c, d);
3501 skip(')');3751 skip(')');
@@ -3505,7 +3755,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 3 unmodified lines
3505 if (tok == TOK_SWITCH) {3755 if (tok == TOK_SWITCH) {
3506 next();3756 next();
3507 skip('(');3757 skip('(');
3508 expr();3758 gexpr();
3509 case_reg = gv();3759 case_reg = gv();
3510 vpop();3760 vpop();
3511 skip(')');3761 skip(')');
@@ -3582,7 +3832,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 70 unmodified lines
3582 } else {3832 } else {
3583 /* expression case */3833 /* expression case */
3584 if (tok != ';') {3834 if (tok != ';') {
3585 expr();3835 gexpr();
3586 vpop();3836 vpop();
3587 }3837 }
3588 skip(';');3838 skip(';');
@@ -3670,28 +3920,50 @@ void decl_designator(int t, int c, · 81 unmodified lines
3670 3920
3671void init_putv(int t, int c, int v, int is_expr)3921void init_putv(int t, int c, int v, int is_expr)
3672{3922{
3673 int saved_global_expr;3923 int saved_global_expr, bt;
3674 3924
3675 if ((t & VT_VALMASK) == VT_CONST) {3925 if ((t & VT_VALMASK) == VT_CONST) {
3676 if (is_expr) {3926 if (is_expr) {
3677 /* compound literals must be allocated globally in this case */3927 /* compound literals must be allocated globally in this case */
3678 saved_global_expr = global_expr;3928 saved_global_expr = global_expr;
3679 global_expr = 1;3929 global_expr = 1;
3680 v = expr_const();3930 expr_const1();
3681 global_expr = saved_global_expr;3931 global_expr = saved_global_expr;
3932 } else {
3933 vset(VT_CONST | VT_INT, v);
3682 }3934 }
3683 if ((t & VT_BTYPE) == VT_BYTE)3935 /* XXX: do casting */
3684 *(char *)c = v;3936 /* XXX: not portable */
3685 else if ((t & VT_BTYPE) == VT_SHORT)3937 bt = vtop->t & VT_BTYPE;
3686 *(short *)c = v;3938 switch(bt) {
3687 else3939 case VT_BYTE:
3688 *(int *)c = v;3940 *(char *)c = vtop->c.i;
3941 break;
3942 case VT_SHORT:
3943 *(short *)c = vtop->c.i;
3944 break;
3945 case VT_DOUBLE:
3946 *(double *)c = vtop->c.d;
3947 break;
3948 case VT_LDOUBLE:
3949 *(long double *)c = vtop->c.ld;
3950 break;
3951#if 0
3952 case VT_LLONG:
3953 *(long long *)c = vtop->c.ll;
3954 break;
3955#endif
3956 default:
3957 *(int *)c = vtop->c.i;
3958 break;
3959 }
3960 vpop();
3689 } else {3961 } else {
3690 vset(t, c);3962 vset(t, c);
3691 if (is_expr)3963 if (is_expr)
3692 expr_eq();3964 expr_eq();
3693 else3965 else
3694 vset(VT_CONST, v);3966 vset(VT_CONST | VT_INT, v);
3695 vstore();3967 vstore();
3696 vpop();3968 vpop();
3697 }3969 }
@@ -3969,6 +4241,13 @@ void decl(int l) · 271 unmodified lines
3969 }4241 }
3970 while (1) { /* iterate thru each declaration */4242 while (1) { /* iterate thru each declaration */
3971 t = type_decl(&v, b, TYPE_DIRECT);4243 t = type_decl(&v, b, TYPE_DIRECT);
4244#if 0
4245 {
4246 char buf[500];
4247 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
4248 printf("type = '%s'\n", buf);
4249 }
4250#endif
3972 if (tok == '{') {4251 if (tok == '{') {
3973 if (l == VT_LOCAL)4252 if (l == VT_LOCAL)
3974 error("cannot use local functions");4253 error("cannot use local functions");
@@ -3982,7 +4261,7 @@ void decl(int l) · 7 unmodified lines
3982 /* put function address */4261 /* put function address */
3983 sym_push1(&global_stack, v, VT_CONST | t, ind);4262 sym_push1(&global_stack, v, VT_CONST | t, ind);
3984 }4263 }
3985 funcname = get_tok_str(v, 0);4264 funcname = get_tok_str(v, NULL);
3986 /* push a dummy symbol to enable local sym storage */4265 /* push a dummy symbol to enable local sym storage */
3987 sym_push1(&local_stack, 0, 0, 0);4266 sym_push1(&local_stack, 0, 0, 0);
3988 /* define parameters */4267 /* define parameters */
@@ -4002,27 +4281,21 @@ void decl(int l) · 13 unmodified lines
4002 sym_push(sym->v & ~SYM_FIELD, 4281 sym_push(sym->v & ~SYM_FIELD,
4003 u | VT_LOCAL | VT_LVAL, 4282 u | VT_LOCAL | VT_LVAL,
4004 addr);4283 addr);
4005 if ((u & VT_BTYPE) == VT_STRUCT) {4284 size = type_size(u, &align);
4285 size = (size + 3) & ~3;
4006#ifdef FUNC_STRUCT_PARAM_AS_PTR4286#ifdef FUNC_STRUCT_PARAM_AS_PTR
4007 /* structs are passed as pointer */4287 /* structs are passed as pointer */
4008 size = 4;4288 if ((u & VT_BTYPE) == VT_STRUCT) {
4009#else
4010 /* structs are directly put on stack (x86
4011 like) */
4012 size = type_size(u, &align);
4013 size = (size + 3) & ~3;
4014#endif
4015 } else {
4016 /* XXX: size will be different someday */
4017 size = 4;4289 size = 4;
4018 }4290 }
4291#endif
4019 addr += size;4292 addr += size;
4020 }4293 }
4021 loc = 0;4294 loc = 0;
4022 o(0xe58955); /* push %ebp, mov %esp, %ebp */4295 o(0xe58955); /* push %ebp, mov %esp, %ebp */
4023 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */4296 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
4024 rsym = 0;4297 rsym = 0;
4025 block(0, 0, 0, 0, 0);4298 block(NULL, NULL, NULL, NULL, 0);
4026 gsym(rsym);4299 gsym(rsym);
4027 o(0xc3c9); /* leave, ret */4300 o(0xc3c9); /* leave, ret */
4028 *a = (-loc + 3) & -4; /* align local size to word & 4301 *a = (-loc + 3) & -4; /* align local size to word &
@@ -4063,9 +4336,9 @@ void decl(int l) · 34 unmodified lines
4063 goto do_def;4336 goto do_def;
4064 if (!is_compatible_types(sym->t, u))4337 if (!is_compatible_types(sym->t, u))
4065 error("incompatible types for redefinition of '%s'", 4338 error("incompatible types for redefinition of '%s'",
4066 get_tok_str(v, 0));4339 get_tok_str(v, NULL));
4067 if (!(sym->t & VT_FORWARD))4340 if (!(sym->t & VT_FORWARD))
4068 error("redefinition of '%s'", get_tok_str(v, 0));4341 error("redefinition of '%s'", get_tok_str(v, NULL));
4069 greloc_patch(sym, addr);4342 greloc_patch(sym, addr);
4070 } else {4343 } else {
4071 do_def:4344 do_def:
@@ -4120,7 +4393,7 @@ void resolve_global_syms(void) · 48 unmodified lines
4120 /* external symbol already exists and is defined :4393 /* external symbol already exists and is defined :
4121 patch all references to it */4394 patch all references to it */
4122 if (!(s->t & VT_FORWARD))4395 if (!(s->t & VT_FORWARD))
4123 error("'%s' defined twice", get_tok_str(s->v, 0));4396 error("'%s' defined twice", get_tok_str(s->v, NULL));
4124 greloc_patch(s, ext_sym->c);4397 greloc_patch(s, ext_sym->c);
4125 }4398 }
4126 } 4399 }
@@ -4192,7 +4465,7 @@ void resolve_extern_syms(void) · 65 unmodified lines
4192 /* if there is at least one relocation to do, then find it4465 /* if there is at least one relocation to do, then find it
4193 and patch it */4466 and patch it */
4194 if (s->c) {4467 if (s->c) {
4195 str = get_tok_str(s->v, 0);4468 str = get_tok_str(s->v, NULL);
4196 addr = (int)dlsym(NULL, str);4469 addr = (int)dlsym(NULL, str);
4197 if (!addr)4470 if (!addr)
4198 error("unresolved external reference '%s'", str);4471 error("unresolved external reference '%s'", str);

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