#! /bin/sh

#Optimization flags
export CCC_OPTS=-O3

llvm_path="<llvm_path>"
#COMPILER=gcc
clang=$llvm_path/bin/clang
opt=$llvm_path/bin/opt
llc=$llvm_path/bin/llc
llvmlink=$llvm_path/bin/llvm-link

dumpfeat="<llvm_path>/bin/opt"

cross_flags=" --target=x86_64-linux-gnu "
#cross_flags=" --target=aarch64-linux-gnu "

#if [ ! -z $1 ] ; then
 #COMPILER=$1
#fi

#  C_FLAGS
#C_FLAGS="$C_FLAGS -DFPM_DEFAULT"
# 
#C_FLAGS="$C_FLAGS -DFPM_64BIT"  #  64
#C_FLAGS="$C_FLAGS -DFPM_32BIT"  #  32
#C_FLAGS="$C_FLAGS -DFPM_ARM"    #  ARM 

C_FLAG=" -O3 -emit-llvm -S -Xclang -disable-llvm-passes -DFPM_DEFAULT -DLAMESNDFILE -DHAVEMPGLIB -DLAMEPARSE -I. \
  -DFPM_DEFAULT -DHAVE_CONFIG_H -DNO_ESD -DNO_OSS -DLOCALEDIR=\"/usr/local/share/locale\" -w \
  -include unistd.h -include sys/wait.h -include fcntl.h -include errno.h \
  -DUNIX -DPORTABLE -DSASR(x,y)=((x)>>(y))"
LD_FLAG=" -no-pie -DFPM_DEFAULT -lmp3lame -lm -lstdc++ -lz -lpng  -lfreetype "

__extract_IR() {
  for cc in ./src_work/*.c
  do
    $clang $C_FLAG $cross_flags $cc -o "./IR/base/$(basename $cc .c).ll"
  done
  find ./IR/base/ -name "*.ll" -type f -exec sed -i 's/noinline//g' {} +  #  noinline 

  $llvmlink -S ./IR/base/*.ll -o ./IR/linked.ll
}

__opt() {
 $opt -S -passes="default<O3>" ./IR/linked.ll -o ./IR/linked.o3.ll -pass-remarks='.*' -pass-remarks-missed='.*' -pass-remarks-analysis='.*' -pass-remarks-output="remarks/custom.remarks.yaml" > /dev/null 2>&1
  #$dumpfeat -load-pass-plugin <project_path>/external/lib/lib/libLLVMDumpFeaturePass.so -passes=dump-feature IR/linked.ll -disable-output #  features.csv,  titles
  $dumpfeat -passes=dump-feature IR/linked.ll -disable-output #  features.csv,  titles
  mv ./features.csv "features/linked.csv"
  python3 ./input_schema_gen.py ./features/
}

__compile() {
  $llc -filetype=obj ./IR/linked.o3.ll -o ./obj/o3.o
  $clang $cross_flags ./obj/o3.o $LD_FLAG -o ./bin/o3.out -v
}

bench_list_exist=0
if [ -f bench_list ]
then
  benchmarks=`grep -v ^# bench_list`
  #benchmarks=`grep -v ^# bench_not_compiled`
  bench_list_exist=1

else
  benchmarks=*  
fi

for i in $benchmarks
do
  if [ -d "$i" ] 
  then
  tmp=$PWD
  cp ./input_schema_gen.py $i/
  cd $i
  if [ -d "src_work" ] 
  then
    # *** process directory ***
    echo "**********************************************************"
    echo $i
    #cd src_work
    ## replace all dprintf -> debug_printf, <(use)config.h> -> "(use)config.h", { value: { 1, hlen, x, y } } -> { .value = { 1, hlen, x, y } }
    ## fprintf(stderr, "%s: failed to malloc %d bytes -- abort\n", progname, len); -> fprintf(stderr, "%s: failed to malloc %zu bytes -- abort\n", progname, len);
    mkdir -p IR/base obj bin features remarks jsons
    #rm -rf ./src_work/__* ./src_work/_ccc* ./src_work/Makefile* ./o3.out > /dev/null 2>&1
    __extract_IR
    __opt
    __compile
    echo ""
    ls -l bin
    if [ $bench_list_exist -eq 1 ]
    then
      echo "bench_list exist"
    else
      if [ -f "bin/o3.out" ]
      then
        #  i  bench_list 
        echo "$i" >> "$tmp/bench_list_compiled"
        echo "  $i  bench_list_compiled"
      else
        echo "$i" >> "$tmp/bench_not_compiled"
        echo "  bin/o3.out "
      fi
    fi
    echo ""
    # *************************
  fi
  cd $tmp
  fi

done

