From c803b44e75834ebd66d7bef1f4979f11be3f661c Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Fri, 18 Mar 2016 11:15:23 +0100
Subject: [PATCH] soc2c: fix a bug in the reset functions (that were not used
 before --2c-global-ctx)

The bug was (in soc2c.ml::137) that, for array of instances of size
n, it was generating n+1 initialisations, causing a seg fault.
---
 _oasis                 |  2 +-
 src/lv6MainArgs.ml     |  2 +-
 src/lv6version.ml      |  4 ++--
 src/soc2c.ml           |  7 ++++---
 src/soc2cInstances.ml  |  7 ++++---
 src/soc2cInstances.mli | 28 +++++++++++++++++-----------
 src/socPredef.ml       | 31 +++++++++++++++++++------------
 test/lus2lic.sum       | 24 +++++++++++-------------
 8 files changed, 59 insertions(+), 46 deletions(-)

diff --git a/_oasis b/_oasis
index 0dbab7de..c7b4bb69 100644
--- a/_oasis
+++ b/_oasis
@@ -1,6 +1,6 @@
 OASISFormat: 0.4
 Name:        lustre-v6
-Version:     1.647
+Version:     1.648
 Synopsis:    The Lustre V6 Verimag compiler
 Description: This package contains:
              - lus2lic: the (current) name of the compiler (and interpreter via -exec).
