From 2646d0ed7f3ec570f36b19662ad6a0d7d0beefaa Mon Sep 17 00:00:00 2001
From: Kostyantyn Vorobyov <kostyantyn.vorobyov@cea.fr>
Date: Thu, 10 Nov 2016 11:39:02 +0100
Subject: [PATCH] [scripts] Added --rt-debug feature to e-acsl-gcc.sh allowing
 to enable debug features in instrumented executables at runtime

---
 src/plugins/e-acsl/.gitignore              |  6 +++--
 src/plugins/e-acsl/Makefile.in             | 29 +++++++++++++---------
 src/plugins/e-acsl/configure.ac            |  7 ------
 src/plugins/e-acsl/doc/Changelog           |  2 ++
 src/plugins/e-acsl/man/e-acsl-gcc.sh.1     |  4 +++
 src/plugins/e-acsl/scripts/e-acsl-gcc.comp |  2 +-
 src/plugins/e-acsl/scripts/e-acsl-gcc.sh   | 24 +++++++++++++-----
 src/plugins/e-acsl/scripts/testrun.sh      |  7 +++---
 8 files changed, 50 insertions(+), 31 deletions(-)

diff --git a/src/plugins/e-acsl/.gitignore b/src/plugins/e-acsl/.gitignore
index 639f914e55f..c28471d3f89 100644
--- a/src/plugins/e-acsl/.gitignore
+++ b/src/plugins/e-acsl/.gitignore
@@ -73,5 +73,7 @@ doc/doxygen/html
 doc/doxygen/warn.log
 lib/libeacsl-jemalloc.a
 lib/libeacsl-gmp.a
-lib/libeacsl-bittree-rtl.a
-lib/libeacsl-segment-rtl.a
+lib/libeacsl-bittree-rtl-opt.a
+lib/libeacsl-segment-rtl-opt.a
+lib/libeacsl-bittree-rtl-debug.a
+lib/libeacsl-segment-rtl-debug.a
diff --git a/src/plugins/e-acsl/Makefile.in b/src/plugins/e-acsl/Makefile.in
index 0adea0bae40..3c2275c69bf 100644
--- a/src/plugins/e-acsl/Makefile.in
+++ b/src/plugins/e-acsl/Makefile.in
@@ -183,35 +183,40 @@ EACSL_SHARE := $(EACSL_PLUGIN_DIR)/share/e-acsl
 
 EACSL_RTL_SRC = $(wildcard $(EACSL_SHARE)/*.[ch] $(EACSL_SHARE)/*/*.[ch])
 
-RTLFAGS = -std=c99 -Wall -Wno-unused -Wno-attributes -I$(EACSL_SHARE) \
+RTLFLAGS = -std=c99 -Wall -Wno-unused -Wno-attributes -I$(EACSL_SHARE) \
   -fno-omit-frame-pointer -DE_ACSL_BUILTINS
 
-ifeq (@OPTIMIZED_RTL@,no)
-RTLFAGS += -DE_ACSL_DEBUG -g3 -O0
-else
-RTLFAGS += -O2
-endif
+RTLOPT 	 = $(RTLFLAGS) -O2
+RTLDEBUG = $(RTLFLAGS) -DE_ACSL_DEBUG -g3 -O0
 
 # Build a static library
   # $1 Compile-time flags
   # $2 Library path
   # $3 Source files
 MKRTL = \
+	echo "Making $2"; \
   object=$$(basename -s .a $2).o && \
-  echo 'CC          $3' && \
+  echo ' CC $3' && \
   $(CC) $1 -c -o$$object $3 && \
-  echo 'AR          $2' && \
+  echo ' AR $2' && \
   $(AR) crus $2 $$object && \
   $(RM) -f $$object && \
   ranlib $2
 
-$(EACSL_LIBDIR)/libeacsl-%-rtl.a: $(EACSL_SHARE)/%_model/e_acsl_*_mmodel.c $(EACSL_RTL_SRC)
-	$(call MKRTL, $(RTLFAGS), $@, $<)
+$(EACSL_LIBDIR)/libeacsl-%-rtl-opt.a: $(EACSL_SHARE)/%_model/e_acsl_*_mmodel.c $(EACSL_RTL_SRC)
+	$(call MKRTL, $(RTLOPT),$@,$<,Optimized bittree)
+
+$(EACSL_LIBDIR)/libeacsl-%-rtl-debug.a: $(EACSL_SHARE)/%_model/e_acsl_*_mmodel.c $(EACSL_RTL_SRC)
+	$(call MKRTL,$(RTLDEBUG),$@,$<,debug)
 
