/* radare - Apache 2.0 - Copyright 2013 - Adam Pridgen <dso@rice.edu || adam.pridgen@thecoverofnight.com> */
#include <r_anal.h>

#ifndef R2_JAVA_OPS_H
#define R2_JAVA_OPS_H

enum {
	R_ANAL_JAVA_ILL_OP = -1, /* illegal instruction // trap */
	R_ANAL_JAVA_NULL_OP = 0,
	R_ANAL_JAVA_NOP = 1, /* does nothing */
	R_ANAL_JAVA_STORE_OP = 1 << 20, // Load or Store memory operation
	R_ANAL_JAVA_LOAD_OP = 1 << 21, // Load or Store memory operation
	R_ANAL_JAVA_REG_OP = 1 << 22, // register operation
	R_ANAL_JAVA_OBJ_OP = 1 << 23, // operates on an object
	R_ANAL_JAVA_STACK_OP = 1 << 25, // stack based operation
	R_ANAL_JAVA_BIN_OP = 1 << 26, // binary operation
	R_ANAL_JAVA_CODE_OP = 1 << 27, // operates on code
	R_ANAL_JAVA_DATA_OP = 1 << 28, // operates on data
	R_ANAL_JAVA_UNK_OP = 1 << 29, /* unknown opcode type */
	R_ANAL_JAVA_REP_OP = 1 << 30, /* repeats next instruction N times */
	R_ANAL_JAVA_COND_OP = 1U << 31,
};

enum {
	R_ANAL_JAVA_TYPE_REF_NULL = 0,
	R_ANAL_JAVA_TYPE_REF_UNK = 1 << 1,
	R_ANAL_JAVA_TYPE_REF = 1 << 2,
	R_ANAL_JAVA_TYPE_SIGNED = 1 << 3,
	R_ANAL_JAVA_TYPE_PRIM = 1 << 4,
	R_ANAL_JAVA_TYPE_CONST = 1 << 5,
	R_ANAL_JAVA_TYPE_STATIC = 1 << 6,
	R_ANAL_JAVA_TYPE_VOLATILE = 1 << 7,
	R_ANAL_JAVA_TYPE_PUBLIC = 1 << 8,

	R_ANAL_JAVA_TYPE_BOOL = 1 << 10,
	R_ANAL_JAVA_TYPE_BYTE = 1 << 11,
	R_ANAL_JAVA_TYPE_SHORT = 1 << 12,
	R_ANAL_JAVA_TYPE_INT32 = 1 << 13,
	R_ANAL_JAVA_TYPE_INTEGER = 1 << 13,
	R_ANAL_JAVA_TYPE_INT64 = 1 << 14,
	R_ANAL_JAVA_TYPE_LONG = 1 << 14,
	R_ANAL_JAVA_TYPE_FLOAT = 1 << 15,
	R_ANAL_JAVA_TYPE_DOUBLE = 1 << 16,
	R_ANAL_JAVA_TYPE_STRING = 1 << 17,
	R_ANAL_JAVA_TYPE_CHAR = 1 << 18,
	R_ANAL_JAVA_TYPE_VOID = 1 << 19,
};

// code ops
enum {
	R_ANAL_JAVA_CODEOP_JMP = (ut32) (1 << 1) | (ut32)R_ANAL_JAVA_CODE_OP, /* mandatory jump */
	R_ANAL_JAVA_CODEOP_CALL = (ut32) (1 << 2) | (ut32)R_ANAL_JAVA_CODE_OP, /* call to subroutine (branch+link) */
	R_ANAL_JAVA_CODEOP_RET = (ut32) (1 << 3) | (ut32)R_ANAL_JAVA_CODE_OP, /* returns from subrutine */
	R_ANAL_JAVA_CODEOP_TRAP = (ut32) (1 << 4) | (ut32)R_ANAL_JAVA_CODE_OP, /* it's a trap! */
	R_ANAL_JAVA_CODEOP_SWI = (ut32) (1 << 5) | (ut32)R_ANAL_JAVA_CODE_OP, /* syscall  software interrupt */
	R_ANAL_JAVA_CODEOP_IO = (ut32) (1 << 6) | (ut32)R_ANAL_JAVA_CODE_OP,
	R_ANAL_JAVA_CODEOP_LEAVE = (ut32) (1 << 7) | (ut32)R_ANAL_JAVA_CODE_OP,
	R_ANAL_JAVA_CODEOP_SWITCH = (ut32) (1 << 8) | (ut32)R_ANAL_JAVA_CODE_OP,
	R_ANAL_JAVA_CODEOP_CJMP = (ut32)R_ANAL_JAVA_COND_OP | (ut32)R_ANAL_JAVA_CODE_OP | (ut32)R_ANAL_JAVA_CODEOP_JMP,
	R_ANAL_JAVA_CODEOP_EOB = (ut32)R_ANAL_JAVA_CODEOP_JMP | (ut32)R_ANAL_JAVA_CODEOP_RET | (ut32)R_ANAL_JAVA_CODEOP_LEAVE | (ut32)R_ANAL_JAVA_CODEOP_SWITCH,
};

