From 5c49e7335661a1b0a3d88355598e10223fadedc5 Mon Sep 17 00:00:00 2001
From: ZaynouneFatimaZahrae
 <131474298+ZaynouneFatimaZahrae@users.noreply.github.com>
Date: Wed, 10 Apr 2024 15:59:17 +0200
Subject: [PATCH] Fg

---
 src/fg/Fg.java | 70 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 64 insertions(+), 6 deletions(-)

diff --git a/src/fg/Fg.java b/src/fg/Fg.java
index 7913ff4..3bc8d7d 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;
     }
-- 
GitLab