Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.antlr.runtime.tree.Tree;
- import java.util.ArrayList;
- public class GenCode {
- private Node tds;
- private Tree t;
- private String code;
- private int compteurif;
- private int compteurwhile;
- public GenCode(Node tds, Tree t) {
- this.tds = tds;
- this.t = t;
- this.compteurif = 0;
- this.compteurwhile = 0;
- this.code = "";
- this.code += this.genFichierMain();
- this.genererCodefct(tds);
- }
- public void genererCodefct(Node n) {
- if (n.getId().equals("ROOT")) {
- } else if (n.getId().equals("main")) {
- this.code += this.genCodeMainHead();
- this.code += this.genDeclarationVariable(n);
- this.parcourtArbre(n, n.getT(), 0, 0);
- this.code += this.genCodeMainTail();
- } else if (!n.getId().contains("If") && !n.getId().contains("While")) {
- this.code += this.genFctHead(n);
- this.parcourtArbre(n, n.getT(), 0, 0);
- this.code += this.genFctTail(n);
- }
- for (int y = 0; y < n.getFils().size(); y++) {
- this.genererCodefct(n.getFils().get(y));
- }
- }
- public void parcourtArbre(Node tds, Tree t, int dec, int deb) {
- int res;
- DeclarationSymbole sb;
- int j;
- if (t != null) {
- for (int i = deb; i < t.getChildCount() - dec; i++) {
- Tree tfils = t.getChild(i);
- switch (tfils.getText()) {
- case "LET":
- int depl1;
- int depl2;
- for (j = 0; j < tfils.getChildCount(); j++) {
- if (estUnEntier(tfils.getChild(j).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
- this.code += this.genAffectationVal(sb, Integer.parseInt(tfils.getChild(j).getText()));
- } else if (tfils.getChild(j).getText().equals("+")) {
- if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
- depl1 = sb.getDepl();
- } else {
- depl1 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
- }
- if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
- depl2 = sb.getDepl();
- } else {
- depl2 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
- }
- this.code += this.genOp(-depl1, -depl2, "+");
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
- //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
- this.code += this.genRegistredansVariable(sb.depl, 1);
- } else if (tfils.getChild(j).getText().equals("-")) {
- if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
- depl1 = sb.getDepl();
- } else {
- depl1 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
- }
- if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
- depl2 = sb.getDepl();
- } else {
- depl2 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
- }
- this.code += this.genOp(-depl1, -depl2, "-");
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
- //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
- this.code += this.genRegistredansVariable(sb.depl, 1);
- } else if (tfils.getChild(j).getText().equals("*")) {
- if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
- depl1 = sb.getDepl();
- } else {
- depl1 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
- }
- if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
- depl2 = sb.getDepl();
- } else {
- depl2 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
- }
- this.code += this.genOp(-depl1, -depl2, "*");
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
- //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
- this.code += this.genRegistredansVariable(sb.depl, 1);
- } else if (tfils.getChild(j).getText().equals("/")) {
- if (!estUnEntier(tfils.getChild(j).getChild(0).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(0).getText());
- depl1 = sb.getDepl();
- } else {
- depl1 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(0).getText()),1);
- }
- if (!estUnEntier(tfils.getChild(j).getChild(1).getText())) {
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j).getChild(1).getText());
- depl2 = sb.getDepl();
- } else {
- depl2 = 1;
- this.genValdansRegistre(Integer.parseInt(tfils.getChild(j).getChild(1).getText()),1);
- }
- this.code += this.genOp(-depl1, -depl2, "/");
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(j - 1).getText());
- //res = Integer.parseInt(tfils.getChild(j).getChild(0).getText())+Integer.parseInt(tfils.getChild(j).getChild(1).getText()) ;
- this.code += this.genRegistredansVariable(sb.depl, 1);
- }
- }
- parcourtArbre(tds, tfils, 0, 0);
- break;
- case "RETURN":
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(0).getText());
- this.code += this.genFctReturn(sb.depl);
- break;
- case "IF":
- this.compteurif += 1;
- // exemple x == 6
- // la condition ici ==
- System.out.println(tfils.getChild(0));
- // x
- System.out.println(tfils.getChild(0).getChild(0));
- // 6
- System.out.println(tfils.getChild(0).getChild(1));
- // BLOC DE CODE
- System.out.println(tfils.getChild(1));
- System.out.println(tfils.getChild(1).getChild(0));
- this.code += this.genIfEg(tfils.getChild(0).getChild(0).getText(), tfils.getChild(0).getChild(1).getText(), tfils.getChild(0).getText());
- this.code += "if" + this.compteurif + " \n";
- parcourtArbre(tds, tfils.getChild(1), 0, 0);
- this.code += "finif" + compteurif + " \n";
- break;
- case "WHILE":
- this.compteurwhile += 1;
- this.code += "Debwhile" + this.compteurwhile + " \n";
- this.code += this.genWhile(tfils.getChild(0).getChild(0).getText(), tfils.getChild(0).getChild(1).getText(), tfils.getChild(0).getText());
- this.code += "while" + this.compteurwhile + " \n";
- parcourtArbre(tds, tfils, 2, 0);
- this.code += " BMP Debwhile" + compteurwhile + "-$-2\n";
- this.code += "finwhile" + compteurwhile + " \n";
- for (int l = 0; l < tfils.getChildCount(); l++) {
- System.out.println("Ici : " + tfils.getChild(l).getChild(0).getText());
- }
- parcourtArbre(tds, tfils, 0, tfils.getChildCount() - 2);
- break;
- case "PRINT":
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(0).getText());
- this.code += this.genCodePrintInt(sb.depl);
- break;
- case "APPLFUNCT":
- ArrayList<Integer> atrib = new ArrayList<Integer>();
- if (tfils.getChild(1).getText().equals("PARAMS")) {
- for (int h = 0; h < tfils.getChild(1).getChildCount(); h++) {
- System.out.println("test" + tfils.getChild(1).getChild(h));
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getChild(1).getChild(h).getText());
- atrib.add(sb.depl);
- }
- }
- this.code += this.genAplFct(tfils.getChild(0).getText(), atrib);
- sb = (DeclarationSymbole) tds.getSymbole(tfils.getParent().getChild(1).getText());
- this.code += genRegistredansVariable(sb.depl, 0);
- break;
- default:
- parcourtArbre(tds, tfils, 0, 0);
- }
- }
- }
- }
- public String genFichierMain() {
- return "// FICHIER HEAD \n" +
- "sp equ r15\n" +
- "wr equ r14\n" +
- "bp equ r13\n" +
- "SP equ r15\n" +
- "WR equ r14\n" +
- "BP equ r13\n" +
- "EXIT_EXC EQU 64\n" +
- "READ_EXC EQU 65\n" +
- "WRITE_EXC EQU 66\n" +
- "NUL equ 0 \n" +
- "NULL equ 0 \n" +
- "NIL equ 0\n" +
- "STACK_ADRS equ 0x1000\n" +
- "LOAD_ADRS equ 0xFE00\n" +
- " org LOAD_ADRS\n" +
- " start main_\n" +
- "ASCII_PLUS equ 43\n" +
- "ASCII_SPACE equ 32\n" +
- "ASCII_MINUS equ 45\n" +
- "ASCII_ZERO equ 48\n" +
- "printint_ NOP\n" +
- " ADQ -2,SP\n" +
- " LDW R1,SP\n" +
- " LDW R2,#0\n" +
- " STB R2,(SP)\n" +
- " ADQ -1,SP\n" +
- " LDW R2,#10\n" +
- " STB R2,(SP)\n" +
- " ADQ -1,SP\n" +
- " LDQ ASCII_PLUS, R5\n" +
- " TST R0\n" +
- " BGE POSIT-$-2\n" +
- " NEG R0, R0\n" +
- " LDQ ASCII_MINUS, R5\n" +
- "POSIT NOP\n" +
- " LDW R3,#10\n" +
- "CONV NOP\n" +
- " LDW R4,R0\n" +
- " DIV R4,R3,R0\n" +
- " LDQ ASCII_ZERO,R2\n" +
- " ADD R2,R4,R2\n" +
- " STB R2,(SP)\n" +
- " ADQ -1,SP\n" +
- " TST R0\n" +
- " JEQ #END_P-$-2\n" +
- " JMP #CONV-$-2\n" +
- "END_P NOP\n" +
- " STB R5,(SP)\n" +
- " ADQ -1,SP\n" +
- " LDW R0,SP\n" +
- " ADQ 1,R0\n" +
- " TRP #WRITE_EXC\n" +
- " LDW SP,R1\n" +
- " ADQ 2,SP\n" +
- " JEA (WR)\n" +
- "print_ LDQ 0,R1\n" +
- " ADQ -2, SP\n" +
- " STW BP, (SP)\n" +
- " LDW BP, SP\n" +
- " SUB SP, R1, SP\n" +
- " LDW R0, BP\n" +
- " ADQ 4,R0\n" +
- " LDW R0, (R0)\n" +
- " LDW WR, #WRITE_EXC\n" +
- " TRP WR\n" +
- " LDW SP, BP\n" +
- " LDW BP, (SP)\n" +
- " ADQ 2,SP\n" +
- " LDW WR, (SP)\n" +
- " ADQ 2, SP\n" +
- " JEA (WR)\n";
- }
- public String genCodeMainHead() {
- return "// MAIN HEAD \n" +
- "main_ ldw sp, #STACK_ADRS\n" +
- " ldw bp, #NIL\n";
- }
- public String genAplFct(String nom, ArrayList<Integer> atrib) {
- FonctionSymbole fs = (FonctionSymbole) tds.getSymbole(nom);
- String arg = "";
- for (int i = 0; i < atrib.size(); i++) {
- arg +=
- "//CHARGE " + atrib.get(i) + " \n" +
- " LDW WR, BP\n" +
- " ADQ -" + atrib.get(i) + ", WR\n" +
- " LDW R1, (WR)\n" +
- "//EMPILE " + atrib.get(i) + " \n" +
- " ADQ -2, SP \n" +
- " STW R1, (SP) \n";
- }
- return arg +
- " // APL FCT " + nom + "\n" +
- " LDW R1, #" + nom + "\n" +
- " MPC WR\n" +
- " ADQ 8, WR\n" +
- " ADQ -2, SP\n" +
- " STW WR, (SP)\n" +
- " JEA(R1)\n" +
- " ADQ 2*2, SP\n";
- }
- public String genCodeMainTail() {
- return "// MAIN TAIL \n" +
- " ldw SP, BP\n" +
- " ldw BP, (SP)\n" +
- " ADQ 2, SP\n" +
- " TRP #EXIT_EXC \n" +
- " JEA @main_\n";
- }
- public String genCodePrintString(String g) {
- return "// PRINT STRING \n" +
- "TOTO string \"" + g + "\"\n" +
- " LDW R0, #TOTO\n" +
- " STW R0, -(SP)\n" +
- " JSR @print_\n";
- }
- public String genCodePrintInt(int depl) {
- String depla = Integer.toString(depl);
- return "// PRINT INT \n" +
- this.genVariabledansRegistre(depl, 0) +
- " LDW R1, #printint_\n" + // appel de la fct
- " MPC WR\n" +
- " ADQ 8,WR\n" +
- " ADQ -2,SP\n" +
- " STW WR, (SP)\n" +
- " JEA (R1)\n";
- }
- public String genFctHead(Node n) {
- return "// FCT HEAD ( " + n.getId() + " )\n" +
- n.getId() + " LDQ " + n.getDepl() + ", R1\n" +
- " ADQ -2, SP\n" +
- " STW BP, (SP) \n" +
- " LDW BP, SP \n" +
- " SUB SP, R1, SP\n";
- }
- public String genFctReturn(int depl) {
- return "// FCT RETURN \n" +
- " LDW WR, BP\n" +
- " ADQ -" + depl + ", WR \n" +
- " LDW R0, (WR)\n";
- }
- public String genFctTail(Node n) {
- return "// FCT TAIL ( " + n.getId() + " )\n" +
- " LDW SP, BP\n" +
- " LDW BP, (SP)\n" +
- " ADQ 2, SP \n" +
- // retour appelant
- " LDW WR, (SP)\n" +
- " ADQ 2, SP \n" +
- " JEA (WR) \n";
- }
- public String genDeclarationVariable(Node n) {
- String code = "";
- int depla = n.getDepl();
- // calcul deplacement
- if (depla > 0) {
- code += "// DECLARATION VAR \n" +
- " LDQ " + Integer.toString(depla) + ", R1\n" +
- " ADQ -2, SP\n" +
- " STW BP, (SP)\n" +
- " LDW BP, SP\n" +
- " SUB SP, R1, SP\n";
- return code;
- } else {
- return "";
- }
- }
- public String genValdansRegistre(int val,int reg){
- return "// Store " + val + "dans \n" +
- " LDW R"+reg+", #" + Integer.toString(val) + " \n" ;
- }
- public String genAffectationVal(DeclarationSymbole vs, int val) {
- return "// AFFECTATION " + vs.nom + " -> " + val + "\n" +
- " LDW R1, #" + Integer.toString(val) + " \n" +
- " LDW WR, BP\n" +
- " ADQ -" + vs.getDepl() + ", WR\n" +
- " STW R1, (WR)\n";
- }
- public String genVariabledansRegistre(int depl, int numr) {
- // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
- return "// AFFECTATION du registre " + numr + " \n" +
- " LDW WR, BP\n" +
- " ADQ -" + depl + ",WR\n" +
- " LDW R" + numr + ", (WR)\n";
- }
- public String genRegistredansVariable(int depl, int numr) {
- // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
- return "// AFFECTATION du registre " + numr + " \n" +
- " LDW WR, BP\n" +
- " ADQ -" + depl + ",WR\n" +
- " STW R" + numr + ", (WR)\n";
- }
- public String genOp(int depl1, int depl2, String op) {
- // return le code asm mettant la valeur de la var de deplacement depl dans le registre de num numr
- String ope = "";
- switch (op) {
- case ("+"):
- ope = "ADD R1,R2,R1";
- break;
- case ("-"):
- ope = "SUB R1,R2,R1";
- break;
- case ("*"):
- ope = "MUL R1,R2,R1";
- break;
- case ("/"):
- ope = "DIV R1,R2,R1";
- break;
- default:
- break;
- }
- return "// OP ARITHM " + op + " \n" +
- " LDW WR, BP\n" +
- " ADQ " + depl1 + ",WR\n" +
- " LDW R1, (WR)\n" +
- " LDW WR, BP\n" +
- " ADQ " + depl2 + ",WR\n" +
- " LDW R2, (WR)\n" +
- " " + ope + "\n";
- }
- public String genIfEg(String val, String n, String eq) {
- String s = "//IF " + val + eq + n + " \n" +
- " LDW R6, #" + n + " \n" +
- " CMP R1, R6\n";
- if (eq.equals("==")) {
- s += " BEQ " + "if" + compteurif + "-$-2\n" +
- " BNE finif" + compteurif + "-$-2\n";
- } else if (eq.equals(">=")) {
- s += " BAE " + "if" + compteurif + "-$-2\n" +
- " BBL finif" + compteurif + "-$-2\n";
- } else if (eq.equals("<=")) {
- s += " BBE " + "if" + compteurif + "-$-2\n" +
- " BAB finif" + compteurif + "-$-2\n";
- } else if (eq.equals(">")) {
- s += " BAB " + "if" + compteurif + "-$-2\n" +
- " BBE finif" + compteurif + "-$-2\n";
- } else if (eq.equals("<")) {
- s += " BBL " + "if" + compteurif + "-$-2\n" +
- " BAE finif" + compteurif + "-$-2\n";
- } else if (eq.equals("!=")) {
- s += " BNE " + "if" + compteurif + "-$-2\n" +
- " BEQ finif" + compteurif + "-$-2\n";
- }
- return s;
- }
- public String genWhile(String val, String n, String eq) {
- String s = "//WHILE " + val + eq + n + " \n" +
- " LDW R6, #" + n + " \n" +
- " CMP R1, R6\n";
- if (eq.equals("==")) {
- s += " BEQ " + "while" + compteurwhile + "-$-2\n" +
- " BNE finwhile" + compteurwhile + "-$-2\n";
- } else if (eq.equals(">=")) {
- s += " BAE " + "while" + compteurwhile + "-$-2\n" +
- " BBL finwhile" + compteurwhile + "-$-2\n";
- } else if (eq.equals("<=")) {
- s += " BBE " + "while" + compteurwhile + "-$-2\n" +
- " BAB finwhile" + compteurwhile + "-$-2\n";
- } else if (eq.equals(">")) {
- s += " BAB " + "while" + compteurwhile + "-$-2\n" +
- " BBE finwhile" + compteurwhile + "-$-2\n";
- } else if (eq.equals("<")) {
- s += " BBL " + "while" + compteurwhile + "-$-2\n" +
- " BAE finwhile" + compteurwhile + "-$-2\n";
- } else if (eq.equals("!=")) {
- s += " BNE " + "while" + compteurwhile + "-$-2\n" +
- " BEQ finwhile" + compteurwhile + "-$-2\n";
- }
- return s;
- }
- public String getCode() {
- return code;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public boolean estUnEntier(String chaine) {
- try {
- Integer.parseInt(chaine);
- } catch (NumberFormatException e) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement