From ec662f2854628055e0b1e1ce992a6eb3189fca6d Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Thu, 9 Oct 2014 16:33:29 +0200
Subject: [PATCH] soc2c: add an eponymous  --2c-no-switch (hidden) option

---
 _oasis                 |  2 +-
 src/lv6MainArgs.ml     |  9 ++++++++-
 src/lv6MainArgs.mli    |  1 +
 src/lv6version.ml      |  4 ++--
 src/soc2c.ml           | 13 +++++--------
 src/soc2cUtil.ml       | 19 ++++++++++++++++++-
 src/soc2cUtil.mli      | 24 +++++++++++++++++++++++-
 src/socPredef2cHeap.ml | 15 ++++++++-------
 test/lus2lic.sum       | 24 +++++++++++-------------
 test/lus2lic.time      | 14 ++++++--------
 10 files changed, 83 insertions(+), 42 deletions(-)

diff --git a/_oasis b/_oasis
index 57aec059..e25ebfdd 100644
--- a/_oasis
+++ b/_oasis
@@ -18,7 +18,7 @@ Executable lus2lic
   BuildDepends: str,unix,num,rdbg-plugin
   Build:true
   CompiledObject: native
-#  CompiledObject: byte
+	#  CompiledObject: byte
 
 # to use ocamldebug:
 #  - here: set CompiledObject from native to byte
diff --git a/src/lv6MainArgs.ml b/src/lv6MainArgs.ml
index 4c6c51d6..506f5acc 100644
--- a/src/lv6MainArgs.ml
+++ b/src/lv6MainArgs.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 03/10/2014 (at 11:26) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/10/2014 (at 16:33) 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,
@@ -54,6 +54,7 @@ type global_opt = {
   mutable line_num : int;
   mutable line_start_pos : int;
   mutable c_code_gen : c_code_gen_kind;
+  mutable soc2c_no_switch : bool;
 }
 let (global_opt:global_opt) =
   {
@@ -68,6 +69,7 @@ let (global_opt:global_opt) =
     current_file =  "";
     expand_enums =  AsInt;
     c_code_gen = Heap;
+    soc2c_no_switch = false
   }
 let (make_opt : unit -> t) = 
   fun () -> 
@@ -345,6 +347,11 @@ let mkoptab (opt:t) : unit = (
       ["Test the syntactic analysis"]
     ;
     *)
+    mkopt opt ~hide:true
+      ["--2c-no-switch"]
+      (Arg.Unit (fun () -> global_opt.soc2c_no_switch <-true))
+      ["Use if-then-else instead of switch when generating C codes"]
+    ;
     mkopt opt ~hide:true
       ["-interface"]
       (Arg.Unit (fun () -> opt.print_interface<-true))
diff --git a/src/lv6MainArgs.mli b/src/lv6MainArgs.mli
index 533843a1..4dc824c9 100644
--- a/src/lv6MainArgs.mli
+++ b/src/lv6MainArgs.mli
@@ -51,6 +51,7 @@ type global_opt = {
   mutable line_num : int;
   mutable line_start_pos : int;
   mutable c_code_gen : c_code_gen_kind;
+  mutable soc2c_no_switch : bool;
 }
 val paranoid : Verbose.flag 
 
diff --git a/src/lv6version.ml b/src/lv6version.ml
index 46e704dd..425e0081 100644
--- a/src/lv6version.ml
+++ b/src/lv6version.ml
@@ -1,7 +1,7 @@
 (** Automatically generated from Makefile *) 
 let tool = "lus2lic"
 let branch = "(no"
-let commit = "536"
-let sha_1 = "6c4f5c6abdf19fe5558e0caed3388f80ed4f7cfb"
+let commit = "537"
+let sha_1 = "244b252de2d9002b9e87972eb28532fea2fedcad"
 let str = (branch ^ "." ^ commit ^ " (" ^ sha_1 ^ ")")
 let maintainer = "jahier@imag.fr"