-EACSL_RTL = $(EACSL_LIBDIR)/libeacsl-bittree-rtl.a
+EACSL_RTL = \
+	$(EACSL_LIBDIR)/libeacsl-bittree-rtl-opt.a \
+	$(EACSL_LIBDIR)/libeacsl-bittree-rtl-debug.a
 
 ifneq ($(wildcard $(EACSL_SHARE)/segment_model),)
-  EACSL_RTL += $(EACSL_LIBDIR)/libeacsl-segment-rtl.a
+EACSL_RTL += \
+	$(EACSL_LIBDIR)/libeacsl-segment-rtl-opt.a \
+	$(EACSL_LIBDIR)/libeacsl-segment-rtl-debug.a
 endif
 
 clean::
diff --git a/src/plugins/e-acsl/configure.ac b/src/plugins/e-acsl/configure.ac
index 1e306468bcd..e801b7b5bb4 100644
--- a/src/plugins/e-acsl/configure.ac
+++ b/src/plugins/e-acsl/configure.ac
@@ -110,13 +110,6 @@ AC_ARG_ENABLE(full-gmp,
     ["Disable runtime assertions in the RTL"]),
     [FULL_GMP="yes"],[FULL_GMP="no"])
 
-# RTL
-#####
-AC_ARG_ENABLE(optimized-rtl,
-    AS_HELP_STRING([--enable-optimized-rtl],
-    ["Enable runtime library optimizations"]),
-    [OPTIMIZED_RTL="yes"],[OPTIMIZED_RTL="no"])
-
 #######################
 # Generating Makefile #
 #######################
diff --git a/src/plugins/e-acsl/doc/Changelog b/src/plugins/e-acsl/doc/Changelog
index 995c81193f6..80fca39458d 100644
--- a/src/plugins/e-acsl/doc/Changelog
+++ b/src/plugins/e-acsl/doc/Changelog
@@ -15,6 +15,8 @@
 #   E-ACSL: the Whole E-ACSL plug-in
 ###############################################################################
 
+-* E-ACSL       [2016/08/02] Added --rt-debug feature to e-acsl-gcc.sh.
+	        --enable-optimized-rtl configure option removed
 -* E-ACSL       [2016/08/02] Added --enable-optimized-rtl option to configure
 -* E-ACSL       [2016/08/02] Removed --production|-P, --no-stdlib|-N and
 	        --debug-log|-D options of e-acsl-gcc.sh.
diff --git a/src/plugins/e-acsl/man/e-acsl-gcc.sh.1 b/src/plugins/e-acsl/man/e-acsl-gcc.sh.1
index 60b1cad9c45..6658f67ce76 100644
--- a/src/plugins/e-acsl/man/e-acsl-gcc.sh.1
+++ b/src/plugins/e-acsl/man/e-acsl-gcc.sh.1
@@ -45,6 +45,10 @@ show a help page.
 compile the generated and the original (supplied) sources.
 By default no compilation is performed.
 .TP
+.B -D, --rt-debug
+Enable runtime debug features, i.e., compile unoptimized executable
+with assertions and extra checks.
+.TP
 .B -X, --instrumented-only
 Do not compile original code. Has effect only in the presence of the \fI-c\fP
 flag.
