Unified · Split
| @@ -270,7 +270,7 @@ void expr(); · 269 unmodified lines | ||
| 270 | 270 | void decl(); |
| 271 | 271 | void decl_initializer(int t, int c, int first, int size_only); |
| 272 | 272 | int decl_initializer_alloc(int t, int has_init); |
| 273 | −int gv(); | |
| 273 | +int gv(void); | |
| 274 | 274 | void move_reg(); |
| 275 | 275 | void save_reg(); |
| 276 | 276 | void macro_subst(int **tok_str, int *tok_len, |
| @@ -1436,6 +1436,10 @@ void vset(t, v) · 1159 unmodified lines | ||
| 1436 | 1436 | /******************************************************/ |
| 1437 | 1437 | /* X86 code generator */ |
| 1438 | 1438 | |
| 1439 | +typedef struct GFuncContext { | |
| 1440 | + int nb_args; | |
| 1441 | +} GFuncContext; | |
| 1442 | + | |
| 1439 | 1443 | void g(c) |
| 1440 | 1444 | { |
| 1441 | 1445 | *(char *)ind++ = c; |
| @@ -1555,12 +1559,42 @@ void store(r, ft, fc) · 113 unmodified lines | ||
| 1555 | 1559 | } |
| 1556 | 1560 | } |
| 1557 | 1561 | |
| 1558 | −void gfunc_param(void) | |
| 1562 | +/* start function call and return function call context */ | |
| 1563 | +void gfunc_start(GFuncContext *c) | |
| 1564 | +{ | |
| 1565 | + c->nb_args = 0; | |
| 1566 | +} | |
| 1567 | + | |
| 1568 | +/* push function parameter which is in (vt, vc) */ | |
| 1569 | +void gfunc_param(GFuncContext *c) | |
| 1559 | 1570 | { |
| 1560 | 1571 | o(0x50 + gv()); /* push r */ |
| 1572 | + c->nb_args++; | |
| 1573 | +} | |
| 1574 | + | |
| 1575 | +/* generate function call with address in (ft, fc) and free function */ | |
| 1576 | +void gfunc_call(GFuncContext *c, int ft, int fc) | |
| 1577 | +{ | |
| 1578 | + int r; | |
| 1579 | + r = ft & VT_VALMASK; | |
| 1580 | + if (r == VT_CONST) { | |
| 1581 | + /* forward reference */ | |
| 1582 | + if (ft & VT_FORWARD) { | |
| 1583 | + *(int *)fc = psym(0xe8, *(int *)fc); | |
| 1584 | + } else | |
| 1585 | + oad(0xe8, fc - ind - 5); | |
| 1586 | + } else if (r == VT_LOCAL) { | |
| 1587 | + oad(0x95ff, fc); /* call *xxx(%ebp) */ | |
| 1588 | + } else { | |
| 1589 | + /* not actually used */ | |
| 1590 | + o(0xff); /* call *reg */ | |
| 1591 | + o(0xd0 + r); | |
| 1592 | + } | |
| 1593 | + if (c->nb_args) | |
| 1594 | + oad(0xc481, c->nb_args * 4); /* addl $xxx, %esp */ | |
| 1561 | 1595 | } |
| 1562 | 1596 | |
| 1563 | −int gjmp(t) | |
| 1597 | +int gjmp(int t) | |
| 1564 | 1598 | { |
| 1565 | 1599 | return psym(0xe9, t); |
| 1566 | 1600 | } |
| @@ -1758,13 +1792,16 @@ void move_reg(r, s) · 191 unmodified lines | ||
| 1758 | 1792 | } |
| 1759 | 1793 | } |
| 1760 | 1794 | |
| 1761 | −/* convert a stack entry in register */ | |
| 1795 | +/* convert a stack entry in register. lvalues are converted as | |
| 1796 | + values. Cannot be used if cannot be converted to register value | |
| 1797 | + (such as structures). */ | |
| 1762 | 1798 | int gvp(int *p) |
| 1763 | 1799 | { |
| 1764 | 1800 | int r; |
| 1765 | 1801 | r = p[0] & VT_VALMASK; |
| 1766 | 1802 | if (r >= VT_CONST || (p[0] & VT_LVAL)) |
| 1767 | 1803 | r = get_reg(); |
| 1804 | + /* NOTE: get_reg can modify p[] */ | |
| 1768 | 1805 | load(r, p[0], p[1]); |
| 1769 | 1806 | p[0] = (p[0] & VT_TYPE) | r; |
| 1770 | 1807 | return r; |
| @@ -1788,7 +1825,7 @@ void vpop(int *ft, int *fc) · 17 unmodified lines | ||
| 1788 | 1825 | } |
| 1789 | 1826 | |
| 1790 | 1827 | /* generate a value in a register from vt and vc */ |
| 1791 | −int gv() | |
| 1828 | +int gv(void) | |
| 1792 | 1829 | { |
| 1793 | 1830 | int r; |
| 1794 | 1831 | vpush(); |
| @@ -1997,19 +2034,50 @@ int mk_pointer(int t) · 202 unmodified lines | ||
| 1997 | 2034 | /* store value in lvalue pushed on stack */ |
| 1998 | 2035 | void vstore() |
| 1999 | 2036 | { |
| 2000 | − int ft, fc, r, t; | |
| 2037 | + int ft, fc, r, t, size, align; | |
| 2038 | + GFuncContext gf; | |
| 2001 | 2039 | |
| 2002 | − r = gv(); /* generate value */ | |
| 2003 | − vpush(); | |
| 2004 | − ft = vstack_ptr[-4]; | |
| 2005 | − fc = vstack_ptr[-3]; | |
| 2006 | − if ((ft & VT_VALMASK) == VT_LLOCAL) { | |
| 2007 | − t = get_reg(); | |
| 2008 | − load(t, VT_LOCAL | VT_LVAL, fc); | |
| 2009 | − ft = (ft & ~VT_VALMASK) | t; | |
| 2040 | + if (vt & VT_STRUCT) { | |
| 2041 | + /* if structure, only generate pointer */ | |
| 2042 | + /* structure assignment : generate memcpy */ | |
| 2043 | + /* XXX: optimize if small size */ | |
| 2044 | + | |
| 2045 | + gfunc_start(&gf); | |
| 2046 | + /* type size */ | |
| 2047 | + ft = vt; | |
| 2048 | + fc = vc; | |
| 2049 | + size = type_size(vt, &align); | |
| 2050 | + vset(VT_CONST, size); | |
| 2051 | + gfunc_param(&gf); | |
| 2052 | + /* source */ | |
| 2053 | + vt = ft & ~VT_LVAL; | |
| 2054 | + vc = fc; | |
| 2055 | + gfunc_param(&gf); | |
| 2056 | + /* destination */ | |
| 2057 | + vpop(&vt, &vc); | |
| 2058 | + vt &= ~VT_LVAL; | |
| 2059 | + gfunc_param(&gf); | |
| 2060 | + | |
| 2061 | + save_regs(); | |
| 2062 | + gfunc_call(&gf, VT_CONST, (int)&memcpy); | |
| 2063 | + | |
| 2064 | + /* generate again current type */ | |
| 2065 | + vt = ft; | |
| 2066 | + vc = fc; | |
| 2067 | + } else { | |
| 2068 | + r = gv(); /* generate value */ | |
| 2069 | + vpush(); | |
| 2070 | + ft = vstack_ptr[-4]; | |
| 2071 | + fc = vstack_ptr[-3]; | |
| 2072 | + /* if lvalue was saved on stack, must read it */ | |
| 2073 | + if ((ft & VT_VALMASK) == VT_LLOCAL) { | |
| 2074 | + t = get_reg(); | |
| 2075 | + load(t, VT_LOCAL | VT_LVAL, fc); | |
| 2076 | + ft = (ft & ~VT_VALMASK) | t; | |
| 2077 | + } | |
| 2078 | + store(r, ft, fc); | |
| 2079 | + vstack_ptr -= 4; | |
| 2010 | 2080 | } |
| 2011 | − store(r, ft, fc); | |
| 2012 | − vstack_ptr -= 4; | |
| 2013 | 2081 | } |
| 2014 | 2082 | |
| 2015 | 2083 | /* post defines POST/PRE add. c is the token ++ or -- */ |
| @@ -2330,8 +2398,9 @@ void indir() · 314 unmodified lines | ||
| 2330 | 2398 | |
| 2331 | 2399 | void unary() |
| 2332 | 2400 | { |
| 2333 | − int n, t, ft, fc, p, r, align; | |
| 2401 | + int n, t, ft, fc, p, align; | |
| 2334 | 2402 | Sym *s; |
| 2403 | + GFuncContext gf; | |
| 2335 | 2404 | |
| 2336 | 2405 | if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) { |
| 2337 | 2406 | vset(VT_CONST, tokc); |
| @@ -2498,16 +2567,10 @@ void unary() · 160 unmodified lines | ||
| 2498 | 2567 | skip(']'); |
| 2499 | 2568 | } else if (tok == '(') { |
| 2500 | 2569 | /* function call */ |
| 2570 | + vt &= ~VT_LVAL; /* no lvalue */ | |
| 2571 | + vpush(); /* push function address */ | |
| 2501 | 2572 | save_regs(); /* save used temporary registers */ |
| 2502 | − /* lvalue is implied */ | |
| 2503 | − vt = vt & VT_LVALN; | |
| 2504 | − if ((vt & VT_VALMASK) != VT_CONST) { | |
| 2505 | − /* evaluate function address */ | |
| 2506 | − r = gv(); | |
| 2507 | − o(0x50 + r); /* push r */ | |
| 2508 | − } | |
| 2509 | − ft = vt; | |
| 2510 | − fc = vc; | |
| 2573 | + gfunc_start(&gf); | |
| 2511 | 2574 | next(); |
| 2512 | 2575 | #ifdef INVERT_FUNC_PARAMS |
| 2513 | 2576 | { |
| @@ -2542,13 +2605,11 @@ void unary() · 28 unmodified lines | ||
| 2542 | 2605 | |
| 2543 | 2606 | /* now generate code in reverse order by reading the stack */ |
| 2544 | 2607 | saved_macro_ptr = macro_ptr; |
| 2545 | − t = 0; | |
| 2546 | 2608 | while (args) { |
| 2547 | − t += 4; | |
| 2548 | 2609 | macro_ptr = (int *)args->c; |
| 2549 | 2610 | next(); |
| 2550 | 2611 | expr_eq(); |
| 2551 | − gfunc_param(); | |
| 2612 | + gfunc_param(&gf); | |
| 2552 | 2613 | s1 = args->prev; |
| 2553 | 2614 | free((int *)args->c); |
| 2554 | 2615 | free(args); |
| @@ -2559,16 +2620,17 @@ void unary() · 4 unmodified lines | ||
| 2559 | 2620 | tok = ')'; |
| 2560 | 2621 | } |
| 2561 | 2622 | #else |
| 2562 | − t = 0; | |
| 2563 | 2623 | while (tok != ')') { |
| 2564 | − t += 4; | |
| 2565 | 2624 | expr_eq(); |
| 2566 | − gfunc_param(); | |
| 2625 | + gfunc_param(&gf); | |
| 2567 | 2626 | if (tok == ',') |
| 2568 | 2627 | next(); |
| 2569 | 2628 | } |
| 2570 | 2629 | #endif |
| 2571 | 2630 | skip(')'); |
| 2631 | + vpop(&ft, &fc); | |
| 2632 | + gfunc_call(&gf, ft, fc); | |
| 2633 | +#if 0 | |
| 2572 | 2634 | if ((ft & VT_VALMASK) == VT_CONST) { |
| 2573 | 2635 | /* forward reference */ |
| 2574 | 2636 | if (ft & VT_FORWARD) { |
| @@ -2581,6 +2643,7 @@ void unary() · 6 unmodified lines | ||
| 2581 | 2643 | } |
| 2582 | 2644 | if (t) |
| 2583 | 2645 | oad(0xc481, t); |
| 2646 | +#endif | |
| 2584 | 2647 | /* get return type */ |
| 2585 | 2648 | s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT); |
| 2586 | 2649 | vt = s->t | 0; /* return register is eax */ |
| @@ -3041,23 +3104,19 @@ void init_putv(int t, int c, int v, int is_expr) · 454 unmodified lines | ||
| 3041 | 3104 | /* put zeros for variable based init */ |
| 3042 | 3105 | void init_putz(int t, int c, int size) |
| 3043 | 3106 | { |
| 3044 | − int memset_addr, r; | |
| 3107 | + GFuncContext gf; | |
| 3045 | 3108 | |
| 3046 | 3109 | if ((t & VT_VALMASK) == VT_CONST) { |
| 3047 | 3110 | /* nothing to do because global are already set to zero */ |
| 3048 | 3111 | } else { |
| 3112 | + gfunc_start(&gf); | |
| 3049 | 3113 | vset(VT_CONST, size); |
| 3050 | − r = gv(); | |
| 3051 | − o(0x50 + r); | |
| 3114 | + gfunc_param(&gf); | |
| 3052 | 3115 | vset(VT_CONST, 0); |
| 3053 | − r = gv(); | |
| 3054 | − o(0x50 + r); | |
| 3116 | + gfunc_param(&gf); | |
| 3055 | 3117 | vset(VT_LOCAL, c); |
| 3056 | − r = gv(); | |
| 3057 | − o(0x50 + r); | |
| 3058 | − memset_addr = (int)&memset; | |
| 3059 | − oad(0xe8, memset_addr - ind - 5); | |
| 3060 | − oad(0xc481, 3 * 4); | |
| 3118 | + gfunc_param(&gf); | |
| 3119 | + gfunc_call(&gf, VT_CONST, (int)&memset); | |
| 3061 | 3120 | } |
| 3062 | 3121 | } |
| 3063 | 3122 | |