From 946ca4cf5781377ea43c0247b80d09606517ea56 Mon Sep 17 00:00:00 2001
From: Erwan Jahier <jahier@imag.fr>
Date: Mon, 9 Jan 2017 13:58:12 +0100
Subject: [PATCH] soc2c: fix the code generated for array constants

which was not even compiling...
---
 Makefile.dev      |  2 +-
 _oasis            |  2 +-
 src/evalConst.ml  |  2 +-
 src/lic.ml        |  4 ++--
 src/licDump.ml    |  7 ++++---
 src/lv6errors.ml  |  5 +++--
 src/lv6version.ml |  4 ++--
 src/soc2c.ml      | 44 +++++++++++++++++++++++++++++++-------------
 test/lus2lic.sum  | 26 +++++++++++++-------------
 9 files changed, 58 insertions(+), 38 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev
index a4dd501d..2e2e7258 100644
--- a/Makefile.dev
+++ b/Makefile.dev
@@ -23,7 +23,7 @@ qtest:
 
 #######################################################################################
 # Workflow:
-# C=git commit ;  A=git amend ; U=update_version ; O=opam pack
+# C=git commit ;  A=git amend ; U=update_version ; P=opam pack
 # to make sure the sha and the version are good, one should never
 # do U and then A.
 # to avoid such problems, legal trace are defined by this automata:
diff --git a/_oasis b/_oasis
index de764e3c..89993b92 100644
--- a/_oasis
+++ b/_oasis
@@ -1,6 +1,6 @@
 OASISFormat: 0.4
 Name:        lustre-v6
-Version:     1.677
+Version:     1.678
 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/evalConst.ml b/src/evalConst.ml
index af1d0f9b..f80846d2 100644
--- a/src/evalConst.ml
+++ b/src/evalConst.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 27/05/2016 (at 16:01) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/01/2017 (at 11:00) by Erwan Jahier> *)
 
 
 open Printf 
diff --git a/src/lic.ml b/src/lic.ml
index 62af29fe..b38ac363 100644
--- a/src/lic.ml
+++ b/src/lic.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 30/11/2016 (at 16:52) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/01/2017 (at 11:08) by Erwan Jahier> *)
 
 (** Define the Data Structure representing Compiled programs. By
     compiled we mean that constant are propagated, packages are
@@ -230,7 +230,7 @@ and const =
   (* type_ structure : liste (champ,valeur) + type_ structure *)
   | Struct_const_eff of ((Lv6Id.t * const) list * type_)
   (* type_ tableau : liste des valeurs + type_ des elts + taille *)
-  | Array_const_eff of (const list * type_)
+  | Array_const_eff of (const list * type_) (* type of the const element *)
   | Tuple_const_eff of const list
 (*---------------------------------------------------------------------
   Type: val   
diff --git a/src/licDump.ml b/src/licDump.ml
index 7f13b214..ff29b103 100644
--- a/src/licDump.ml
+++ b/src/licDump.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 14/09/2016 (at 23:51) by jahier> *)
+(* Time-stamp: <modified the 09/01/2017 (at 11:03) by Erwan Jahier> *)
 
 open Lv6errors
 open Printf
@@ -75,7 +75,7 @@ let rec string_of_const_eff =
     let flst = List.map string_of_field fl in
     (string_of_type_eff t)^"{"^(String.concat "; " flst)^"}"
   )
-  | Array_const_eff (ctab, t) -> (
+  | Array_const_eff (ctab, _t) -> (
     let vl = List.map string_of_const_eff ctab in
     "["^(String.concat ", " vl)^"]"
   )
@@ -126,7 +126,8 @@ and string_ident_of_const_eff c =
     | Struct_type_eff (sn,_) -> Lv6Id.no_pack_string_of_long sn
     | _ -> assert false
   )
-  | Array_const_eff (ctab, t) -> string_of_type_eff t
+  | Array_const_eff (ctab, t) ->
+     (string_of_type_eff t) ^ "_" ^(string_of_int (List.length ctab))
   | Tuple_const_eff cl ->  string_ident_of_const_eff_list cl
 
 and string_ident_of_const_eff_list cl =
diff --git a/src/lv6errors.ml b/src/lv6errors.ml
index 6e1b6146..984d41ec 100644
--- a/src/lv6errors.ml
+++ b/src/lv6errors.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 26/02/2015 (at 11:22) by Erwan Jahier> *)
+(* Time-stamp: <modified the 06/01/2017 (at 16:32) by Erwan Jahier> *)
 
 (** *)
 
