From 85804240652117595d47f55e0b8ac6765fa9120c Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Tue, 10 Jan 2017 18:33:55 +0100
Subject: [PATCH] -2cw7: for generate for loops instead of memcpy for assigning
 arrays

---
 _oasis                     |  2 +-
 src/l2lCheckKcgKeyWord.mli |  4 +--
 src/lic2soc.ml             |  8 ++++--
 src/lv6version.ml          |  4 +--
 src/soc2c.ml               |  7 +++--
 src/soc2cGenAssign.ml      | 22 +++++++++++++--
 src/soc2cGenAssign.mli     |  6 +++--
 src/socPredef2cHeap.ml     | 55 ++++++++++++++++++++++----------------
 src/socPredef2cStack.ml    | 45 +++++++++++++++++++++----------
 test/lus2lic.sum           | 22 +++++++--------
 10 files changed, 114 insertions(+), 61 deletions(-)

diff --git a/_oasis b/_oasis
index 89993b92..26e52ca7 100644
--- a/_oasis
+++ b/_oasis
@@ -1,6 +1,6 @@
 OASISFormat: 0.4
 Name:        lustre-v6
-Version:     1.678
+Version:     1.679
 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/l2lCheckKcgKeyWord.mli b/src/l2lCheckKcgKeyWord.mli
index e945c5a6..e6c1ebc0 100644
--- a/src/l2lCheckKcgKeyWord.mli
+++ b/src/l2lCheckKcgKeyWord.mli
@@ -1,5 +1,5 @@
-(* Time-stamp: <modified the 17/08/2015 (at 16:58) by Erwan Jahier> *)
+(* Time-stamp: <modified the 10/01/2017 (at 18:07) by Erwan Jahier> *)
 
-(** Check that the inoput program does not use KCG keywords an ident *)
+(** Check that the input program does not use KCG keywords an ident *)
 
 val doit : LicPrg.t -> unit
diff --git a/src/lic2soc.ml b/src/lic2soc.ml
index 8d653db2..6ce04a93 100644
--- a/src/lic2soc.ml
+++ b/src/lic2soc.ml
@@ -1,4 +1,4 @@
-(** Time-stamp: <modified the 24/11/2016 (at 17:19) by Erwan Jahier> *)
+(** Time-stamp: <modified the 10/01/2017 (at 17:27) by Erwan Jahier> *)
 
 (* XXX ce module est mal écrit. A reprendre. (R1) *)
  
@@ -80,7 +80,11 @@ actuellement lors des l2l*.ml
 *)
 
  (* R1: je vire le "_" parce que ca m'emmerde ce truc. 
-                        On verra plus tard les clash de nom...*)
+                        On verra plus tard les clash de nom...
+
+bon, des clashs de nom, y'en a pas trop, mais avec les mots clefs C, si !
+ex: si un noeud a une variable qui s'appelle "long"...
+*)
 let user_var_prefix = "_"
 let user_var_prefix = ""
 
diff --git a/src/lv6version.ml b/src/lv6version.ml
index ab7cce7a..a6307319 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 = "678"
-let sha_1 = "50197035822142c871f65b41f584fde225359e4f"
+let commit = "679"
+let sha_1 = "946ca4cf5781377ea43c0247b80d09606517ea56"
 let str = (branch ^ "." ^ commit ^ " (" ^ sha_1 ^ ")")
 let maintainer = "jahier@imag.fr"
