diff --git a/src/fg/Fg.java b/src/fg/Fg.java index 7913ff4d759d87ded83305a1772461611f04cd97..3bc8d7d0e86fc4ee1ce7256dea2d236b6bc5cb58 100644 --- a/src/fg/Fg.java +++ b/src/fg/Fg.java @@ -48,15 +48,73 @@ public class Fg implements NasmVisitor <Void> { } public Void visit(NasmAdd inst){ - for(int i = 0; i < inst2Node.size();i++) - { - NasmInst nasmInst = nasm.sectionText.get(i); - if(nasmInst instanceof NasmAdd) { - + return null; + } + private void initFg() { + int nbInst = nasm.sectionText.size(); + for (int i = 0; i < nbInst; i++) { + NasmInst inst = nasm.sectionText.get(i); + if (inst instanceof NasmJg) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) inst.address).val))); + if (nasm.sectionText.get(i + 1) != null) { + NasmInst prochaineInst = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(prochaineInst)); + } + } + if (inst instanceof NasmJle) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) inst.address).val))); + if (nasm.sectionText.get(i + 1) != null) { + NasmInst prochaineInst = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(prochaineInst)); + } + } + if (inst instanceof NasmJne) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) inst.address).val))); + if (nasm.sectionText.get(i + 1) != null) { + NasmInst prochaineInst = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(prochaineInst)); + } + } + if (!this.inst2Node.containsKey(inst)) { + continue; + } + if (inst instanceof NasmJmp) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) (inst).address).val))); + continue; + } + if (inst instanceof NasmJe) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) inst.address).val))); + if (nasm.sectionText.get(i + 1) != null) { + NasmInst prochaineInst = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(prochaineInst)); + } + } + if (inst instanceof NasmJe) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(this.label2Inst.get(((NasmLabel) inst.address).val))); + if (nasm.sectionText.get(i + 1) != null) { + NasmInst prochaineInst = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(prochaineInst)); + } + } + if (inst instanceof NasmRet) { + if (i < nbInst - 1) { + NasmLabel destination = (NasmLabel) inst.address; + if (destination != null && !destination.val.equals("iprintLF") && !destination.val.equals("atoi") && !destination.val.equals("readline")) { + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(nasm.sectionText.get(i + 1))); + } + } + continue; + } + if (inst instanceof NasmRet) { + continue; + } + if (i < nasm.sectionText.size() - 1) { + NasmInst InstPro = nasm.sectionText.get(i + 1); + this.graph.addEdge(this.inst2Node.get(inst), this.inst2Node.get(InstPro)); } } - return null; } + public Void visit(NasmCall inst){ return null; }