History

simpler function call API

bellard committed 23 years ago

Commit 214cccc1 parent 0d6f8022 files · +108 −182Browse files

Unified · Split

2 files
  1. i386-gen.c +58 −70
  2. tcc.c +50 −112
i386-gen.c+58−70
@@ -73,12 +73,6 @@ int reg_classes[NB_REGS] = { · 72 unmodified lines
73/* relocation type for 32 bit data relocation */73/* relocation type for 32 bit data relocation */
74#define R_DATA_32 R_386_3274#define R_DATA_32 R_386_32
75 75
76/* function call context */
77typedef struct GFuncContext {
78 int args_size;
79 int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
80} GFuncContext;
81
82/******************************************************/76/******************************************************/
83 77
84static unsigned long func_sub_sp_offset;78static unsigned long func_sub_sp_offset;
@@ -282,65 +276,6 @@ void store(int r, SValue *v) · 197 unmodified lines
282 }276 }
283}277}
284 278
285/* start function call and return function call context */
286void gfunc_start(GFuncContext *c, int func_call)
287{
288 c->args_size = 0;
289 c->func_call = func_call;
290}
291
292/* push function parameter which is in (vtop->t, vtop->c). Stack entry
293 is then popped. */
294void gfunc_param(GFuncContext *c)
295{
296 int size, align, r;
297
298 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
299 size = type_size(&vtop->type, &align);
300 /* align to stack align size */
301 size = (size + 3) & ~3;
302 /* allocate the necessary size on stack */
303 oad(0xec81, size); /* sub $xxx, %esp */
304 /* generate structure store */
305 r = get_reg(RC_INT);
306 o(0x89); /* mov %esp, r */
307 o(0xe0 + r);
308 vset(&vtop->type, r | VT_LVAL, 0);
309 vswap();
310 vstore();
311 c->args_size += size;
312 } else if (is_float(vtop->type.t)) {
313 gv(RC_FLOAT); /* only one float register */
314 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
315 size = 4;
316 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
317 size = 8;
318 else
319 size = 12;
320 oad(0xec81, size); /* sub $xxx, %esp */
321 if (size == 12)
322 o(0x7cdb);
323 else
324 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
325 g(0x24);
326 g(0x00);
327 c->args_size += size;
328 } else {
329 /* simple type (currently always same size) */
330 /* XXX: implicit cast ? */
331 r = gv(RC_INT);
332 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
333 size = 8;
334 o(0x50 + vtop->r2); /* push r */
335 } else {
336 size = 4;
337 }
338 o(0x50 + r); /* push r */
339 c->args_size += size;
340 }
341 vtop--;
342}
343
344static void gadd_sp(int val)279static void gadd_sp(int val)
345{280{
346 if (val == (char)val) {281 if (val == (char)val) {
@@ -375,13 +310,66 @@ static void gcall_or_jmp(int is_jmp) · 28 unmodified lines
375 }310 }
376}311}
377 312
378/* generate function call with address in (vtop->t, vtop->c) and free function313/* Generate function call. The function address is pushed first, then
379 context. Stack entry is popped */314 all the parameters in call order. This functions pops all the
380void gfunc_call(GFuncContext *c)315 parameters and the function address. */
316void gfunc_call(int nb_args)
381{317{
318 int size, align, r, args_size, i;
319 Sym *func_sym;
320
321 args_size = 0;
322 for(i = 0;i < nb_args; i++) {
323 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
324 size = type_size(&vtop->type, &align);
325 /* align to stack align size */
326 size = (size + 3) & ~3;
327 /* allocate the necessary size on stack */
328 oad(0xec81, size); /* sub $xxx, %esp */
329 /* generate structure store */
330 r = get_reg(RC_INT);
331 o(0x89); /* mov %esp, r */
332 o(0xe0 + r);
333 vset(&vtop->type, r | VT_LVAL, 0);
334 vswap();
335 vstore();
336 args_size += size;
337 } else if (is_float(vtop->type.t)) {
338 gv(RC_FLOAT); /* only one float register */
339 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
340 size = 4;
341 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
342 size = 8;
343 else
344 size = 12;
345 oad(0xec81, size); /* sub $xxx, %esp */
346 if (size == 12)
347 o(0x7cdb);
348 else
349 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
350 g(0x24);
351 g(0x00);
352 args_size += size;
353 } else {
354 /* simple type (currently always same size) */
355 /* XXX: implicit cast ? */
356 r = gv(RC_INT);
357 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
358 size = 8;
359 o(0x50 + vtop->r2); /* push r */
360 } else {
361 size = 4;
362 }
363 o(0x50 + r); /* push r */
364 args_size += size;
365 }
366 vtop--;
367 }
368 save_regs(0); /* save used temporary registers */
369 func_sym = vtop->type.ref;
382 gcall_or_jmp(0);370 gcall_or_jmp(0);
383 if (c->args_size && c->func_call == FUNC_CDECL)371 if (args_size && func_sym->r == FUNC_CDECL)
384 gadd_sp(c->args_size);372 gadd_sp(args_size);
385 vtop--;373 vtop--;
386}374}
387 375
tcc.c+50−112
@@ -4589,7 +4589,9 @@ void lbuild(int t) · 4588 unmodified lines
4589 vpop();4589 vpop();
4590}4590}
4591 4591
4592/* rotate n first stack elements to the bottom */4592/* rotate n first stack elements to the bottom
4593 I1 ... In -> I2 ... In I1 [top is right]
4594*/
4593void vrotb(int n)4595void vrotb(int n)
4594{4596{
4595 int i;4597 int i;
@@ -4601,6 +4603,20 @@ void vrotb(int n) · 5 unmodified lines
4601 vtop[0] = tmp;4603 vtop[0] = tmp;
4602}4604}
4603 4605
4606/* rotate n first stack elements to the top
4607 I1 ... In -> In I1 ... I(n-1) [top is right]
4608 */
4609void vrott(int n)
4610{
4611 int i;
4612 SValue tmp;
4613
4614 tmp = vtop[0];
4615 for(i = 0;i < n - 1; i++)
4616 vtop[-i] = vtop[-i - 1];
4617 vtop[-n + 1] = tmp;
4618}
4619
4604/* pop stack value */4620/* pop stack value */
4605void vpop(void)4621void vpop(void)
4606{4622{
@@ -4665,7 +4681,6 @@ void gen_opl(int op) · 58 unmodified lines
4665{4681{
4666 int t, a, b, op1, c, i;4682 int t, a, b, op1, c, i;
4667 int func;4683 int func;
4668 GFuncContext gf;
4669 SValue tmp;4684 SValue tmp;
4670 4685
4671 switch(op) {4686 switch(op) {
@@ -4683,11 +4698,9 @@ void gen_opl(int op) · 11 unmodified lines
4683 func = TOK___umoddi3;4698 func = TOK___umoddi3;
4684 gen_func:4699 gen_func:
4685 /* call generic long long function */4700 /* call generic long long function */
4686 gfunc_start(&gf, FUNC_CDECL);
4687 gfunc_param(&gf);
4688 gfunc_param(&gf);
4689 vpush_global_sym(&func_old_type, func);4701 vpush_global_sym(&func_old_type, func);
4690 gfunc_call(&gf);4702 vrott(3);
4703 gfunc_call(2);
4691 vpushi(0);4704 vpushi(0);
4692 vtop->r = REG_IRET;4705 vtop->r = REG_IRET;
4693 vtop->r2 = REG_LRET;4706 vtop->r2 = REG_LRET;
@@ -5227,20 +5240,17 @@ void gen_op(int op) · 533 unmodified lines
5227/* generic itof for unsigned long long case */5240/* generic itof for unsigned long long case */
5228void gen_cvt_itof1(int t)5241void gen_cvt_itof1(int t)
5229{5242{
5230 GFuncContext gf;
5231
5232 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == 5243 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5233 (VT_LLONG | VT_UNSIGNED)) {5244 (VT_LLONG | VT_UNSIGNED)) {
5234 5245
5235 gfunc_start(&gf, FUNC_CDECL);
5236 gfunc_param(&gf);
5237 if (t == VT_FLOAT)5246 if (t == VT_FLOAT)
5238 vpush_global_sym(&func_old_type, TOK___ulltof);5247 vpush_global_sym(&func_old_type, TOK___ulltof);
5239 else if (t == VT_DOUBLE)5248 else if (t == VT_DOUBLE)
5240 vpush_global_sym(&func_old_type, TOK___ulltod);5249 vpush_global_sym(&func_old_type, TOK___ulltod);
5241 else5250 else
5242 vpush_global_sym(&func_old_type, TOK___ulltold);5251 vpush_global_sym(&func_old_type, TOK___ulltold);
5243 gfunc_call(&gf);5252 vrott(2);
5253 gfunc_call(1);
5244 vpushi(0);5254 vpushi(0);
5245 vtop->r = REG_FRET;5255 vtop->r = REG_FRET;
5246 } else {5256 } else {
@@ -5251,21 +5261,19 @@ void gen_cvt_itof1(int t) · 4 unmodified lines
5251/* generic ftoi for unsigned long long case */5261/* generic ftoi for unsigned long long case */
5252void gen_cvt_ftoi1(int t)5262void gen_cvt_ftoi1(int t)
5253{5263{
5254 GFuncContext gf;
5255 int st;5264 int st;
5256 5265
5257 if (t == (VT_LLONG | VT_UNSIGNED)) {5266 if (t == (VT_LLONG | VT_UNSIGNED)) {
5258 /* not handled natively */5267 /* not handled natively */
5259 gfunc_start(&gf, FUNC_CDECL);
5260 st = vtop->type.t & VT_BTYPE;5268 st = vtop->type.t & VT_BTYPE;
5261 gfunc_param(&gf);
5262 if (st == VT_FLOAT)5269 if (st == VT_FLOAT)
5263 vpush_global_sym(&func_old_type, TOK___fixunssfdi);5270 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5264 else if (st == VT_DOUBLE)5271 else if (st == VT_DOUBLE)
5265 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);5272 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5266 else5273 else
5267 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);5274 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5268 gfunc_call(&gf);5275 vrott(2);
5276 gfunc_call(1);
5269 vpushi(0);5277 vpushi(0);
5270 vtop->r = REG_IRET;5278 vtop->r = REG_IRET;
5271 vtop->r2 = REG_LRET;5279 vtop->r2 = REG_LRET;
@@ -5700,7 +5708,6 @@ static void gen_assign_cast(CType *dt) · 428 unmodified lines
5700void vstore(void)5708void vstore(void)
5701{5709{
5702 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;5710 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5703 GFuncContext gf;
5704 5711
5705 ft = vtop[-1].type.t;5712 ft = vtop[-1].type.t;
5706 sbt = vtop->type.t & VT_BTYPE;5713 sbt = vtop->type.t & VT_BTYPE;
@@ -5720,25 +5727,24 @@ void vstore(void) · 13 unmodified lines
5720 /* structure assignment : generate memcpy */5727 /* structure assignment : generate memcpy */
5721 /* XXX: optimize if small size */5728 /* XXX: optimize if small size */
5722 if (!nocode_wanted) {5729 if (!nocode_wanted) {
5723 vdup();
5724 gfunc_start(&gf, FUNC_CDECL);
5725 /* type size */
5726 size = type_size(&vtop->type, &align);5730 size = type_size(&vtop->type, &align);
5727 vpushi(size);5731
5728 gfunc_param(&gf);5732 vpush_global_sym(&func_old_type, TOK_memcpy);
5729 /* source */5733
5734 /* destination */
5735 vpushv(vtop - 2);
5730 vtop->type.t = VT_INT;5736 vtop->type.t = VT_INT;
5731 gaddrof();5737 gaddrof();
5732 gfunc_param(&gf);5738 /* source */
5733 /* destination */5739 vpushv(vtop - 2);
5734 vswap();
5735 vtop->type.t = VT_INT;5740 vtop->type.t = VT_INT;
5736 gaddrof();5741 gaddrof();
5737 gfunc_param(&gf);5742 /* type size */
5743 vpushi(size);
5744 gfunc_call(3);
5738 5745
5739 save_regs(0);5746 vswap();
5740 vpush_global_sym(&func_old_type, TOK_memcpy);5747 vpop();
5741 gfunc_call(&gf);
5742 } else {5748 } else {
5743 vswap();5749 vswap();
5744 vpop();5750 vpop();
@@ -6436,7 +6442,7 @@ static void indir(void) · 691 unmodified lines
6436}6442}
6437 6443
6438/* pass a parameter to a function and do type checking and casting */6444/* pass a parameter to a function and do type checking and casting */
6439void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)6445static void gfunc_param_typed(Sym *func, Sym *arg)
6440{6446{
6441 int func_type;6447 int func_type;
6442 CType type;6448 CType type;
@@ -6454,11 +6460,6 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 11 unmodified lines
6454 } else {6460 } else {
6455 gen_assign_cast(&arg->type);6461 gen_assign_cast(&arg->type);
6456 }6462 }
6457 if (!nocode_wanted) {
6458 gfunc_param(gf);
6459 } else {
6460 vpop();
6461 }
6462}6463}
6463 6464
6464/* parse an expression of the form '(type)' or '(expr)' and return its6465/* parse an expression of the form '(type)' or '(expr)' and return its
@@ -6489,7 +6490,6 @@ static void unary(void) · 24 unmodified lines
6489 int n, t, align, size, r;6490 int n, t, align, size, r;
6490 CType type;6491 CType type;
6491 Sym *s;6492 Sym *s;
6492 GFuncContext gf;
6493 AttributeDef ad;6493 AttributeDef ad;
6494 6494
6495 /* XXX: GCC 2.95.3 does not generate a table although it should be6495 /* XXX: GCC 2.95.3 does not generate a table although it should be
@@ -6764,6 +6764,7 @@ static void unary(void) · 268 unmodified lines
6764 } else if (tok == '(') {6764 } else if (tok == '(') {
6765 SValue ret;6765 SValue ret;
6766 Sym *sa;6766 Sym *sa;
6767 int nb_args;
6767 6768
6768 /* function call */6769 /* function call */
6769 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {6770 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
@@ -6781,63 +6782,9 @@ static void unary(void) · 11 unmodified lines
6781 }6782 }
6782 /* get return type */6783 /* get return type */
6783 s = vtop->type.ref;6784 s = vtop->type.ref;
6784 if (!nocode_wanted) {
6785 save_regs(0); /* save used temporary registers */
6786 gfunc_start(&gf, s->r);
6787 }
6788 next();6785 next();
6789 sa = s->next; /* first parameter */6786 sa = s->next; /* first parameter */
6790#ifdef INVERT_FUNC_PARAMS6787 nb_args = 0;
6791 {
6792 int parlevel;
6793 Sym *args, *s1;
6794 ParseState saved_parse_state;
6795 TokenString str;
6796
6797 /* read each argument and store it on a stack */
6798 args = NULL;
6799 if (tok != ')') {
6800 for(;;) {
6801 tok_str_new(&str);
6802 parlevel = 0;
6803 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
6804 tok != TOK_EOF) {
6805 if (tok == '(')
6806 parlevel++;
6807 else if (tok == ')')
6808 parlevel--;
6809 tok_str_add_tok(&str);
6810 next();
6811 }
6812 tok_str_add(&str, -1); /* end of file added */
6813 tok_str_add(&str, 0);
6814 s1 = sym_push2(&args, 0, 0, (int)str.str);
6815 s1->next = sa; /* add reference to argument */
6816 if (sa)
6817 sa = sa->next;
6818 if (tok == ')')
6819 break;
6820 skip(',');
6821 }
6822 }
6823
6824 /* now generate code in reverse order by reading the stack */
6825 save_parse_state(&saved_parse_state);
6826 while (args) {
6827 macro_ptr = (int *)args->c;
6828 next();
6829 expr_eq();
6830 if (tok != -1)
6831 expect("',' or ')'");
6832 gfunc_param_typed(&gf, s, args->next);
6833 s1 = args->prev;
6834 tok_str_free((int *)args->c);
6835 tcc_free(args);
6836 args = s1;
6837 }
6838 restore_parse_state(&saved_parse_state);
6839 }
6840#endif
6841 /* compute first implicit argument if a structure is returned */6788 /* compute first implicit argument if a structure is returned */
6842 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {6789 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6843 /* get some space for the returned structure */6790 /* get some space for the returned structure */
@@ -6849,10 +6796,7 @@ static void unary(void) · 5 unmodified lines
6849 problems */6796 problems */
6850 vseti(VT_LOCAL, loc);6797 vseti(VT_LOCAL, loc);
6851 ret.c = vtop->c;6798 ret.c = vtop->c;
6852 if (!nocode_wanted)6799 nb_args++;
6853 gfunc_param(&gf);
6854 else
6855 vtop--;
6856 } else {6800 } else {
6857 ret.type = s->type; 6801 ret.type = s->type;
6858 ret.r2 = VT_CONST;6802 ret.r2 = VT_CONST;
@@ -6866,11 +6810,11 @@ static void unary(void) · 7 unmodified lines
6866 }6810 }
6867 ret.c.i = 0;6811 ret.c.i = 0;
6868 }6812 }
6869#ifndef INVERT_FUNC_PARAMS
6870 if (tok != ')') {6813 if (tok != ')') {
6871 for(;;) {6814 for(;;) {
6872 expr_eq();6815 expr_eq();
6873 gfunc_param_typed(&gf, s, sa);6816 gfunc_param_typed(s, sa);
6817 nb_args++;
6874 if (sa)6818 if (sa)
6875 sa = sa->next;6819 sa = sa->next;
6876 if (tok == ')')6820 if (tok == ')')
@@ -6878,14 +6822,14 @@ static void unary(void) · 1 unmodified line
6878 skip(',');6822 skip(',');
6879 }6823 }
6880 }6824 }
6881#endif
6882 if (sa)6825 if (sa)
6883 error("too few arguments to function");6826 error("too few arguments to function");
6884 skip(')');6827 skip(')');
6885 if (!nocode_wanted)6828 if (!nocode_wanted) {
6886 gfunc_call(&gf);6829 gfunc_call(nb_args);
6887 else6830 } else {
6888 vtop--;6831 vtop -= (nb_args + 1);
6832 }
6889 /* return value */6833 /* return value */
6890 vsetc(&ret.type, ret.r, &ret.c);6834 vsetc(&ret.type, ret.r, &ret.c);
6891 vtop->r2 = ret.r2;6835 vtop->r2 = ret.r2;
@@ -7793,20 +7737,14 @@ static void init_putv(CType *type, Section *sec, unsigned long c, · 901 unmodified lines
7793/* put zeros for variable based init */7737/* put zeros for variable based init */
7794static void init_putz(CType *t, Section *sec, unsigned long c, int size)7738static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7795{7739{
7796 GFuncContext gf;
7797
7798 if (sec) {7740 if (sec) {
7799 /* nothing to do because globals are already set to zero */7741 /* nothing to do because globals are already set to zero */
7800 } else {7742 } else {
7801 gfunc_start(&gf, FUNC_CDECL);
7802 vpushi(size);
7803 gfunc_param(&gf);
7804 vpushi(0);
7805 gfunc_param(&gf);
7806 vseti(VT_LOCAL, c);
7807 gfunc_param(&gf);
7808 vpush_global_sym(&func_old_type, TOK_memset);7743 vpush_global_sym(&func_old_type, TOK_memset);
7809 gfunc_call(&gf);7744 vseti(VT_LOCAL, c);
7745 vpushi(0);
7746 vpushi(size);
7747 gfunc_call(3);
7810 }7748 }
7811}7749}
7812 7750

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