diff --git a/src/lv6MainArgs.ml b/src/lv6MainArgs.ml
index 4fd25931..9b736c98 100644
--- a/src/lv6MainArgs.ml
+++ b/src/lv6MainArgs.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 18/03/2016 (at 10:10) by Erwan Jahier> *)
+(* Time-stamp: <modified the 18/03/2016 (at 11:13) by Erwan Jahier> *)
 (*
 Le manager d'argument adapté de celui de lutin, plus joli
 N.B. solution un peu batarde : les options sont stockées, comme avant, dans Global,
diff --git a/src/lv6version.ml b/src/lv6version.ml
index 5cdf07b8..29f93db1 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 = "647"
-let sha_1 = "f3ac7e54d710ca031bdee874ccf7f831005f12f3"
+let commit = "648"
+let sha_1 = "7a1821319f3451f3e7c4d3bfbe96dd3ba15fd8d5"
 let str = (branch ^ "." ^ commit ^ " (" ^ sha_1 ^ ")")
 let maintainer = "jahier@imag.fr"
diff --git a/src/soc2c.ml b/src/soc2c.ml
index c8f8a583..4fb82c27 100644
--- a/src/soc2c.ml
+++ b/src/soc2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 14/03/2016 (at 17:47) by Erwan Jahier> *)
+(* Time-stamp: <modified the 18/03/2016 (at 10:41) by Erwan Jahier> *)
 
 
 (* let put (os: out_channel) (fmt:('a, unit, string, unit) format4) : 'a = *)
@@ -134,7 +134,7 @@ let (gen_instance_init_call : 'a soc_pp -> Soc.key * int -> unit) =
   fun sp (key,i) -> 
     let ctx_name = get_ctx_name key in
     if Lv6MainArgs.global_opt.Lv6MainArgs.soc2c_inline_loops || i<4 then
-      for k=0 to i do
+      for k=0 to i-1 do
         sp.cfmt "\n    %s_reset(&ctx->%s_tab[%d]);" ctx_name ctx_name k
       done
     else (
@@ -165,7 +165,8 @@ let (soc2c: int -> out_channel -> out_channel -> Soc.tbl -> Soc.t -> unit) =
         (* Call the reset_ctx functions of the soc instances *)
         if Lv6MainArgs.global_opt.Lv6MainArgs.soc2c_inline_loops then () else 
           sp.cput "\n  int _i;\n";
-        List.iter (gen_instance_init_call sp) (fst (Soc2cInstances.to_array soc.instances));
+        List.iter (gen_instance_init_call sp)
+                  (fst (Soc2cInstances.to_array soc.instances));
         (match soc.key with
           (* set the parameter fields that have a default value (arrow,fby) *)
           | (_,_,MemInit (ve)) -> 
diff --git a/src/soc2cInstances.ml b/src/soc2cInstances.ml
index 996857db..287e0b91 100644
--- a/src/soc2cInstances.ml
+++ b/src/soc2cInstances.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 10/04/2015 (at 11:10) by Erwan Jahier> *)
+(* Time-stamp: <modified the 18/03/2016 (at 10:35) by Erwan Jahier> *)
 
 
 module SocKey = struct
@@ -19,7 +19,7 @@ let (get_pos : 'a -> 'a list -> int) =
     assert (List.nth l pos = x);
     pos
 
-(** gathers instances with the same key into an array *)
+(** gathers instances of the same soc  into an array *)
 
 open Soc
 (* exported *)
@@ -29,7 +29,8 @@ let find k t = try SkMap.find k t with Not_found ->
   flush stdout;
   assert false
 
-let to_array : ((ident * Soc.key) list -> (Soc.key * int) list * (ident * Soc.key -> int)) =
+let to_array :
+      ((ident * Soc.key) list -> (Soc.key * int) list * (ident * Soc.key -> int)) =
   fun l -> 
     let rec aux tab = function
       | [] -> tab
diff --git a/src/soc2cInstances.mli b/src/soc2cInstances.mli
index 4082d4d8..d35e51db 100644
--- a/src/soc2cInstances.mli
+++ b/src/soc2cInstances.mli
@@ -1,19 +1,25 @@
-(* Time-stamp: <modified the 09/04/2015 (at 15:09) by Erwan Jahier> *)
+(* Time-stamp: <modified the 18/03/2016 (at 10:35) by Erwan Jahier> *)
 
 (** Each soc has a list of soc instances, made of an (unique) ident
     and a Soc.key.  
 
-    In order to be able to iterate of such instances (e.g., with a
-    for loop), we want to store them into arrays. This module will
-    help us to do so.
-*)
+    In order to be able to iterate on such instances at the C level
+    (e.g., with a for loop), instances are stored into arrays.  
 
-(** from a list of soc instances l, this function returns :
+    The to_array function helps to do that, by gathering instances of
+    the same soc.
+ 
+    More precisely from a list of soc instances l, this function
+    returns :
 
-    - a list made of all the soc.key in l + their occurences in l
+    - a list made of all the soc.key in l + their number of occ in l
+    (which will help to compute the array size)
 
-    - a function that maps each instance ident to the corresponding
-    ident in the array where it is stored
-*)
+    - a function that maps each instance ident to its position in the array
+     where it is stored
+ *)
 
-val to_array : (Soc.ident * Soc.key) list -> (Soc.key * int) list * (Soc.ident * Soc.key -> int)
+val to_array : (Soc.ident * Soc.key) list -> 
+               (Soc.key * int) list * (Soc.ident * Soc.key -> int)
+
+(* XXX the 2nd looks unused. rm ? *)
diff --git a/src/socPredef.ml b/src/socPredef.ml
index 34ab39e5..ddd48c8c 100644
--- a/src/socPredef.ml
+++ b/src/socPredef.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 26/02/2015 (at 11:25) by Erwan Jahier> *)
+(* Time-stamp: <modified the 18/03/2016 (at 10:14) by Erwan Jahier> *)
 
 (** Synchronous Object Code for Predefined operators. *)
 
@@ -84,7 +84,9 @@ let of_fby_soc_key : Soc.var_expr -> Soc.key -> Soc.t =
     let t = List.hd tl in
     let pre_mem:var = (get_mem_name sk t, t) in
     let prof = soc_profile_of_types tl in
-    let v1,v2,vout = match prof with ([v1;v2],[vout]) -> v1,v2,vout | _ -> assert false in
+    let v1,v2,vout = 
+      match prof with ([v1;v2],[vout]) -> v1,v2,vout | _ -> assert false 
+    in
     {
       key      = sk;
       profile  = prof;
@@ -98,10 +100,10 @@ let of_fby_soc_key : Soc.var_expr -> Soc.key -> Soc.t =
           idx_ins  = [];
           idx_outs = [0];
           impl    = Gaol([pre_mem],[Call([Var(vout)], Assign, [Var(pre_mem)])]);
-(*           impl    = Gaol([pre_mem],[ *)
-(*             Case("$first_step", (["t", [Call([Var(vout)], Assign, [Var(v1)])]; *)
-(*                                   "f", [Call([Var(vout)], Assign, [Var(pre_mem)])]])) *)
-(*           ]); *)
+        (* impl    = Gaol([pre_mem],[ *)
+        (* Case("$first_step", (["t", [Call([Var(vout)], Assign, [Var(v1)])]; *)
+        (* "f", [Call([Var_Expr_Is_Not_A_Slice(vout)], Assign, [Var(pre_mem)])]])) *)
+        (*           ]); *)
         };
         {
           name    = "set";  
@@ -174,7 +176,9 @@ let of_soc_key : Soc.key -> Soc.t =
          let t = List.hd (List.tl tl) in 
          let mem:var = (get_mem_name sk t, t) in 
          let prof:var list * var list = sp tl in 
-         let cv,vin,vout = match prof with ([cv;vin],[vout]) -> cv,vin,vout | _ -> assert false in 
+         let cv,vin,vout = 
+           match prof with ([cv;vin],[vout]) -> cv,vin,vout | _ -> assert false 
+         in 
          { 
            key      = sk; 
            profile  = (sp tl); 
@@ -189,7 +193,8 @@ let of_soc_key : Soc.key -> Soc.t =
                impl    = 
                  Gaol([], 
                       [Case((fst cv),[
-                        (Lv6Id.string_of_long2 cc, [Call([Var(mem)], Assign, [Var(vin)])])]);
+                        (Lv6Id.string_of_long2 cc, [Call([Var(mem)],
+                                                         Assign, [Var(vin)])])]);
                        Call([Var(vout)], Assign, [Var(mem)])]) 
              }; 
            ]; 
