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
270270 void decl();
271271 void decl_initializer(int t, int c, int first, int size_only);
272272 int decl_initializer_alloc(int t, int has_init);
273−int gv();
273+int gv(void);
274274 void move_reg();
275275 void save_reg();
276276 void macro_subst(int **tok_str, int *tok_len,
@@ -1436,6 +1436,10 @@ void vset(t, v) · 1159 unmodified lines
14361436 /******************************************************/
14371437 /* X86 code generator */
14381438
1439+typedef struct GFuncContext {
1440+ int nb_args;
1441+} GFuncContext;
1442+
14391443 void g(c)
14401444 {
14411445 *(char *)ind++ = c;
@@ -1555,12 +1559,42 @@ void store(r, ft, fc) · 113 unmodified lines
15551559 }
15561560 }
15571561
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)
15591570 {
15601571 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 */
15611595 }
15621596
1563−int gjmp(t)
1597+int gjmp(int t)
15641598 {
15651599 return psym(0xe9, t);
15661600 }
@@ -1758,13 +1792,16 @@ void move_reg(r, s) · 191 unmodified lines
17581792 }
17591793 }
17601794
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). */
17621798 int gvp(int *p)
17631799 {
17641800 int r;
17651801 r = p[0] & VT_VALMASK;
17661802 if (r >= VT_CONST || (p[0] & VT_LVAL))
17671803 r = get_reg();
1804+ /* NOTE: get_reg can modify p[] */
17681805 load(r, p[0], p[1]);
17691806 p[0] = (p[0] & VT_TYPE) | r;
17701807 return r;
@@ -1788,7 +1825,7 @@ void vpop(int *ft, int *fc) · 17 unmodified lines
17881825 }
17891826
17901827 /* generate a value in a register from vt and vc */
1791−int gv()
1828+int gv(void)
17921829 {
17931830 int r;
17941831 vpush();
@@ -1997,19 +2034,50 @@ int mk_pointer(int t) · 202 unmodified lines
19972034 /* store value in lvalue pushed on stack */
19982035 void vstore()
19992036 {
2000− int ft, fc, r, t;
2037+ int ft, fc, r, t, size, align;
2038+ GFuncContext gf;
20012039
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;
20102080 }
2011− store(r, ft, fc);
2012− vstack_ptr -= 4;
20132081 }
20142082
20152083 /* post defines POST/PRE add. c is the token ++ or -- */
@@ -2330,8 +2398,9 @@ void indir() · 314 unmodified lines
23302398
23312399 void unary()
23322400 {
2333− int n, t, ft, fc, p, r, align;
2401+ int n, t, ft, fc, p, align;
23342402 Sym *s;
2403+ GFuncContext gf;
23352404
23362405 if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_LCHAR) {
23372406 vset(VT_CONST, tokc);
@@ -2498,16 +2567,10 @@ void unary() · 160 unmodified lines
24982567 skip(']');
24992568 } else if (tok == '(') {
25002569 /* function call */
2570+ vt &= ~VT_LVAL; /* no lvalue */
2571+ vpush(); /* push function address */
25012572 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);
25112574 next();
25122575 #ifdef INVERT_FUNC_PARAMS
25132576 {
@@ -2542,13 +2605,11 @@ void unary() · 28 unmodified lines
25422605
25432606 /* now generate code in reverse order by reading the stack */
25442607 saved_macro_ptr = macro_ptr;
2545− t = 0;
25462608 while (args) {
2547− t += 4;
25482609 macro_ptr = (int *)args->c;
25492610 next();
25502611 expr_eq();
2551− gfunc_param();
2612+ gfunc_param(&gf);
25522613 s1 = args->prev;
25532614 free((int *)args->c);
25542615 free(args);
@@ -2559,16 +2620,17 @@ void unary() · 4 unmodified lines
25592620 tok = ')';
25602621 }
25612622 #else
2562− t = 0;
25632623 while (tok != ')') {
2564− t += 4;
25652624 expr_eq();
2566− gfunc_param();
2625+ gfunc_param(&gf);
25672626 if (tok == ',')
25682627 next();
25692628 }
25702629 #endif
25712630 skip(')');
2631+ vpop(&ft, &fc);
2632+ gfunc_call(&gf, ft, fc);
2633+#if 0
25722634 if ((ft & VT_VALMASK) == VT_CONST) {
25732635 /* forward reference */
25742636 if (ft & VT_FORWARD) {
@@ -2581,6 +2643,7 @@ void unary() · 6 unmodified lines
25812643 }
25822644 if (t)
25832645 oad(0xc481, t);
2646+#endif
25842647 /* get return type */
25852648 s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT);
25862649 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
30413104 /* put zeros for variable based init */
30423105 void init_putz(int t, int c, int size)
30433106 {
3044− int memset_addr, r;
3107+ GFuncContext gf;
30453108
30463109 if ((t & VT_VALMASK) == VT_CONST) {
30473110 /* nothing to do because global are already set to zero */
30483111 } else {
3112+ gfunc_start(&gf);
30493113 vset(VT_CONST, size);
3050− r = gv();
3051− o(0x50 + r);
3114+ gfunc_param(&gf);
30523115 vset(VT_CONST, 0);
3053− r = gv();
3054− o(0x50 + r);
3116+ gfunc_param(&gf);
30553117 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);
30613120 }
30623121 }
30633122

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