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
7373 /* relocation type for 32 bit data relocation */
7474 #define R_DATA_32 R_386_32
7575
76−/* function call context */
77−typedef struct GFuncContext {
78− int args_size;
79− int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
80−} GFuncContext;
81−
8276 /******************************************************/
8377
8478 static unsigned long func_sub_sp_offset;
@@ -282,65 +276,6 @@ void store(int r, SValue *v) · 197 unmodified lines
282276 }
283277 }
284278
285−/* start function call and return function call context */
286−void 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. */
294−void 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−
344279 static void gadd_sp(int val)
345280 {
346281 if (val == (char)val) {
@@ -375,13 +310,66 @@ static void gcall_or_jmp(int is_jmp) · 28 unmodified lines
375310 }
376311 }
377312
378−/* generate function call with address in (vtop->t, vtop->c) and free function
379− context. Stack entry is popped */
380−void gfunc_call(GFuncContext *c)
313+/* Generate function call. The function address is pushed first, then
314+ all the parameters in call order. This functions pops all the
315+ parameters and the function address. */
316+void gfunc_call(int nb_args)
381317 {
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;
382370 gcall_or_jmp(0);
383− if (c->args_size && c->func_call == FUNC_CDECL)
384− gadd_sp(c->args_size);
371+ if (args_size && func_sym->r == FUNC_CDECL)
372+ gadd_sp(args_size);
385373 vtop--;
386374 }
387375
tcc.c+50−112
@@ -4589,7 +4589,9 @@ void lbuild(int t) · 4588 unmodified lines
45894589 vpop();
45904590 }
45914591
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+*/
45934595 void vrotb(int n)
45944596 {
45954597 int i;
@@ -4601,6 +4603,20 @@ void vrotb(int n) · 5 unmodified lines
46014603 vtop[0] = tmp;
46024604 }
46034605
4606+/* rotate n first stack elements to the top
4607+ I1 ... In -> In I1 ... I(n-1) [top is right]
4608+ */
4609+void 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+
46044620 /* pop stack value */
46054621 void vpop(void)
46064622 {
@@ -4665,7 +4681,6 @@ void gen_opl(int op) · 58 unmodified lines
46654681 {
46664682 int t, a, b, op1, c, i;
46674683 int func;
4668− GFuncContext gf;
46694684 SValue tmp;
46704685
46714686 switch(op) {
@@ -4683,11 +4698,9 @@ void gen_opl(int op) · 11 unmodified lines
46834698 func = TOK___umoddi3;
46844699 gen_func:
46854700 /* call generic long long function */
4686− gfunc_start(&gf, FUNC_CDECL);
4687− gfunc_param(&gf);
4688− gfunc_param(&gf);
46894701 vpush_global_sym(&func_old_type, func);
4690− gfunc_call(&gf);
4702+ vrott(3);
4703+ gfunc_call(2);
46914704 vpushi(0);
46924705 vtop->r = REG_IRET;
46934706 vtop->r2 = REG_LRET;
@@ -5227,20 +5240,17 @@ void gen_op(int op) · 533 unmodified lines
52275240 /* generic itof for unsigned long long case */
52285241 void gen_cvt_itof1(int t)
52295242 {
5230− GFuncContext gf;
5231−
52325243 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
52335244 (VT_LLONG | VT_UNSIGNED)) {
52345245
5235− gfunc_start(&gf, FUNC_CDECL);
5236− gfunc_param(&gf);
52375246 if (t == VT_FLOAT)
52385247 vpush_global_sym(&func_old_type, TOK___ulltof);
52395248 else if (t == VT_DOUBLE)
52405249 vpush_global_sym(&func_old_type, TOK___ulltod);
52415250 else
52425251 vpush_global_sym(&func_old_type, TOK___ulltold);
5243− gfunc_call(&gf);
5252+ vrott(2);
5253+ gfunc_call(1);
52445254 vpushi(0);
52455255 vtop->r = REG_FRET;
52465256 } else {
@@ -5251,21 +5261,19 @@ void gen_cvt_itof1(int t) · 4 unmodified lines
52515261 /* generic ftoi for unsigned long long case */
52525262 void gen_cvt_ftoi1(int t)
52535263 {
5254− GFuncContext gf;
52555264 int st;
52565265
52575266 if (t == (VT_LLONG | VT_UNSIGNED)) {
52585267 /* not handled natively */
5259− gfunc_start(&gf, FUNC_CDECL);
52605268 st = vtop->type.t & VT_BTYPE;
5261− gfunc_param(&gf);
52625269 if (st == VT_FLOAT)
52635270 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
52645271 else if (st == VT_DOUBLE)
52655272 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
52665273 else
52675274 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5268− gfunc_call(&gf);
5275+ vrott(2);
5276+ gfunc_call(1);
52695277 vpushi(0);
52705278 vtop->r = REG_IRET;
52715279 vtop->r2 = REG_LRET;
@@ -5700,7 +5708,6 @@ static void gen_assign_cast(CType *dt) · 428 unmodified lines
57005708 void vstore(void)
57015709 {
57025710 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5703− GFuncContext gf;
57045711
57055712 ft = vtop[-1].type.t;
57065713 sbt = vtop->type.t & VT_BTYPE;
@@ -5720,25 +5727,24 @@ void vstore(void) · 13 unmodified lines
57205727 /* structure assignment : generate memcpy */
57215728 /* XXX: optimize if small size */
57225729 if (!nocode_wanted) {
5723− vdup();
5724− gfunc_start(&gf, FUNC_CDECL);
5725− /* type size */
57265730 size = type_size(&vtop->type, &align);
5727− vpushi(size);
5728− gfunc_param(&gf);
5729− /* source */
5731+
5732+ vpush_global_sym(&func_old_type, TOK_memcpy);
5733+
5734+ /* destination */
5735+ vpushv(vtop - 2);
57305736 vtop->type.t = VT_INT;
57315737 gaddrof();
5732− gfunc_param(&gf);
5733− /* destination */
5734− vswap();
5738+ /* source */
5739+ vpushv(vtop - 2);
57355740 vtop->type.t = VT_INT;
57365741 gaddrof();
5737− gfunc_param(&gf);
5742+ /* type size */
5743+ vpushi(size);
5744+ gfunc_call(3);
57385745
5739− save_regs(0);
5740− vpush_global_sym(&func_old_type, TOK_memcpy);
5741− gfunc_call(&gf);
5746+ vswap();
5747+ vpop();
57425748 } else {
57435749 vswap();
57445750 vpop();
@@ -6436,7 +6442,7 @@ static void indir(void) · 691 unmodified lines
64366442 }
64376443
64386444 /* pass a parameter to a function and do type checking and casting */
6439−void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
6445+static void gfunc_param_typed(Sym *func, Sym *arg)
64406446 {
64416447 int func_type;
64426448 CType type;
@@ -6454,11 +6460,6 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 11 unmodified lines
64546460 } else {
64556461 gen_assign_cast(&arg->type);
64566462 }
6457− if (!nocode_wanted) {
6458− gfunc_param(gf);
6459− } else {
6460− vpop();
6461− }
64626463 }
64636464
64646465 /* parse an expression of the form '(type)' or '(expr)' and return its
@@ -6489,7 +6490,6 @@ static void unary(void) · 24 unmodified lines
64896490 int n, t, align, size, r;
64906491 CType type;
64916492 Sym *s;
6492− GFuncContext gf;
64936493 AttributeDef ad;
64946494
64956495 /* 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
67646764 } else if (tok == '(') {
67656765 SValue ret;
67666766 Sym *sa;
6767+ int nb_args;
67676768
67686769 /* function call */
67696770 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
@@ -6781,63 +6782,9 @@ static void unary(void) · 11 unmodified lines
67816782 }
67826783 /* get return type */
67836784 s = vtop->type.ref;
6784− if (!nocode_wanted) {
6785− save_regs(0); /* save used temporary registers */
6786− gfunc_start(&gf, s->r);
6787− }
67886785 next();
67896786 sa = s->next; /* first parameter */
6790−#ifdef INVERT_FUNC_PARAMS
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
6787+ nb_args = 0;
68416788 /* compute first implicit argument if a structure is returned */
68426789 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
68436790 /* get some space for the returned structure */
@@ -6849,10 +6796,7 @@ static void unary(void) · 5 unmodified lines
68496796 problems */
68506797 vseti(VT_LOCAL, loc);
68516798 ret.c = vtop->c;
6852− if (!nocode_wanted)
6853− gfunc_param(&gf);
6854− else
6855− vtop--;
6799+ nb_args++;
68566800 } else {
68576801 ret.type = s->type;
68586802 ret.r2 = VT_CONST;
@@ -6866,11 +6810,11 @@ static void unary(void) · 7 unmodified lines
68666810 }
68676811 ret.c.i = 0;
68686812 }
6869−#ifndef INVERT_FUNC_PARAMS
68706813 if (tok != ')') {
68716814 for(;;) {
68726815 expr_eq();
6873− gfunc_param_typed(&gf, s, sa);
6816+ gfunc_param_typed(s, sa);
6817+ nb_args++;
68746818 if (sa)
68756819 sa = sa->next;
68766820 if (tok == ')')
@@ -6878,14 +6822,14 @@ static void unary(void) · 1 unmodified line
68786822 skip(',');
68796823 }
68806824 }
6881−#endif
68826825 if (sa)
68836826 error("too few arguments to function");
68846827 skip(')');
6885− if (!nocode_wanted)
6886− gfunc_call(&gf);
6887− else
6888− vtop--;
6828+ if (!nocode_wanted) {
6829+ gfunc_call(nb_args);
6830+ } else {
6831+ vtop -= (nb_args + 1);
6832+ }
68896833 /* return value */
68906834 vsetc(&ret.type, ret.r, &ret.c);
68916835 vtop->r2 = ret.r2;
@@ -7793,20 +7737,14 @@ static void init_putv(CType *type, Section *sec, unsigned long c, · 901 unmodified lines
77937737 /* put zeros for variable based init */
77947738 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
77957739 {
7796− GFuncContext gf;
7797−
77987740 if (sec) {
77997741 /* nothing to do because globals are already set to zero */
78007742 } 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);
78087743 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);
78107748 }
78117749 }
78127750

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