#!/bin/bash
#
# V1 = "on demand" treatment of loop bounds:
# - first search the necessary bounds with mkff
# - try to fill the "NOCOMP" using information comming form several sources:
#   . user pragmas (i.e. "looploc" file)
#   . knowledge base (i.e. c library utils)
#   . orange (opt)

#-----------------------------------------------------------
# Note in orange integration:
#
# there are 2 strategies:
#
# 1) integrate the result of orange in the "fixffx" process,
#   i.e. ONLY LOOP FLOW FACTS WILL BE USED
#   in this case a single ffx, (fixed.ffx) is generated
#
# 2) leave orange ffx alone ane let owcet use it 
#   in this case a second ffx, (orange.ffx) is generated
#
# In fact, we do BOTH here: it's a way to check whether all bounds are
# actually known, and it does not disturb owcet...
# (info is just duplicated)
# Moreoveover, sometimes orange is lost in line numbering and fixffx
# helps to fix this problem !
#------------------------------------------------------------

IDIR=`dirname $0`
source "$IDIR"/cnt_utils.sh

## TOOL / USAGE / OPTION
TOOL=cnt_bounds
VERSION=0.1

HELP="
version: $VERSION
usage:   $TOOL <working dir> <option>
options:
  -orange    use orange to find bounds
  -noprag    don't use pragma to find bounds
  -strict    fails if NOCOMP remains
  -lmargin <n> accept a margin of -<int> lines for loop head position (dflt 3)
  -v/-vv     verbose/more verbose
input: a working dir _cnt-FILE-FUNC, properly initialized
  with cnt_init, that MUST contain:
  - $(n_ref file)
  - $(n_looploc file)
  - $(n_elf file)
output: files are added to working dir
  - $(n_fixed_ffx file)
  - (opt) $(n_orange_ffx file)
requires:
  - mkff
  - orange (opt)
  - fixffx
"

function usage {
   echo "$HELP"
}

DO_ORANGE=false
DO_STRICT=false
NO_PRAGMA=false
FIXFFX_OPTS=""
VERBOSE=
#OPTS=`getopt -a -o vh --long noprag,orange,strict,vv,help -n '$TOOL' -- "$@"`
#checkret "Error: try '$TOOL -h' for usage"

if [ $# -lt 1 ]; then
	usage ; exit 0
fi


#eval set -- "$OPTS"
while true ; do
echo arg=\"$1\"
case "$1" in
	"") break;;
	-h|--help) usage ; exit 0;;
	-orange|--orange) DO_ORANGE=true; shift ;;
	-noprag|--noprag) NO_PRAGMA=true; shift ;;
	-strict|--strict) DO_STRICT=true; shift ;;
   -lmargin) shift; FIXFFX_OPTS+=" -lmargin $1"; shift ;;
	-v)       VERBOSE=-v; shift ;;
	-vv|--vv) VERBOSE=-vv; shift ;;
	*) ODIR="$1"; shift
#	--) shift ; break ;;
esac
done

echo DO_ORANGE="$DO_ORANGE"
echo NO_PRAGMA="$NO_PRAGMA"


#-------------------------------
# 0 - GET WORKING DIR
#     deduce BASE and FUNC
#     check necessary files ...
#-------------------------------
#ODIR=${1%/}
if [ ! -d "$ODIR" ]; then
	error "Error: can't open dir \"$ODIR\""
fi

# split ODIR
BASE=`base_of_workdir "$ODIR"`
FUNC=`func_of_workdir "$ODIR"`

# INIT ALL VARS
init_vars "$BASE"

echo BASE=$BASE
echo FUNC=$FUNC

checkfile "$ODIR/$REF" "Error: file \"%s\" is missing"

checkfile "$ODIR/$ELF" "Error: file \"%s\" is missing"

checkfile "$ODIR"/"$LOOPLOC" "Error: file \"%s\" is missing"


#============================================
# 1 - BUILD THE RAW FFX
#============================================
#-------------------------------
# - ref_$BASE.ffx MUST NOT exists
#   cause it "disturb" the behavior of mkff
#-------------------------------
FFX=`basename "$REF" .c`.ffx
rm -f "$ODIR/ref_$BASE.ffx"
#-------------------------------
# Build the ref (incomplete) ffx
# with the extention "mkffx"
#-------------------------------
checkcommand "mkff" "Error: command \"%s\" is missing"
RAW_FFX="ref_$BASE.mkffx"
(cd "$ODIR"; mkff -x "$ELF" "$FUNC" > "$RAW_FFX") 
checkret "Error: mkff failed, abort ..."
echo "-> (mkff) ffx done" 

#============================================
# 2 - (opt) BUILD THE ORANGE FFX
#============================================
#-----------------------------------
# 
#
#-----------------------------------
#ORANGE_FFX is a shared name (see utils)
rm -f "$ODIR/$ORANGE_FFX"
if $DO_ORANGE; then
	checkcommand "orange" "Error: command \"%s\" is missing"
	(cd  "$ODIR"; orange "$REF" "$FUNC" -o "$ORANGE_FFX")
	checkret "Error: orange failed, abort ..."
	echo "-> (orange) ffx done" 
else
	ORANGE_FFX=
fi

#============================================
# 2 - fix the ref ffx
#     i.e. try to replace all NOCOMP
#     by actual bounds coming from "looploc" files
# we use:
# $LOOPLOC  -> info from pragmas, maybe empty
# arm.looploc -> hand-made gnu/arm lib info
# (opt) $ORANGE_FFX
# N.B.
#   - not necessary if LOOPLOC is empty
#   - not (really) necessary if FFX
#     does not contain 'NOCOMP'
#   -> however, calling fixffx 
#      helps to find inconsistancy
#-------------------------------

# arm.looploc is supposed to found where THIS script is !
#IDIR=`readlink -f "$0"`
IDIR=`which "$0"`
IDIR=`dirname "$IDIR"`
ARM_LOOPLOC="$IDIR/arm.looploc"
checkfile "$ARM_LOOPLOC" "Error: file \"%s\" is missing"

echo $ARM_LOOPLOC
echo $LOOPLOC

if $NO_PRAGMA; then
	LOOPLOC=
fi

#FIXED_FFX is a shared name (see utils)
rm -f "$ODIR/$FIXED_FFX"
code=0
echo fixffx -i "$RAW_FFX" "$LOOPLOC" "$ARM_LOOPLOC" "$ORANGE_FFX" $FIXFFX_OPTS -v $VERBOSE
(cd "$ODIR"; \
fixffx -i "$RAW_FFX" "$LOOPLOC" "$ARM_LOOPLOC" "$ORANGE_FFX" $FIXFFX_OPTS -v $VERBOSE 2> "$BOUNDSLOG" > "$FIXED_FFX";\
code=$?;\
echo code=$code;\
checkcode $code "Error: fixffx failed, abort ...";
echo code=$code;\
)
code=$?
cat "$ODIR/$BOUNDSLOG" 2>&1
if [ $code != 0 ]; then
	exit $code
fi
echo "-> (fixffx) ffx done" 

# does it still remain NOCOMP ?
if $DO_STRICT; then 
	res=`grep NOCOMP "$ODIR"/"$FIXED_FFX" | wc -l`
	if [ $res != 0 ]; then
		error "Error: some 'NOCOMP' remains in \"$ODIR/$FIXED_FFX\""
	fi
fi
