# Developed by Aditya Atluri
# Date: 18 Jan 2014
# Mail: pyurutu@gmail.com
# This file contains the OpenCL implementation of the Python Code
# It converts Python Code to OpenCL code
# Modified: 15 Jun 2014

import inspect,shlex
import numpy as np
import execl
import threads, blocks, declare, grammar
import device

class cl_test:
	arguments = []
	arg_nam = [""]
	returns = []
	var_nam = []
	var_val = []
	kernel_final = []
	kernel_args = ""
	kernel = "/*This is callable OpenCL kernel code generated by Urutu*/\n"
	device = "/*This is OpenCL device code generated by Urutu*/\n"
	threads = [1, 1, 1]
	blocks = [1, 1, 1]
	threads_dec = [False, False, False]
	blocks_dec = [False, False, False]
	global_func = []
	code = ""
	args = []
	type_args = []
	type_vars = []
	words = []
	sentences = []
	__global = []
	__shared = []
	__register = []
	__constant = []
	tabs = 0
	count_def = 0
	num_return = 0
	count_return = 0
	is_device_code = False
	device_py = [[]]
	device_tab = 0
	device_body_buff = ""
	device_func_name = []
	device_var_nam = [[]]
	device_type_vars = [[]]
	device_scope = False
	device_sentences = [[]]
	device_threads_dec = [False,False,False]
	device_blocks_dec = [False, False, False]
	return_kernel = False
	modules = []
	arg_mask = []

	def __init__(self, fn, args):
		stri = inspect.getsource(fn)
		sh = shlex.shlex(stri)
		self.code = stri
		self.args = args
		if type(args[0]) is list and type(args[1]) is not list:
			self.threads = args[0]
			self.args = args[1:]
		if type(args[0]) is list and type(args[1]) is list:
			self.threads = args[0]
			self.blocks = args[1]
			self.args = args[2:]
		if args[0] == True:
			self.return_kernel = True
			self.args = args[1:]
		self.typeargs()

	def decarrays(self, phrase):
#		print phrase
		if phrase[0] == '__global' and phrase[1] == 'is':