gesamt: noch zwei optimierungen ->
[uebersetzerbau-ss10.git] / gesamt / code.bfe
index fb0044768de8a55751b29e1b723b739efa7faa0d..cad2d58c65c320dadee096a663bed538cc3efe1a 100644 (file)
@@ -187,11 +187,11 @@ void assign_var(struct treenode *bnode)
        printf("\tsub $%d, %%%s\n", KIDKID_VAL(0,1), KID_REG(1));
 }
 
-void make_call(struct treenode *bnode)
+void make_call(struct treenode *bnode, short skippop)
 {
        short *sc = bnode->sc;
        int j, off = 0;
-       printf("\t//params pushen\n");
+       printf("\t//params pushen (skippop: %d)\n", skippop);
        for(j = 0; j < bnode->soffset; j++) {
                if(sc[j] == 1)
                        printf("\txchg %%%s, %d(%%rsp)\n", param_reg(j), (j - off)*8);
@@ -218,11 +218,15 @@ void make_call(struct treenode *bnode)
        printf("\tcall %s\n", bnode->name);
        if(strcmp(BN_REG, "rax")!=0) {
                move("rax", BN_REG);
+               if(skippop)
+                       goto cleanup;
                if(strcmp(BN_REG, "r10")!=0) {
                        printf("\tpopq %%r10\n");
                }
                printf("\tpopq %%rax\n");
        }
+       if(skippop)
+               goto cleanup;
 
        printf("\t//vars poppen\n");
        for(j = bnode->soffset + bnode->vars - 1; j > bnode->soffset - 1; j--) {
@@ -240,6 +244,7 @@ void make_call(struct treenode *bnode)
                        printf("\tpopq %%%s\n", param_reg(j));
        }
 
+cleanup:
        /* clear stack_control array */
        for(j = 0; j < sizeof sc / sizeof sc[0]; j++)
                sc[j] = 0;
@@ -281,6 +286,8 @@ void gen_sub_field(struct treenode *bnode)
        printf("\tsub $%d, %d(%%%s)\n", bnode->kids[0]->kids[1]->val, bnode->kids[1]->soffset * 8, KIDKID_REG(1,0));
 }
 
+int was_already_in_bfe = 0;
+
 %}
 
 %start begin
@@ -288,10 +295,12 @@ void gen_sub_field(struct treenode *bnode)
 
 %%
 
-begin: ret # 0 # printf("\n");
-begin: assign # 0 #
-begin: ifstat # 0 #
-begin: expr # 0 #
+begin: ret # 0 # printf("\n"); was_already_in_bfe = 1;
+begin: assign # 0 # was_already_in_bfe = 1;
+begin: ifstat # 0 # was_already_in_bfe = 1;
+begin: expr # 0 # was_already_in_bfe = 1;
+begin: O_RET(O_CALL(O_NOTHING, O_ARG(O_NOTHING, O_NOTHING))) # 1 # if(was_already_in_bfe == 0) { printf("\tjmp %s\n", bnode->kids[0]->name); } else { make_call(bnode->kids[0], 1); } was_already_in_bfe = 1;
+begin: O_RET(O_CALL(expr,expr)) # 1 # make_call(bnode->kids[0], 1); was_already_in_bfe = 1;
 
 
 assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmovq %%%s, %%%s\n", BN_REG, KID_REG(1));
@@ -321,7 +330,7 @@ expr: O_ID # 0 #
 expr: imm # 1 # moveimm(BN_VAL, BN_REG);
 expr: O_BOOL(expr) # 0 #
 
-expr: O_CALL(expr,expr) # 0 # make_call(bnode);
+expr: O_CALL(expr,expr) # 2 # make_call(bnode, 0);
 expr: O_ARG(expr,expr) # 1 # prep_arg(bnode, 1);
 expr: O_ARG(O_ID,expr) # 1 # prep_arg(bnode, 0);
 expr: O_ARG(expr,O_ID) # 1 # prep_arg(bnode, 2);