diff --git a/src/soc2c.ml b/src/soc2c.ml
index 98058d6f..c06d3d2a 100644
--- a/src/soc2c.ml
+++ b/src/soc2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 09/01/2017 (at 11:06) by Erwan Jahier> *)
+(* Time-stamp: <modified the 10/01/2017 (at 14:37) by Erwan Jahier> *)
 
 
 (* let put (os: out_channel) (fmt:('a, unit, string, unit) format4) : 'a = *)
@@ -943,7 +943,10 @@ typedef float _float;
     let l = (Soc2cGenAssign.gen_used_types socs) in
     if l <> [] then (
       output_string types_h_oc "// Defining array and extern types assignments \n";
-      List.iter (fun t -> output_string types_h_oc (Soc2cGenAssign.f t)) l
+      if Lv6MainArgs.global_opt.Lv6MainArgs.gen_wcet then 
+        List.iter (fun t -> output_string types_h_oc (Soc2cGenAssign.f_forloop t)) l
+      else
+        List.iter (fun t -> output_string types_h_oc (Soc2cGenAssign.f t)) l
     );
     puth "#endif\n";
     flush occ; close_out occ;
diff --git a/src/soc2cGenAssign.ml b/src/soc2cGenAssign.ml
index 22936915..5b12c602 100644
--- a/src/soc2cGenAssign.ml
+++ b/src/soc2cGenAssign.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 06/09/2016 (at 11:15) by Erwan Jahier> *)
+(* Time-stamp: <modified the 10/01/2017 (at 17:04) by Erwan Jahier> *)
 
 open Data
 open Lic
@@ -61,4 +61,22 @@ let (f: Data.t -> string) =
     in
     t_def
 
-
+let (f_forloop: Data.t -> string) =
+  fun t ->
+  let rec (aux: int -> string -> Data.t -> string) = fun i index t ->
+    match t with
+    | Array(t, size) ->
+       let index = Printf.sprintf "%s[_j%i]" index i in
+       let body = aux (i+1) index t in
+       Printf.sprintf "{ int _j%i; for (_j%i=0; _j%i<%i; _j%i++) { %s }}"
+                      i                     i       i size   i      body
+    | _ ->
+       Printf.sprintf " dest%s=source%s;" index index
+  in
+  let t_str = Soc2cIdent.type_to_short_string t in
+  let loop_def = aux 0 "" t in
+  Printf.sprintf "#ifndef _assign_%s 
+#define _assign_%s(dest, source, _dummy) %s 
+#endif
+                  " t_str t_str loop_def
+                 
diff --git a/src/soc2cGenAssign.mli b/src/soc2cGenAssign.mli
index 0abc6108..4c7553b2 100644
--- a/src/soc2cGenAssign.mli
+++ b/src/soc2cGenAssign.mli
@@ -1,9 +1,11 @@
- (* Time-stamp: <modified the 06/09/2016 (at 11:07) by Erwan Jahier> *)
+ (* Time-stamp: <modified the 10/01/2017 (at 17:49) by Erwan Jahier> *)
 
 (* Returns the list of non-trivial data types (i.e., arrays and
    extern) that are used in a program, with no duplicates *)
 val gen_used_types : Soc.t list -> Data.t list
 
-(* Generates a ccp macro the provides a default definition copying Data.t *)
+(* Generates a ccp macro that provides a default definition copying Data.t *)
 val f: Data.t -> string
 
+(* Ditto, but using a for loop instead of memcpy  *)
+val f_forloop: Data.t -> string
diff --git a/src/socPredef2cHeap.ml b/src/socPredef2cHeap.ml
index d4254d45..44509b58 100644
--- a/src/socPredef2cHeap.ml
+++ b/src/socPredef2cHeap.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 30/11/2016 (at 17:10) by Erwan Jahier> *)
+(* Time-stamp: <modified the 10/01/2017 (at 17:59) by Erwan Jahier> *)
 
 open Data
 open Soc
@@ -72,29 +72,38 @@ let (lustre_array: Soc.key -> string) =
  
 let (lustre_concat: Soc.key -> string) =
   fun (n,tl,si_opt) -> 
