gesamt: methodenaufrufe der art 'ID(...)'
authorBernhard Urban <lewurm@gmail.com>
Sun, 6 Jun 2010 19:14:40 +0000 (21:14 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sun, 6 Jun 2010 19:14:40 +0000 (21:14 +0200)
gesamt/chelper.c
gesamt/code.bfe
gesamt/parser.y
gesamt/tree.c
gesamt/tree.h

index edef8449a52f5522147691e48a72578faa50dd2e..448b07b2d47e76cf5c8bd85a5fdf9f809cc94568 100644 (file)
@@ -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);
        }
index 15eb808460cc8514a40a7b70e769c88cafad0885..8e3dfde8eb1444485cf2824b265d089798c4236f 100644 (file)
@@ -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");
index 9c1309de18678d78a85eac4605db75eef34ba2db..052e67faa178644c8da3d9724ea5699696259665 100644 (file)
@@ -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
 @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;
+         @}
        ;
 %%
 
index 4dcf0d76361f91f5efc5da5f6bd026341a6febb5..7d70718c5e748ea1894630bb2ff738181eda3579 100644 (file)
@@ -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;
 }
 
index b99b8d2a251b40806cbbd799943f36830ddde3e9..a6d52c4d1937c7948ff0584fc5961d4073e0c314 100644 (file)
@@ -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