enum {
	// call return types
	R_ANAL_JAVA_RET_TYPE_REF_NULL = 1 << 10,
	R_ANAL_JAVA_RET_TYPE_REF = 1 << 11,
	R_ANAL_JAVA_RET_TYPE_PRIM = 1 << 12,
	R_ANAL_JAVA_RET_TYPE_CONST = 1 << 13,
	R_ANAL_JAVA_RET_TYPE_STATIC = 1 << 14,
};

// jmp conditionals
enum {
	// TODO these should be mapped to some sort of
	// flags register
	R_ANAL_JAVA_COND_EQ = 1 << 11,
	R_ANAL_JAVA_COND_NE = 1 << 12,
	R_ANAL_JAVA_COND_GE = 1 << 13,
	R_ANAL_JAVA_COND_GT = 1 << 14,
	R_ANAL_JAVA_COND_LE = 1 << 15,
	R_ANAL_JAVA_COND_LT = 1 << 16,
	R_ANAL_JAVA_COND_AL = 1 << 17,
	R_ANAL_JAVA_COND_NV = 1 << 18,
	R_ANAL_JAVA_COND_NULL = 1 << 19,
};

// bin ops
enum {
	R_ANAL_JAVA_BINOP_NEG = (ut32)0 | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_XCHG = (ut32) (1 << 1) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_CMP = (ut32) (1 << 2) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_ADD = (ut32) (1 << 3) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_SUB = (ut32) (1 << 4) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_MUL = (ut32) (1 << 6) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_DIV = (ut32) (1 << 7) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_SHR = (ut32) (1 << 8) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_SHL = (ut32) (1 << 9) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_SAL = (ut32) (1 << 10) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_SAR = (ut32) (1 << 11) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_OR = (ut32) (1 << 12) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_AND = (ut32) (1 << 14) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_XOR = (ut32) (1 << 15) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_NOT = (ut32) (1 << 16) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_MOD = (ut32) (1 << 17) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_ROR = (ut32) (1 << 18) | (ut32)R_ANAL_JAVA_BIN_OP,
	R_ANAL_JAVA_BINOP_ROL = (ut32) (1 << 19) | (ut32)R_ANAL_JAVA_BIN_OP,
};

// Object ops
enum {
	R_ANAL_JAVA_OBJOP_CAST = (ut32) (1 << 0) | (ut32)R_ANAL_JAVA_OBJ_OP,
	R_ANAL_JAVA_OBJOP_CHECK = (ut32) (1 << 1) | (ut32)R_ANAL_JAVA_OBJ_OP,
	R_ANAL_JAVA_OBJOP_NEW = (ut32) (1 << 2) | (ut32)R_ANAL_JAVA_OBJ_OP,
	R_ANAL_JAVA_OBJOP_DEL = (ut32) (1 << 3) | (ut32)R_ANAL_JAVA_OBJ_OP,
	R_ANAL_JAVA_OBJOP_SIZE = (ut32) (1 << 4) | (ut32)R_ANAL_JAVA_OBJ_OP,
};

// Memory or Data Operations
// Locations of item loaded (base of indirect)
enum {
	R_ANAL_JAVA_LDST_FROM_REF = 1 << 1,
	R_ANAL_JAVA_LDST_FROM_MEM = 1 << 1,

