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
4040
4141 /* number of available temporary registers */
4242 #define NB_REGS 3
43+/* return register for functions */
44+#define FUNC_RET_REG 0
4345 /* defined if function parameters must be evaluated in reverse order */
4446 #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
4550
4651 /* token symbol management */
4752 typedef struct TokenSym {
@@ -99,6 +104,8 @@ int tok, tok1, tokc, rsym, anon_sym, · 51 unmodified lines
99104 prog, ind, loc, glo, vt, vc, const_wanted, line_num;
100105 int global_expr; /* true if compound literals must be allocated
101106 globally (used during initializers parsing */
107+int func_vt, func_vc; /* current function return type (used by
108+ return instruction) */
102109 int tok_ident;
103110 TokenSym **table_ident;
104111 TokenSym *hash_ident[521];
@@ -273,9 +280,12 @@ int decl_initializer_alloc(int t, int has_init); · 168 unmodified lines
273280 int gv(void);
274281 void move_reg();
275282 void save_reg();
283+void vpush(void);
284+int get_reg(void);
276285 void macro_subst(int **tok_str, int *tok_len,
277286 Sym **nested_list, int *macro_str);
278287 int save_reg_forced(int r);
288+void vstore(void);
279289 int type_size(int t, int *a);
280290 int pointed_type(int t);
281291 int pointed_size(int t);
@@ -1437,7 +1447,7 @@ void vset(t, v) · 1155 unmodified lines
14371447 /* X86 code generator */
14381448
14391449 typedef struct GFuncContext {
1440− int nb_args;
1450+ int args_size;
14411451 } GFuncContext;
14421452
14431453 void g(c)
@@ -1562,14 +1572,39 @@ void store(r, ft, fc) · 118 unmodified lines
15621572 /* start function call and return function call context */
15631573 void gfunc_start(GFuncContext *c)
15641574 {
1565− c->nb_args = 0;
1575+ c->args_size = 0;
15661576 }
15671577
15681578 /* push function parameter which is in (vt, vc) */
15691579 void gfunc_param(GFuncContext *c)
15701580 {
1571− o(0x50 + gv()); /* push r */
1572− c->nb_args++;
1581+ int size, align, ft, fc, r;
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+ }
15731608 }
15741609
15751610 /* 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
15871622 oad(0x95ff, fc); /* call *xxx(%ebp) */
15881623 } else {
15891624 /* not actually used */
1590− o(0xff); /* call *reg */
1625+ o(0xff); /* call *r */
15911626 o(0xd0 + r);
15921627 }
1593− if (c->nb_args)
1594− oad(0xc481, c->nb_args * 4); /* addl $xxx, %esp */
1628+ if (c->args_size)
1629+ oad(0xc481, c->args_size); /* add $xxx, %esp */
15951630 }
15961631
15971632 int gjmp(int t)
@@ -1743,7 +1778,7 @@ void save_reg(r) · 145 unmodified lines
17431778 }
17441779
17451780 /* find a free register. If none, save one register */
1746−int get_reg()
1781+int get_reg(void)
17471782 {
17481783 int r, i, *p;
17491784
@@ -1807,7 +1842,7 @@ int gvp(int *p) · 57 unmodified lines
18071842 return r;
18081843 }
18091844
1810−void vpush()
1845+void vpush(void)
18111846 {
18121847 if (vstack_ptr >= vstack + VSTACK_SIZE)
18131848 error("memory full");
@@ -1987,6 +2022,36 @@ void gen_op(int op) · 173 unmodified lines
19872022 }
19882023 }
19892024
2025+/* cast (vt, vc) to 't' type */
2026+void 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+
19902055 /* return type size. Put alignment at 'a' */
19912056 int type_size(int t, int *a)
19922057 {
@@ -2032,7 +2097,7 @@ int mk_pointer(int t) · 39 unmodified lines
20322097 }
20332098
20342099 /* store value in lvalue pushed on stack */
2035−void vstore()
2100+void vstore(void)
20362101 {
20372102 int ft, fc, r, t, size, align;
20382103 GFuncContext gf;
@@ -2240,15 +2305,15 @@ int ist(void) · 201 unmodified lines
22402305
22412306 int post_type(t)
22422307 {
2243− int p, n, pt, l, a;
2244− Sym *last, *s;
2308+ int p, n, pt, l;
2309+ Sym **plast, *s, *first;
22452310
22462311 if (tok == '(') {
22472312 /* function declaration */
22482313 next();
2249− a = 4;
22502314 l = 0;
2251− last = NULL;
2315+ first = NULL;
2316+ plast = &first;
22522317 while (tok != ')') {
22532318 /* read param name and compute offset */
22542319 if (l != FUNC_OLD) {
@@ -2267,16 +2332,14 @@ int post_type(t) · 12 unmodified lines
22672332 } else {
22682333 old_proto:
22692334 n = tok;
2270− pt = 0; /* int type */
2335+ pt = VT_INT;
22712336 next();
22722337 }
22732338 /* array must be transformed to pointer according to ANSI C */
22742339 pt &= ~VT_ARRAY;
2275− /* XXX: size will be different someday */
2276− a = a + 4;
2277− s = sym_push(n | SYM_FIELD, VT_LOCAL | VT_LVAL | pt, a);
2278− s->next = last;
2279− last = s;
2340+ s = sym_push(n | SYM_FIELD, pt, 0);
2341+ *plast = s;
2342+ plast = &s->next;
22802343 if (tok == ',') {
22812344 next();
22822345 if (l == FUNC_NEW && tok == TOK_DOTS) {
@@ -2291,7 +2354,7 @@ int post_type(t) · 8 unmodified lines
22912354 /* we push a anonymous symbol which will contain the function prototype */
22922355 p = anon_sym++;
22932356 s = sym_push(p, t, l);
2294− s->next = last;
2357+ s->next = first;
22952358 t = VT_FUNC | (p << VT_STRUCT_SHIFT);
22962359 } else if (tok == '[') {
22972360 /* array definition */
@@ -2398,7 +2461,7 @@ void indir() · 100 unmodified lines
23982461
23992462 void unary()
24002463 {
2401− int n, t, ft, fc, p, align;
2464+ int n, t, ft, fc, p, align, size;
24022465 Sym *s;
24032466 GFuncContext gf;
24042467
@@ -2449,7 +2512,7 @@ void unary() · 44 unmodified lines
24492512 vset(ft, fc);
24502513 } else {
24512514 unary();
2452− vt = (vt & VT_TYPEN) | ft;
2515+ gen_cast(ft);
24532516 }
24542517 } else {
24552518 expr();
@@ -2566,7 +2629,14 @@ void unary() · 110 unmodified lines
25662629 indir();
25672630 skip(']');
25682631 } else if (tok == '(') {
2632+ int rett, retc;
2633+
25692634 /* 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+
25702640 vt &= ~VT_LVAL; /* no lvalue */
25712641 vpush(); /* push function address */
25722642 save_regs(); /* save used temporary registers */
@@ -2619,7 +2689,23 @@ void unary() · 46 unmodified lines
26192689 /* restore token */
26202690 tok = ')';
26212691 }
2622−#else
2692+#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
26232709 while (tok != ')') {
26242710 expr_eq();
26252711 gfunc_param(&gf);
@@ -2630,23 +2716,9 @@ void unary() · 4 unmodified lines
26302716 skip(')');
26312717 vpop(&ft, &fc);
26322718 gfunc_call(&gf, ft, fc);
2633−#if 0
2634− if ((ft & VT_VALMASK) == VT_CONST) {
2635− /* forward reference */
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 */
2719+ /* return value */
2720+ vt = rett;
2721+ vc = retc;
26502722 } else {
26512723 break;
26522724 }
@@ -2855,8 +2927,21 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 202 unmodified lines
28552927 } else if (tok == TOK_RETURN) {
28562928 next();
28572929 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+ }
28582937 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+ }
28602945 }
28612946 skip(';');
28622947 rsym = gjmp(rsym); /* jmp */
@@ -3345,7 +3430,7 @@ int decl_initializer_alloc(int t, int has_init) · 482 unmodified lines
33453430 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
33463431 void decl(l)
33473432 {
3348− int *a, t, b, v, u, n, addr, has_init;
3433+ int *a, t, b, v, u, n, addr, has_init, size, align;
33493434 Sym *sym;
33503435
33513436 while (1) {
@@ -3381,8 +3466,37 @@ void decl(l) · 29 unmodified lines
33813466 sym_push1(&local_stack, 0, 0, 0);
33823467 /* define parameters */
33833468 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
3384− while (sym = sym->next)
3385− sym_push(sym->v & ~SYM_FIELD, sym->t, sym->c);
3469+ /* XXX: the following is x86 dependant -> move it to
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+ }
33863500 loc = 0;
33873501 o(0xe58955); /* push %ebp, mov %esp, %ebp */
33883502 a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
@@ -3394,7 +3508,8 @@ void decl(l) · 5 unmodified lines
33943508 save local variables */
33953509 sym_pop(&label_stack, 0); /* reset label stack */
33963510 sym_pop(&local_stack, 0); /* reset local stack */
3397− funcname = "";
3511+ funcname = ""; /* for safety */
3512+ func_vt = VT_VOID; /* for safety */
33983513 break;
33993514 } else {
34003515 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