diff --git a/src/plugins/e-acsl/scripts/e-acsl-gcc.comp b/src/plugins/e-acsl/scripts/e-acsl-gcc.comp
index fa4a92dbe1e..b8229dd1647 100644
--- a/src/plugins/e-acsl/scripts/e-acsl-gcc.comp
+++ b/src/plugins/e-acsl/scripts/e-acsl-gcc.comp
@@ -31,7 +31,7 @@ _eacsl_gcc() {
   opts="
     -i -C -p -d -o -O -v -f -E -L -M -l -e -g -q -s -F -P -N -D -I -G -X -a
     -h -c -msegment -mbittree
-    --verbose= --debug= --debug-log= --logfile= --quiet --production --help
+    --verbose= --debug= --debug-log= --logfile= --quiet --rt-debug --help
     --ocode= --oexec= --oexec-e-acsl=
     --ld-flags= --cpp-flags= --extra-cpp-args=
     --frama-c-extra= --frama-c= --gcc= --e-acsl-share= --memory-model=
diff --git a/src/plugins/e-acsl/scripts/e-acsl-gcc.sh b/src/plugins/e-acsl/scripts/e-acsl-gcc.sh
index 11b6bb946df..4ea21de6f5e 100755
--- a/src/plugins/e-acsl/scripts/e-acsl-gcc.sh
+++ b/src/plugins/e-acsl/scripts/e-acsl-gcc.sh
@@ -125,17 +125,22 @@ rte_options() {
 #    model. In this case the following function prints the full path to a static
 #    library representing this memory model.
 mmodel_lib() {
-  local models="$(find $LIBDIR/ -name 'libeacsl-*-rtl.a' -exec basename {} \; \
-    | sed 's/^libeacsl-\(.*\)-rtl.a/\1/')"
+  local rt_feature="opt"
+  if [ -n "$OPTION_RT_DEBUG" ]; then
+    rt_feature="debug"
+  fi
+
+  local models="$(find $LIBDIR/ -name 'libeacsl-*-rtl-opt.a' -exec basename {} \; \
+    | sed 's/^libeacsl-\(.*\)-rtl-opt.a/\1/')"
 
   if [ -n "$1" ]; then
     local modelname="$(echo $models | tr ' ' '\n' | grep "^$1$")"
-    local modelpath="$(realpath $LIBDIR/libeacsl-$modelname-rtl.a 2>/dev/null)"
+    local modelpath="$(realpath $LIBDIR/libeacsl-$modelname-rtl-$rt_feature.a 2>/dev/null)"
 
     if [ -n "$modelpath" ]; then
       echo $modelpath
     else
-      error "Memory model '$1' is not available in this distribution"
+      error "Memory model '$1'-$rt_feature is not available in this distribution"
     fi
   else
     echo $models
@@ -151,8 +156,8 @@ LONGOPTIONS="help,compile,compile-only,print,debug:,ocode:,oexec:,verbose:,
   frama-c-only,extra-cpp-args:,frama-c-stdlib,full-mmodel,gmp,quiet,logfile:,
   ld-flags:,cpp-flags:,frama-c-extra:,memory-model:,
   frama-c:,gcc:,e-acsl-share:,instrumented-only,rte:,oexec-e-acsl:,
-  print-mmodels"
-SHORTOPTIONS="h,c,C,p,d:,o:,O:,v:,f,E:,L,M,l:,e:,g,q,s:,F:,m:,I:,G:,X,a:"
+  print-mmodels,rt-debug"
+SHORTOPTIONS="h,c,C,p,d:,D,o:,O:,v:,f,E:,L,M,l:,e:,g,q,s:,F:,m:,I:,G:,X,a:"
 # Prefix for an error message due to wrong arguments
 ERROR="ERROR parsing arguments:"
 
@@ -168,6 +173,7 @@ OPTION_PRINT=                            # Output instrumented code
 OPTION_DEBUG=                            # Set Frama-C debug flag
 OPTION_VERBOSE=                          # Set Frama-C verbose flag
 OPTION_COMPILE=                          # Compile instrumented program
+OPTION_RT_DEBUG=                         # Enable runtime debug features
 OPTION_OUTPUT_CODE="a.out.frama.c"       # Name of the translated file
 OPTION_OUTPUT_EXEC="a.out"               # Generated executable name
 OPTION_EACSL_OUTPUT_EXEC=""              # Name of E-ACSL executable
@@ -246,6 +252,12 @@ do
       exec 2> $1
       shift;
     ;;
+    # Enable runtime debug features, i.e., compile unoptimized executable
+    # with assertions, extra checks and other debug features
+    --rt-debug|-D)
+      shift
+      OPTION_RT_DEBUG=1
+    ;;
     # Pass an option to a Frama-C invocation
     --frama-c-extra|-F)
       shift;
diff --git a/src/plugins/e-acsl/scripts/testrun.sh b/src/plugins/e-acsl/scripts/testrun.sh
index 3158d01f559..1365313c02e 100755
--- a/src/plugins/e-acsl/scripts/testrun.sh
+++ b/src/plugins/e-acsl/scripts/testrun.sh
@@ -132,9 +132,10 @@ run_test() {
 
   # Compile instrumented source and run executable
   for model in $MODELS; do
-    # Command for compiling the instrumented file with a given memory model
-    COMMAND="$EACSLGCC --compile-only --memory-model=$model --logfile=$logfile \
-      --oexec-e-acsl=$oexec-$model $ocode"
+    # Command for compiling the instrumented file with a given memory model.
+    # Make sure executables are compiled in the debug mode.
+    COMMAND="$EACSLGCC --compile-only --rt-debug --memory-model=$model \
+      --logfile=$logfile --oexec-e-acsl=$oexec-$model $ocode"
     debug "Run $COMMAND"
     $COMMAND || error "Command $COMMAND failed" "$logfile"
     run_executable $oexec-$model $oexeclog-$model $model
-- 
GitLab