History

first stage for structure assign - added more generic function call code generation

bellard committed 24 years ago

Commit cb1c8c81 parent f22a3231 file · +101 −42Browse files

Unified · Split

tcc.c+101−42
@@ -270,7 +270,7 @@ void expr(); · 269 unmodified lines
270void decl();270void decl();
271void decl_initializer(int t, int c, int first, int size_only);271void decl_initializer(int t, int c, int first, int size_only);
272int decl_initializer_alloc(int t, int has_init);272int decl_initializer_alloc(int t, int has_init);
273int gv();273int gv(void);
274void move_reg();274void move_reg();
275void save_reg();275void save_reg();
276void macro_subst(int **tok_str, int *tok_len, 276void macro_subst(int **tok_str, int *tok_len,
@@ -1436,6 +1436,10 @@ void vset(t, v) · 1159 unmodified lines
1436/******************************************************/1436/******************************************************/
1437/* X86 code generator */1437/* X86 code generator */
1438 1438
1439typedef struct GFuncContext {
1440 int nb_args;
1441} GFuncContext;
1442
1439void g(c)1443void g(c)
1440{1444{
1441 *(char *)ind++ = c;1445 *(char *)ind++ = c;
@@ -1555,12 +1559,42 @@ void store(r, ft, fc) · 113 unmodified lines
1555 }1559 }
1556}1560}
1557 1561
1558void gfunc_param(void)1562/* start function call and return function call context */
1563void gfunc_start(GFuncContext *c)
1564{
1565 c->nb_args = 0;
1566}
1567
1568/* push function parameter which is in (vt, vc) */
1569void gfunc_param(GFuncContext *c)
1559{1570{
1560 o(0x50 + gv()); /* push r */1571 o(0x50 + gv()); /* push r */
1572 c->nb_args++;
1573}
1574
1575/* generate function call with address in (ft, fc) and free function */
1576void 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
1563int gjmp(t)1597int gjmp(int t)
1564{1598{
1565 return psym(0xe9, t);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). */
1762int gvp(int *p)1798int gvp(int *p)
1763{1799{
1764 int r;1800 int r;
1765 r = p[0] & VT_VALMASK;1801 r = p[0] & VT_VALMASK;
1766 if (r >= VT_CONST || (p[0] & VT_LVAL))1802 if (r >= VT_CONST || (p[0] & VT_LVAL))
1767 r = get_reg();1803 r = get_reg();
1804 /* NOTE: get_reg can modify p[] */
1768 load(r, p[0], p[1]);1805 load(r, p[0], p[1]);
1769 p[0] = (p[0] & VT_TYPE) | r;1806 p[0] = (p[0] & VT_TYPE) | r;
1770 return r;1807 return r;
@@ -1788,7 +1825,7 @@ void vpop(int *ft, int *fc) · 17 unmodified lines
1788}1825}
1789 1826
1790/* generate a value in a register from vt and vc */1827/* generate a value in a register from vt and vc */
1791int gv()1828int gv(void)
1792{1829{
1793 int r;1830 int r;
1794 vpush();1831 vpush();
@@ -1997,19 +2034,50 @@ int mk_pointer(int t) · 202 unmodified lines
1997/* store value in lvalue pushed on stack */2034/* store value in lvalue pushed on stack */
1998void vstore()2035void 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 */2040 if (vt & VT_STRUCT) {
2003 vpush();2041 /* if structure, only generate pointer */
2004 ft = vstack_ptr[-4];2042 /* structure assignment : generate memcpy */
2005 fc = vstack_ptr[-3];2043 /* XXX: optimize if small size */
2006 if ((ft & VT_VALMASK) == VT_LLOCAL) {2044
2007 t = get_reg();2045 gfunc_start(&gf);
2008 load(t, VT_LOCAL | VT_LVAL, fc);2046 /* type size */
2009 ft = (ft & ~VT_VALMASK) | t;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/* post defines POST/PRE add. c is the token ++ or -- */2083/* post defines POST/PRE add. c is the token ++ or -- */
@@ -2330,8 +2398,9 @@ void indir() · 314 unmodified lines
2330 2398
2331void unary()2399void unary()
2332{2400{
2333 int n, t, ft, fc, p, r, align;2401 int n, t, ft, fc, p, align;
2334 Sym *s;2402 Sym *s;
2403 GFuncContext gf;
2335 2404
2336 if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {2405 if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {
2337 vset(VT_CONST, tokc);2406 vset(VT_CONST, tokc);
@@ -2498,16 +2567,10 @@ void unary() · 160 unmodified lines
2498 skip(']');2567 skip(']');
2499 } else if (tok == '(') {2568 } else if (tok == '(') {
2500 /* function call */2569 /* function call */
2570 vt &= ~VT_LVAL; /* no lvalue */
2571 vpush(); /* push function address */
2501 save_regs(); /* save used temporary registers */2572 save_regs(); /* save used temporary registers */
2502 /* lvalue is implied */2573 gfunc_start(&gf);
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;
2511 next();2574 next();
2512#ifdef INVERT_FUNC_PARAMS2575#ifdef INVERT_FUNC_PARAMS
2513 {2576 {
@@ -2542,13 +2605,11 @@ void unary() · 28 unmodified lines
2542 2605
2543 /* now generate code in reverse order by reading the stack */2606 /* now generate code in reverse order by reading the stack */
2544 saved_macro_ptr = macro_ptr;2607 saved_macro_ptr = macro_ptr;
2545 t = 0;
2546 while (args) {2608 while (args) {
2547 t += 4;
2548 macro_ptr = (int *)args->c;2609 macro_ptr = (int *)args->c;
2549 next();2610 next();
2550 expr_eq();2611 expr_eq();
2551 gfunc_param();2612 gfunc_param(&gf);
2552 s1 = args->prev;2613 s1 = args->prev;
2553 free((int *)args->c);2614 free((int *)args->c);
2554 free(args);2615 free(args);
@@ -2559,16 +2620,17 @@ void unary() · 4 unmodified lines
2559 tok = ')';2620 tok = ')';
2560 }2621 }
2561#else2622#else
2562 t = 0;
2563 while (tok != ')') {2623 while (tok != ')') {
2564 t += 4;
2565 expr_eq();2624 expr_eq();
2566 gfunc_param();2625 gfunc_param(&gf);
2567 if (tok == ',')2626 if (tok == ',')
2568 next();2627 next();
2569 }2628 }
2570#endif2629#endif
2571 skip(')');2630 skip(')');
2631 vpop(&ft, &fc);
2632 gfunc_call(&gf, ft, fc);
2633#if 0
2572 if ((ft & VT_VALMASK) == VT_CONST) {2634 if ((ft & VT_VALMASK) == VT_CONST) {
2573 /* forward reference */2635 /* forward reference */
2574 if (ft & VT_FORWARD) {2636 if (ft & VT_FORWARD) {
@@ -2581,6 +2643,7 @@ void unary() · 6 unmodified lines
2581 }2643 }
2582 if (t)2644 if (t)
2583 oad(0xc481, t);2645 oad(0xc481, t);
2646#endif
2584 /* get return type */2647 /* get return type */
2585 s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT);2648 s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT);
2586 vt = s->t | 0; /* return register is eax */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/* put zeros for variable based init */3104/* put zeros for variable based init */
3042void init_putz(int t, int c, int size)3105void init_putz(int t, int c, int size)
3043{3106{
3044 int memset_addr, r;3107 GFuncContext gf;
3045 3108
3046 if ((t & VT_VALMASK) == VT_CONST) {3109 if ((t & VT_VALMASK) == VT_CONST) {
3047 /* nothing to do because global are already set to zero */3110 /* nothing to do because global are already set to zero */
3048 } else {3111 } else {
3112 gfunc_start(&gf);
3049 vset(VT_CONST, size);3113 vset(VT_CONST, size);
3050 r = gv();3114 gfunc_param(&gf);
3051 o(0x50 + r);
3052 vset(VT_CONST, 0);3115 vset(VT_CONST, 0);
3053 r = gv();3116 gfunc_param(&gf);
3054 o(0x50 + r);
3055 vset(VT_LOCAL, c);3117 vset(VT_LOCAL, c);
3056 r = gv();3118 gfunc_param(&gf);
3057 o(0x50 + r);3119 gfunc_call(&gf, VT_CONST, (int)&memset);
3058 memset_addr = (int)&memset;
3059 oad(0xe8, memset_addr - ind - 5);
3060 oad(0xc481, 3 * 4);
3061 }3120 }
3062}3121}
3063 3122

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