module Interpreter
using CUDA
using StaticArrays
using ..ExpressionProcessing
using ..Utils

export interpret

"Interprets the given expressions with the values provided.
# Arguments
 - cudaExprs::CuArray{ExpressionProcessing.PostfixType} : The expressions to execute in postfix form and already sent to the GPU. The type information in the signature is missing, because creating a CuArray{ExpressionProcessing.PostfixType} results in a mor everbose type definition
 - cudaVars::CuArray{Float32} : The variables to use. Each column is mapped to the variables x1..xn. The type information is missing due to the same reasons as cudaExprs
 - parameters::Vector{Vector{Float32}} : The parameters to use. Each Vector contains the values for the parameters p1..pn. The number of parameters can be different for every expression
 - kwparam ```frontendCache```: The cache that stores the (partial) results of the frontend
"
function interpret(cudaExprs, numExprs::Integer, exprsInnerLength::Integer,
	cudaVars, variableColumns::Integer, variableRows::Integer, parameters::Vector{Vector{Float32}})::Matrix{Float32}

	cudaParams = Utils.create_cuda_array(parameters, NaN32) # column corresponds to data for one expression
	# put into seperate cuArray, as this is static and would be inefficient to send seperatly to each kernel
	cudaStepsize = CuArray([exprsInnerLength, Utils.get_max_inner_length(parameters), variableRows]) # max num of values per expression; max nam of parameters per expression; number of variables per expression

	# each expression has nr. of variable sets (nr. of columns of the variables) results and there are n expressions
	cudaResults = CuArray{Float32}(undef, variableColumns, numExprs)

	# Start kernel for each expression to ensure that no warp is working on different expressions
	@inbounds Threads.@threads for i in 1:numExprs # multithreaded to speedup dispatching (seems to have improved performance)
		numThreads = min(variableColumns, 256)
		numBlocks = cld(variableColumns, numThreads)

		@cuda threads=numThreads blocks=numBlocks fastmath=true interpret_expression(cudaExprs, cudaVars, cudaParams, cudaResults, cudaStepsize, i)
	end

	return cudaResults
end

const MAX_STACK_SIZE = 25 # The depth of the stack to store the values and intermediate results
function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, variables::CuDeviceArray{Float32}, parameters::CuDeviceArray{Float32}, results::CuDeviceArray{Float32}, stepsize::CuDeviceArray{Int}, exprIndex::Int)
	varSetIndex = (blockIdx().x - 1) * blockDim().x + threadIdx().x # ctaid.x * ntid.x + tid.x (1-based)
	@inbounds variableCols = length(variables) / stepsize[3] # number of variable sets

	if varSetIndex > variableCols
		return
	end

	@inbounds firstExprIndex = ((exprIndex - 1) * stepsize[1]) + 1 # Inclusive
	@inbounds lastExprIndex = firstExprIndex + stepsize[1] - 1 # Inclusive
	@inbounds firstParamIndex = ((exprIndex - 1) * stepsize[2]) # Exclusive
	# TODO: Use @cuDynamicSharedMem/@cuStaticSharedMem for variables and or parameters

	operationStack = MVector{MAX_STACK_SIZE, Float32}(undef) # Try to get this to function with variable size too, to allow better memory usage
	operationStackTop = 0 # stores index of the last defined/valid value
	
	@inbounds firstVariableIndex = ((varSetIndex-1) * stepsize[3]) # Exclusive
	
	@inbounds for i in firstExprIndex:lastExprIndex
		token = expressions[i]
		if token.Type == EMPTY
			break
		elseif token.Type == VARIABLE
			operationStackTop += 1
			operationStack[operationStackTop] = variables[firstVariableIndex + token.Value]
		elseif token.Type == PARAMETER
			operationStackTop += 1
			operationStack[operationStackTop] = parameters[firstParamIndex + token.Value]
		elseif token.Type == FLOAT32
			operationStackTop += 1
			operationStack[operationStackTop] = reinterpret(Float32, token.Value)
		elseif token.Type == OPERATOR
			opcode = reinterpret(Operator, token.Value)
			if opcode == ADD
				operationStackTop -= 1
				operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1]
			elseif opcode == SUBTRACT
				operationStackTop -= 1
				operationStack[operationStackTop] = operationStack[operationStackTop] - operationStack[operationStackTop + 1]
			elseif opcode == MULTIPLY
				operationStackTop -= 1
				operationStack[operationStackTop] = operationStack[operationStackTop] * operationStack[operationStackTop + 1]
			elseif opcode == DIVIDE
				operationStackTop -= 1
				operationStack[operationStackTop] = operationStack[operationStackTop] / operationStack[operationStackTop + 1]
			elseif opcode == POWER
				operationStackTop -= 1
				operationStack[operationStackTop] = operationStack[operationStackTop] ^ operationStack[operationStackTop + 1]
			elseif opcode == ABS
				operationStack[operationStackTop] = abs(operationStack[operationStackTop])
			elseif opcode == LOG
				operationStack[operationStackTop] = log(operationStack[operationStackTop])
			elseif opcode == EXP
				operationStack[operationStackTop] = exp(operationStack[operationStackTop])
			elseif opcode == SQRT
				operationStack[operationStackTop] = sqrt(operationStack[operationStackTop])
			elseif opcode == INV
				# operationStack[operationStackTop] = 1f0 / operationStack[operationStackTop]
				operationStack[operationStackTop] = inv(operationStack[operationStackTop])
			end
		else
			operationStack[operationStackTop] = NaN32
			break
		end
	end

	# "(exprIndex - 1) * variableCols" -> calculates the column in which to insert the result (expression = column)
	# "+ varSetIndex" -> to get the row inside the column at which to insert the result of the variable set (variable set = row)
	resultIndex = convert(Int, (exprIndex - 1) * variableCols + varSetIndex) # Inclusive
	@inbounds results[resultIndex] = operationStack[operationStackTop]

	return
end

end