@@ -216,7 +221,7 @@ let of_soc_key : Soc.key -> Soc.t =
               idx_outs = [0];
                (*               impl    = Predef; *)
               impl    = Gaol([],[Call([Var(vout)], Assign, [Var(pre_mem)])]); 
-            (*               impl    = Gaol([pre_mem],[Call([Var(vout)], Assign, [Var(pre_mem)])]); *)
+            (*impl    = Gaol([pre_mem],[Call([Var(vout)], Assign, [Var(pre_mem)])]); *)
             };
             {
               name    = "set";  
@@ -225,7 +230,7 @@ let of_soc_key : Soc.key -> Soc.t =
               idx_outs = [];
                (*               impl    = Predef; *)
               impl    = Gaol([],[Call([Var(pre_mem)], Assign, [Var(v1)])]); 
-            (*               impl    = Gaol([pre_mem],[Call([Var(pre_mem)], Assign, [Var(v1)])]); *)
+            (* impl    = Gaol([pre_mem],[Call([Var(pre_mem)], Assign, [Var(v1)])]); *)
             };
           ];
           precedences = ["set", ["get"]];
@@ -370,7 +375,8 @@ let make_array_slice_soc : Lic.slice_info -> int -> Data.t -> Soc.t =
     let array_type_out = Array(t,size) in
     let key_prof = [array_type_in; array_type_out] in
     {
-      key = ("Lustre::array_slice", key_prof, Slic(si.Lic.se_first,si.Lic.se_last,si.Lic.se_step));
+      key = ("Lustre::array_slice", key_prof, 
+             Slic(si.Lic.se_first,si.Lic.se_last,si.Lic.se_step));
       profile  = (["x", array_type_in], ["z", array_type_out]);
       instances = [];
       step  = [
@@ -493,7 +499,8 @@ let (soc_interface_of_pos_op:
         soc
       | Lic.FBY, _, Some init ->
         let concrete_type = List.nth types 0 in
-        let soc = of_fby_soc_key init (("Lustre::fby"), types@[concrete_type], MemInit init) in
+        let soc = of_fby_soc_key init (("Lustre::fby"), 
+                                       types@[concrete_type], MemInit init) in
         instanciate_soc soc concrete_type
       | Lic.FBY, _, None -> assert false (* should ot occur *)
       | Lic.PRE, _, _ ->
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index b2848124..4b34c447 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test Run By jahier on Fri Mar 18 10:06:06 
+Test Run By jahier on Fri Mar 18 10:52:09 
 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 Mar 18 10:06:06 
+Test Run By jahier on Fri Mar 18 10:52:13 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic1 tests ===
@@ -396,7 +396,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 Mar 18 10:06:22 
+Test Run By jahier on Fri Mar 18 10:52:11 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic2 tests ===
@@ -741,7 +741,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 Mar 18 10:06:58 
+Test Run By jahier on Fri Mar 18 10:52:15 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic3 tests ===
@@ -1243,7 +1243,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 Mar 18 10:07:10 
+Test Run By jahier on Fri Mar 18 10:52:09 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -1765,13 +1765,11 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node zzz2.lus {}
 ===============================
 # Total number of failures: 23
 lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 0 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 15 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 36 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 12 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 40 seconds
+lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 21 seconds
+lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 42 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 15 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 44 seconds
 * Ref time: 
-0.03user 0.04system 1:44.14elapsed 0%CPU (0avgtext+0avgdata 5292maxresident)k
-32inputs+0outputs (0major+5583minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.04user 0.02system 0:48.64elapsed 0%CPU (0avgtext+0avgdata 5168maxresident)k
-64inputs+0outputs (0major+5614minor)pagefaults 0swaps
+0.03user 0.02system 0:44.16elapsed 0%CPU (0avgtext+0avgdata 5228maxresident)k
+64inputs+0outputs (0major+5617minor)pagefaults 0swaps
-- 
GitLab