From a0a265539fdde844b8b7861346983e551b62b775 Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Tue, 13 Sep 2016 16:10:45 +0200
Subject: [PATCH] LicDump: declare local vars in a topological order wrt clock
 dependancies.

Because otherwise, V4 ec tools behaves strangely.
---
 _oasis            |  2 +-
 src/lic.ml        | 57 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/licDump.ml    |  3 ++-
 src/licTab.ml     |  4 +++-
 src/lv6version.ml |  4 ++--
 test/lus2lic.sum  | 28 +++++++++++------------
 6 files changed, 78 insertions(+), 20 deletions(-)

diff --git a/_oasis b/_oasis
index bc2af73f..4844bc7b 100644
--- a/_oasis
+++ b/_oasis
@@ -1,6 +1,6 @@
 OASISFormat: 0.4
 Name:        lustre-v6
-Version:     1.663
+Version:     1.664
 Synopsis:    The Lustre V6 Verimag compiler
 Description: This package contains:
    (1) lus2lic: the (current) name of the compiler (and interpreter via -exec).
diff --git a/src/lic.ml b/src/lic.ml
index 6794ff2d..498b90e5 100644
--- a/src/lic.ml
+++ b/src/lic.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 09/09/2016 (at 08:32) by Erwan Jahier> *)
+(* Time-stamp: <modified the 13/09/2016 (at 15:54) by Erwan Jahier> *)
 
 (** Define the Data Structure representing Compiled programs. By
     compiled we mean that constant are propagated, packages are
@@ -731,3 +731,58 @@ Une erreur associ
 ----------------------------------------------------------------------*)
 exception Compile_node_error of node_key * Lxm.t * string
 exception Global_node_error of node_key * string
+
+(******************************************************************************)
+(* topologically sort vars wrt their clock dependecency *)
+
+  
+module TopoSortVarInfo = 
+  TopoSort.Make(
+      struct 
+        type elt = var_info
+        type store = (var_info, var_info list) Hashtbl.t
+        let find_dep tbl x = try Hashtbl.find tbl x with Not_found -> []
+        let have_dep tbl x = try Hashtbl.find tbl x <> [] with Not_found -> false
+        let remove_dep tbl x = Hashtbl.remove tbl x; tbl
+      end
+    )
+
+(* Looks like the one in LicTab *)
+let (sort_var_info :  var_info list -> var_info list) =
+  fun vars  ->  (* we sort vars according to their clock deps *)
+  let vartable = Hashtbl.create (List.length vars) in
+  let find_direct_dep  v vars =
+    match snd v.var_clock_eff with
+    | BaseLic -> None
+    | ClockVar _ -> None
+    | On((_cc,cv,_ct), _sclk) ->
+       Printf.printf "sort_var_info: %s depends on %s\n" v.var_name_eff cv;
+       flush stdout;
+       try Some (Hashtbl.find vartable cv)
+       with Not_found -> None
+  in 
+  let dep_star vars =
+    let tbl = Hashtbl.create (List.length vars) in
+    let rec find_deps v = 
+      if Hashtbl.mem tbl v then Hashtbl.find tbl v else
+        match find_direct_dep v vars with
+        | None -> []
+        | Some v2 ->
+           let v2_deps = find_deps v2 in
+           let v_deps = v2::v2_deps in
+           Hashtbl.replace tbl v v_deps;
+           v_deps
+    in
+    List.iter  
+      (fun v ->   
+       match find_deps v with 
+       | [] -> Hashtbl.remove tbl v (* cleaning *) 
+       | _::_ -> () 
+      ) 
+      vars; 
+    tbl
+  in
+  List.iter (fun v -> Hashtbl.add vartable v.var_name_eff v) vars;
+  TopoSortVarInfo.f (dep_star vars) vars
+   
+ 
diff --git a/src/licDump.ml b/src/licDump.ml
index fa3f28a7..a102d560 100644
--- a/src/licDump.ml
+++ b/src/licDump.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 02/09/2016 (at 10:28) by Erwan Jahier> *)
+(* Time-stamp: <modified the 13/09/2016 (at 14:59) by Erwan Jahier> *)
 
 open Lv6errors
 open Printf
@@ -778,6 +778,7 @@ and node_of_node_exp_eff (neff: Lic.node_exp): string =
               | None -> ""
               | Some [] -> ""
               | Some l ->
+                 let l = Lic.sort_var_info l in
 	              ("var\n   " ^ (string_of_type_decl_list l ";\n   ") ^ ";\n"
 	              )
              ) 
