#!/bin/bash
# This string is changed by cmake when building and installing aomp utils.
PROGVERSION="11.12-0"

function version(){
   echo $PROGVERSION
   exit 0
}

function usage(){
/bin/cat 2>&1 <<"EOF"
   Extract and disassemble AOMP region.

   Usage: aompExtractRegion <options> <executable>

   Options:
    -h           Print this help message
    -d <device>  Set GPU model to this device
    -s           Save the extracted amdgcn section file
    -version     Print the version of aompExtractRegion
    -n           Dump the first few lines of image .note section

   Copyright (c) 2019 ADVANCED MICRO DEVICES, INC.

EOF
   exit 1
}

if [ -z "$1" ] ; then
  usage
fi
savetempfile=0
dumpNotes=0
if [ -z ${AOMP+x} ]; then
  aomp=/home/abuild/rpmbuild/BUILD/rocm_11.12-0
else
  aomp=$AOMP
fi
aompgpu=`$aomp/bin/mygpu`
while [ $# -gt 0 ] ; do
   case "$1" in
      -d)               aompgpu=$2; shift ;;
      -h)               usage ;;
      -help)            usage ;;
      --help)           usage ;;
      -s)               savetempfile=1 ;;
      -n)               dumpNotes=1 ;;
      -version)         version ;;
      --version)        version ;;
      --v)              version ;;
      -v)               version ;;
      --)               shift ; break;;
      *)                break;echo $1 ignored;
   esac
   shift
done


$aomp/bin/llvm-objcopy --dump-section .omp_offloading.amdgcn-amd-amdhsa=$$amd-out $1
$aomp/bin/llvm-objdump -d -mcpu=$aompgpu $$amd-out
if [ $dumpNotes -ne 0 ] ; then
  $aomp/bin/llvm-objdump -s --section=.note -mcpu=$aompgpu $$amd-out | head -10
fi
if [ $savetempfile -eq 0 ] ; then
  rm -f $$amd-out
fi
