From 41fb42c62b060d1ceb88eb781b36dd5f3ca11501 Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Tue, 24 Jun 2014 15:27:09 +0200
Subject: [PATCH] Soc2c: make sure to use memcpy (ie, gen_assign) everywhere it
 is necessary.

I've sligthly modified the profile of gen_assign to explicitly ask for the
size of the data structure to be copied.

nb:  unexpected failures 131 -> 127
---
 src/soc2cUtil.ml   | 18 +++++-----
 src/socPredef2c.ml | 34 ++++++++++++------
 test/lus2lic.sum   | 16 ++++-----
 test/lus2lic.time  |  2 +-
 todo.org           | 86 +++++++++++++++++++++-------------------------
 5 files changed, 82 insertions(+), 74 deletions(-)

diff --git a/src/soc2cUtil.ml b/src/soc2cUtil.ml
index 5f749b70..466f46aa 100644
--- a/src/soc2cUtil.ml
+++ b/src/soc2cUtil.ml
@@ -1,18 +1,18 @@
-(* Time-stamp: <modified the 20/06/2014 (at 17:23) by Erwan Jahier> *)
+(* Time-stamp: <modified the 24/06/2014 (at 14:28) by Erwan Jahier> *)
 
 
 (* exported *) 
-let rec (gen_assign : Data.t  -> string -> string -> string) =
-  fun t vi vo -> 
+let rec (gen_assign : Data.t  -> string -> string -> string -> string) =
+  fun t vi vo size -> 
     match t with
-      | Alias(_,t) -> gen_assign t vi vo
+      | Alias(_,t) -> gen_assign t vi vo size
       | Enum _  
       | Struct(_) (* should I rather use memcpy for struct? *)
       | Bool | Int | Real -> 
         Printf.sprintf "  %s = %s;\n" vi vo
       | Alpha(_) (* dead code ? *)
       | Array(_) -> 