-    let ctx = get_ctx_name (n,tl,si_opt) in
-    let t,s1,s2 = match tl with
-      | [Data.Array(t,s1); Data.Array(_,s2); _] -> t,s1,s2
-      | _ -> assert false
-    in
-    let t1 = Printf.sprintf "%s.i1" ctx
-    and t2 = Printf.sprintf "%s.i2" ctx  
-    and t12 = Printf.sprintf "%s.out" ctx 
-    and t1_type = Soc2cUtil.data_type_to_c (Data.Array(t,s1)) ""
-    and t2_type = Soc2cUtil.data_type_to_c (Data.Array(t,s2)) "" 
-    in  
-      (Printf.sprintf "  memcpy(&%s, &%s, sizeof(%s));\n" t12 t1 t1_type)^ 
+  let ctx = get_ctx_name (n,tl,si_opt) in
+  let t,s1,s2 = match tl with
+    | [Data.Array(t,s1); Data.Array(_,s2); _] -> t,s1,s2
+    | _ -> assert false
+  in
+  let t1 = Printf.sprintf "%s.i1" ctx
+  and t2 = Printf.sprintf "%s.i2" ctx  
+  and t12 = Printf.sprintf "%s.out" ctx 
+  and t1_type = Soc2cUtil.data_type_to_c (Data.Array(t,s1)) ""
+  and t2_type = Soc2cUtil.data_type_to_c (Data.Array(t,s2)) "" 
+  in  
+  if not Lv6MainArgs.global_opt.Lv6MainArgs.gen_wcet then (
+    (Printf.sprintf "  memcpy(&%s, &%s, sizeof(%s));\n" t12 t1 t1_type)^ 
       (Printf.sprintf " // memcpy(&%s+%d, &%s, sizeof(%s));\n" t12 s1 t2 t2_type) ^
-      (Printf.sprintf "  memcpy(&%s[%d], &%s, sizeof(%s));\n" t12 s1 t2 t2_type)
-(* Both seems to work *)
-(*      let buff = ref "" in  *)
-(*      for j=0 to s1-1 do  *)
-(*        buff := !buff^(Soc2cHeap.gen_assign t (Printf.sprintf "%s.z[%d]" ctx j));  *)
-(*      done;  *)
-(*      for j=s1 to s1+s2-1 do  *)
-(*        buff := !buff^(Soc2cHeap.gen_assign t (Printf.sprintf "%s.z[%d]" ctx j));   *)
-(*      done;  *)
-(*      !buff  *)
+        (Printf.sprintf "  memcpy(&%s[%d], &%s, sizeof(%s));\n" t12 s1 t2 t2_type)
+  )
+  else
+    (* Both seems to work *)
+    let buff = ref "" in
+    for j=0 to s1-1 do
+      buff := !buff^(Soc2cHeap.gen_assign t
+                                          (Printf.sprintf "%s[%d]" t12 j)
+                                          (Printf.sprintf "%s[%d]" t1 j)
+                    );
+    done;
+    for j=s1 to s1+s2-1 do
+      buff := !buff^(Soc2cHeap.gen_assign t
+                                          (Printf.sprintf "%s[%d]" t12 j)
+                                          (Printf.sprintf "%s[%d]" t2 (j-s1))
+                    );
+    done;
+    !buff
 
 let (lustre_slice: Soc.key -> string) =
   fun (n,tl,si_opt) -> 
diff --git a/src/socPredef2cStack.ml b/src/socPredef2cStack.ml
index fc0773d3..8260a645 100644
--- a/src/socPredef2cStack.ml
+++ b/src/socPredef2cStack.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 30/11/2016 (at 17:09) by Erwan Jahier> *)
+(* Time-stamp: <modified the 10/01/2017 (at 18:04) by Erwan Jahier> *)
 
 open Data
 open Soc
@@ -72,19 +72,36 @@ let (lustre_array: Soc.key -> string) =
  
 let (lustre_concat: Soc.key -> string) =
   fun (n,tl,si_opt) -> 
