private static void RenameTupleSourcesHelper(IRTuple tuple, Ident replaceIdent, int numbering) { Console.WriteLine(); Console.Write("Before: "); tuple.Print(); Console.WriteLine(); if (tuple.GetType() == typeof(IRTupleTwoOp)) { IRTupleTwoOp twoTuple = (IRTupleTwoOp)tuple; if (twoTuple.getSrc2() == replaceIdent) { twoTuple.setSrc2(RenameVar(replaceIdent, numbering)); } if (twoTuple.getSrc1() == replaceIdent) { twoTuple.setSrc1(RenameVar(replaceIdent, numbering)); } } else if (tuple.GetType() == typeof(IRTupleOneOpIdent)) { IRTupleOneOpIdent oneTuple = (IRTupleOneOpIdent)tuple; if (oneTuple.getSrc1() == replaceIdent) { oneTuple.setSrc1(RenameVar(replaceIdent, numbering)); } } else { throw new Exception("You need to deal with more types: " + tuple.GetType()); } Console.Write("After: "); tuple.Print(); Console.WriteLine(); }
public static string IRToARM(IRTuple IR) { IRTupleOneOpIdent IROOI = IR as IRTupleOneOpIdent; IRTupleOneOpImm<string> IROOImm = IR as IRTupleOneOpImm<string>; IRTupleTwoOp IRTO = IR as IRTupleTwoOp; if(IR.getOp() == IrOp.NEG){ return "Neg " + IROOI.getDest(); } if(IR.getOp() == IrOp.ADD){ return "ADD " + IR.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } if(IR.getOp() == IrOp.SUB){ return "SUB " + IR.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } if(IR.getOp() == IrOp.AND){ return "AND " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } if(IR.getOp() == IrOp.MUL){ if(IRTO.getDest() != IRTO.getSrc1()){ return "MUL " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } else{ return "MUL " + IRTO.getDest() + ", " + IRTO.getSrc2() + ", " + IRTO.getSrc1(); } } if(IR.getOp() == IrOp.CALL){ string str = "STRMFD sp, {R1-R12, lr}\n"; str += "BL " + IROOI.getDest(); if(IR.getDest()[0] == 'R'){ str = "MOV " + IROOI.getSrc1() + ", R0"; } else{ str = "LDR " + IROOI.getSrc1() + ", R0"; } return str; } if(IR.getOp() == IrOp.RET){ string str = ""; if(IR.getDest()[0] == 'R'){ str = "MOV R0, " + IR.getDest(); } else{ str = "LDR R0, " + IR.getDest(); } str += "\nLDMFD sp, {R1-R12, pc}"; return str; } // DIV if(IR.getOp() == IrOp.EQU){ string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() +'\n'; str += "MOVEQ " + IRTO.getDest() + ", #1\n"; str += "MOVNE " + IRTO.getDest() + ", #0"; return str; } if(IR.getOp() == IrOp.NEQ){ string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() +'\n'; str += "MOVEQ " + IRTO.getDest() + ", #0\n"; str += "MOVNE " + IRTO.getDest() + ", #1"; return str; } if(IR.getOp() == IrOp.LT){ string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() +'\n'; str += "MOVGE " + IRTO.getDest() + ", #0\n"; str += "MOVLT " + IRTO.getDest() + ", #1"; return str; } if(IR.getOp() == IrOp.GT){ string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() +'\n'; str += "MOVLE " + IRTO.getDest() + ", #0\n"; str += "MOVGT " + IRTO.getDest() + ", #1"; return str; } if(IR.getOp() == IrOp.JMP){ return "JMP " + IR.getDest(); } if(IR.getOp() == IrOp.JMPF){ string str = "CMP " + IROOI.getSrc1() + ", #0\n"; str += "JMPEQ " + IROOI.getDest(); return str; } if(IR.getOp() == IrOp.LABEL){ return IR.getDest() + ':'; } if(IR.getOp() == IrOp.FUNC) { return IR.getDest() + ':'; } // MOD if(IR.getOp() == IrOp.NOT){ return "MVN " + IROOI.getDest() + ", " + IROOI.getSrc1(); } if(IR.getOp() == IrOp.OR){ return "ORR " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } if(IR.getOp() == IrOp.XOR){ return "EOR " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2(); } if(IR.getOp() == IrOp.STORE){ if(IR.getDest()[0] == 'R'){ return "LDR " + IROOI.getDest() + ", " + IROOI.getSrc1(); } return "STR " + IROOI.getDest() + ", " + IROOI.getSrc1(); } IR.Print(); return ""; }
private static void RenameTupleSourcesHelper(IRTuple tuple, Ident replaceIdent, int numbering) { Console.WriteLine(); Console.Write("Before: "); tuple.Print(); Console.WriteLine(); if (tuple.GetType() == typeof(IRTupleTwoOp)) { IRTupleTwoOp twoTuple = (IRTupleTwoOp)tuple; if (twoTuple.getSrc2() == replaceIdent) twoTuple.setSrc2(RenameVar(replaceIdent, numbering)); if (twoTuple.getSrc1() == replaceIdent) twoTuple.setSrc1(RenameVar(replaceIdent, numbering)); } else if (tuple.GetType() == typeof(IRTupleOneOpIdent)) { IRTupleOneOpIdent oneTuple = (IRTupleOneOpIdent)tuple; if (oneTuple.getSrc1() == replaceIdent) oneTuple.setSrc1(RenameVar(replaceIdent, numbering)); } else throw new Exception("You need to deal with more types: " + tuple.GetType()); Console.Write("After: "); tuple.Print(); Console.WriteLine(); }
public static void Main(string [] args) { IRTuple[] tuples = { new IRTuple(IrOp.LABEL, "F$1"), new IRTupleOneOpIdent(IrOp.STORE, "T", "R$2"), new IRTupleOneOpIdent(IrOp.STORE, "A", "R$0"), new IRTupleOneOpIdent(IrOp.STORE, "B", "R$1"), new IRTupleOneOpImm <int>(IrOp.STORE, "C", 0), new IRTupleOneOpIdent(IrOp.STORE, "D", "A"), new IRTuple(IrOp.LABEL, "L$1"), new IRTupleTwoOp(IrOp.ADD, "C", "C", "B"), new IRTupleOneOpImm <int>(IrOp.STORE, "T$1", 1), new IRTupleTwoOp(IrOp.SUB, "D", "D", "T$1"), new IRTupleOneOpImm <int>(IrOp.STORE, "T$2", 0), new IRTupleTwoOp(IrOp.LTE, "T$3", "D", "T$2"), new IRTupleOneOpIdent(IrOp.JMPF, "L$1", "T$3"), new IRTupleOneOpIdent(IrOp.STORE, "R$0", "C"), new IRTupleOneOpIdent(IrOp.STORE, "R$2", "T") }; Dictionary <string, string> translations = new Dictionary <string, string>() { { "R$0", "R$0" }, { "R$1", "R$1" }, { "R$2", "R$2" }, { "D", "R$1" }, { "C", "R$2" }, { "B", "R$3" }, { "A", "R$0" }, { "T", "R$4" }, { "T$1", "R$0" }, { "T$2", "R$0" }, { "T$3", "R$0" } }; foreach (IRTuple irt in tuples) { irt.Print(); HashSet <Ident> usedvars = irt.GetUsedVars(); Console.Write("\tUses: "); foreach (Ident ident in usedvars) { Console.Write(ident + " "); } HashSet <Ident> definedvars = irt.GetDefinedVars(); Console.Write("\tDefines: "); foreach (Ident ident in definedvars) { Console.Write(ident + " "); } Console.WriteLine(); } Console.WriteLine("-------"); foreach (IRTuple irt in tuples) { IRTuple translated = irt.TranslateNames(translations); translated.Print(); Console.WriteLine(); } }
public static string IRToARM(IRTuple IR) { IRTupleOneOpIdent IROOI = IR as IRTupleOneOpIdent; IRTupleOneOpImm <string> IROOImm = IR as IRTupleOneOpImm <string>; IRTupleTwoOp IRTO = IR as IRTupleTwoOp; if (IR.getOp() == IrOp.NEG) { return("Neg " + IROOI.getDest()); } if (IR.getOp() == IrOp.ADD) { return("ADD " + IR.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } if (IR.getOp() == IrOp.SUB) { return("SUB " + IR.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } if (IR.getOp() == IrOp.AND) { return("AND " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } if (IR.getOp() == IrOp.MUL) { if (IRTO.getDest() != IRTO.getSrc1()) { return("MUL " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } else { return("MUL " + IRTO.getDest() + ", " + IRTO.getSrc2() + ", " + IRTO.getSrc1()); } } if (IR.getOp() == IrOp.CALL) { string str = "STRMFD sp, {R1-R12, lr}\n"; str += "BL " + IROOI.getDest(); if (IR.getDest()[0] == 'R') { str = "MOV " + IROOI.getSrc1() + ", R0"; } else { str = "LDR " + IROOI.getSrc1() + ", R0"; } return(str); } if (IR.getOp() == IrOp.RET) { string str = ""; if (IR.getDest()[0] == 'R') { str = "MOV R0, " + IR.getDest(); } else { str = "LDR R0, " + IR.getDest(); } str += "\nLDMFD sp, {R1-R12, pc}"; return(str); } // DIV if (IR.getOp() == IrOp.EQU) { string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() + '\n'; str += "MOVEQ " + IRTO.getDest() + ", #1\n"; str += "MOVNE " + IRTO.getDest() + ", #0"; return(str); } if (IR.getOp() == IrOp.NEQ) { string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() + '\n'; str += "MOVEQ " + IRTO.getDest() + ", #0\n"; str += "MOVNE " + IRTO.getDest() + ", #1"; return(str); } if (IR.getOp() == IrOp.LT) { string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() + '\n'; str += "MOVGE " + IRTO.getDest() + ", #0\n"; str += "MOVLT " + IRTO.getDest() + ", #1"; return(str); } if (IR.getOp() == IrOp.GT) { string str = "CMP " + IRTO.getSrc1() + ", " + IRTO.getSrc2() + '\n'; str += "MOVLE " + IRTO.getDest() + ", #0\n"; str += "MOVGT " + IRTO.getDest() + ", #1"; return(str); } if (IR.getOp() == IrOp.JMP) { return("JMP " + IR.getDest()); } if (IR.getOp() == IrOp.JMPF) { string str = "CMP " + IROOI.getSrc1() + ", #0\n"; str += "JMPEQ " + IROOI.getDest(); return(str); } if (IR.getOp() == IrOp.LABEL) { return(IR.getDest() + ':'); } if (IR.getOp() == IrOp.FUNC) { return(IR.getDest() + ':'); } // MOD if (IR.getOp() == IrOp.NOT) { return("MVN " + IROOI.getDest() + ", " + IROOI.getSrc1()); } if (IR.getOp() == IrOp.OR) { return("ORR " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } if (IR.getOp() == IrOp.XOR) { return("EOR " + IRTO.getDest() + ", " + IRTO.getSrc1() + ", " + IRTO.getSrc2()); } if (IR.getOp() == IrOp.STORE) { if (IR.getDest()[0] == 'R') { return("LDR " + IROOI.getDest() + ", " + IROOI.getSrc1()); } return("STR " + IROOI.getDest() + ", " + IROOI.getSrc1()); } IR.Print(); return(""); }