-        Printf.sprintf "  memcpy(%s, %s, sizeof(%s));\n" vi vo vo
+        Printf.sprintf "  memcpy(%s, %s, %s);\n" vi vo size
 
       | Extern (id) -> 
         (* what should i do for extern types? Ask the user to provide the
@@ -48,7 +48,7 @@ let (gen_step_call : Soc.t -> Soc.t -> string list -> string list ->
         let inputs = fst called_soc.profile in
         let l = try (
           List.map2 (fun (name, t) ve -> 
-            gen_assign t (Printf.sprintf "%s.%s" ctx name) ve) 
+            gen_assign t (Printf.sprintf "%s.%s" ctx name) ve (Printf.sprintf "sizeof(%s)" ve)) 
             inputs vel_in
         ) with _ -> assert false (* are all parameters necessaryly used? *)
         in
@@ -59,9 +59,9 @@ let (gen_step_call : Soc.t -> Soc.t -> string list -> string list ->
         let outputs = snd called_soc.profile in
         let l = try (
           List.map2
-            (fun  (name,t)  ve -> 
-              gen_assign t ve
-                (Printf.sprintf "%s.%s" ctx name)) outputs vel_out
+            (fun  (name,t) ve -> 
+              let ve2 = Printf.sprintf "%s.%s" ctx name in
+              gen_assign t ve ve2 (Printf.sprintf "sizeof(%s)" ve2)) outputs vel_out
         ) with _ -> assert false
         in
         (String.concat "" l) ^"\n"
diff --git a/src/socPredef2c.ml b/src/socPredef2c.ml
index 76e2d716..87897380 100644
--- a/src/socPredef2c.ml
+++ b/src/socPredef2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 23/06/2014 (at 16:54) by Erwan Jahier> *)
+(* Time-stamp: <modified the 24/06/2014 (at 15:22) by Erwan Jahier> *)
 
 open Data
 open Soc
@@ -6,6 +6,9 @@ open Soc2cIdent
 
 (* A boring but simple module... *)
 
+(* XXX should i use gen_assign here? for the time being, its useless as
+   there is no binop (nor unop) over arrays.
+*)
 let (lustre_binop : Soc.key -> string -> string) =
   fun sk op  -> 
     let ctx = get_ctx_name sk in
@@ -14,24 +17,31 @@ let (lustre_binop : Soc.key -> string -> string) =
 let (lustre_unop : Soc.key -> string -> string) =
   fun sk op  -> 
     let ctx = get_ctx_name sk in
+    (* use gen_assign? *)
     Printf.sprintf"  %s.z = %s %s.x;\n" ctx op ctx
 
 let (lustre_ite : Soc.key -> string) =
   fun sk -> 
     let ctx = get_ctx_name sk in
-    Printf.sprintf"  %s.z = (%s.c)? %s.xt : %s.xe;\n" ctx ctx ctx ctx
+    let (_,_::t::_,_) = sk in
+(*     Printf.sprintf"  %s.z = (%s.c)? %s.xt : %s.xe;\n" ctx ctx ctx ctx *)
+    Soc2cUtil.gen_assign t (Printf.sprintf "%s.z" ctx)
+       (Printf.sprintf "(%s.c)? %s.xt : %s.xe" ctx ctx ctx)
+       (Printf.sprintf "sizeof(%s.z)" ctx)
 
 let (lustre_impl : Soc.key -> string) =
   fun sk -> 
     let ctx = get_ctx_name sk in
-    Printf.sprintf"  %s.z = (!%s.x || %s.y);\n" ctx ctx ctx
+    (* use gen_assign? *)
+    Printf.sprintf"  %s.z = (!%s.x || %s.y);\n" ctx ctx ctx 
 
 let (lustre_arrow : Soc.key -> string) =
   fun sk -> 
-(*     let ctx = get_ctx_name sk in *)
-(*     let x,y,z = ctx^".x", ctx^".y", ctx^".z" in *)
     let x,y,z = "ctx->x", "ctx->y", "ctx->z" in
-    (Printf.sprintf"  %s = (ctx->_memory)? %s : %s;\n" z x y) ^
+    let (_,t::_,_) = sk in
+    let vo = Printf.sprintf"((ctx->_memory)? %s : %s)" x y in
+    let size = Printf.sprintf "sizeof(%s)" x in
+    (Soc2cUtil.gen_assign t z vo size) ^ 
       ("  ctx->_memory = _false;\n")
       
 let (lustre_merge : Soc.key -> string) =
@@ -66,7 +76,7 @@ let (lustre_hat : Soc.key -> string) =
     let buff = ref "" in
     for j=0 to i-1 do
       buff := !buff^(Soc2cUtil.gen_assign t (Printf.sprintf "%s.z[%d]" ctx j)
-                       (Printf.sprintf "%s.x" ctx)); 
+                       (Printf.sprintf "%s.x" ctx) (Printf.sprintf "sizeof(%s.x)" ctx)); 
     done;
     !buff
 
@@ -80,7 +90,8 @@ let (lustre_array: Soc.key -> string) =
     let buff = ref "" in
     for j=0 to i-1 do
       buff := !buff^(Soc2cUtil.gen_assign t (Printf.sprintf "%s.z[%d]" ctx j)
-                       (Printf.sprintf "%s.x%d" ctx (j+1))); 
+                       (Printf.sprintf "%s.x%d" ctx (j+1)) 
+                       (Printf.sprintf "sizeof(%s.x%d)" ctx (j+1))); 
     done;
     !buff
  
@@ -122,7 +133,9 @@ let (lustre_slice: Soc.key -> string) =
         for i = b to e do
           if (i-b) mod step = 0 then (
             buff := !buff^(Soc2cUtil.gen_assign t (Printf.sprintf "%s.z[%d]" ctx !j)  
-                             (Printf.sprintf "%s.x[%d]" ctx i));   
+                             (Printf.sprintf "%s.x[%d]" ctx i)
+                             (Printf.sprintf "sizeof(%s.x[%d])" ctx i)
+            );   
             incr j);
           done; 
           !buff
@@ -254,7 +267,8 @@ let (get_iterator : Soc.t -> string -> Soc.t -> int -> string) =
       let type_in  = snd (List.hd iter_inputs) in
       let a_in  = ctx_access ^ (fst (List.hd iter_inputs)) in
       let a_out = ctx_access ^ (fst (List.hd iter_outputs)) in
