#include "armdefs.h"
#include "arm2x86_self.h"

uint8_t *
get_op_lsl_T1_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsl_T1_im");
	T1 = T1 << 31;
	OP_END("get_op_lsl_T1_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_lsr_T1_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsr_T1_im");
	T1 = T1 >> 31;
	OP_END("get_op_lsr_T1_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_asr_T1_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_asr_T1_im");
	T1 = (int32_t)T1 >> 31;
	OP_END("get_op_asr_T1_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_ror_T1_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_ror_T1_im");
	__asm__ __volatile__ ("rorl	$31,%%"AREG_T1"\n\t"::);
	OP_END("get_op_ror_T1_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

op_table_t	op_shift_T1_im[4];
int
op_shift_T1_im_init()
{
	int	ret=0;

	op_shift_T1_im[0].op = get_op_lsl_T1_im(&op_shift_T1_im[0].len);
	if (op_shift_T1_im[0].op == NULL)
		return(-1);
	if (op_shift_T1_im[0].len > ret)
		ret = op_shift_T1_im[0].len;

	op_shift_T1_im[1].op = get_op_lsr_T1_im(&op_shift_T1_im[1].len);
	if (op_shift_T1_im[1].op == NULL)
		return(-1);
	if (op_shift_T1_im[1].len > ret)
		ret = op_shift_T1_im[1].len;

	op_shift_T1_im[2].op = get_op_asr_T1_im(&op_shift_T1_im[2].len);
	if (op_shift_T1_im[2].op == NULL)
		return(-1);
	if (op_shift_T1_im[2].len > ret)
		ret = op_shift_T1_im[2].len;

	op_shift_T1_im[3].op = get_op_ror_T1_im(&op_shift_T1_im[3].len);
	if (op_shift_T1_im[3].op == NULL)
		return(-1);
	if (op_shift_T1_im[3].len > ret)
		ret = op_shift_T1_im[3].len;

	ret += sizeof(uint8_t);

	return(ret);
}
//--------------------------------------------------------------------------------------------------
uint8_t *
get_op_lsl_T1_im_sc(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsl_T1_im_sc");
	st->CFlag = (T1 >> (32 - 2)) & 1;
	OP_END("get_op_lsl_T1_im_sc");
	*len = end - begin;
	ret=(uint8_t *)begin;
	if (*len < 5)
		return(NULL);
	if (ret[4] != (32 - 2))
		return(NULL);

	return(ret);
}

uint8_t *
get_op_lsr_T1_im_sc(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsr_T1_im_cc");
	st->CFlag = (T1 >> (31 - 1)) & 1;
	OP_END("get_op_lsr_T1_im_cc");
	*len = end - begin;
	ret=(uint8_t *)begin;
	if (*len < 5)
		return(NULL);
	if (ret[4] != 30)
		return(NULL);

	return(ret);
}

op_table_t	op_shift_T1_im_sc[4];
int
op_shift_T1_im_sc_init()
{
	op_shift_T1_im_sc[0].op = get_op_lsl_T1_im_sc(&op_shift_T1_im_sc[0].len);
	if (op_shift_T1_im_sc[0].op == NULL)
		return(-1);

	op_shift_T1_im_sc[1].op = get_op_lsr_T1_im_sc(&op_shift_T1_im_sc[1].len);
	if (op_shift_T1_im_sc[1].op == NULL)
		return(-1);

	op_shift_T1_im_sc[2].op = op_shift_T1_im_sc[1].op;
	op_shift_T1_im_sc[2].len = op_shift_T1_im_sc[1].len;

	op_shift_T1_im_sc[3].op = op_shift_T1_im_sc[1].op;
	op_shift_T1_im_sc[3].len = op_shift_T1_im_sc[1].len;

	return(0);
}
//--------------------------------------------------------------------------------------------------
uint8_t *
get_op_lsl_T1_T0(int *len)
{
	unsigned int	begin=0, end=0;

	OP_BEGIN("get_op_lsl_T1_T0");
	if (T0)
		T1 = T1 << T0;
	OP_END("get_op_lsl_T1_T0");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_lsr_T1_T0(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsr_T1_T0");
	if (T0)
		T1 = T1 >> T0;
	OP_END("get_op_lsr_T1_T0");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_asr_T1_T0(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_asr_T1_T0");
	if (T0)
		T1 = (int32_t)T1 >> T0;
	OP_END("get_op_asr_T1_T0");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_ror_T1_T0(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_ror_T1_T0");
	if (T0)
		T1 = (T1 >> T0) | (T1 << (32 - T0));
	OP_END("get_op_ror_T1_T0");
	*len = end - begin;

	return((uint8_t *)begin);
}

op_table_t	op_shift_T1_T0[4];
int
op_shift_T1_T0_init()
{
	op_shift_T1_T0[0].op = get_op_lsl_T1_T0(&op_shift_T1_T0[0].len);
	if (op_shift_T1_T0[0].op == NULL)
		return(-1);

	op_shift_T1_T0[1].op = get_op_lsr_T1_T0(&op_shift_T1_T0[1].len);
	if (op_shift_T1_T0[1].op == NULL)
		return(-1);

	op_shift_T1_T0[2].op = get_op_asr_T1_T0(&op_shift_T1_T0[2].len);
	if (op_shift_T1_T0[2].op == NULL)
		return(-1);

	op_shift_T1_T0[3].op = get_op_ror_T1_T0(&op_shift_T1_T0[3].len);
	if (op_shift_T1_T0[3].op == NULL)
		return(-1);

	return(0);
}
//--------------------------------------------------------------------------------------------------
uint8_t *
get_op_lsl_T1_T0_sc(int *len)
{
	unsigned int	begin=0, end=0;

	OP_BEGIN("get_op_lsl_T1_T0_sc");
	if (T0) {
		st->CFlag = (T1 >> (32 - T0)) & 1;
		T1 = T1 << T0;
	}
	OP_END("get_op_lsl_T1_T0_sc");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_lsr_T1_T0_sc(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsr_T1_T0_sc");
	if (T0) {
		st->CFlag = (T1 >> (T0 - 1)) & 1;
		T1 = T1 >> T0;
	}
	OP_END("get_op_lsr_T1_T0_sc");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_asr_T1_T0_sc(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_asr_T1_T0_sc");
	if (T0) {
		st->CFlag = (T1 >> (T0 - 1)) & 1;
		T1 = (int32_t)T1 >> T0;
	}
	OP_END("get_op_asr_T1_T0_sc");
	*len = end - begin;

	return((uint8_t *)begin);
}

uint8_t *
get_op_ror_T1_T0_sc(int *len)
{
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_ror_T1_T0_sc");
	if (T0) {
		st->CFlag = (T1 >> (T0 - 1)) & 1;
		T1 = (T1 >> T0) | (T1 << (32 - T0));
	}
	OP_END("get_op_ror_T1_T0_sc");
	*len = end - begin;

	return((uint8_t *)begin);
}

op_table_t	op_shift_T1_T0_sc[4];
int
op_shift_T1_T0_sc_init()
{
	op_shift_T1_T0_sc[0].op = get_op_lsl_T1_T0_sc(&op_shift_T1_T0_sc[0].len);
	if (op_shift_T1_T0_sc[0].op == NULL)
		return(-1);

	op_shift_T1_T0_sc[1].op = get_op_lsr_T1_T0_sc(&op_shift_T1_T0_sc[1].len);
	if (op_shift_T1_T0_sc[1].op == NULL)
		return(-1);

	op_shift_T1_T0_sc[2].op = get_op_asr_T1_T0_sc(&op_shift_T1_T0_sc[2].len);
	if (op_shift_T1_T0_sc[2].op == NULL)
		return(-1);

	op_shift_T1_T0_sc[3].op = get_op_ror_T1_T0_sc(&op_shift_T1_T0_sc[3].len);
	if (op_shift_T1_T0_sc[3].op == NULL)
		return(-1);

	return(0);
}
//--------------------------------------------------------------------------------------------------
uint8_t *
get_op_lsl_T2_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsl_T2_im");
	T2 = T2 << 31;
	OP_END("get_op_lsl_T2_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_lsr_T2_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_lsr_T2_im");
	T2 = T2 >> 31;
	OP_END("get_op_lsr_T2_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_asr_T2_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_asr_T2_im");
	T2 = (int32_t)T2 >> 31;
	OP_END("get_op_asr_T2_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

uint8_t *
get_op_ror_T2_im(int *len)
{
	uint8_t		*ret;
	unsigned int	begin=0,end=0;

	OP_BEGIN("get_op_ror_T2_im");
	__asm__ __volatile__ ("rorl	$31,%%"AREG_T2"\n\t"::);
	OP_END("get_op_ror_T2_im");
	*len = end - begin;
	if (*len <= sizeof(uint8_t)) {
		return(NULL);
	}
	else {
		*len -= sizeof(uint8_t);
	}
	ret=(uint8_t *)begin;
	if (ret[*len] != 31)
		return(NULL);

	return(ret);
}

op_table_t	op_shift_T2_im[4];
int
op_shift_T2_im_init()
{
	int	ret=0;

	op_shift_T2_im[0].op = get_op_lsl_T2_im(&op_shift_T2_im[0].len);
	if (op_shift_T2_im[0].op == NULL)
		return(-1);
	if (op_shift_T2_im[0].len > ret)
		ret = op_shift_T2_im[0].len;

	op_shift_T2_im[1].op = get_op_lsr_T2_im(&op_shift_T2_im[1].len);
	if (op_shift_T2_im[1].op == NULL)
		return(-1);
	if (op_shift_T2_im[1].len > ret)
		ret = op_shift_T2_im[1].len;

	op_shift_T2_im[2].op = get_op_asr_T2_im(&op_shift_T2_im[2].len);
	if (op_shift_T2_im[2].op == NULL)
		return(-1);
	if (op_shift_T2_im[2].len > ret)
		ret = op_shift_T2_im[2].len;

	op_shift_T2_im[3].op = get_op_ror_T2_im(&op_shift_T2_im[3].len);
	if (op_shift_T2_im[3].op == NULL)
		return(-1);
	if (op_shift_T2_im[3].len > ret)
		ret = op_shift_T2_im[3].len;

	ret += sizeof(uint8_t);

	return(ret);
}
//--------------------------------------------------------------------------------------------------
int
arm2x86_shift_init()
{
	if (op_shift_T1_im_init()<0) {
		return(-1);
	}
	if (op_shift_T1_im_sc_init()<0) {
		return(-1);
	}
	if (op_shift_T1_T0_init()<0) {
		return(-1);
	}
	if (op_shift_T1_T0_sc_init()<0) {
		return(-1);
	}
	if (op_shift_T2_im_init()<0) {
		return(-1);
	}

	return(0);
}