-    let t,s1,s2 = match tl with
-      | [Data.Array(t,s1); Data.Array(_,s2); _] -> t,s1,s2
-      | _ -> assert false
-    in
-    let t1 = Printf.sprintf "i1"
-    and t2 = Printf.sprintf "i2" 
-    and t12 = Printf.sprintf "out" 
-    and t1_type = Soc2cUtil.data_type_to_c (Data.Array(t,s1)) ""
-    and t2_type = Soc2cUtil.data_type_to_c (Data.Array(t,s2)) "" 
-    in  
-      (Printf.sprintf "  memcpy(%s, %s, sizeof(%s));\n" t12 t1 t1_type)^ 
-      (Printf.sprintf "  memcpy(%s+%d, %s, sizeof(%s));\n" t12 s1 t2 t2_type) 
-(* Both seems to work *)
+  let t,s1,s2 = match tl with
+    | [Data.Array(t,s1); Data.Array(_,s2); _] -> t,s1,s2
+    | _ -> assert false
+  in
+  let t1 = Printf.sprintf "i1"
+  and t2 = Printf.sprintf "i2" 
+  and t12 = Printf.sprintf "out" 
+  and t1_type = Soc2cUtil.data_type_to_c (Data.Array(t,s1)) ""
+  and t2_type = Soc2cUtil.data_type_to_c (Data.Array(t,s2)) "" 
+  in  
+  if not Lv6MainArgs.global_opt.Lv6MainArgs.gen_wcet then (
+    (Printf.sprintf "  memcpy(%s, %s, sizeof(%s));\n" t12 t1 t1_type)^ 
+      (Printf.sprintf "  memcpy(%s+%d, %s, sizeof(%s));\n" t12 s1 t2 t2_type)
+  ) else (
+    (* Both seems to work *)
+    let buff = ref "" in
+    for j=0 to s1-1 do
+      buff := !buff^(Soc2cStack.gen_assign t
+                                          (Printf.sprintf "%s[%d]" t12 j)
+                                          (Printf.sprintf "%s[%d]" t1 j)
+                    );
+    done;
+    for j=s1 to s1+s2-1 do
+      buff := !buff^(Soc2cStack.gen_assign t
+                                          (Printf.sprintf "%s[%d]" t12 j)
+                                          (Printf.sprintf "%s[%d]" t2 (j-s1))
+                    );
+    done;
+    !buff
+  )
 (*      let buff = ref "" in  *)
 (*      for j=0 to s1-1 do  *)
 (*        buff := !buff^(Soc2cStack.gen_assign t (Printf.sprintf "%s.z[%d]" ctx j));   *)
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index 91608572..90829ba9 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test Run By jahier on Mon Jan  9 11:08:30 
+Test Run By jahier on Tue Jan 10 18:31: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 Mon Jan  9 11:08:31 
+Test Run By jahier on Tue Jan 10 18:31:10 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic1 tests ===
@@ -399,7 +399,7 @@ PASS: sh multipar.sh
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c multipar.lus  {}
 
 ==> lus2lic2.sum <==
-Test Run By jahier on Mon Jan  9 11:08:50 
+Test Run By jahier on Tue Jan 10 18:31:29 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic2 tests ===
@@ -745,7 +745,7 @@ PASS: sh zzz2.sh
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c zzz2.lus  {}
 
 ==> lus2lic3.sum <==
-Test Run By jahier on Mon Jan  9 11:09:29 
+Test Run By jahier on Tue Jan 10 18:32:08 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic3 tests ===
@@ -1251,7 +1251,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 Mon Jan  9 11:09:40 
+Test Run By jahier on Tue Jan 10 18:32:19 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -1778,11 +1778,11 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node zzz2.lus {}
 lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 1 seconds
 lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 19 seconds
 lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 39 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 10 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 29 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 11 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 30 seconds
 * Ref time: 
-0.06user 0.03system 1:39.43elapsed 0%CPU (0avgtext+0avgdata 5696maxresident)k
-128inputs+0outputs (0major+6056minor)pagefaults 0swaps
+0.05user 0.04system 1:39.96elapsed 0%CPU (0avgtext+0avgdata 5672maxresident)k
+96inputs+0outputs (0major+6149minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.05user 0.03system 0:45.66elapsed 0%CPU (0avgtext+0avgdata 5640maxresident)k
-96inputs+0outputs (0major+6039minor)pagefaults 0swaps
+0.06user 0.03system 0:50.78elapsed 0%CPU (0avgtext+0avgdata 5672maxresident)k
+64inputs+0outputs (0major+6154minor)pagefaults 0swaps
-- 
GitLab