-      buff := !buff^(Soc2cUtil.gen_assign type_in a_out a_in)  (* a_out=a_n *)
+      buff := !buff^(Soc2cUtil.gen_assign type_in a_out a_in
+                       (Printf.sprintf "sizeof(%s)" a_in))  (* a_out=a_n *)
     );
     !buff
  
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index 58244935..37211f2b 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,4 +1,4 @@
-Test Run By jahier on Tue Jun 24 15:01:05 2014
+Test Run By jahier on Tue Jun 24 15:23:11 2014
 Native configuration is i686-pc-linux-gnu
 
 		=== lus2lic tests ===
@@ -55,7 +55,7 @@ PASS: ./lus2lic {-ec -o /tmp/Gyroscope2.ec should_work/Gyroscope2.lus}
 PASS: ./myec2c {-o /tmp/Gyroscope2.c /tmp/Gyroscope2.ec}
 FAIL: Try to compare lus2lic -exec and ecexe: ../utils/test_lus2lic_no_node should_work/Gyroscope2.lus
 PASS: ./lus2lic {-2c should_work/Gyroscope2.lus -n Gyroscope2}
-FAIL: Check that the generated C code compiles  : gcc Gyroscope2_Gyroscope2.c Gyroscope2_Gyroscope2_loop.c 
+PASS: gcc Gyroscope2_Gyroscope2.c Gyroscope2_Gyroscope2_loop.c 
 PASS: ./lus2lic {-o /tmp/mouse2.lic should_work/mouse2.lus}
 PASS: ./lus2lic {-ec -o /tmp/mouse2.ec should_work/mouse2.lus}
 PASS: ./myec2c {-o /tmp/mouse2.c /tmp/mouse2.ec}
@@ -365,7 +365,7 @@ PASS: ./lus2lic {-ec -o /tmp/is_stable.ec should_work/is_stable.lus}
 PASS: ./myec2c {-o /tmp/is_stable.c /tmp/is_stable.ec}
 PASS: ../utils/test_lus2lic_no_node should_work/is_stable.lus
 PASS: ./lus2lic {-2c should_work/is_stable.lus -n is_stable}
-FAIL: Check that the generated C code compiles  : gcc is_stable_is_stable.c is_stable_is_stable_loop.c 
+PASS: gcc is_stable_is_stable.c is_stable_is_stable_loop.c 
 PASS: ./lus2lic {-o /tmp/test_clash.lic should_work/test_clash.lus}
 PASS: ./lus2lic {-ec -o /tmp/test_clash.ec should_work/test_clash.lus}
 PASS: ./myec2c {-o /tmp/test_clash.c /tmp/test_clash.ec}
@@ -1156,7 +1156,7 @@ PASS: ./lus2lic {-ec -o /tmp/hanane.ec should_work/hanane.lus}
 PASS: ./myec2c {-o /tmp/hanane.c /tmp/hanane.ec}
 FAIL: Try to compare lus2lic -exec and ecexe: ../utils/test_lus2lic_no_node should_work/hanane.lus
 PASS: ./lus2lic {-2c should_work/hanane.lus -n hanane}
-FAIL: Check that the generated C code compiles  : gcc hanane_hanane.c hanane_hanane_loop.c 
+PASS: gcc hanane_hanane.c hanane_hanane_loop.c 
 PASS: ./lus2lic {-o /tmp/lustre.lic should_work/lustre.lus}
 PASS: ./lus2lic {-ec -o /tmp/lustre.ec should_work/lustre.lus}
 PASS: ./myec2c {-o /tmp/lustre.c /tmp/lustre.ec}
@@ -1280,7 +1280,7 @@ PASS: ./lus2lic {-ec -o /tmp/arrays.ec should_work/arrays.lus}
 PASS: ./myec2c {-o /tmp/arrays.c /tmp/arrays.ec}
 PASS: ../utils/test_lus2lic_no_node should_work/arrays.lus
 PASS: ./lus2lic {-2c should_work/arrays.lus -n arrays}
