www.pudn.com > ASM86_64.rar > a64-2.h


#ifndef a64_H_
#define a64_H_

/*
 *	a64.h for mik's assembler attribute
 */

#include "i_attribute.h"
#include "opcode.h"

/* memory operand type key word */
typedef struct {
	unsigned char base;		/* base register id */
	unsigned char index;		/* index register id */
	unsigned char scale;		/* scale value id */
	long long disp;			/* disp64 value */
	unsigned char oseg;		/* segment override id */
	unsigned char cast;		/* operand size cast value */
	unsigned char transfer_cast;	/* near or far cast */
	unsigned char addr;		/* address size value */
} mem_t;

/* immediate operand type key word */
typedef struct {
	long long imme_value;
	unsigned char cast;	
} imme_t;



/* encode information type */
typedef struct prefix_struct {
/*
	unsigned char os_override;
	unsigned char as_override;
	unsigned char seg_override;
*/
	unsigned char lock;
	unsigned char rep;
} prefix_t;


/* operand type key word */
typedef struct {
	unsigned char reg;
	mem_t *mem;
/*	long long imme;	*/
	imme_t *imme; 	
} o_key_t;
	


/* operands attribute key words */
typedef struct {
	long to_attr;
	long do_attr;
	long so_attr;
} ops_attr_t;		


typedef struct {
	o_key_t *to_key;
	o_key_t *do_key;
	o_key_t *so_key;
} ops_key_t;


typedef struct o_link_t {
	ops_key_t *ops_key;		/* operands */
	struct o_link_t *next;		/* next operand */
} o_link_t;


/* the struct for greate instruct encodes key */
typedef struct e_key_t {
	ops_key_t *ops_key;		/* operands key word */
	i_key_t *i_key;			/* instruction key word */
	prefix_t *i_prefix;		/* encode information */
	unsigned int bits;
	unsigned int length;		/* encodes length */
	unsigned long long current_pc;	/* next instructin pc value */
	unsigned int line;		/* line */
	struct e_key_t *next;		/* next encode key word */
} e_key_t;



/**************************************************************/
typedef struct {
	char *reg;
	unsigned char reg_id;
} reg_t;

typedef struct {
	char *oseg;
	unsigned char oseg_id;
} oseg_t;

typedef struct {
	char *scale;
	unsigned char scale_id;
} scale_t;

typedef struct {
	char *cast;
	unsigned char cast_id;
} cast_t;

typedef struct {
	char *prefix;
	unsigned char prefix_id;
} i_prefix_t;

typedef struct {
	char *mode;
	unsigned char mode_id;
} bitmode_t;

typedef struct {
	char *bits;
	unsigned char bits_id;
} bits_t;
/***************************************************************/


/*  define registers id */
#define AL	0x10		/* al */
#define AX	0x20		/* ax */
#define EAX	0x40		/* eax */
#define RAX	0x80		/* rax */
#define MMX0	0xe0		/* mmx0 */
#define XMM0	0xf0		/* xmm0 */
#define ES	0xa0		/* es */
#define CR0	0xc0		/* cr0 */
#define DR0	0xd0		/* dr0 */

#define R8B	0x18		/* r8b */
#define R8W	0x28		/* r8w */
#define R8D	0x48		/* r8d */
#define R8	0x88		/* r8 */
#define XMM8	0xf8		/* xmm8 */
#define CR8	0xc8		/* cr8 */
#define DR8	0xd8		/* dr8 */

#define CL	0x11		/* cl */
#define CX	0x21		/* cx */
#define ECX	0x41		/* ecx */
#define RCX	0x81		/* rcx */
#define MMX1	0xe1		/* mmx1 */
#define XMM1	0xf1		/* xmm1 */
#define CS	0xa1		/* cs */
#define CR1	0xc1		/* cr1 */
#define DR1	0xd1		/* dr1 */

#define R9B	0x19		/* r9b */
#define R9W	0x29		/* r9w */
#define R9D	0x49		/* r9d */
#define R9	0x89		/* r9 */
#define XMM9	0xf9		/* xmm9 */
#define CR9	0xc9		/* cr9 */
#define DR9	0xd9		/* dr9 */