	R_ANAL_JAVA_LDST_FROM_REG = 1 << 2,
	R_ANAL_JAVA_LDST_FROM_STACK = 1 << 3,
	R_ANAL_JAVA_LDST_FROM_CONST = 1 << 4,
	R_ANAL_JAVA_LDST_FROM_VAR = 1 << 5,

	// If indirect load, where are we getting the indirection,
	R_ANAL_JAVA_LDST_INDIRECT_REF = 1 << 6,
	R_ANAL_JAVA_LDST_INDIRECT_MEM = 1 << 6,

	R_ANAL_JAVA_LDST_INDIRECT_REG = 1 << 7,
	R_ANAL_JAVA_LDST_INDIRECT_STACK = 1 << 8,
	R_ANAL_JAVA_LDST_INDIRECT_IDX = 1 << 9,
	R_ANAL_JAVA_LDST_INDIRECT_VAR = 1 << 10,

	// Location to put the item,
	R_ANAL_JAVA_LDST_TO_REF = 1 << 11,
	R_ANAL_JAVA_LDST_TO_MEM = 1 << 11,

	R_ANAL_JAVA_LDST_TO_REG = 1 << 12,
	R_ANAL_JAVA_LDST_TO_STACK = 1 << 13,
	R_ANAL_JAVA_LDST_TO_VAR = 1 << 14,

	// Stack, Memory, Register, Bss, Data ,
	R_ANAL_JAVA_LDST_OP_PUSH = 1 << 15,
	R_ANAL_JAVA_LDST_OP_POP = 1 << 16,
	R_ANAL_JAVA_LDST_OP_MOV = 1 << 17,
	R_ANAL_JAVA_LDST_OP_EFF_ADDR = 1 << 18,
};

enum {

	R_ANAL_JAVA_LDST_LOAD_FROM_CONST_REF_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_FROM_CONST |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK |
		(ut32)R_ANAL_JAVA_TYPE_REF,

	R_ANAL_JAVA_LDST_LOAD_FROM_CONST_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_CONST |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_CONST_INDIRECT_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_CONST |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_VAR_INDIRECT_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_VAR |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_VAR_INDIRECT_TO_STACK_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_VAR |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_VAR_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_VAR |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_VAR_TO_STACK_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_VAR |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_REF_INDIRECT_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_LOAD_FROM_REF_INDIRECT_TO_STACK_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_INDIRECT_TO_VAR = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_VAR,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_INDIRECT_TO_VAR_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_INDIRECT_IDX |
		(ut32)R_ANAL_JAVA_LDST_TO_VAR,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_TO_VAR = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_VAR,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_TO_VAR_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_VAR,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_INDIRECT_TO_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_REF,

	R_ANAL_JAVA_LDST_STORE_FROM_STACK_INDIRECT_TO_REF_REF = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_REF,

	R_ANAL_JAVA_LDST_LOAD_FROM_REF_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK |
		(ut32)R_ANAL_JAVA_TYPE_PRIM,

	R_ANAL_JAVA_LDST_LOAD_FROM_PRIM_VAR_TO_STACK = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_VAR |
		(ut32)R_ANAL_JAVA_TYPE_PRIM,

	R_ANAL_JAVA_LDST_LOAD_GET_STATIC = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK |
		(ut32)R_ANAL_JAVA_TYPE_REF,

	R_ANAL_JAVA_LDST_STORE_PUT_STATIC = (ut32)R_ANAL_JAVA_LDST_OP_POP |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_REF |
		(ut32)R_ANAL_JAVA_TYPE_REF,

	R_ANAL_JAVA_LDST_LOAD_GET_FIELD = (ut32)R_ANAL_JAVA_LDST_OP_PUSH |
		(ut32)R_ANAL_JAVA_LOAD_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_REF |
		(ut32)R_ANAL_JAVA_LDST_TO_STACK |
		(ut32)R_ANAL_JAVA_TYPE_REF,

	R_ANAL_JAVA_LDST_STORE_PUT_FIELD = (ut32)R_ANAL_JAVA_LDST_OP_POP |
		(ut32)R_ANAL_JAVA_STORE_OP |
		(ut32)R_ANAL_JAVA_LDST_FROM_STACK |
		(ut32)R_ANAL_JAVA_LDST_TO_REF |
		(ut32)R_ANAL_JAVA_TYPE_REF,
};

#endif