@@ -120,7 +120,8 @@ let print_compile_error lxm msg = (
 Warning (associé à un lexeme) sur stderr
 ----------------------------------------------------------------------*)
 let warning lxm msg = (
-   Printf.eprintf  "Warning. %s:\n---> %s\n" (Lxm.details lxm) msg
+  Printf.eprintf  "Warning. %s:\n---> %s\n" (Lxm.details lxm) msg;
+  flush stderr
 )
 
 (** ---------------------------------------------------------------------
diff --git a/src/lv6version.ml b/src/lv6version.ml
index e70b02ba..ab7cce7a 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 = "677"
-let sha_1 = "85c5034f2b9a788cc1ab75c18815caae7bdb7d5c"
+let commit = "678"
+let sha_1 = "50197035822142c871f65b41f584fde225359e4f"
 let str = (branch ^ "." ^ commit ^ " (" ^ sha_1 ^ ")")
 let maintainer = "jahier@imag.fr"
diff --git a/src/soc2c.ml b/src/soc2c.ml
index 18fb422c..98058d6f 100644
--- a/src/soc2c.ml
+++ b/src/soc2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 06/01/2017 (at 10:24) by Erwan Jahier> *)
+(* Time-stamp: <modified the 09/01/2017 (at 11:06) by Erwan Jahier> *)
 
 
 (* let put (os: out_channel) (fmt:('a, unit, string, unit) format4) : 'a = *)
@@ -94,7 +94,8 @@ let (gao2c : Soc.tbl -> 'a soc_pp -> Soc.gao -> unit) =
             List.iter (fun ve  -> assert(var_expr_is_not_a_slice ve)) vel_in;
             List.iter (fun ve  -> assert(var_expr_is_not_a_slice ve)) vel_out;
           with _ -> 
-            print_string "Error. Slices in left part not yet supported, sorry\n";
+            print_string
+              "*** Error. Slices in left part not yet supported in the C code generator, sorry\n";
             flush stdout;
             raise Delete_C_files
           ); 
@@ -424,24 +425,39 @@ let rec (const_to_c: Lic.const -> string) =
     )
     | Lic.Array_const_eff (ctab, t) -> (
       let vl = List.map const_to_c ctab in
-      "["^(String.concat ", " vl)^"]"
+      "{"^(String.concat ", " vl)^"}"
     )
     | Lic.Tuple_const_eff   cl -> assert false
 
-let (constdef : LicPrg.t -> string) = 
+(* returns a pair: the lhs for the .h, the rhs for the .c
+Indeed, arrays constant need to be defined in a .c
+*)
+let (constdef : LicPrg.t -> string*string) = 
   fun licprg -> 
     let to_c k = function
-    | Lic.Extern_const_eff _ -> ""
-    | c  -> 
+      | Lic.Extern_const_eff _ -> "",""
+      (*       | Lic.Array_const_eff (ctab, Array_type_eff(_t,s)) -> ( *)
+      | Lic.Array_const_eff (ctab, t) -> (
+        let vl = List.map const_to_c ctab in
+        let s = List.length vl in
+        let tab_exp = "{"^(String.concat ", " vl)^"}" in
+         Printf.sprintf "const %s [%i];\n" (long2s k) s, 
+         Printf.sprintf "const %s [%i] = %s;\n" (long2s k) s tab_exp 
+      )       
+      | c  -> 
       Printf.sprintf "#define %s %s\n"
-(*       Printf.sprintf "const %s = %s;\n" *)
         (long2s k)
-        (const_to_c c)
+        (const_to_c c),""
     in
-    let str = LicPrg.fold_consts (fun k t acc -> acc ^ (to_c k t)) licprg  "" in
-    if str = "" then "" else
-      "\n// Constant definitions \n" ^ str
-
+    let strh,strc = LicPrg.fold_consts
+                (fun k t (acc_h,acc_c) ->
+                 let h,c = to_c k t in
+                 (acc_h^h,acc_c^c)) licprg  ("","")
+    in
+    (if strh = "" then "" else
+       "\n// Constant definitions \n" ^ strh),
+    (if strc = "" then "" else
+       "\n// Constant definitions \n" ^ strc)
 
 (****************************************************************************)
 
@@ -852,10 +868,12 @@ let (f : Lv6MainArgs.t -> Soc.key -> Soc.tbl -> LicPrg.t -> unit) =
   let consts_h_oc = open_out "lustre_consts.h" in
   let consts_c_oc = open_out "lustre_consts.c" in
   let cfiles_acc = ["lustre_consts.c"; cfile] in
+  let const_def_h, const_def_c = constdef licprg in
   Lv6util.entete consts_h_oc "/*" "*/" ;
-  output_string consts_h_oc (constdef licprg);
+  output_string consts_h_oc const_def_h;
   Lv6util.entete consts_c_oc "/*" "*/" ;
   output_string consts_c_oc "#include \"lustre_consts.h\"";
+  output_string consts_c_oc const_def_c;
   Lv6util.entete types_h_oc "/*" "*/" ;
   output_string types_h_oc ("
 #ifndef _SOC2C_PREDEF_TYPES
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index 3b50a23e..91608572 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test Run By jahier on Fri Jan  6 10:32:53 
+Test Run By jahier on Mon Jan  9 11:08:30 
 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 Jan  6 10:32:54 
+Test Run By jahier on Mon Jan  9 11:08:31 
 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 Fri Jan  6 10:33:15 
+Test Run By jahier on Mon Jan  9 11:08:50 
 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 Fri Jan  6 10:33:56 
+Test Run By jahier on Mon Jan  9 11:09:29 
 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 Fri Jan  6 10:34:06 
+Test Run By jahier on Mon Jan  9 11:09:40 
 Native configuration is x86_64-unknown-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -1775,14 +1775,14 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node zzz2.lus {}
 # of unexpected failures	4
 ===============================
 # Total number of failures: 24
-lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 0 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 20 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 40 seconds
+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 31 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 29 seconds
 * Ref time: 
-0.06user 0.02system 1:44.06elapsed 0%CPU (0avgtext+0avgdata 5624maxresident)k
-96inputs+0outputs (0major+6038minor)pagefaults 0swaps
+0.06user 0.03system 1:39.43elapsed 0%CPU (0avgtext+0avgdata 5696maxresident)k
+128inputs+0outputs (0major+6056minor)pagefaults 0swaps
 * Quick time (-j 4):
-0.06user 0.02system 0:49.80elapsed 0%CPU (0avgtext+0avgdata 5716maxresident)k
-96inputs+0outputs (0major+6055minor)pagefaults 0swaps
+0.05user 0.03system 0:45.66elapsed 0%CPU (0avgtext+0avgdata 5640maxresident)k
+96inputs+0outputs (0major+6039minor)pagefaults 0swaps
-- 
GitLab