#define DL	0x12		/* dl */
#define DX	0x22		/* dx */
#define EDX	0x42		/* edx */
#define RDX	0x82		/* rdx */
#define MMX2	0xe2		/* mmx2 */
#define XMM2	0xf2		/* xmm2 */
#define SS	0xa2		/* ss */
#define CR2	0xc2		/* cr2 */
#define DR2	0xd2		/* dr2 */

#define R10B	0x1a		/* r10b */
#define R10W	0x2a		/* r10w */
#define R10D	0x4a		/* r10d */
#define R10	0x8a		/* r10 */
#define XMM10	0xfa		/* xmm10 */
#define CR10	0xca		/* cr10 */
#define DR10	0xda		/* dr10 */

#define BL	0x13		/* bl */
#define BX	0x23		/* bx */
#define EBX	0x43		/* ebx */
#define RBX	0x83		/* rbx */
#define MMX3	0xe3		/* mmx3 */
#define XMM3	0xf3		/* xmm3 */
#define DS	0xa3		/* ds */
#define CR3	0xc3		/* cr3 */
#define DR3	0xd3		/* dr3 */

#define R11B	0x1b		/* r11b */
#define R11W	0x2b		/* r11w */
#define R11D	0x4b		/* r11d */
#define R11	0x8b		/* r11 */
#define XMM11	0xfb		/* xmm11 */
#define CR11	0xcb		/* cr11 */
#define DR11	0xdb		/* dr11 */

#define AH	0x14		/* ah */
#define SPL	0x14		/* spl */
#define SP	0x24		/* sp */
#define ESP	0x44		/* esp */
#define RSP	0x84		/* rsp */
#define MMX4	0xe4		/* mmx4 */
#define XMM4	0xf4		/* xmm4 */
#define FS	0xa4		/* fs */
#define CR4	0xc4		/* cr4 */
#define DR4	0xd4		/* dr4 */

#define R12B	0x1c		/* r12b */
#define R12W	0x2c		/* r12w */
#define R12D	0x4c		/* r12d */
#define R12	0x8c		/* r12 */
#define XMM12	0xfc		/* xmm12 */
#define CR12	0xcc		/* cr12 */
#define DR12	0xdc		/* dr12 */

#define CH	0x15		/* ch */
#define BPL	0x15		/* bpl */
#define BP	0x25		/* bp */
#define EBP	0x45		/* ebp */
#define RBP	0x85		/* rbp */
#define MMX5	0xe5		/* mmx5 */
#define XMM5	0xf5		/* xmm5 */
#define GS	0xa5		/* gs */
#define CR5	0xc5		/* cr5 */
#define DR5	0xd5		/* dr5 */

#define R13B	0x1d		/* r13b */
#define R13W	0x2d		/* r13w */
#define R13D	0x4d		/* r13d */
#define R13	0x8d		/* r13 */
#define XMM13	0xfd		/* xmm13 */
#define CR13	0xcd		/* cr13 */
#define DR13	0xdd		/* dr13 */

#define DH	0x16		/* dh */
#define SIL	0x16		/* sil */
#define SI	0x26		/* si */
#define ESI	0x46 		/* esi */
#define RSI	0x86		/* rsi */
#define MMX6	0xe6		/* mmx6 */
#define XMM6	0xf6		/* xmm6 */
#define CR6	0xc6		/* cr6 */
#define DR6	0xd6		/* dr6 */

#define R14B	0x1e		/* r14b */
#define R14W	0x2e		/* r14w */
#define R14D	0x4e		/* r14d */
#define R14	0x8e		/* r14 */
#define XMM14	0xfe		/* xmm14 */
#define CR14	0xce		/* cr14 */
#define DR14	0xde		/* dr14 */

#define BH	0x17		/* bh */
#define DIL	0x17		/* dil */
#define DI	0x27		/* di */
#define EDI	0x47		/* edi */
#define RDI	0x87		/* rdi */
#define MMX7	0xa7		/* mmx7 */
#define XMM7	0xf7		/* xmm7 */
#define CR7	0xc7		/* cr7 */
#define DR7	0xd7		/* dr7 */