-FAIL: Check that the generated C code compiles  : gcc arrays_arrays.c arrays_arrays_loop.c 
+PASS: gcc arrays_arrays.c arrays_arrays_loop.c 
 PASS: ./lus2lic {-o /tmp/nc3.lic should_work/nc3.lus}
 PASS: ./lus2lic {-ec -o /tmp/nc3.ec should_work/nc3.lus}
 PASS: ./myec2c {-o /tmp/nc3.c /tmp/nc3.ec}
@@ -1482,9 +1482,9 @@ XPASS: Test bad programs (semantics): lus2lic {-o /tmp/bug.lic should_fail/seman
 
 		=== lus2lic Summary ===
 
-# of expected passes		1281
-# of unexpected failures	131
+# of expected passes		1285
+# of unexpected failures	127
 # of unexpected successes	21
 # of expected failures		37
-testcase ./lus2lic.tests/non-reg.exp completed in 137 seconds
+testcase ./lus2lic.tests/non-reg.exp completed in 138 seconds
 testcase ./lus2lic.tests/progression.exp completed in 0 seconds
diff --git a/test/lus2lic.time b/test/lus2lic.time
index 484c0604..287b41c9 100644
--- a/test/lus2lic.time
+++ b/test/lus2lic.time
@@ -1,2 +1,2 @@
-testcase ./lus2lic.tests/non-reg.exp completed in 137 seconds
+testcase ./lus2lic.tests/non-reg.exp completed in 138 seconds
 testcase ./lus2lic.tests/progression.exp completed in 0 seconds
diff --git a/todo.org b/todo.org
index 8180560b..5b35d401 100644
--- a/todo.org
+++ b/todo.org
@@ -51,52 +51,46 @@ http://www.di.ens.fr/~pouzet/bib/lctes12.pdf
 ** TODO les programmes qui ne passent pas le test:
 
 
-1. file:test/should_work/Gyroscope2.lus
-2. file:test/should_work/mouse2.lus
-3. file:test/should_work/mappredef.lus
-4. file:test/should_work/predefOp.lus
-5. file:test/should_work/matrice2.lus
-6. file:test/should_work/ply02.lus
-7. file:test/should_work/is.lus
-8. file:test/should_work/onlyroll2.lus
-9. file:test/should_work/morel3.lus
-10. file:test/should_work/ply03.lus
-11. file:test/should_work/plus.lus
-12. file:test/should_work/xx.lus
-13. file:test/should_work/call07.lus
-14. file:test/should_work/morel5.lus
-15. file:test/should_work/test.lus
-16. file:test/should_work/clock.lus
-17. file:test/should_work/morel4.lus
-18. file:test/should_work/bad.lus
-19. file:test/should_work/onlyroll.lus
-20. file:test/should_work/mapinf.lus
-21. file:test/should_work/over2.lus
-22. file:test/should_work/over3.lus
-23. file:test/should_work/test.lus
-24. file:test/should_work/carV2.lus
-25. file:test/should_work/test.lus
-26. file:test/should_work/CURRENT.lus
-27. file:test/should_work/left.lus
-28. file:test/should_work/morel.lus
-29. file:test/should_work/matrice.lus
-30. file:test/should_work/exclusion.lus
-31. file:test/should_work/pack1.lus
-32. file:test/should_work/bob.lus
-33. file:test/should_work/morel2.lus
-34. file:test/should_work/xxx.lus
-35. file:test/should_work/filliter.lus
-36. file:test/should_work/Gyroscope.lus
-37. file:test/should_work/mapdeRed.lus
-38. file:test/should_work/simpleRed.lus
-39. file:test/should_work/hanane.lus
-40. file:test/should_work/ck7.lus
-41. file:test/should_work/redoptest.lus
-42. file:test/should_work/cond01.lus
-43. file:test/should_work/arrays.lus
-44. file:test/should_work/overload.lus
-45. file:test/should_work/simple.lus
-46. file:test/should_work/minus.lus
+1. file:test/should_work/mouse2.lus lus2lic -2c should_work/mouse2.lus -n mouse2
+2. file:test/should_work/mappredef.lus lus2lic -2c should_work/mappredef.lus -n mappredef
+3. file:test/should_work/predefOp.lus lus2lic -2c should_work/predefOp.lus -n predefOp
+4. file:test/should_work/matrice2.lus lus2lic -2c should_work/matrice2.lus -n matrice2
+5. file:test/should_work/ply02.lus lus2lic -2c should_work/ply02.lus -n ply02
+6. file:test/should_work/onlyroll2.lus lus2lic -2c should_work/onlyroll2.lus -n onlyroll2
+7. file:test/should_work/morel3.lus lus2lic -2c should_work/morel3.lus -n morel3
+8. file:test/should_work/ply03.lus lus2lic -2c should_work/ply03.lus -n ply03
+9. file:test/should_work/plus.lus lus2lic -2c should_work/plus.lus -n plus
+10. file:test/should_work/xx.lus lus2lic -2c should_work/xx.lus -n xx
+11. file:test/should_work/call07.lus lus2lic -2c should_work/call07.lus -n call07
+12. file:test/should_work/test.lus lus2lic -2c should_work/test.lus -n test
+13. file:test/should_work/clock.lus lus2lic -2c should_work/clock.lus -n clock
+14. file:test/should_work/morel4.lus lus2lic -2c should_work/morel4.lus -n morel4
+15. file:test/should_work/bad.lus lus2lic -2c should_work/bad.lus -n bad
+16. file:test/should_work/onlyroll.lus lus2lic -2c should_work/onlyroll.lus -n onlyroll
+17. file:test/should_work/mapinf.lus lus2lic -2c should_work/mapinf.lus -n mapinf
+18. file:test/should_work/over2.lus lus2lic -2c should_work/over2.lus -n over2
+19. file:test/should_work/over3.lus lus2lic -2c should_work/over3.lus -n over3
+20. file:test/should_work/test.lus lus2lic -2c should_work/test.lus -n test
+21. file:test/should_work/carV2.lus lus2lic -2c should_work/carV2.lus -n carV2
+22. file:test/should_work/test.lus lus2lic -2c should_work/test.lus -n test
+23. file:test/should_work/CURRENT.lus lus2lic -2c should_work/CURRENT.lus -n CURRENT
+24. file:test/should_work/left.lus lus2lic -2c should_work/left.lus -n left
+25. file:test/should_work/morel.lus lus2lic -2c should_work/morel.lus -n morel
+26. file:test/should_work/matrice.lus lus2lic -2c should_work/matrice.lus -n matrice
+27. file:test/should_work/exclusion.lus lus2lic -2c should_work/exclusion.lus -n exclusion
+28. file:test/should_work/pack1.lus lus2lic -2c should_work/pack1.lus -n pack1
+29. file:test/should_work/bob.lus lus2lic -2c should_work/bob.lus -n bob
+30. file:test/should_work/morel2.lus lus2lic -2c should_work/morel2.lus -n morel2
+31. file:test/should_work/xxx.lus lus2lic -2c should_work/xxx.lus -n xxx
+32. file:test/should_work/filliter.lus lus2lic -2c should_work/filliter.lus -n filliter
+33. file:test/should_work/mapdeRed.lus lus2lic -2c should_work/mapdeRed.lus -n mapdeRed
+34. file:test/should_work/simpleRed.lus lus2lic -2c should_work/simpleRed.lus -n simpleRed
+35. file:test/should_work/ck7.lus lus2lic -2c should_work/ck7.lus -n ck7
+36. file:test/should_work/redoptest.lus lus2lic -2c should_work/redoptest.lus -n redoptest
+37. file:test/should_work/cond01.lus lus2lic -2c should_work/cond01.lus -n cond01
+38. file:test/should_work/overload.lus lus2lic -2c should_work/overload.lus -n overload
+39. file:test/should_work/simple.lus lus2lic -2c should_work/simple.lus -n simple
+40. file:test/should_work/minus.lus lus2lic -2c should_work/minus.lus -n minus
 
 
 * Packages, modeles, etc.
-- 
GitLab