From: Bernhard Urban Date: Sun, 6 Jun 2010 19:14:40 +0000 (+0200) Subject: gesamt: methodenaufrufe der art 'ID(...)' X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=uebersetzerbau-ss10.git;a=commitdiff_plain;h=cba86813c2e4004a1fc73d7888b99998d18c3be7 gesamt: methodenaufrufe der art 'ID(...)' --- diff --git a/gesamt/chelper.c b/gesamt/chelper.c index edef844..448b07b 100644 --- a/gesamt/chelper.c +++ b/gesamt/chelper.c @@ -39,11 +39,12 @@ void func_footer(void) { if(need_stack) printf("\tleave\n"); - printf("\tret\n"); + printf("\tret\n\n\n"); } void move(char *src, char *dst) { + if(src == (char*) NULL) return; if(strcmp(src,dst) != 0) { printf("\tmovq %%%s, %%%s\n", src, dst); } diff --git a/gesamt/code.bfe b/gesamt/code.bfe index 15eb808..8e3dfde 100644 --- a/gesamt/code.bfe +++ b/gesamt/code.bfe @@ -182,16 +182,49 @@ void assign_var(struct treenode *bnode) printf("\tsub $%d, %%%s\n", KIDKID_VAL(0,1), KID_REG(1)); } +void make_call(struct treenode *bnode) +{ + int j; + /* vars pushen */ + for(j = bnode->paramges; j < bnode->paramges + bnode->vars; j++) { + printf("\tpushq %%%s\n", param_reg(j)); + } + printf("\tcall %s\n", bnode->name); + + /* vars poppen */ + for(j = bnode->paramges + bnode->vars - 1; j >= bnode->paramges; j--) { + printf("\tpopq %%%s\n", param_reg(j)); + } + + /* params poppen */ + for(j = bnode->soffset - 1; j >= 0; j--) { + printf("\tpopq %%%s\n", param_reg(j)); + } +} + +void prep_arg(struct treenode *bnode, int moveit) +{ + printf("\t//args-nr.: %%%s to %%%s (moveit: %i)\n", BN_REG, param_reg(bnode->soffset), moveit); + printf("\tpushq %%%s\n", param_reg(bnode->soffset)); + if(moveit) { /* expr */ + move(BN_REG, param_reg(bnode->soffset)); + } else { /* just O_ID */ + KIDREG2PARM(0); + move(KID_REG(0), param_reg(bnode->soffset)); + } +} + %} %start begin -%term O_RET=1 O_NULL=2 O_SUB=3 O_MUL=4 O_OR=5 O_LESS=6 O_EQ=7 O_ID=8 O_ADD=9 O_NUM=10 O_FIELD=11 O_MTWO=12 O_MFOUR=13 O_MEIGHT=14 O_MONE=15 O_ASSIGN=16 O_IF=17 O_BOOL=18 O_CALL=19 +%term O_RET=1 O_NULL=2 O_SUB=3 O_MUL=4 O_OR=5 O_LESS=6 O_EQ=7 O_ID=8 O_ADD=9 O_NUM=10 O_FIELD=11 O_MTWO=12 O_MFOUR=13 O_MEIGHT=14 O_MONE=15 O_ASSIGN=16 O_IF=17 O_BOOL=18 O_CALL=19 O_ARG=20 O_NOTHING=21 %% begin: ret # 0 # printf("\n"); begin: assign # 0 # begin: ifstat # 0 # +begin: args # 0 # assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmovq %%%s, %%%s\n", BN_REG, KID_REG(1)); @@ -219,8 +252,10 @@ expr: O_ID # 0 # expr: imm # 1 # moveimm(BN_VAL, BN_REG); expr: O_BOOL(expr) # 0 # -expr: O_CALL # 0 # printf("\tcall %s\n", bnode->name); - +expr: O_CALL(expr,expr) # 0 # make_call(bnode); +expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1); +expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0); +expr: O_NOTHING # 0 # expr: O_SUB(expr,expr) # 2 # gen_e_eno(bnode, "subq"); expr: O_SUB(expr,O_FIELD(expr)) # 2 # gen_e_field(bnode, "subq"); diff --git a/gesamt/parser.y b/gesamt/parser.y index 9c1309d..052e67f 100644 --- a/gesamt/parser.y +++ b/gesamt/parser.y @@ -40,7 +40,7 @@ * sin: symboltabelle fuer statement ("eingabe") * sout: symboltabelle die aus einem statement wieder rauskommt ("ausgabe") */ -@autoinh s gparamges +@autoinh s gparamges vars_in @autosyn node imm call @attributes { char *name; } IDENT @@ -49,11 +49,11 @@ @attributes { struct symbol *f; } Program Structdef; @attributes { struct symbol *f; int offsetcount; } FeldID @attributes { struct symbol *s; } Methoddef -@attributes { struct symbol *s; int gparamges; } Exprs +@attributes { struct symbol *s; int gparamges; int vars_in; int paramcount; struct treenode *node; } Exprs @attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; int vars_in; int vars_out; int call; } Statseq @attributes { struct symbol *s; int gparamges; int lblcnt_in; int lblcnt_out; int reallblcnt; int vars_in; int vars_out; int call; } Elsestat -@attributes { struct symbol *s; int gparamges; struct treenode *node; short imm; int call; } Expr Minusterm Multerm Orterm Feld Term -@attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr +@attributes { struct symbol *s; int gparamges; int vars_in; struct treenode *node; short imm; int call; } Expr Minusterm Multerm Orterm Feld Term +@attributes { struct symbol *s; int gparamges; int vars_in; struct treenode *node; } Lexpr @attributes { struct symbol *sin; int gparamges; struct symbol *sout; struct treenode *node; int vars_in; int vars_out; int lblcnt_in; int lblcnt_out; int call; } Statement @traversal @postorder c @@ -556,9 +556,15 @@ Term: | IDENT '(' Exprs ')' @{ - @i @Term.node@ = new_call(@IDENT.name@, TREENULL, TREENULL); + /* TODO: soffset sollte eigentlich anzahl des paramcounts in Exprs + vars die existieren sein */ + @i { + @Term.node@ = new_call(@IDENT.name@, new_arg(new_nothing(), new_nothing(), 0) /*this*/, + @Exprs.node@, @Term.gparamges@, @Term.vars_in@); + @Term.node@->soffset = @Exprs.paramcount@; + } @i @Term.imm@ = 0; @i @Term.call@ = 1; + @reg @Exprs.node@->reg = @Term.node@->reg; @} | Term '.' IDENT '(' Exprs ')' @@ -572,8 +578,22 @@ Term: Exprs: Expr ',' Exprs + @{ + @i @Exprs.0.node@ = new_arg(@Expr.0.node@, @Exprs.1.node@, @Exprs.1.paramcount@); + @i @Exprs.0.paramcount@ = @Exprs.1.paramcount@ + 1; + @reg @Expr.node@->reg = @Exprs.1.node@->reg = @Exprs.0.node@->reg; + @} | Expr + @{ + @i @Exprs.0.node@ = new_arg(@Expr.0.node@, new_nothing(), 1); + @i @Exprs.paramcount@ = 2; + @reg @Expr.node@->reg = @Exprs.0.node@->reg; + @} | + @{ + @i @Exprs.0.node@ = new_nothing(); + @i @Exprs.paramcount@ = 1; + @} ; %% diff --git a/gesamt/tree.c b/gesamt/tree.c index 4dcf0d7..7d70718 100644 --- a/gesamt/tree.c +++ b/gesamt/tree.c @@ -15,11 +15,16 @@ static struct treenode *_new_plain(int op) new->parent = TREENULL; new->label = NULL; new->name = new->reg = (char *)NULL; - new->val = 0; + new->val = new->paramges = new->vars = 0; new->param_index = -1; return new; } +struct treenode *new_nothing(void) +{ + return new_node(O_NOTHING, TREENULL, TREENULL); +} + struct treenode *new_node(int op, struct treenode *l, struct treenode *r) { struct treenode *new = TREENULL; @@ -63,7 +68,20 @@ struct treenode *new_field(char *name, struct treenode *l, struct treenode *r, i return new; } -struct treenode *new_call(char *name, struct treenode *l, struct treenode *r) +struct treenode *new_arg(struct treenode *l, struct treenode *r, int soffset) +{ + struct treenode *new = new_node(O_ARG, l, r); + +#ifdef DDTREE + fprintf(stderr, "new_arg: %i (soffset)\n", soffset); +#endif + new->soffset = soffset; + new->name = (char *) malloc(10); + sprintf(new->name, "%i", soffset); + return new; +} + +struct treenode *new_call(char *name, struct treenode *l, struct treenode *r, int paramges, int vars) { struct treenode *new = new_node(O_CALL, l, r); @@ -71,6 +89,8 @@ struct treenode *new_call(char *name, struct treenode *l, struct treenode *r) fprintf(stderr, "new_call\n"); #endif new->name = name; + new->paramges = paramges; + new->vars = vars; return new; } diff --git a/gesamt/tree.h b/gesamt/tree.h index b99b8d2..a6d52c4 100644 --- a/gesamt/tree.h +++ b/gesamt/tree.h @@ -13,14 +13,16 @@ enum { O_RET=1, O_NULL, O_SUB, O_MUL, O_OR=5, O_LESS, O_EQ, O_ID, O_ADD, O_NUM=10, O_FIELD, O_MTWO, O_MFOUR, O_MEIGHT, - O_MONE=15, O_ASSIGN, O_IF, O_BOOL, O_CALL + O_MONE=15, O_ASSIGN, O_IF, O_BOOL, O_CALL, + O_ARG=20, O_NOTHING }; static char *o_names[] = { "", "O_RET", "O_NULL", "O_SUB", "O_MUL", "O_OR", "O_LESS", "O_EQ", "O_ID", "O_ADD", "O_NUM", "O_FIELD", "O_MTWO", "O_MFOUR", "O_MEIGHT", - "O_MONE", "O_ASSIGN", "O_IF", "O_BOOL", "O_CALL" + "O_MONE", "O_ASSIGN", "O_IF", "O_BOOL", "O_CALL", + "O_ARG", "O_NOTHING" }; struct treenode { @@ -33,6 +35,8 @@ struct treenode { struct treenode *parent; int param_index; int soffset; + int paramges; + int vars; }; typedef struct treenode *treenodep; @@ -48,7 +52,9 @@ struct treenode *new_node(int op, struct treenode *l, struct treenode *r); struct treenode *new_number(long val); struct treenode *new_param(int op, char *name, struct treenode *l, struct treenode *r, int param_index); struct treenode *new_field(char *name, struct treenode *l, struct treenode *r, int soffset); -struct treenode *new_call(char *name, struct treenode *l, struct treenode *r); +struct treenode *new_call(char *name, struct treenode *l, struct treenode *r, int paramges, int vars); +struct treenode *new_arg(struct treenode *l, struct treenode *r, int soffset); +struct treenode *new_nothing(void); void write_tree(struct treenode *node, int ident); #endif