diff --git a/src/soc2c.ml b/src/soc2c.ml
index ff1430fc..bd693c77 100644
--- a/src/soc2c.ml
+++ b/src/soc2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 08/10/2014 (at 18:35) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/10/2014 (at 15:56) by Erwan Jahier> *)
 
 
 (* let put (os: out_channel) (fmt:('a, unit, string, unit) format4) : 'a = *)
@@ -61,9 +61,9 @@ let (gao2c : Soc.tbl -> 'a soc_pp -> Soc.gao -> unit) =
       match gao with
         | Case(id, id_gao_l) -> ( 
           let to_case_str (v,gaol) =
-            let gaol_str = (List.map gao2str gaol)@["break;"] in
-            let gaol_block = String.concat "      " gaol_str in
-            sprintf "\n    case %s:\n   %s" (id2s v) gaol_block
+            let gaol_str = (List.map gao2str gaol) in
+            let gaol_block = String.concat "" gaol_str in
+            (id2s v), gaol_block
           in
           let cases = List.map to_case_str id_gao_l in
           let ctx_opt = 
@@ -73,9 +73,7 @@ let (gao2c : Soc.tbl -> 'a soc_pp -> Soc.gao -> unit) =
                else Soc2cUtil.M_IO)
             else Soc2cUtil.Local 
           in
-          let str = sprintf "   switch(%s){%s\n   }\n" 
-            (Soc2cDep.ctx_var ctx_opt id) 
-            (String.concat "\n" cases) in
+          let str = Soc2cUtil.gen_c_switch  (Soc2cDep.ctx_var ctx_opt id) cases in
           str
         )
         | Call(vel_out, Assign, vel_in) -> (
@@ -102,7 +100,6 @@ let (gao2c : Soc.tbl -> 'a soc_pp -> Soc.gao -> unit) =
             raise Delete_C_files
           ); 
           Soc2cDep.gen_step_call sp.soc called_soc vel_out vel_in ctx "step" ""
-          
         )
     in
     sp.cput (gao2str gao)
diff --git a/src/soc2cUtil.ml b/src/soc2cUtil.ml
index a697ff3f..8a66ad10 100644
--- a/src/soc2cUtil.ml
+++ b/src/soc2cUtil.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 03/10/2014 (at 10:21) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/10/2014 (at 16:31) by Erwan Jahier> *)
 
 open Soc2cIdent
 open Data
@@ -69,3 +69,20 @@ let rec (lic_type_to_c_old: Lic.type_  -> string -> string) =
     | Lic.TypeVar Lic.AnyNum -> assert false
 
 
+(* exported *)
+let (gen_c_switch : string -> (string * string) list -> string) = 
+  fun cond_var cases ->
+    if Lv6MainArgs.global_opt.soc2c_no_switch then
+      let l = List.map
+        (fun (v,code) -> Printf.sprintf "  if (%s == %s) {\n%s\n  } else {\n" cond_var v code) 
+        cases
+      in
+      let switch = String.concat "" l in
+      let closing_curly = String.concat "" (List.map (fun _ -> "}") cases) in
+      Printf.sprintf "%s  %s" switch closing_curly
+    else
+        let case_list = List.map
+          (fun (v, code) -> Printf.sprintf "  case %s:\n%s  break;\n" v code) cases  
+        in
+        let cases = String.concat "" case_list in
+        Printf.sprintf "  switch (%s){\n%s}\n" cond_var cases
diff --git a/src/soc2cUtil.mli b/src/soc2cUtil.mli
index 344f941e..7cf0e1fc 100644
--- a/src/soc2cUtil.mli
+++ b/src/soc2cUtil.mli
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 03/10/2014 (at 10:17) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/10/2014 (at 15:37) by Erwan Jahier> *)
 
 (** *)
 
@@ -10,3 +10,25 @@ type var_kind = (* XXX poor names: fixme! *)
   | ML_IO of Soc.key (* for idents that are part of a MemoryLess soc interface *)
   | M_IO (* for idents that are part of a soc interface with Memories*)
   | Local (* for soc local variables *)
+
+
+(**  Dependending on Lv6MainArgs.global.soc2_no_switch 
+    [gen_c_switch "c" ["case1","stmt1" ; "case2","stmt2" ; ... ]
+returns
+
+    switch(c) {
+      case case1: stmt1 ; break;
+      case case2: stmt2 ; break;
+      ...
+    }
+
+or
+
+    if (c == case1) { stmt1 } else {
+    if (c == case2) { stmt2 } else {
+    ...
+    nop;
+    }....}
+
+*)
+val gen_c_switch : string -> (string * string) list -> string
diff --git a/src/socPredef2cHeap.ml b/src/socPredef2cHeap.ml
index 7fb512a3..79bd7bdc 100644
--- a/src/socPredef2cHeap.ml
+++ b/src/socPredef2cHeap.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 08/10/2014 (at 17:44) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/10/2014 (at 16:09) by Erwan Jahier> *)
 
 open Data
 open Soc
@@ -54,16 +54,17 @@ let (lustre_merge : Soc.key -> string) =
         Printf.sprintf"  %s.z = (%s.clk) ? %s.x1 : %s.x2  ;\n" ctx ctx ctx ctx
       | Enum(en,el) ->
         let case_list = List.mapi
-          (fun i e -> Printf.sprintf "    case %s: %s.x%i; break;\n" (id2s e) ctx i) el  
+          (fun i e -> (id2s e), Printf.sprintf "%s.x%i;"  ctx i) el  
         in
-        let cases = String.concat "" case_list in
-        Printf.sprintf"  %s.z =\n    switch (%s.clk){\n%s}\n" ctx ctx cases
+        let switch = Soc2cUtil.gen_c_switch  (Printf.sprintf"%s.clk" ctx) case_list in
+        Printf.sprintf"  %s.z =\n    %s"  ctx switch
       | Int ->
         let case_list = List.mapi
-          (fun i e -> Printf.sprintf "    case %i: %s.z = %s.x%i; break;\n" i ctx ctx i) tl  
+          (fun i e -> (string_of_int i), (Printf.sprintf "    %s.z = %s.x%i;" ctx ctx i)) 
+          tl  
         in
-        let cases = String.concat "" case_list in
-        Printf.sprintf"  switch (%s.clk){\n%s}\n" ctx cases
+        Soc2cUtil.gen_c_switch  (Printf.sprintf"%s.clk" ctx) case_list
+
       | _ -> assert false
 
 
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index 559dfa27..0b56e984 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test Run By jahier on Thu Oct  9 10:16:53 
+Test Run By jahier on Thu Oct  9 16:24:18 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic0 tests ===
@@ -63,7 +63,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 Thu Oct  9 10:16:55 
+Test Run By jahier on Thu Oct  9 16:24:19 
 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 Thu Oct  9 10:16:57 
+Test Run By jahier on Thu Oct  9 16:24:38 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic2 tests ===
@@ -727,7 +727,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 Thu Oct  9 10:17:01 
+Test Run By jahier on Thu Oct  9 16:25:39 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic3 tests ===
@@ -1230,7 +1230,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 Thu Oct  9 10:16:59 
+Test Run By jahier on Thu Oct  9 16:25:57 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -1727,13 +1727,11 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node zzz2.lus {}
 ===============================
 # Total number of failures: 14
 lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 0 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 27 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 68 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 21 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 68 seconds
+lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 19 seconds
+lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 61 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 18 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 65 seconds
 * Ref time: 
-0.04user 0.09system 3:31.78elapsed 0%CPU (0avgtext+0avgdata 3004maxresident)k
-120inputs+0outputs (0major+14863minor)pagefaults 0swaps
+0.06user 0.07system 2:44.11elapsed 0%CPU (0avgtext+0avgdata 3000maxresident)k
+120inputs+0outputs (0major+14855minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.06user 0.08system 1:14.05elapsed 0%CPU (0avgtext+0avgdata 3004maxresident)k
-120inputs+0outputs (0major+14862minor)pagefaults 0swaps
diff --git a/test/lus2lic.time b/test/lus2lic.time
index 363a6028..a1e9d348 100644
--- a/test/lus2lic.time
+++ b/test/lus2lic.time
@@ -1,11 +1,9 @@
 lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 0 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 27 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 68 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 21 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 68 seconds
+lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 19 seconds
+lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 61 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 18 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 65 seconds
 * Ref time: 
-0.04user 0.09system 3:31.78elapsed 0%CPU (0avgtext+0avgdata 3004maxresident)k
-120inputs+0outputs (0major+14863minor)pagefaults 0swaps
+0.06user 0.07system 2:44.11elapsed 0%CPU (0avgtext+0avgdata 3000maxresident)k
+120inputs+0outputs (0major+14855minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.06user 0.08system 1:14.05elapsed 0%CPU (0avgtext+0avgdata 3004maxresident)k
-120inputs+0outputs (0major+14862minor)pagefaults 0swaps
-- 
GitLab