History

added structure assignment (for = and function calls) - added cast handling for short/char (still not complete)

bellard committed 24 years ago

Commit ebdec641 parent cb1c8c81 file · +161 −46Browse files

Unified · Split

tcc.c+161−46
@@ -40,8 +40,13 @@ · 39 unmodified lines
40 40
41/* number of available temporary registers */41/* number of available temporary registers */
42#define NB_REGS 342#define NB_REGS 3
43/* return register for functions */
44#define FUNC_RET_REG 0
43/* defined if function parameters must be evaluated in reverse order */45/* defined if function parameters must be evaluated in reverse order */
44#define INVERT_FUNC_PARAMS46#define INVERT_FUNC_PARAMS
47/* defined if structures are passed as pointers. Otherwise structures
48 are directly pushed on stack. */
49//#define FUNC_STRUCT_PARAM_AS_PTR
45 50
46/* token symbol management */51/* token symbol management */
47typedef struct TokenSym {52typedef struct TokenSym {
@@ -99,6 +104,8 @@ int tok, tok1, tokc, rsym, anon_sym, · 51 unmodified lines
99 prog, ind, loc, glo, vt, vc, const_wanted, line_num;104 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
100int global_expr; /* true if compound literals must be allocated105int global_expr; /* true if compound literals must be allocated
101 globally (used during initializers parsing */106 globally (used during initializers parsing */
107int func_vt, func_vc; /* current function return type (used by
108 return instruction) */
102int tok_ident;109int tok_ident;
103TokenSym **table_ident;110TokenSym **table_ident;
104TokenSym *hash_ident[521];111TokenSym *hash_ident[521];
@@ -273,9 +280,12 @@ int decl_initializer_alloc(int t, int has_init); · 168 unmodified lines
273int gv(void);280int gv(void);
274void move_reg();281void move_reg();
275void save_reg();282void save_reg();
283void vpush(void);
284int get_reg(void);
276void macro_subst(int **tok_str, int *tok_len, 285void macro_subst(int **tok_str, int *tok_len,
277 Sym **nested_list, int *macro_str);286 Sym **nested_list, int *macro_str);
278int save_reg_forced(int r);287int save_reg_forced(int r);
288void vstore(void);
279int type_size(int t, int *a);289int type_size(int t, int *a);
280int pointed_type(int t);290int pointed_type(int t);
281int pointed_size(int t);291int pointed_size(int t);
@@ -1437,7 +1447,7 @@ void vset(t, v) · 1155 unmodified lines
1437/* X86 code generator */1447/* X86 code generator */
1438 1448
1439typedef struct GFuncContext {1449typedef struct GFuncContext {
1440 int nb_args;1450 int args_size;
1441} GFuncContext;1451} GFuncContext;
1442 1452
1443void g(c)1453void g(c)
@@ -1562,14 +1572,39 @@ void store(r, ft, fc) · 118 unmodified lines
1562/* start function call and return function call context */1572/* start function call and return function call context */
1563void gfunc_start(GFuncContext *c)1573void gfunc_start(GFuncContext *c)
1564{1574{
1565 c->nb_args = 0;1575 c->args_size = 0;
1566}1576}
1567 1577
1568/* push function parameter which is in (vt, vc) */1578/* push function parameter which is in (vt, vc) */
1569void gfunc_param(GFuncContext *c)1579void gfunc_param(GFuncContext *c)
1570{1580{
1571 o(0x50 + gv()); /* push r */1581 int size, align, ft, fc, r;
1572 c->nb_args++;1582
1583 if ((vt & (VT_STRUCT | VT_LVAL)) == (VT_STRUCT | VT_LVAL)) {
1584 size = type_size(vt, &align);
1585 /* align to stack align size */
1586 size = (size + 3) & ~3;
1587 /* allocate the necessary size on stack */
1588 oad(0xec81, size); /* sub $xxx, %esp */
1589 /* generate structure store */
1590 r = get_reg();
1591 o(0x89); /* mov %esp, r */
1592 o(0xe0 + r);
1593 ft = vt;
1594 fc = vc;
1595 vset(VT_INT | r, 0);
1596 vpush();
1597 vt = ft;
1598 vc = fc;
1599 vstore();
1600 c->args_size += size;
1601 } else {
1602 /* simple type (currently always same size) */
1603 /* XXX: implicit cast ? */
1604 r = gv();
1605 o(0x50 + r); /* push r */
1606 c->args_size += 4;
1607 }
1573}1608}
1574 1609
1575/* generate function call with address in (ft, fc) and free function */1610/* generate function call with address in (ft, fc) and free function */
@@ -1587,11 +1622,11 @@ void gfunc_call(GFuncContext *c, int ft, int fc) · 11 unmodified lines
1587 oad(0x95ff, fc); /* call *xxx(%ebp) */1622 oad(0x95ff, fc); /* call *xxx(%ebp) */
1588 } else {1623 } else {
1589 /* not actually used */1624 /* not actually used */
1590 o(0xff); /* call *reg */1625 o(0xff); /* call *r */
1591 o(0xd0 + r);1626 o(0xd0 + r);
1592 }1627 }
1593 if (c->nb_args)1628 if (c->args_size)
1594 oad(0xc481, c->nb_args * 4); /* addl $xxx, %esp */1629 oad(0xc481, c->args_size); /* add $xxx, %esp */
1595}1630}
1596 1631
1597int gjmp(int t)1632int gjmp(int t)
@@ -1743,7 +1778,7 @@ void save_reg(r) · 145 unmodified lines
1743}1778}
1744 1779
1745/* find a free register. If none, save one register */1780/* find a free register. If none, save one register */
1746int get_reg()1781int get_reg(void)
1747{1782{
1748 int r, i, *p;1783 int r, i, *p;
1749 1784
@@ -1807,7 +1842,7 @@ int gvp(int *p) · 57 unmodified lines
1807 return r;1842 return r;
1808}1843}
1809 1844
1810void vpush()1845void vpush(void)
1811{1846{
1812 if (vstack_ptr >= vstack + VSTACK_SIZE)1847 if (vstack_ptr >= vstack + VSTACK_SIZE)
1813 error("memory full");1848 error("memory full");
@@ -1987,6 +2022,36 @@ void gen_op(int op) · 173 unmodified lines
1987 }2022 }
1988}2023}
1989 2024
2025/* cast (vt, vc) to 't' type */
2026void gen_cast(int t)
2027{
2028 int r, bits;
2029 r = vt & VT_VALMASK;
2030 if (!(t & VT_LVAL)) {
2031 /* if not lvalue, then we convert now */
2032 if ((t & VT_TYPE & ~VT_UNSIGNED) == VT_BYTE)
2033 bits = 8;
2034 else if ((t & VT_TYPE & ~VT_UNSIGNED) == VT_SHORT)
2035 bits = 16;
2036 else
2037 goto the_end;
2038 vpush();
2039 if (t & VT_UNSIGNED) {
2040 vset(VT_CONST, (1 << bits) - 1);
2041 gen_op('&');
2042 } else {
2043 bits = 32 - bits;
2044 vset(VT_CONST, bits);
2045 gen_op(TOK_SHL);
2046 vpush();
2047 vset(VT_CONST, bits);
2048 gen_op(TOK_SAR);
2049 }
2050 }
2051 the_end:
2052 vt = (vt & VT_TYPEN) | t;
2053}
2054
1990/* return type size. Put alignment at 'a' */2055/* return type size. Put alignment at 'a' */
1991int type_size(int t, int *a)2056int type_size(int t, int *a)
1992{2057{
@@ -2032,7 +2097,7 @@ int mk_pointer(int t) · 39 unmodified lines
2032}2097}
2033 2098
2034/* store value in lvalue pushed on stack */2099/* store value in lvalue pushed on stack */
2035void vstore()2100void vstore(void)
2036{2101{
2037 int ft, fc, r, t, size, align;2102 int ft, fc, r, t, size, align;
2038 GFuncContext gf;2103 GFuncContext gf;
@@ -2240,15 +2305,15 @@ int ist(void) · 201 unmodified lines
2240 2305
2241int post_type(t)2306int post_type(t)
2242{2307{
2243 int p, n, pt, l, a;2308 int p, n, pt, l;
2244 Sym *last, *s;2309 Sym **plast, *s, *first;
2245 2310
2246 if (tok == '(') {2311 if (tok == '(') {
2247 /* function declaration */2312 /* function declaration */
2248 next();2313 next();
2249 a = 4;
2250 l = 0;2314 l = 0;
2251 last = NULL;2315 first = NULL;
2316 plast = &first;
2252 while (tok != ')') {2317 while (tok != ')') {
2253 /* read param name and compute offset */2318 /* read param name and compute offset */
2254 if (l != FUNC_OLD) {2319 if (l != FUNC_OLD) {
@@ -2267,16 +2332,14 @@ int post_type(t) · 12 unmodified lines
2267 } else {2332 } else {
2268 old_proto:2333 old_proto:
2269 n = tok;2334 n = tok;
2270 pt = 0; /* int type */2335 pt = VT_INT;
2271 next();2336 next();
2272 }2337 }
2273 /* array must be transformed to pointer according to ANSI C */2338 /* array must be transformed to pointer according to ANSI C */
2274 pt &= ~VT_ARRAY;2339 pt &= ~VT_ARRAY;
2275 /* XXX: size will be different someday */2340 s = sym_push(n | SYM_FIELD, pt, 0);
2276 a = a + 4;2341 *plast = s;
2277 s = sym_push(n | SYM_FIELD, VT_LOCAL | VT_LVAL | pt, a);2342 plast = &s->next;
2278 s->next = last;
2279 last = s;
2280 if (tok == ',') {2343 if (tok == ',') {
2281 next();2344 next();
2282 if (l == FUNC_NEW && tok == TOK_DOTS) {2345 if (l == FUNC_NEW && tok == TOK_DOTS) {
@@ -2291,7 +2354,7 @@ int post_type(t) · 8 unmodified lines
2291 /* we push a anonymous symbol which will contain the function prototype */2354 /* we push a anonymous symbol which will contain the function prototype */
2292 p = anon_sym++;2355 p = anon_sym++;
2293 s = sym_push(p, t, l);2356 s = sym_push(p, t, l);
2294 s->next = last;2357 s->next = first;
2295 t = VT_FUNC | (p << VT_STRUCT_SHIFT);2358 t = VT_FUNC | (p << VT_STRUCT_SHIFT);
2296 } else if (tok == '[') {2359 } else if (tok == '[') {
2297 /* array definition */2360 /* array definition */
@@ -2398,7 +2461,7 @@ void indir() · 100 unmodified lines
2398 2461
2399void unary()2462void unary()
2400{2463{
2401 int n, t, ft, fc, p, align;2464 int n, t, ft, fc, p, align, size;
2402 Sym *s;2465 Sym *s;
2403 GFuncContext gf;2466 GFuncContext gf;
2404 2467
@@ -2449,7 +2512,7 @@ void unary() · 44 unmodified lines
2449 vset(ft, fc);2512 vset(ft, fc);
2450 } else {2513 } else {
2451 unary();2514 unary();
2452 vt = (vt & VT_TYPEN) | ft;2515 gen_cast(ft);
2453 }2516 }
2454 } else {2517 } else {
2455 expr();2518 expr();
@@ -2566,7 +2629,14 @@ void unary() · 110 unmodified lines
2566 indir();2629 indir();
2567 skip(']');2630 skip(']');
2568 } else if (tok == '(') {2631 } else if (tok == '(') {
2632 int rett, retc;
2633
2569 /* function call */2634 /* function call */
2635 if (!(vt & VT_FUNC))
2636 expect("function type");
2637 /* get return type */
2638 s = sym_find((unsigned)vt >> VT_STRUCT_SHIFT);
2639
2570 vt &= ~VT_LVAL; /* no lvalue */2640 vt &= ~VT_LVAL; /* no lvalue */
2571 vpush(); /* push function address */2641 vpush(); /* push function address */
2572 save_regs(); /* save used temporary registers */2642 save_regs(); /* save used temporary registers */
@@ -2619,7 +2689,23 @@ void unary() · 46 unmodified lines
2619 /* restore token */2689 /* restore token */
2620 tok = ')';2690 tok = ')';
2621 }2691 }
2622#else2692#endif
2693 /* compute first implicit argument if a structure is returned */
2694 if (s->t & VT_STRUCT) {
2695 /* get some space for the returned structure */
2696 size = type_size(s->t, &align);
2697 loc = (loc - size) & -align;
2698 rett = s->t | VT_LOCAL | VT_LVAL;
2699 /* pass it as 'int' to avoid structure arg passing
2700 problems */
2701 vset(VT_INT | VT_LOCAL, loc);
2702 retc = vc;
2703 gfunc_param(&gf);
2704 } else {
2705 rett = s->t | FUNC_RET_REG; /* return in register */
2706 retc = 0;
2707 }
2708#ifndef INVERT_FUNC_PARAMS
2623 while (tok != ')') {2709 while (tok != ')') {
2624 expr_eq();2710 expr_eq();
2625 gfunc_param(&gf);2711 gfunc_param(&gf);
@@ -2630,23 +2716,9 @@ void unary() · 4 unmodified lines
2630 skip(')');2716 skip(')');
2631 vpop(&ft, &fc);2717 vpop(&ft, &fc);
2632 gfunc_call(&gf, ft, fc);2718 gfunc_call(&gf, ft, fc);
2633#if 02719 /* return value */
2634 if ((ft & VT_VALMASK) == VT_CONST) {2720 vt = rett;
2635 /* forward reference */2721 vc = retc;
2636 if (ft & VT_FORWARD) {
2637 *(int *)fc = psym(0xe8, *(int *)fc);
2638 } else
2639 oad(0xe8, fc - ind - 5);
2640 } else {
2641 oad(0x2494ff, t); /* call *xxx(%esp) */
2642 t = t + 4;
2643 }
2644 if (t)
2645 oad(0xc481, t);
2646#endif
2647 /* get return type */
2648 s = sym_find((unsigned)ft >> VT_STRUCT_SHIFT);
2649 vt = s->t | 0; /* return register is eax */
2650 } else {2722 } else {
2651 break;2723 break;
2652 }2724 }
@@ -2855,8 +2927,21 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 202 unmodified lines
2855 } else if (tok == TOK_RETURN) {2927 } else if (tok == TOK_RETURN) {
2856 next();2928 next();
2857 if (tok != ';') {2929 if (tok != ';') {
2930 if (func_vt & VT_STRUCT) {
2931 /* if returning structure, must copy it to implicit
2932 first pointer arg location */
2933 vset(mk_pointer(func_vt) | VT_LOCAL | VT_LVAL, func_vc);
2934 indir();
2935 vpush();
2936 }
2858 expr();2937 expr();
2859 move_reg(0, gv());2938 if (func_vt & VT_STRUCT) {
2939 /* copy structure value to pointer */
2940 vstore();
2941 } else {
2942 /* move return value to standard return register */
2943 move_reg(FUNC_RET_REG, gv());
2944 }
2860 }2945 }
2861 skip(';');2946 skip(';');
2862 rsym = gjmp(rsym); /* jmp */2947 rsym = gjmp(rsym); /* jmp */
@@ -3345,7 +3430,7 @@ int decl_initializer_alloc(int t, int has_init) · 482 unmodified lines
3345/* 'l' is VT_LOCAL or VT_CONST to define default storage type */3430/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
3346void decl(l)3431void decl(l)
3347{3432{
3348 int *a, t, b, v, u, n, addr, has_init;3433 int *a, t, b, v, u, n, addr, has_init, size, align;
3349 Sym *sym;3434 Sym *sym;
3350 3435
3351 while (1) {3436 while (1) {
@@ -3381,8 +3466,37 @@ void decl(l) · 29 unmodified lines
3381 sym_push1(&local_stack, 0, 0, 0);3466 sym_push1(&local_stack, 0, 0, 0);
3382 /* define parameters */3467 /* define parameters */
3383 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);3468 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
3384 while (sym = sym->next)3469 /* XXX: the following is x86 dependant -> move it to
3385 sym_push(sym->v & ~SYM_FIELD, sym->t, sym->c);3470 x86 code gen */
3471 addr = 8;
3472 /* if the function returns a structure, then add an
3473 implicit pointer parameter */
3474 func_vt = sym->t;
3475 if (func_vt & VT_STRUCT) {
3476 func_vc = addr;
3477 addr += 4;
3478 }
3479 while (sym = sym->next) {
3480 u = sym->t;
3481 sym_push(sym->v & ~SYM_FIELD,
3482 u | VT_LOCAL | VT_LVAL,
3483 addr);
3484 if (u & VT_STRUCT) {
3485#ifdef FUNC_STRUCT_PARAM_AS_PTR
3486 /* structs are passed as pointer */
3487 size = 4;
3488#else
3489 /* structs are directly put on stack (x86
3490 like) */
3491 size = type_size(u, &align);
3492 size = (size + 3) & ~3;
3493#endif
3494 } else {
3495 /* XXX: size will be different someday */
3496 size = 4;
3497 }
3498 addr += size;
3499 }
3386 loc = 0;3500 loc = 0;
3387 o(0xe58955); /* push %ebp, mov %esp, %ebp */3501 o(0xe58955); /* push %ebp, mov %esp, %ebp */
3388 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */3502 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
@@ -3394,7 +3508,8 @@ void decl(l) · 5 unmodified lines
3394 save local variables */3508 save local variables */
3395 sym_pop(&label_stack, 0); /* reset label stack */3509 sym_pop(&label_stack, 0); /* reset label stack */
3396 sym_pop(&local_stack, 0); /* reset local stack */3510 sym_pop(&local_stack, 0); /* reset local stack */
3397 funcname = "";3511 funcname = ""; /* for safety */
3512 func_vt = VT_VOID; /* for safety */
3398 break;3513 break;
3399 } else {3514 } else {
3400 if (b & VT_TYPEDEF) {3515 if (b & VT_TYPEDEF) {

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