diff --git a/src/Compiler.java b/src/Compiler.java index 883912e4d58e0d8cd16922e761e635e0db991f62..977291c20b801df28092d91b73c151a89a0e55aa 100644 --- a/src/Compiler.java +++ b/src/Compiler.java @@ -44,12 +44,11 @@ public class Compiler System.out.println("[BUILD PRE NASM] "); buildPreNasm(); - //System.out.println("[BUILD FLOW GRAPH] "); - //buildFg(); - /* + System.out.println("[BUILD FLOW GRAPH] "); + buildFg(); System.out.println("[SOLVE FLOW GRAPH]"); solveFg(); - + /* System.out.println("[BUILD INTERFERENCE GRAPH] "); buildIg(); System.out.println("[ALLOCATE REGISTERS]"); diff --git a/src/fg/Fg.java b/src/fg/Fg.java index a0a150921b496eacc8324fa88fc7889eeeebc0c2..a308ef975773dd16571e9738b320256fff067a31 100644 --- a/src/fg/Fg.java +++ b/src/fg/Fg.java @@ -17,12 +17,93 @@ public class Fg implements NasmVisitor <Void> { this.node2Inst = new HashMap< Node, NasmInst>(); this.label2Inst = new HashMap< String, NasmInst>(); this.graph = new Graph(); + + List<NasmInst> forbidden = getForbiddenList(); + + for(int i = 1; i < nasm.sectionText.size(); i++) { + while (secretMethod(nasm.sectionText.get(i), forbidden)) { + i++; + } + Node n = graph.newNode(); + inst2Node.put(nasm.sectionText.get(i), n); + node2Inst.put(n, nasm.sectionText.get(i)); + String label = nasm.sectionText.get(i).label != null ? nasm.sectionText.get(i).label.toString() : null; + if (label != null) + label2Inst.put(label, nasm.sectionText.get(i)); + + + + } + + + for(int i = 1; i < nasm.sectionText.size(); i++) { + while (secretMethod(nasm.sectionText.get(i), forbidden)) + i++; + Node n = inst2Node.get(nasm.sectionText.get(i)); + int next = i+1; + if (next == nasm.sectionText.size()) + break; + while (secretMethod(nasm.sectionText.get(next), forbidden)) + next++; + + + if (nasm.sectionText.get(i) instanceof NasmCall || nasm.sectionText.get(i) instanceof NasmJe || nasm.sectionText.get(i) instanceof NasmJg || nasm.sectionText.get(i) instanceof NasmJl || nasm.sectionText.get(i) instanceof NasmJge || nasm.sectionText.get(i) instanceof NasmJle || nasm.sectionText.get(i) instanceof NasmJmp ||nasm.sectionText.get(i) instanceof NasmJne) { + + String label = nasm.sectionText.get(i).address.toString(); + Node n3 = inst2Node.getOrDefault((label2Inst.get(label)), null); + if (n3 != null) + graph.addEdge(n, n3); + if (!(nasm.sectionText.get(i) instanceof NasmCall || nasm.sectionText.get(i) instanceof NasmJmp)) + graph.addEdge(n, inst2Node.get(nasm.sectionText.get(next))); + + } + else + graph.addEdge(n, inst2Node.get(nasm.sectionText.get(next))); + + } + affiche("cacaca"); + } + + List<NasmInst> getForbiddenList() { + List<NasmInst> forbidden = new ArrayList<>(); + NasmRegister register2 = new NasmRegister(-1); + register2.colorRegister(Nasm.REG_EAX); + forbidden.add(new NasmPush(null, register2,"sauvegarde de eax")); + + NasmRegister register3 = new NasmRegister(-1); + register3.colorRegister(Nasm.REG_EBX); + forbidden.add(new NasmPush(null, register3,"sauvegarde de ebx")); + + NasmRegister register4 = new NasmRegister(-1); + register4.colorRegister(Nasm.REG_ECX); + forbidden.add(new NasmPush(null, register4,"sauvegarde de ecx")); + + NasmRegister register5 = new NasmRegister(-1); + register5.colorRegister(Nasm.REG_EDX); + forbidden.add(new NasmPush(null, register5,"sauvegarde de edx")); + + forbidden.add(new NasmPop(null, register5,"restaure edx")); + + forbidden.add(new NasmPop(null, register4,"restaure ecx")); + + forbidden.add(new NasmPop(null, register3,"restaure ebx")); + + forbidden.add(new NasmPop(null, register2,"restaure eax")); + forbidden.add(new NasmPop(null, new NasmRegister(0),"récupération de la valeur de retour")); + + return forbidden; + } + + boolean secretMethod(NasmInst inst, List<NasmInst> list) { + for (NasmInst nasmInst : list) + if (nasmInst.toString().equals(inst.toString())) + return true; + return false; } public void affiche(String baseFileName){ String fileName; PrintStream out = System.out; - if (baseFileName != null){ try { baseFileName = baseFileName; @@ -34,28 +115,26 @@ public class Fg implements NasmVisitor <Void> { System.err.println("Error: " + e.getMessage()); } } - - for(NasmInst nasmInst : nasm.sectionText){ - Node n = this.inst2Node.get(nasmInst); - /* - graph.addEdge(n,); - if (nasmInst instanceof NasmJe || nasmInst instanceof NasmJg || nasmInst instanceof NasmJl || nasmInst instanceof NasmJge || nasmInst instanceof NasmJle || nasmInst instanceof NasmJmp ||nasmInst instanceof NasmJne) { - String label = nasmInst.address.toString(); - Node n3 = inst2Node.get((label2Inst.get(label))); - graph.addEdge(n, n3); - } - */ - out.print(n + " : ( "); - for(NodeList q=n.succ(); q!=null; q=q.tail) { - out.print(q.head.toString()); - out.print(" "); - } - out.println(")\t" + nasmInst); + List<NasmInst> forbidden = getForbiddenList(); + + for(int i = 1; i < nasm.sectionText.size(); i++) { + while (secretMethod(nasm.sectionText.get(i), forbidden)) { + i++; + } + Node n = this.inst2Node.get(nasm.sectionText.get(i)); + + out.print(n + " : ( "); + for(NodeList q=n.succ(); q!=null; q=q.tail) { + out.print(q.head.toString()); + out.print(" "); + } + out.println(")\t" + nasm.sectionText.get(i)); } } public Void visit(NasmAdd inst){ + return null; } public Void visit(NasmCall inst){ @@ -96,12 +175,18 @@ public class Fg implements NasmVisitor <Void> { return null; } public Void visit(NasmPop inst){ + + return null; } public Void visit(NasmRet inst){ + + return null; } public Void visit(NasmXor inst){ + + return null; } public Void visit(NasmAnd inst){ @@ -111,11 +196,19 @@ public class Fg implements NasmVisitor <Void> { return null; } public Void visit(NasmJmp inst){return null;} - public Void visit(NasmMov inst){return null;} + public Void visit(NasmMov inst){ + + return null;} public Void visit(NasmPush inst){return null;} - public Void visit(NasmSub inst){return null;} - public Void visit(NasmEmpty inst){return null;} - public Void visit(NasmInt inst){return null;} + public Void visit(NasmSub inst){ + + return null;} + public Void visit(NasmEmpty inst){ + + return null;} + public Void visit(NasmInt inst){ + + return null;} public Void visit(NasmAddress operand){return null;} public Void visit(NasmConstant operand){return null;} public Void visit(NasmLabel operand){return null;} diff --git a/src/fg/FgSolution.java b/src/fg/FgSolution.java index 0970759509bbf4365786cb26710f8aecbd3766b0..a600132614e44d1b41f50a44a3324534e45ac26f 100644 --- a/src/fg/FgSolution.java +++ b/src/fg/FgSolution.java @@ -21,8 +21,40 @@ public class FgSolution{ this.def = new HashMap< NasmInst, IntSet>(); this.in = new HashMap< NasmInst, IntSet>(); this.out = new HashMap< NasmInst, IntSet>(); + int intSetSize = nasm.getTempCounter(); + + List<NasmInst> forbidden = fg.getForbiddenList(); + + for (int i = 1; i < nasm.sectionText.size(); i++) { + while (fg.secretMethod(nasm.sectionText.get(i), forbidden)) { + i++; + } + NasmInst inst = nasm.sectionText.get(i); + use.put(inst, new IntSet(intSetSize)); + def.put(inst, new IntSet(intSetSize)); + in.put(inst, new IntSet(intSetSize)); + out.put(inst, new IntSet(intSetSize)); + + if (inst.srcDef) { + def.get(inst); + } + if (inst.srcUse){ + use.get(inst); + } + if (inst.destDef){ + def.get(inst); + } + if (inst.destUse){ + use.get(inst); + } + } + + } - + + + + public void affiche(String baseFileName){ String fileName; PrintStream out = System.out; diff --git a/src/nasm/C3a2nasm.java b/src/nasm/C3a2nasm.java index a7c89a1ac285010aafab617cbebdb6b9ad51ae8b..9a60b9466c4f363ac698348b5a0a0713063481eb 100644 --- a/src/nasm/C3a2nasm.java +++ b/src/nasm/C3a2nasm.java @@ -53,7 +53,6 @@ public class C3a2nasm implements C3aVisitor <NasmOperand> { } public NasmOperand visit(C3aInstCall inst){ - System.out.println(inst); NasmOperand label = (inst.label != null) ? inst.label.accept(this) : null; @@ -384,7 +383,6 @@ public class C3a2nasm implements C3aVisitor <NasmOperand> { public NasmOperand visit(C3aTemp oper){ - System.out.println(oper.num); System.out.println(oper.getClass()); return new NasmRegister(oper.num); }