#define R15B	0x1f		/* r15b */
#define R15W	0x2f		/* r15w */
#define R15D	0x4f		/* r15d */
#define R15	0x8f		/* r15 */
#define XMM15	0xff		/* xmm15 */
#define CR15	0xcf		/* cr15 */
#define DR15	0xdf		/* dr15 */

/********************************************************************/

#define LONG_64			0x40
#define LONG_COMPATIBILITY	0x20
#define PROTECTED		0x20
#define V86			0x10
#define REAL			0x10

#define BITS64			0x40
#define BITS32			0x20
#define BITS16			0x10


#define NEAR_ID		(0x80|16)
#define FAR_ID		(0x80|32)

/*******************************************************************/
typedef enum {
	KW_MODE = 1,
	KW_BITS,
	KW_ORG,
	KW_BYTE,
	KW_WORD,
	KW_DWORD,
	KW_QWORD,
	KW_DQWORD,
	KW_PTR,
	KW_CONST,
	KW_FUNCTION,
	KW_VAR,
	KW_MACRO,
	KW_TEXT,
	KW_CODE
} kw_id_t;


typedef struct {
	char *keyword;
	kw_id_t keyword_id;
} keyword_t;
/*********************************************************************/
#define BUF_PAGE	4096	/* for encodes a page */


/*
 *	x86-64 full registers
 */

reg_t regs[131];

/******************************************************/
oseg_t osegs[7];
scale_t scales[5];
cast_t casts[7];
//cast_t transfer_cast[3];
i_prefix_t i_prefixs[7];

/******************************************************************/

keyword_t keyword[16];
bitmode_t modes[6];
bits_t bits[4];

/************************************************************/


/***** 	a64 gloabl variable *****/          
unsigned int current_mode;
unsigned int current_bits;
unsigned long long current_pc;
unsigned long long org;
unsigned long line;

e_key_t *e_key_link;

/* a64's function */
void mem_zero(void *ptr, unsigned int size);
void *a64_malloc(unsigned int);
void a64_free(void *ptr);

int get_sum_of_1bit(unsigned int b);

int str_cmp(char *d, char *s);
int str_len(char *s);
int str_cat(char *d, char *s);
int get_c(char *s, char c);

int trim_space(char *d, char *s);


int is_keyword(char *s);

unsigned char get_regid(char *s);
unsigned char get_osegid(char *s);
unsigned char get_scaleid(char *s);
unsigned char get_castid(char *s);
unsigned char get_modeid(char *s);
unsigned char get_bitsid(char *s);
unsigned char get_prefixid(char *s);

/**********************************/
int check_cast(unsigned char cast);
int check_oseg(unsigned char oseg);
int check_mreg(unsigned char regid);


int is_c(char c);
int is_n(char c);

/* the string is numeric ? */
int is_hex(char *s);
int is_dec(char *s);
int is_binary(char *s);
int is_numeric(char *s);

int get_sizeof(long long n);


/* string to numeric */
long long str_to_n(char *s);



e_key_t *get_ekey_from_elem(void *elem);

/********** instruction function ***********/
i_key_t *get_ins(char *s);
i_key_t *get_transfer_ins(char *s);
i_key_t *get_i_key(i_key_t *i_key, ops_attr_t *ops_attr);

int is_free_sym(char *s);	/* true if the symbol is free & can use */
int is_label(char *s);		/* true if operand is label  */
int is_variable(char *s);	/* true if operand is variable */
int is_valid_sym(char *s);	/* for operands */


#define is_GPR(regid)	((regid) && ((regid & 0xf0) < 0xa0))
#define is_sREG(regid)	((regid) && ((regid & 0xf0) == 0xa0))
#define is_cREG(regid) 	((regid) && ((regid & 0xf0) == 0xc0))
#define is_dREG(regid)	((regid) && ((regid & 0xf0) == 0xd0))
#define is_MMX(regid)	((regid) && ((regid & 0xf0) == 0xe0))
#define is_XMM(regid)	((regid) && ((regid & 0xf0) == 0xf0))

#define is_qGPR(regid)	((regid) && ((regid & 0xf0) == 0x80))
#define is_eGPR(regid)	(is_qGPR(regid) && (regid & 0x08))

/**************************************/
void release_resource();



#endif