#include "sysdep.h"
#include "dis-asm.h"
#include "opcode/dlx.h"
#define R_ERROR 0x1
#define R_TYPE 0x2
#define ILD_TYPE 0x3
#define IST_TYPE 0x4
#define IAL_TYPE 0x5
#define IBR_TYPE 0x6
#define IJ_TYPE 0x7
#define IJR_TYPE 0x8
#define NIL 0x9
#define OPC(x) ((x >> 26) & 0x3F)
#define FUNC(x) (x & 0x7FF)
unsigned char opc, rs1, rs2, rd;
unsigned long imm26, imm16, func, current_insn_addr;
static unsigned char
dlx_get_opcode (unsigned long opcode)
{
return (unsigned char) ((opcode >> 26) & 0x3F);
}
static unsigned char
dlx_get_rs1 (unsigned long opcode)
{
return (unsigned char) ((opcode >> 21) & 0x1F);
}
static unsigned char
dlx_get_rs2 (unsigned long opcode)
{
return (unsigned char) ((opcode >> 16) & 0x1F);
}
static unsigned char
dlx_get_rdR (unsigned long opcode)
{
return (unsigned char) ((opcode >> 11) & 0x1F);
}
static unsigned long
dlx_get_func (unsigned long opcode)
{
return (unsigned char) (opcode & 0x7FF);
}
static unsigned long
dlx_get_imm16 (unsigned long opcode)
{
return (unsigned long) (opcode & 0xFFFF);
}
static unsigned long
dlx_get_imm26 (unsigned long opcode)
{
return (unsigned long) (opcode & 0x03FFFFFF);
}
static void
operand_deliminator (struct disassemble_info *info, char *ptr)
{
int difft = 8 - (int) strlen (ptr);
while (difft > 0)
{
(*info->fprintf_func) (info->stream, "%c", ' ');
difft -= 1;
}
}
static unsigned char
dlx_r_type (struct disassemble_info *info)
{
unsigned char r_opc[] = { OPC(ALUOP) };
int r_opc_num = (sizeof r_opc) / (sizeof (char));
struct _r_opcode
{
unsigned long func;
char *name;
}
dlx_r_opcode[] =
{
{ NOPF, "nop" },
{ ADDF, "add" },
{ ADDUF, "addu" },
{ SUBF, "sub" },
{ SUBUF, "subu" },
{ MULTF, "mult" },
{ MULTUF, "multu" },
{ DIVF, "div" },
{ DIVUF, "divu" },
{ ANDF, "and" },
{ ORF, "or" },
{ XORF, "xor" },
{ SLLF, "sll" },
{ SRAF, "sra" },
{ SRLF, "srl" },
{ SEQF, "seq" },
{ SNEF, "sne" },
{ SLTF, "slt" },
{ SGTF, "sgt" },
{ SLEF, "sle" },
{ SGEF, "sge" },
{ SEQUF, "sequ" },
{ SNEUF, "sneu" },
{ SLTUF, "sltu" },
{ SGTUF, "sgtu" },
{ SLEUF, "sleu" },
{ SGEUF, "sgeu" },
{ MVTSF, "mvts" },
{ MVFSF, "mvfs" },
{ BSWAPF, "bswap" },
{ LUTF, "lut" }
};
int dlx_r_opcode_num = (sizeof dlx_r_opcode) / (sizeof dlx_r_opcode[0]);
int idx;
for (idx = 0; idx < r_opc_num; idx++)
{
if (r_opc[idx] != opc)
continue;
else
break;
}
if (idx == r_opc_num)
return NIL;
for (idx = 0 ; idx < dlx_r_opcode_num; idx++)
if (dlx_r_opcode[idx].func == func)
{
(*info->fprintf_func) (info->stream, "%s", dlx_r_opcode[idx].name);
if (func != NOPF)
{
operand_deliminator (info, dlx_r_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d,", (int)rd);
(*info->fprintf_func) (info->stream, "r%d", (int)rs1);
if (func != MVTSF && func != MVFSF)
(*info->fprintf_func) (info->stream, ",r%d", (int)rs2);
}
return (unsigned char) R_TYPE;
}
return (unsigned char) R_ERROR;
}
static unsigned char
dlx_load_type (struct disassemble_info* info)
{
struct _load_opcode
{
unsigned long opcode;
char *name;
}
dlx_load_opcode[] =
{
{ OPC(LHIOP), "lhi" },
{ OPC(LBOP), "lb" },
{ OPC(LBUOP), "lbu" },
{ OPC(LSBUOP),"ldstbu"},
{ OPC(LHOP), "lh" },
{ OPC(LHUOP), "lhu" },
{ OPC(LSHUOP),"ldsthu"},
{ OPC(LWOP), "lw" },
{ OPC(LSWOP), "ldstw" }
};
int dlx_load_opcode_num =
(sizeof dlx_load_opcode) / (sizeof dlx_load_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_load_opcode_num; idx++)
if (dlx_load_opcode[idx].opcode == opc)
{
if (opc == OPC (LHIOP))
{
(*info->fprintf_func) (info->stream, "%s", dlx_load_opcode[idx].name);
operand_deliminator (info, dlx_load_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d,", (int)rs2);
(*info->fprintf_func) (info->stream, "0x%04x", (int)imm16);
}
else
{
(*info->fprintf_func) (info->stream, "%s", dlx_load_opcode[idx].name);
operand_deliminator (info, dlx_load_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d,", (int)rs2);
(*info->fprintf_func) (info->stream, "0x%04x[r%d]", (int)imm16, (int)rs1);
}
return (unsigned char) ILD_TYPE;
}
return (unsigned char) NIL;
}
static unsigned char
dlx_store_type (struct disassemble_info* info)
{
struct _store_opcode
{
unsigned long opcode;
char *name;
}
dlx_store_opcode[] =
{
{ OPC(SBOP), "sb" },
{ OPC(SHOP), "sh" },
{ OPC(SWOP), "sw" },
};
int dlx_store_opcode_num =
(sizeof dlx_store_opcode) / (sizeof dlx_store_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_store_opcode_num; idx++)
if (dlx_store_opcode[idx].opcode == opc)
{
(*info->fprintf_func) (info->stream, "%s", dlx_store_opcode[idx].name);
operand_deliminator (info, dlx_store_opcode[idx].name);
(*info->fprintf_func) (info->stream, "0x%04x[r%d],", (int)imm16, (int)rs1);
(*info->fprintf_func) (info->stream, "r%d", (int)rs2);
return (unsigned char) IST_TYPE;
}
return (unsigned char) NIL;
}
static unsigned char
dlx_aluI_type (struct disassemble_info* info)
{
struct _aluI_opcode
{
unsigned long opcode;
char *name;
}
dlx_aluI_opcode[] =
{
{ OPC(ADDIOP), "addi" },
{ OPC(ADDUIOP), "addui" },
{ OPC(SUBIOP), "subi" },
{ OPC(SUBUIOP), "subui" },
{ OPC(ANDIOP), "andi" },
{ OPC(ORIOP), "ori" },
{ OPC(XORIOP), "xori" },
{ OPC(SLLIOP), "slli" },
{ OPC(SRAIOP), "srai" },
{ OPC(SRLIOP), "srli" },
{ OPC(SEQIOP), "seqi" },
{ OPC(SNEIOP), "snei" },
{ OPC(SLTIOP), "slti" },
{ OPC(SGTIOP), "sgti" },
{ OPC(SLEIOP), "slei" },
{ OPC(SGEIOP), "sgei" },
{ OPC(SEQUIOP), "sequi" },
{ OPC(SNEUIOP), "sneui" },
{ OPC(SLTUIOP), "sltui" },
{ OPC(SGTUIOP), "sgtui" },
{ OPC(SLEUIOP), "sleui" },
{ OPC(SGEUIOP), "sgeui" },
#if 0
{ OPC(MVTSOP), "mvts" },
{ OPC(MVFSOP), "mvfs" },
#endif
};
int dlx_aluI_opcode_num =
(sizeof dlx_aluI_opcode) / (sizeof dlx_aluI_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_aluI_opcode_num; idx++)
if (dlx_aluI_opcode[idx].opcode == opc)
{
(*info->fprintf_func) (info->stream, "%s", dlx_aluI_opcode[idx].name);
operand_deliminator (info, dlx_aluI_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d,", (int)rs2);
(*info->fprintf_func) (info->stream, "r%d,", (int)rs1);
(*info->fprintf_func) (info->stream, "0x%04x", (int)imm16);
return (unsigned char) IAL_TYPE;
}
return (unsigned char) NIL;
}
static unsigned char
dlx_br_type (struct disassemble_info* info)
{
struct _br_opcode
{
unsigned long opcode;
char *name;
}
dlx_br_opcode[] =
{
{ OPC(BEQOP), "beqz" },
{ OPC(BNEOP), "bnez" }
};
int dlx_br_opcode_num =
(sizeof dlx_br_opcode) / (sizeof dlx_br_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_br_opcode_num; idx++)
if (dlx_br_opcode[idx].opcode == opc)
{
if (imm16 & 0x00008000)
imm16 |= 0xFFFF0000;
imm16 += (current_insn_addr + 4);
(*info->fprintf_func) (info->stream, "%s", dlx_br_opcode[idx].name);
operand_deliminator (info, dlx_br_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d,", (int) rs1);
(*info->fprintf_func) (info->stream, "0x%08x", (int) imm16);
return (unsigned char) IBR_TYPE;
}
return (unsigned char) NIL;
}
static unsigned char
dlx_jmp_type (struct disassemble_info* info)
{
struct _jmp_opcode
{
unsigned long opcode;
char *name;
}
dlx_jmp_opcode[] =
{
{ OPC(JOP), "j" },
{ OPC(JALOP), "jal" },
{ OPC(BREAKOP), "break" },
{ OPC(TRAPOP), "trap" },
{ OPC(RFEOP), "rfe" }
};
int dlx_jmp_opcode_num =
(sizeof dlx_jmp_opcode) / (sizeof dlx_jmp_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_jmp_opcode_num; idx++)
if (dlx_jmp_opcode[idx].opcode == opc)
{
if (imm26 & 0x02000000)
imm26 |= 0xFC000000;
imm26 += (current_insn_addr + 4);
(*info->fprintf_func) (info->stream, "%s", dlx_jmp_opcode[idx].name);
operand_deliminator (info, dlx_jmp_opcode[idx].name);
(*info->fprintf_func) (info->stream, "0x%08x", (int)imm26);
return (unsigned char) IJ_TYPE;
}
return (unsigned char) NIL;
}
static unsigned char
dlx_jr_type (struct disassemble_info* info)
{
struct _jr_opcode
{
unsigned long opcode;
char *name;
}
dlx_jr_opcode[] =
{
{ OPC(JROP), "jr" },
{ OPC(JALROP), "jalr" }
};
int dlx_jr_opcode_num =
(sizeof dlx_jr_opcode) / (sizeof dlx_jr_opcode[0]);
int idx;
for (idx = 0 ; idx < dlx_jr_opcode_num; idx++)
if (dlx_jr_opcode[idx].opcode == opc)
{
(*info->fprintf_func) (info->stream, "%s", dlx_jr_opcode[idx].name);
operand_deliminator (info, dlx_jr_opcode[idx].name);
(*info->fprintf_func) (info->stream, "r%d", (int)rs1);
return (unsigned char) IJR_TYPE;
}
return (unsigned char) NIL;
}
typedef unsigned char (* dlx_insn) (struct disassemble_info *);
int
print_insn_dlx (bfd_vma memaddr, struct disassemble_info* info)
{
bfd_byte buffer[4];
int insn_idx;
unsigned long insn_word;
unsigned char rtn_code;
unsigned long dlx_insn_type[] =
{
(unsigned long) dlx_r_type,
(unsigned long) dlx_load_type,
(unsigned long) dlx_store_type,
(unsigned long) dlx_aluI_type,
(unsigned long) dlx_br_type,
(unsigned long) dlx_jmp_type,
(unsigned long) dlx_jr_type,
(unsigned long) NULL
};
int dlx_insn_type_num = ((sizeof dlx_insn_type) / (sizeof (unsigned long))) - 1;
int status =
(*info->read_memory_func) (memaddr, (bfd_byte *) &buffer[0], 4, info);
if (status != 0)
{
(*info->memory_error_func) (status, memaddr, info);
return -1;
}
insn_word = bfd_getb32 (buffer);
opc = dlx_get_opcode (insn_word);
rs1 = dlx_get_rs1 (insn_word);
rs2 = dlx_get_rs2 (insn_word);
rd = dlx_get_rdR (insn_word);
func = dlx_get_func (insn_word);
imm16= dlx_get_imm16 (insn_word);
imm26= dlx_get_imm26 (insn_word);
#if 0
printf ("print_insn_big_dlx: opc = 0x%02x\n"
" rs1 = 0x%02x\n"
" rs2 = 0x%02x\n"
" rd = 0x%02x\n"
" func = 0x%08x\n"
" imm16 = 0x%08x\n"
" imm26 = 0x%08x\n",
opc, rs1, rs2, rd, func, imm16, imm26);
#endif
rtn_code = 0;
current_insn_addr = (unsigned long) memaddr;
for (insn_idx = 0; dlx_insn_type[insn_idx] != 0x0; insn_idx++)
switch (((dlx_insn) (dlx_insn_type[insn_idx])) (info))
{
case R_TYPE:
case ILD_TYPE:
case IST_TYPE:
case IAL_TYPE:
case IBR_TYPE:
case IJ_TYPE:
case IJR_TYPE:
return 4;
default:
case NIL:
continue;
case R_ERROR:
return -1;
}
if (insn_idx == dlx_insn_type_num)
(*info->fprintf_func) (info->stream, "<%s>", "Unrecognized Opcode");
return 4;
}