From bf646039f586db4ef199d6ed466c1f173689d1a8 Mon Sep 17 00:00:00 2001
From: Tristan Gingold <tgingold@free.fr>
Date: Mon, 18 May 2020 18:40:01 +0200
Subject: netlits: Use Remove_Instance instead of Free_Instance.

---
 src/synth/netlists-cleanup.adb   |  4 +++-
 src/synth/netlists-inference.adb |  2 +-
 src/synth/netlists-utils.adb     | 37 -------------------------------------
 src/synth/netlists-utils.ads     |  4 ----
 src/synth/netlists.adb           |  8 +++++++-
 5 files changed, 11 insertions(+), 44 deletions(-)

diff --git a/src/synth/netlists-cleanup.adb b/src/synth/netlists-cleanup.adb
index c24a89031..2c7045437 100644
--- a/src/synth/netlists-cleanup.adb
+++ b/src/synth/netlists-cleanup.adb
@@ -173,6 +173,7 @@ package body Netlists.Cleanup is
    procedure Mark_And_Sweep (M : Module)
    is
       use Netlists.Gates;
+      --  Table of new gates to be inspected.
       Inspect : Instance_Tables.Instance;
 
       Inst : Instance;
@@ -298,7 +299,8 @@ package body Netlists.Cleanup is
                Inst := First_Unused;
                exit when Inst = No_Instance;
                First_Unused := Get_Next_Instance (Inst);
-
+               Set_Next_Instance (Inst, No_Instance);
+               Set_Prev_Instance (Inst, No_Instance);
                Free_Instance (Inst);
             end loop;
          end if;
diff --git a/src/synth/netlists-inference.adb b/src/synth/netlists-inference.adb
index 4e79e7680..574eed408 100644
--- a/src/synth/netlists-inference.adb
+++ b/src/synth/netlists-inference.adb
@@ -669,7 +669,7 @@ package body Netlists.Inference is
       --  is read).  So redirect the net.
       Redirect_Inputs (Get_Output (Last_Mux, 0), Res);
       if Prev_Mux /= No_Instance then
-         Free_Instance (Prev_Mux);
+         Remove_Instance (Prev_Mux);
       end if;
 
       return Res;
diff --git a/src/synth/netlists-utils.adb b/src/synth/netlists-utils.adb
index 6a8f1a2bf..145692ef4 100644
--- a/src/synth/netlists-utils.adb
+++ b/src/synth/netlists-utils.adb
@@ -252,43 +252,6 @@ package body Netlists.Utils is
       return Disconnect_And_Get (Get_Input (Inst, I));
    end Disconnect_And_Get;
 
-   procedure Disconnect_And_Free (I : Input)
-   is
-      I_Net : constant Net := Get_Driver (I);
-      Inst : constant Instance := Get_Net_Parent (I_Net);
-      Nbr_Inputs : Port_Nbr;
-      Nbr_Outputs : Port_Nbr;
-   begin
-      --  First disconnect.
-      Disconnect (I);
-
-      --  Quick check: is output (of I) still used ?
-      if Is_Connected (I_Net) then
-         return;
-      end if;
-
-      --  Check that all outputs are unused.
-      Nbr_Outputs := Get_Nbr_Outputs (Inst);
-      if Nbr_Outputs > 1 then
-         for K in 0 .. Nbr_Outputs - 1 loop
-            if Is_Connected (Get_Output (Inst, K)) then
-               return;
-            end if;
-         end loop;
-      end if;
-
-      --  First disconnect inputs.
-      Nbr_Inputs := Get_Nbr_Inputs (Inst);
-      if Nbr_Inputs > 0 then
-         for K in 0 .. Nbr_Inputs - 1 loop
-            Disconnect_And_Free (Get_Input (Inst, K));
-         end loop;
-      end if;
-
-      --  Free Inst
-      Free_Instance (Inst);
-   end Disconnect_And_Free;
-
    function Same_Net (L, R : Net) return Boolean is
    begin
       if L = R then
diff --git a/src/synth/netlists-utils.ads b/src/synth/netlists-utils.ads
index aeeb5cc78..a6d63dbec 100644
--- a/src/synth/netlists-utils.ads
+++ b/src/synth/netlists-utils.ads
@@ -81,10 +81,6 @@ package Netlists.Utils is
    --  Return True iff O has one sink (is connected to one input).
    function Has_One_Connection (O : Net) return Boolean;
 
-   --  Disconnect input I.  If the driver of I has no output(s) connected,
-   --  disconnect and free it.
-   procedure Disconnect_And_Free (I : Input);
-
    --  Disconnect an input and return the previous driver.
    function Disconnect_And_Get (I : Input) return Net;
    function Disconnect_And_Get (Inst : Instance; I : Port_Idx) return Net;
diff --git a/src/synth/netlists.adb b/src/synth/netlists.adb
index 09a01f841..aa32138c3 100644
--- a/src/synth/netlists.adb
+++ b/src/synth/netlists.adb
@@ -529,9 +529,15 @@ package body Netlists is
    procedure Free_Instance (Inst : Instance)
    is
       pragma Assert (Is_Valid (Inst));
+      Inst_Rec : Instance_Record renames Instances_Table.Table (Inst);
    begin
       pragma Assert (not Check_Connected (Inst));
-      Instances_Table.Table (Inst).Klass := Free_Module;
+
+      --  Instance must not be linked anymore.
+      pragma Assert (Inst_Rec.Prev_Instance = No_Instance);
+      pragma Assert (Inst_Rec.Next_Instance = No_Instance);
+
+      Inst_Rec.Klass := Free_Module;
    end Free_Instance;
 
    function Get_Module (Inst : Instance) return Module is
-- 
cgit v1.2.3