#!/bin/sh
# pocl-kernel - Examine a OpenCL bytecode and generate a loadable module
#               with kernel function information.
# 
# Copyright (c) 2011-2012 Universidad Rey Juan Carlos, 
#                         Pekka Jääskeläinen / Tampere University of Technology
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

#echo 0=$0 @=$@
#set -x
#set -e

while getopts k:o:t: o
do
    case "$o" in
	k)   kernel="${OPTARG}";;
	o)   output_file="${OPTARG}";;
	t)   target="${OPTARG}";;
	[?]) echo >&2 "Usage: $0 -k <kernel> -t <target> -o <output_file> <input_file>" && exit 1;;
    esac
done
shift $((${OPTIND}-1))

if [ "x$kernel" = x ]
then
    echo >&2 "Usage: $0 -k <kernel> -t <target> -o <output_file> <input_file>" && exit 1
fi

if [ "x$output_file" = x ]
then
    echo >&2 "Usage: $0 -k <kernel> -o <output_file> <input_file>" && exit 1
fi

header="`dirname $1`/`basename $1 .bc`_header.h"

rm -f ${header}

/usr/bin/opt -load=/usr/lib/pocl/llvmopencl.so -break-constgeps -generate-header -kernel=${kernel} -header=${header} -disable-output $1

#pure clang doesn't allow "-target tce-tut-llvm"
case $target in
  tce-*)
    target_flags="" ;;
  *)
    target_flags="-target $target";;
esac
/usr/bin/clang  $target_flags -c -o ${output_file}.o -x c - <<EOF

#include "${header}"

unsigned _num_args = _${kernel}_NUM_ARGS;
int _arg_is_pointer[] = _${kernel}_ARG_IS_POINTER;
int _arg_is_local[] = _${kernel}_ARG_IS_LOCAL;
int _arg_is_image[] = _${kernel}_ARG_IS_IMAGE;
int _arg_is_sampler[] = _${kernel}_ARG_IS_SAMPLER;
int _reqd_wg_size[3] = _${kernel}_REQD_WG_SIZE;
unsigned _num_locals = _${kernel}_NUM_LOCALS;
#if _${kernel}_NUM_LOCALS != 0
unsigned _local_sizes[_${kernel}_NUM_LOCALS] = _${kernel}_LOCAL_SIZE;
#endif
EOF

ld ${output_file}.o -shared -lm -o ${output_file}

# Generate a C global object suitable for using from the device as
# the kernel metadata object. The idea is that multiple of these
# can be linked to the same device binary and the addresses of the
# global metadata objects can be used as the kernel references from
# the host.
cat ${header} > ${output_file}.kernel_obj.c

cat >> ${output_file}.kernel_obj.c <<EOF

#include <pocl_device.h>

void _${kernel}_workgroup(void** args, struct pocl_context*);
void _${kernel}_workgroup_fast(void** args, struct pocl_context*);

__attribute__((address_space(3))) __kernel_metadata _${kernel}_md = {
    "${kernel}", /* name */
    _${kernel}_NUM_ARGS, /* num_args */
    _${kernel}_NUM_LOCALS, /* num_locals */
#if _${kernel}_NUM_LOCALS != 0
    _${kernel}_LOCAL_SIZE,
#else
    {0}, 
#endif
    _${kernel}_ARG_IS_LOCAL,
    _${kernel}_ARG_IS_POINTER,
    _${kernel}_ARG_IS_IMAGE,
    _${kernel}_ARG_IS_SAMPLER,
    _${kernel}_workgroup_fast
};
EOF
