From cc145ae14379370ec47aa1496433cfb8b93f258e Mon Sep 17 00:00:00 2001
From: Erwan Jahier <erwan.jahier@univ-grenoble-alpes.fr>
Date: Wed, 27 Jul 2022 17:32:12 +0200
Subject: [PATCH] generalise the -2c generated .sh file to ease the compilation
 with -2cmc

---
 lib/soc2c.ml                | 34 ++++++++++++++--------
 test/lus2lic.sum            | 37 ++++++++++++++---------
 test/should_work/foo.lus    | 58 +++++++++++++++++++++++++++++++++++++
 utils/compare_exec_and_2cmc | 17 ++++++-----
 4 files changed, 113 insertions(+), 33 deletions(-)
 create mode 100644 test/should_work/foo.lus

diff --git a/lib/soc2c.ml b/lib/soc2c.ml
index 489847f..29d50db 100644
--- a/lib/soc2c.ml
+++ b/lib/soc2c.ml
@@ -1,4 +1,4 @@
-(* Time-stamp: <modified the 22/07/2022 (at 11:12) by Erwan Jahier> *)
+(* Time-stamp: <modified the 27/07/2022 (at 16:22) by Erwan Jahier> *)
 
 
 (* let put (os: out_channel) (fmt:('a, unit, string, unit) format4) : 'a = *)
@@ -689,6 +689,11 @@ let (gen_loop_file : string -> LicPrg.t -> Soc.t -> string ->
   let outputs_list = String.concat "," (List.map (fun (id,_) -> id ) outputs_io) in
   let inputs_fmt  = List.map (fun (_,t) -> type_to_format_string t) inputs_io in
   let outputs_fmt = List.map (fun (_,t) -> type_to_format_string t) outputs_io in
+  let to_rif_decl (n,t) = ("\\\""^n^"\\\":" ^(type_to_string_rif t)) in
+  let inputs_t  = List.map to_rif_decl inputs_io in
+  let outputs_t = List.map to_rif_decl outputs_io in
+  let inputs_decl = Printf.sprintf "#inputs %s" (String.concat " " inputs_t) in
+  let outputs_decl = Printf.sprintf "#outputs %s" (String.concat " " outputs_t) in
 
   Lv6util.entete main_oc "/*" "*/";
   Lv6util.entete readinput_oc_c "/*" "*/";
@@ -827,6 +832,13 @@ void _put_string(char* n, char* _V){
 /* Output procedures **********************/
 "^ (gen_memoryless_ctx stbl) ^ "
 
+/* RIF I/O declarations **********************/
+
+  void print_rif_declaration() {
+    printf(\""^inputs_decl^"\\n\");
+    printf(\""^outputs_decl^"\\n\");
+};
+
 /* I/O functions for the main loop **********************/
 void get_inputs ("^
            (if SocUtils.is_memory_less soc then "" else ctx^"_type* ctx, ")^
@@ -896,6 +908,7 @@ static int ISATTY;
     ");
   puth_ri ("
 /* I/O functions for the main loop **********************/
+void print_rif_declaration();
 void get_inputs ("^
            (if SocUtils.is_memory_less soc then "" else ctx^"_type*, ")^
            (String.concat ","
@@ -909,11 +922,6 @@ void get_inputs ("^
   let c_outputs_profile = String.concat "," (List.map (fun (_,t) -> Soc2cUtil.type_to_string t "") outputs_io) in
   puth_ri  ("void print_outputs ("^c_outputs_profile^");\n");
   puth_ri "/* I/O functions end **********************/\n";
-  let to_rif_decl (n,t) = ("\\\""^n^"\\\":" ^(type_to_string_rif t)) in
-  let inputs_t  = List.map to_rif_decl inputs_io in
-  let outputs_t = List.map to_rif_decl outputs_io in
-  let inputs_decl = Printf.sprintf "#inputs %s" (String.concat " " inputs_t) in
-  let outputs_decl = Printf.sprintf "#outputs %s" (String.concat " " outputs_t) in
   putc  ("
 #include \""^base0 ^"_loop_io.h\"\
 
@@ -923,8 +931,8 @@ void get_inputs ("^
 /* Main procedure *************************/
 int main(){
   int _s = 0;" ^ (gen_main_loop_body inputs outputs soc ctx) ^"
-  printf(\""^inputs_decl^"\\n\");
-  printf(\""^outputs_decl^"\\n\");
+
+  print_rif_declaration();
 
   ISATTY = isatty(0);
   /* Main loop */
@@ -1286,7 +1294,7 @@ struct dro_desc_t DRO_DESC_NAME = {
       if Lv6MainArgs.global_opt.Lv6MainArgs.gen_wcet then 
         base^"_main.c",base^"_loop.c","gcc --specs=linux.specs -g" 
       else 
-        (loopfile^ " " ^ loop_readinput_cfile), "I am a dead string...", "$C_COMPILER"
+        loop_readinput_cfile, "I am a dead string...", "$C_COMPILER"
     in
     let ogensim_exe = node^"4ogensim.exec" in
     let cfiles_acc = if needs_cfile then ext_cfile::cfiles_acc else cfiles_acc in
@@ -1296,9 +1304,11 @@ struct dro_desc_t DRO_DESC_NAME = {
         Printf.sprintf "%s -shared -o %s.dro \\\n\t%s %s" gcc node cfiles cflags,
         "dead string"
       ) else
-        Printf.sprintf "if [ -z ${C_COMPILER} ]; then C_COMPILER=gcc; fi
-%s -o %s \\\n\t%s %s %s" gcc execfile cfiles cflags main_files,
-        Printf.sprintf "%s -o %s \\\n\t%s %s %s"
+        Printf.sprintf "set -x
+if [ -z ${C_COMPILER} ]; then C_COMPILER=gcc; fi
+if [ -z ${MAIN_FILE} ]; then MAIN_FILE=%s; fi
+%s -o %s \\\n\t %s %s ${EXTRA_C_FILES} %s ${MAIN_FILE} ${C_LIBS}" loopfile gcc execfile cfiles cflags main_files,
+        Printf.sprintf "%s -o %s  \\\n\t%s %s %s"
           gcc ogensim_exe cfiles cflags ogensim_main_file
     in
     let main_step = (string_of_soc_key msoc)^"_step" in
diff --git a/test/lus2lic.sum b/test/lus2lic.sum
index f04b10a..36a17e7 100644
--- a/test/lus2lic.sum
+++ b/test/lus2lic.sum
@@ -1,5 +1,5 @@
 ==> lus2lic0.sum <==
-Test run by jahier on Tue Jul 19 15:27:36 
+Test run by jahier on Wed Jul 27 16:58:49 
 Native configuration is x86_64-pc-linux-gnu
 
 		=== lus2lic0 tests ===
@@ -66,7 +66,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 Tue Jul 19 15:27:38 
+Test run by jahier on Wed Jul 27 16:58:50 
 Native configuration is x86_64-pc-linux-gnu
 
 		=== lus2lic1 tests ===
@@ -414,6 +414,11 @@ PASS: sh followed_by.sh
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c followed_by.lus  {}
 PASS: /home/jahier/lus2lic/test/../utils/compare_lv6_and_lv6_en followed_by.lus {}
 PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang followed_by.lus {}
+PASS: ./lus2lic  {-2c foo.lus -n foo}
+PASS: sh foo.sh 
+PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c foo.lus  {}
+PASS: /home/jahier/lus2lic/test/../utils/compare_lv6_and_lv6_en foo.lus {}
+PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang foo.lus {}
 PASS: ./lus2lic  {-2c fresh_name.lus -n fresh_name}
 PASS: sh fresh_name.sh 
 PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c fresh_name.lus  {}
@@ -626,7 +631,7 @@ PASS: /home/jahier/lus2lic/test/../utils/compare_exec_and_2c multipar.lus  {}
 PASS: /home/jahier/lus2lic/test/../utils/compare_lv6_and_lv6_en multipar.lus {}
 
 ==> lus2lic2.sum <==
-Test run by jahier on Tue Jul 19 15:33:58 
+Test run by jahier on Wed Jul 27 17:06:18 
 Native configuration is x86_64-pc-linux-gnu
 
 		=== lus2lic2 tests ===
@@ -1172,7 +1177,7 @@ PASS: /home/jahier/lus2lic/test/../utils/compare_lv6_and_lv6_en zzz2.lus {}
 PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang zzz2.lus {}
 
 ==> lus2lic3.sum <==
-Test run by jahier on Tue Jul 19 15:42:01 
+Test run by jahier on Wed Jul 27 17:14:56 
 Native configuration is x86_64-pc-linux-gnu
 
 		=== lus2lic3 tests ===
@@ -1481,6 +1486,10 @@ PASS: ./lus2lic {} {-lic -o followed_by.lic followed_by.lus -n followed_by}
 PASS: ./lus2lic {-ec -o followed_by.ec followed_by.lus -n followed_by}
 PASS: ./myec2c {-o followed_by.c followed_by.ec}
 PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node followed_by.lus {}
+PASS: ./lus2lic {} {-lic -o foo.lic foo.lus -n foo}
+PASS: ./lus2lic {-ec -o foo.ec foo.lus -n foo}
+PASS: ./myec2c {-o foo.c foo.ec}
+PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node foo.lus {}
 PASS: ./lus2lic {} {-lic -o fresh_name.lic fresh_name.lus -n fresh_name}
 PASS: ./lus2lic {-ec -o fresh_name.ec fresh_name.lus -n fresh_name}
 PASS: ./myec2c {-o fresh_name.c fresh_name.ec}
@@ -1687,7 +1696,7 @@ PASS: /home/jahier/lus2lic/test/../utils/test_lus2lic_no_node multipar.lus {}
 
 
 ==> lus2lic4.sum <==
-Test run by jahier on Tue Jul 19 15:43:30 
+Test run by jahier on Wed Jul 27 17:16:28 
 Native configuration is x86_64-pc-linux-gnu
 
 		=== lus2lic4 tests ===
@@ -2190,7 +2199,7 @@ PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang multipar.lus {}
 
 		=== lus2lic1 Summary ===
 
-# of expected passes		549
+# of expected passes		554
 
 ==> lus2lic2.sum <==
 
@@ -2202,7 +2211,7 @@ PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang multipar.lus {}
 ==> lus2lic3.sum <==
 		=== lus2lic3 Summary ===
 
-# of expected passes		493
+# of expected passes		497
 # of unexpected failures	8
 # of unresolved testcases	1
 
@@ -2214,12 +2223,12 @@ PASS: /home/jahier/lus2lic/test/../utils/compare_gcc_and_clang multipar.lus {}
 # of unexpected failures	6
 ===============================
 # Total number of failures: 18
-lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 2 seconds
-lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 380 seconds
-lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 483 seconds
-lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 89 seconds
-lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 78 seconds
+lus2lic0.log:testcase ./lus2lic.tests/test0.exp completed in 1 seconds
+lus2lic1.log:testcase ./lus2lic.tests/test1.exp completed in 448 seconds
+lus2lic2.log:testcase ./lus2lic.tests/test2.exp completed in 518 seconds
+lus2lic3.log:testcase ./lus2lic.tests/test3.exp completed in 92 seconds
+lus2lic4.log:testcase ./lus2lic.tests/test4.exp completed in 84 seconds
 * Ref time: 
-388.16user 100.67system 17:11.26elapsed 47%CPU (0avgtext+0avgdata 77256maxresident)k
-109960inputs+496592outputs (322major+23046618minor)pagefaults 0swaps
+413.64user 107.22system 19:03.80elapsed 45%CPU (0avgtext+0avgdata 77648maxresident)k
+58976inputs+501592outputs (200major+23204783minor)pagefaults 0swaps
 * Quick time (-j 4):
diff --git a/test/should_work/foo.lus b/test/should_work/foo.lus
new file mode 100644
index 0000000..d742068
--- /dev/null
+++ b/test/should_work/foo.lus
@@ -0,0 +1,58 @@
+type wiz = {
+	bt: real^2;
+	cc: bool;
+};
+
+function A(wi: wiz) returns (wo: wiz);
+let
+	wo.bt[0] = wi.bt[0] - wi.bt[1];
+	wo.bt[1] = wi.bt[0] + wi.bt[1];
+	wo.cc = wi.bt[0] > wi.bt[1];
+tel
+
+node B(
+	w: wiz;
+	b: bool;
+) returns (
+	x: bool;
+	r: real;	
+);
+var pr: real;
+let
+	pr = 0.0 -> pre r;
+	r = if b then w.bt[0] else pr + w.bt[1];
+	x = false -> (r > 0.0);
+tel
+
+node C(i: int; w: wiz) returns (x: bool; o: real);
+var r: real;
+let
+	r = (w.bt[0] - w.bt[1]);
+	o = (0.0 -> pre o) + r;
+	x = (r >= 42.0);
+tel
+
+function D(x,y:real) returns (o: int);
+let
+	o = int(x + 0.5*y);
+tel
+
+node foo(
+	ii: int;
+	win: wiz;
+	bt: bool;
+) returns (
+	xo: bool; cd: int; bto: bool
+);
+var
+	btb: bool;
+	wa: wiz;
+	cc: real;
+	r1, r2: real;
+let
+	wa = A(win);	
+	btb,r1 = B(win, bt);
+	bto,r2 = B(wa, btb);
+	xo,cc = C(ii, wa);
+	cd = D(cc, r2);
+tel
diff --git a/utils/compare_exec_and_2cmc b/utils/compare_exec_and_2cmc
index bf1e139..3d6bad8 100755
--- a/utils/compare_exec_and_2cmc
+++ b/utils/compare_exec_and_2cmc
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 # compares with lurette the results of:
-#   lus2lic -2c
+#   lus2lic -2cmc
 #   lus2lic -exec
 
 lustre_file=$1
@@ -38,27 +38,30 @@ else
 fi
 fi
 
-# XXXXX
+
+
 if
-    ./lus2lic -dir /tmp/lustre-test-$lustre_file-$node $OPT $lustre_file -n $node  -2cmc -cc -o $node.exec;    
+    ./lus2lic -dir /tmp/lustre-test-$lustre_file-$node $OPT $_oracle -n $oracle  -2c -cc -o $node-oracle.exec;    
 then
     echo "lus2lic -2c done"
 else
     echo "Error"
     exit 2
 fi
-# XXXXX
 
+# generated the -2cmc version in $node.exec
 if
-    ./lus2lic -dir /tmp/lustre-test-$lustre_file-$node $OPT $_oracle -n $oracle  -2c -cc -o $node-oracle.exec;    
+    ./lus2lic -dir /tmp/lustre-test-$lustre_file-$node $OPT $lustre_file -n $node  -2cmc ;
+    yaml2pthread /tmp/lustre-test-$lustre_file-$node/${node}_${node}.yml;
+    cp foo_foo_pthread.c /tmp/lustre-test-$lustre_file-$node ;
+    export C_LIBS="-lpthread -lm " ; export MAIN_FILE="foo_foo_pthread.c" ; sh /tmp/lustre-test-$lustre_file-$node/$node.sh
 then
-    echo "lus2lic -2c done"
+    echo "lus2lic -2cmc done"
 else
     echo "Error"
     exit 2
 fi
 
-
 # call-via-socket -server -addr 127.0.0.1 -port $PORT ./$node.exec &
 
 export PATH=/usr/local/tools/lustre/v4/bin/:$PATH
-- 
GitLab