arm: init from ppc
[uebersetzerbau-ss10.git] / gesamt_arm / code.bfe
diff --git a/gesamt_arm/code.bfe b/gesamt_arm/code.bfe
new file mode 100644 (file)
index 0000000..f97b517
--- /dev/null
@@ -0,0 +1,392 @@
+%{
+#define BFEHAX
+
+/* macros zum registerzugriff bei kinder */
+#define KID_REG(A) bnode->kids[A]->reg
+#define KIDKID_REG(A,B) bnode->kids[A]->kids[B]->reg
+#define KIDKIDKID_REG(A,B,C) bnode->kids[A]->kids[B]->kids[C]->reg
+
+/* macros zum wertezugriff bei kindern */
+#define KID_VAL(A) bnode->kids[A]->val
+#define KIDKID_VAL(A,B) bnode->kids[A]->kids[B]->val
+#define KIDKIDKID_VAL(A,B,C) bnode->kids[A]->kids[B]->kids[C]->val
+
+#define KID_PARM(A) bnode->kids[A]->param_index
+#define KIDKID_PARM(A,B) bnode->kids[A]->kids[B]->param_index
+#define KIDKIDKID_PARM(A,B,C) bnode->kids[A]->kids[B]->kids[C]->param_index
+
+/* macros zum zugriff des aktuellen knotens */
+#define BN_REG bnode->reg
+#define BN_VAL bnode->val
+
+/* wenn sich ein parameter auf der "leseseite" (also links bei at&t syntax)
+ * befindet, dann soll dieses register verwendet werden */
+#define KIDREG2PARM(A) if(bnode->kids[A]->param_index > -1) { bnode->kids[A]->reg = param_reg(bnode->kids[A]->param_index); }
+#define KIDKIDREG2PARM(A,B) if(bnode->kids[A]->kids[B]->param_index > -1) { bnode->kids[A]->kids[B]->reg = param_reg(bnode->kids[A]->kids[B]->param_index); }
+#define KIDKIDKIDREG2PARM(A,B,C) if(bnode->kids[A]->kids[B]->kids[C]->param_index > -1) { bnode->kids[A]->kids[B]->kids[C]->reg = param_reg(bnode->kids[A]->kids[B]->kids[C]->param_index); }
+
+/* wenn sich ein parameter auf der "schreibeseite" befindet (also rechts bei
+ * at&t syntax), dann muss es vorher in ein temporaeres register gemovt werden */
+#define KIDREG2ID(A) if(bnode->kids[A]->op == O_ID && bnode->kids[A]->param_index > -1) move(param_reg(bnode->kids[A]->param_index), bnode->kids[A]->reg);
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include "tree.h"
+#include "chelper.h"
+
+void gen_e_eno(struct treenode *bnode, char *instr)
+{
+       printf("\t#gen_e_eno(%s)\n", instr);
+       KIDREG2PARM(0);
+       KIDREG2PARM(1);
+       printf("\t%s %s,%s,%s\n", instr, BN_REG, KID_REG(0), KID_REG(1));
+}
+
+void gen_id_eno(struct treenode *bnode)
+{
+       printf("\t#gen_id_eno\n");
+       KIDKIDREG2PARM(1,0);
+       KIDKIDREG2PARM(1,1);
+       KIDREG2PARM(0);
+
+       printf("\tsub %s,%s,%s\n", BN_REG, KID_REG(0), KIDKID_REG(1,1));
+       printf("\tsub %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,0));
+}
+
+void gen_e_imm(struct treenode *bnode, char *instr)
+{
+       printf("\t#gen_e_imm(%s)\n", instr);
+       KIDREG2PARM(0);
+       KIDREG2ID(1);
+       /* man kann sich ein move der konstante bei der multiplikation ersparen */
+       if(strcmp(instr, "mullw") == 0) {
+               if(KID_VAL(1) == 1 && strcmp(KID_REG(0), BN_REG) == 0) {
+                       printf("\t#multiplikation mit 1 wegoptimiert\n");
+               } else {
+                       if(KID_VAL(1) > (65536)-1 || KID_VAL(1) < -65536) {
+                               moveimm(KID_VAL(1), next_reg(BN_REG,0));
+                               printf("\tmullw %s,%s,%s\n", BN_REG, KID_REG(0), next_reg(BN_REG,0));
+                       } else {
+                               printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(0), KID_VAL(1));
+                       }
+               }
+       } else {
+               if(strcmp(instr, "sub") == 0 && KID_VAL(1) == 0) {
+                       printf("\t#subtraktion mit 0 wegoptimiert\n");
+                       move(KID_REG(0), BN_REG);
+               } else {
+                       if(KID_VAL(1) > (65536)-1 || KID_VAL(1) < -65536) {
+                               moveimm(KID_VAL(1), next_reg(BN_REG,0));
+                               printf("\t%s %s,%s,%s\n", instr, BN_REG, KID_REG(0), next_reg(BN_REG,0));
+                       } else {
+                               printf("\t%si %s,%s,%d\n", instr, BN_REG, KID_REG(0), KID_VAL(1));
+                       }
+               }
+       }
+}
+
+void gen_imm_eno(struct treenode *bnode, char *instr)
+{
+       printf("\t#gen_imm_eno(%s)\n", instr);
+       KIDREG2ID(0);
+       KIDREG2PARM(1);
+       /* man kann sich ein move der konstante bei der multiplikation ersparen */
+       if(strcmp(instr, "mullw") == 0) {
+               if(KID_VAL(0) == 1 && strcmp(KID_REG(1), BN_REG) == 0) {
+                       printf("\t#multiplikation mit 1 wegoptimiert\n");
+               } else {
+                       if(KID_VAL(0) > (65536)-1 || KID_VAL(0) < -65536) {
+                               moveimm(KID_VAL(0), next_reg(BN_REG,0));
+                               printf("\tmullw %s,%s,%s\n", BN_REG, KID_REG(1), next_reg(BN_REG,0));
+                       } else {
+                               printf("\tmulli %s,%s,%d\n", BN_REG, KID_REG(1), KID_VAL(0));
+                       }
+               }
+       } else { /* addq */
+               /* TODO: imm check einbauen */
+               printf("\taddi %s,%s,%d\n", BN_REG, KID_REG(1), KID_VAL(0));
+       }
+}
+
+void gen_eqless(struct treenode *bnode, char *op, short e0, short e1, short deep)
+{
+       printf("\t#gen_eqless_%i%i @ %i (op: %s)\n", e0, e1, deep, op);
+#if 0
+       if(e0) { KIDREG2PARM(0); } else { KIDREG2ID(0); }
+       if(e1) { KIDREG2PARM(1); } else { KIDREG2ID(1); }
+
+       if(e0 && e1) {
+               if(deep) {
+                       KIDKIDREG2PARM(1,0);
+                       printf("\tcmp %d(%%%s), %%%s\n", bnode->kids[1]->soffset *8, KIDKID_REG(1,0), KID_REG(0));
+               } else {
+                       printf("\tcmp %%%s, %%%s\n", KID_REG(1), KID_REG(0));
+               }
+       } else if(e0 && !e1) {
+               if (deep == 0) {
+                       printf("\tcmp $%d, %%%s\n", KID_VAL(1), KID_REG(0));
+               } else if (deep == 1) {
+                       KIDKIDREG2PARM(0,0);
+                       printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKID_REG(0,0));
+               } else if (deep == 2) {
+                       KIDKIDKIDREG2PARM(0,0,0);
+                       printf("\tcmp $%d, %%%s\n", KID_VAL(1), KIDKIDKID_REG(0,0,0));
+               }
+       } else if(!e0 && e1) {
+                       printf("\tcmp $%d, %%%s\n", KID_VAL(0), KID_REG(1));
+       }
+       printf("\tset%s %%%s\n", op, reg_64to8l(BN_REG));
+       printf("\tand $1, %%%s\n", BN_REG);
+#else
+       if(e0) { KIDREG2PARM(0); } else { moveimm(KID_VAL(0), BN_REG); }
+       if(e1) { KIDREG2PARM(1); }
+       if(strcmp(op,"e")==0 && bnode->kids[1]->op == O_NULL) {
+               /* not */
+               printf("\tcntlzw %s,%s\n", KID_REG(0), KID_REG(0));
+               printf("\tsrwi %s,%s,5\n", BN_REG, KID_REG(0));
+       } else {
+               if(!e1) {
+                       moveimm(KID_VAL(1), KID_REG(1));
+               }
+               if(strcmp(op, "e")==0) {
+                       /* eq */
+                       printf("\txor %s,%s,%s\n", BN_REG, KID_REG(0), KID_REG(1));
+                       printf("\tcntlzw %s,%s\n", BN_REG, BN_REG);
+                       printf("\tsrwi %s,%s,5\n", BN_REG, BN_REG);
+               } else if(strcmp(op, "l")==0 || strcmp(op, "g")==0) {
+                       /* less */
+                       printf("\tcmpw 7,%s,%s\n", KID_REG(1), KID_REG(0));
+                       printf("\tmfcr %s\n", BN_REG);
+                       /* EQ, GT, LT */
+                       /* um (32-29)=3 nach rechts shiften und das LSB anschauen */
+                       printf("\trlwinm %s,%s,%i,31,31\n", BN_REG, BN_REG, strcmp(op,"l")==0 ? 30 : 30);
+               }
+       }
+       /* vergleich mit null und in CR0 speichern */
+       printf("\tcmpwi %s,0\n", BN_REG);
+#endif
+}
+
+void gen_subspecial(struct treenode *bnode, short e)
+{
+       /* tritt z.b. bei snafu_05.0 auf */
+       printf("\t#gen_subspecial(%i)\n", e);
+       KIDREG2ID(0);
+       KIDKIDREG2PARM(1,0);
+
+       /* TODO: Loong@ codea_snafu_03.0 */
+
+       if(e) {
+               if(KIDKID_VAL(1,0) != 0) {
+                       printf("\tsubi %s,%s,%d\n", BN_REG, BN_REG, KIDKID_VAL(1,0));
+               }
+       } else {
+               printf("\tsub %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,0));
+       }
+       if(e) KIDKIDREG2PARM(1,1);
+       printf("\tadd %s,%s,%s\n", BN_REG, BN_REG, KIDKID_REG(1,1));
+}
+
+void assign_var(struct treenode *bnode)
+{
+       printf("\t#assign_var\n");
+       KIDREG2PARM(1);
+       if (strcmp(bnode->kids[0]->kids[0]->name, bnode->kids[1]->name) != 0) {
+               KIDKIDREG2PARM(0,0);
+               printf("\tmr %s,%s\n", KID_REG(1), KIDKID_REG(0,0));
+       } /*else:  x := x - 1 geht in einem befehl */
+       printf("\tsubi %s,%s,%d\n", KID_REG(1), KID_REG(1), KIDKID_VAL(0,1));
+}
+
+/* ... dirty */
+static short sc[8] = {0};
+void make_call(struct treenode *bnode)
+{
+       int j;
+       printf("\t#params pushen\n");
+       for(j = 0; j < bnode->soffset; j++) {
+               if(sc[j] == 1) {
+                       printf("\tlwz 20,%d(1)\n", j*4);
+                       printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
+                       printf("\tmr %s,20\n", param_reg(j));
+               } else if (sc[j] == 0) {
+                       printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
+               }
+       }
+       printf("\t#vars pushen\n");
+       for(j = bnode->soffset; j < bnode->soffset + bnode->vars; j++) {
+               printf("\tstw %s,%d(1)\n", param_reg(j), j*4);
+       }
+
+       /* TODO: schoener machen... */
+       if(strcmp(BN_REG, "14")!=0) {
+               printf("\t#tmp register pushen\n");
+               printf("\tstw 14,52(1)\n");
+               if(strcmp(BN_REG, "15")!=0) {
+                       printf("\tstw 15,56(1)\n");
+                       if(strcmp(BN_REG, "16")!=0) {
+                               printf("\tstw 16,60(1)\n");
+                       }
+               }
+       }
+
+       printf("\tbl %s\n", bnode->name);
+       move("3", BN_REG);
+
+       if(strcmp(BN_REG, "14")!=0) {
+               printf("\t#tmp register poppen\n");
+               if(strcmp(BN_REG, "15")!=0) {
+                       if(strcmp(BN_REG, "16")!=0) {
+                               printf("\tlwz 16,60(1)\n");
+                       }
+                       printf("\tlwz 15,56(1)\n");
+               }
+               printf("\tlwz 14,52(1)\n");
+       }
+
+       printf("\t#vars poppen\n");
+       for(j = bnode->soffset + bnode->vars - 1; j > bnode->soffset - 1; j--) {
+               printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
+       }
+
+       printf("\t#params poppen\n");
+       for(j = bnode->soffset - 1; j >= 0; j--) {
+               if(sc[j] == 0)
+                       printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
+       }
+       for(j = 0; j < bnode->soffset; j++) {
+               if(sc[j] > 0)
+                       printf("\tlwz %s,%d(1)\n", param_reg(j), j*4);
+       }
+
+       /* clear stack control array */
+       for(j = 0; j < sizeof sc / sizeof sc[0]; j++)
+               sc[j] = 0;
+}
+
+void prep_arg(struct treenode *bnode, int moveit)
+{
+       printf("\t#args-nr-> %i (%%%s) [moveit= %i]\n", bnode->soffset, param_reg(bnode->soffset), moveit);
+       sc[bnode->soffset] = 1;
+       if(moveit) { /* expr */
+               if((BN_REG == (char *) NULL) || (bnode->kids[1] != TREENULL && bnode->kids[1]->op == O_ID && bnode->kids[1]->kids[0] == TREENULL && bnode->kids[1]->kids[1] == TREENULL)) {
+                       if(bnode->kids[1]->name != (char *) NULL && strcmp(bnode->kids[1]->name,"this")!=0) {
+                               KIDREG2PARM(1);
+                               printf("\tstw %s,%d(1)\n", KID_REG(1),bnode->soffset*4);
+                       } else {
+                               printf("\tstw %s,%d(1)\n", param_reg(bnode->soffset), bnode->soffset*4);
+                               sc[bnode->soffset] = 2;
+                       }
+               } else {
+                       printf("\tstw %s,%d(1)\n", BN_REG, bnode->soffset*4);
+               }
+       } else { /* just O_ID */
+               KIDREG2PARM(0);
+               printf("\tstw %s,%d(1)\n", KID_REG(0), bnode->soffset*4);
+       }
+}
+
+%}
+
+%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 O_ARG=20 O_NOTHING=21 O_EXPR=22
+
+%%
+
+begin: ret # 0 #
+begin: assign # 0 #
+begin: ifstat # 0 #
+begin: args # 0 #
+
+
+assign: O_ASSIGN(expr, O_ID) # 1 # KIDREG2PARM(1); printf("\tmr %s,%s\n", KID_REG(1), BN_REG);
+assign: O_ASSIGN(imm, O_ID) # 1 # KIDREG2PARM(1); moveimm(KID_VAL(0), KID_REG(1));
+assign: O_ASSIGN(O_ID, O_ID) # 1 # KIDREG2PARM(1); KIDREG2PARM(0); printf("\tmr %s,%s\n", KID_REG(1), KID_REG(0));
+
+assign: O_ASSIGN(O_SUB(O_ID,O_NUM), O_ID) # 1 # assign_var(bnode);
+
+assign: O_ASSIGN(expr, O_FIELD(expr)) # 1 # KIDKIDREG2PARM(1,0); printf("\tstw %s,%d(%s)\n", BN_REG, bnode->kids[1]->soffset * 4, KIDKID_REG(1,0));
+assign: O_ASSIGN(O_ID, O_FIELD(expr)) # 1 # KIDREG2PARM(0); KIDKIDREG2PARM(1,0); printf("\tstw %s,%d(%s)\n", KID_REG(0), bnode->kids[1]->soffset * 4, KIDKID_REG(1,0));
+
+
+ifstat: O_IF(O_ID) # 1 # /* fuer faelle wie "if bla then" noetig */ KIDREG2PARM(0); printf("\tcmpwi %s,0\n", KID_REG(0));
+ifstat: O_IF(O_BOOL(imm)) # 1 # printf("\tcmpwi %s,0\n", BN_REG);
+ifstat: O_IF(expr) # 2 # /* iburg beschummeln :/ */ printf("\tcmpwi %s,0\n", BN_REG);
+ifstat: O_IF(O_BOOL(expr)) # 1 # /* dann braucht man kein test */
+
+
+ret: O_RET(retexpr) # 2 # printf("\t/*o_ret(expr)*/\n"); move(BN_REG, "3");
+ret: O_EXPR(expr) # 0 #
+
+retexpr: O_ID # 1 # printf("\t/*retexpr*/\n"); if(bnode->param_index > -1) move(param_reg(bnode->param_index), BN_REG);
+retexpr: expr
+
+
+expr: O_ID # 0 #
+expr: imm # 1 # moveimm(BN_VAL, BN_REG);
+expr: O_BOOL(expr) # 0 #
+
+expr: O_CALL(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, "sub");
+expr: O_SUB(expr,imm)           # 1 # gen_e_imm(bnode, "sub");
+
+expr: O_SUB(expr,O_SUB(O_ID,expr)) # 2 # gen_subspecial(bnode, 0);
+expr: O_SUB(expr,O_SUB(imm,expr))  # 2 # gen_subspecial(bnode, 1);
+
+expr: O_SUB(expr, O_ADD(O_ID,expr))    # 1 # gen_id_eno(bnode);
+
+
+expr: O_ADD(expr,expr)   # 1 # gen_e_eno(bnode, "add");
+expr: O_ADD(expr,imm)    # 1 # gen_e_imm(bnode, "add");
+expr: O_ADD(imm,expr)    # 1 # gen_imm_eno(bnode, "add");
+
+
+expr: O_MUL(expr,expr)   # 1 # gen_e_eno(bnode, "mullw");
+expr: O_MUL(expr,imm)    # 1 # gen_e_imm(bnode, "mullw");
+expr: O_MUL(imm,expr)    # 1 # gen_imm_eno(bnode, "mullw");
+
+
+expr: O_OR(expr,expr)          # 1 # gen_e_eno(bnode, "or");
+expr: O_OR(expr,imm)           # 2 # gen_e_imm(bnode, "or");
+
+
+expr: O_LESS(expr,expr)          # 3 # gen_eqless(bnode, "l", 1, 1, 0);
+expr: O_LESS(expr,imm)           # 3 # gen_eqless(bnode, "l", 1, 0, 0);
+expr: O_LESS(imm,expr)           # 3 # gen_eqless(bnode, "g", 0, 1, 0);
+
+
+expr: O_EQ(expr,expr)          # 3 # gen_eqless(bnode, "e", 1, 1, 0);
+expr: O_EQ(expr,imm)           # 3 # gen_eqless(bnode, "e", 1, 0, 0);
+expr: O_EQ(imm,expr)           # 3 # gen_eqless(bnode, "e", 0, 1, 0);
+expr: O_EQ(expr,O_NULL)        # 3 # gen_eqless(bnode, "e", 1, 0, 0);
+
+expr: O_EQ(O_EQ(expr,O_NULL),O_NULL)  # 3 # gen_eqless(bnode, "ne", 1, 0, 1);
+expr: O_EQ(O_EQ(O_EQ(expr,O_NULL),O_NULL),O_NULL) # 3 # gen_eqless(bnode, "e", 1, 0, 2);
+
+
+expr: O_FIELD(expr) # 1 # printf("\t/* field(expr)*/\n"); KIDREG2PARM(0); printf("\tlwz %s, %d(%s)\n", BN_REG, bnode->soffset * 4, KID_REG(0));
+expr: O_FIELD(imm)  # 1 # printf("\t/* field(imm)*/\n"); moveimm(KID_VAL(0), BN_REG); printf("\tlwz %s,%d(%s)\n", BN_REG, bnode->soffset * 4, BN_REG);
+
+
+imm: O_ADD(imm,imm)  # 0 # BN_VAL = KID_VAL(0) + KID_VAL(1);
+imm: O_SUB(imm,imm)  # 0 # BN_VAL = KID_VAL(0) - KID_VAL(1);
+imm: O_MUL(imm,imm)  # 0 # BN_VAL = KID_VAL(0) * KID_VAL(1);
+imm: O_LESS(imm,imm) # 0 # BN_VAL = KID_VAL(0) < KID_VAL(1) ? 1 : 0;
+imm: O_EQ(imm,imm)   # 0 # BN_VAL = KID_VAL(0) == KID_VAL(1) ? 1 : 0;
+imm: O_OR(imm,imm)   # 0 # BN_VAL = KID_VAL(0) | KID_VAL(1);
+imm: O_NUM # 0 #
+imm: O_MONE # 0 #
+imm: O_MTWO # 0 #
+imm: O_MFOUR # 0 #
+imm: O_MEIGHT # 0 #
+imm: O_NULL # 0 #
+
+%%
+
+/* vim: filetype=c
+ */