diff --git a/src/licTab.ml b/src/licTab.ml
index 8dd05e7f..f4726cec 100644
--- a/src/licTab.ml
+++ b/src/licTab.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 24/08/2016 (at 14:49) by Erwan Jahier> *)
+(* Time-stamp: <modified the 13/09/2016 (at 15:09) by Erwan Jahier> *)
 
 
 open Lxm
@@ -351,6 +351,7 @@ let find_direct_dep lxm v vars =
   | Base -> None
   | NamedClock({it=(_,vclk)}) -> Some vclk
 
+                                     
 let dep_star lxm vl vars =
   let tbl = Hashtbl.create (List.length vl) in
   let rec find_deps v = 
@@ -383,6 +384,7 @@ module TopoSortVars =
       end
     )
 
+(* Looks like the one in Lic *)
 let (sort_vars : Lxm.t -> AstCore.node_vars-> Lv6Id.t list -> Lv6Id.t list) =
   fun lxm vars l ->  (* we sort vars according to their clock deps *)
   profile_info "LicTab.sort_vars\n";
diff --git a/src/lv6version.ml b/src/lv6version.ml
index ce5d6a0d..b5719416 100644
--- a/src/lv6version.ml
+++ b/src/lv6version.ml
@@ -1,7 +1,7 @@
 (** Automatically generated from Makefile *) 
 let tool = "lus2lic"
 let branch = "master"
-let commit = "663"
-let sha_1 = "337a20a9ee8c99cc5711c3f137ab79c8256104df"
+let commit = "664"
+let sha_1 = "0572510ed6e5d03b8eb229a3514fef0b0626967c"
 let str = (branch ^ "." ^ commit ^ " (" ^ sha_1 ^ ")")
 let maintainer = "jahier@imag.fr"
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index 1bffa48a..142aac4d 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test Run By jahier on Fri Sep  9 11:56:59 
+Test Run By jahier on Tue Sep 13 15:57:46 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic0 tests ===
@@ -64,7 +64,7 @@ XFAIL: Test bad programs (assert): test_lus2lic_no_node should_fail/assert/lecte
 XFAIL: Test bad programs (assert): test_lus2lic_no_node should_fail/assert/s.lus
 
 ==> lus2lic1.sum <==
-Test Run By jahier on Fri Sep  9 11:57:06 
+Test Run By jahier on Tue Sep 13 15:57:47 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic1 tests ===
@@ -397,7 +397,7 @@ PASS: gcc -o multipar.exec multipar_multipar.c multipar_multipar_loop.c
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c multipar.lus  {}
 
 ==> lus2lic2.sum <==
-Test Run By jahier on Fri Sep  9 11:58:44 
+Test Run By jahier on Tue Sep 13 15:58:08 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic2 tests ===
@@ -743,7 +743,7 @@ PASS: gcc -o zzz2.exec zzz2_zzz2.c zzz2_zzz2_loop.c
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c zzz2.lus  {}
 
 ==> lus2lic3.sum <==
-Test Run By jahier on Fri Sep  9 12:00:51 
+Test Run By jahier on Tue Sep 13 15:58:57 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic3 tests ===
@@ -1249,7 +1249,7 @@ PASS: ./myec2c {-o multipar.c multipar.ec}
 PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node multipar.lus {}
 
 ==> lus2lic4.sum <==
-Test Run By jahier on Fri Sep  9 12:01:28 
+Test Run By jahier on Tue Sep 13 15:59:13 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -1773,14 +1773,14 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node zzz2.lus {}
 # of unexpected failures	4
 ===============================
 # Total number of failures: 26
-lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 7 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 97 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 126 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 37 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 35 seconds
+lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 1 seconds
+lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 21 seconds
+lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 49 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 16 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 38 seconds
 * Ref time: 
-0.04user 0.03system 5:04.40elapsed 0%CPU (0avgtext+0avgdata 5664maxresident)k
-32inputs+0outputs (0major+6105minor)pagefaults 0swaps
+0.03user 0.04system 2:05.17elapsed 0%CPU (0avgtext+0avgdata 5620maxresident)k
+128inputs+0outputs (0major+6020minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.06user 0.01system 2:14.11elapsed 0%CPU (0avgtext+0avgdata 5644maxresident)k
-32inputs+0outputs (0major+6134minor)pagefaults 0swaps
+0.06user 0.01system 1:28.04elapsed 0%CPU (0avgtext+0avgdata 5708maxresident)k
+96inputs+0outputs (0major+6049minor)pagefaults 0swaps
-- 
GitLab