Files
kaizen/external/capstone/arch/Mips/MipsMapping.c
T
iris 00cc9309cb Squashed 'external/ircolib/' changes from ce3cd726c..de6e324bd
de6e324bd separate emu thread
10d3daf86 Roms List improvements
95d202f37 Let's make the rom list process on a separate thread so the emulator doesnt take ages to load.
fc306967f Wow the ROM Header was just completely busted. Game list view works now
bad1691ee fuck this shit
2b59e5f46 game list in progress
d26417b83 remappable inputs in progress
ac4af8106 input
e72abc240 update readme
430139dc9 Qt6 frontend
3080d4d45 Fix this small bug too
08cd13b85 Cop0 unused functions do not actually pose a threat (as per manual). They don't do anything, so shall we.
61bb4fb44 make idle loop detection a little more specific with where the load goes
b037de4c3 SAZDFsdff
12e81e73e need to figure out why n64-systemtest loops indefinitely at some address that appears to be valid (i think it's me not invalidating the cache properly)
204f0e13b idle skipping seems to work!
cb8bb634a sdkfjlasdf
58e5c89c1 Fix compilation issue on my machine (no idea)
24fb2898e attempting more serious idle skipping
214719577 Place rsp.Step inside cached interpreter. Gains about 3 more fps
bb97dcc23 mmmmm
920b77d38 wjkhasdfjhkasdf
430ccdab4 it's a start...
4f42a673a Cached interpreter plays Mario 64. Start looking into RSP as well
c9a030787 idle skipping works!
5fbda03ce new idea
366637aba Idle skipping... maybe?
609fa2fb0 Cache instructions implemented but broken lmao. Commented out for now
e140a6d12 - Stop using inheritance for CPU, instead use composition. - Introduce KAIZEN_JIT_ENABLED optional define instead of relying on __aarch64__ and the like. - More cache work
68e613057 prep cache impl
811b4d809 fix clang format
fda755f7d idk
d5024ebbf small MI refactor in preparation of (eventually) implementing the RDRAM interface properly
694b45341 Merge commit '206dcdedf195fb320913584180edb12c7731e396' as 'external/SDL'
206dcdedf Squashed 'external/SDL/' content from commit 4d17b99d0a
4d16e1cb4 need to update sdl
848b19920 Fix compilation error
db61b5299 Merge commit 'e94a94559f28e49678fbcf72199a5258137b0fe9' as 'external/imgui'
e94a94559 Squashed 'external/imgui/' content from commit 02e9b8cac
52edb3757 need to update imgui
c1a705e86 Emulate weird JALR behaviour
4b4c32f4b Fix exception for "unusable COP1" in 4 instructions i missed accidentally (again)
df5828142 Bug putting 0s in the log everywhere
f8b580048 Make isviewer a sink to file
8241e9735 Fix exception for "unusable COP1" in 4 instructions i missed accidentally
b29715f20 small changes
d9a620bc1 make use of my new small utility library
0d1aa938e Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
e64eb40b3 Fuck git

git-subtree-dir: external/ircolib
git-subtree-split: de6e324bde
2026-06-15 11:56:38 +02:00

509 lines
14 KiB
C

/* Capstone Disassembly Engine */
/* By Giovanni Dante Grazioli, deroad <wargio@libero.it>, 2024 */
#ifdef CAPSTONE_HAS_MIPS
#include <stdio.h>
#include <string.h>
#include <capstone/capstone.h>
#include <capstone/mips.h>
#include "../../Mapping.h"
#include "../../MCDisassembler.h"
#include "../../cs_priv.h"
#include "../../cs_simple_types.h"
#include "MipsMapping.h"
#include "MipsLinkage.h"
#include "MipsDisassembler.h"
#define GET_REGINFO_ENUM
#define GET_REGINFO_MC_DESC
#include "MipsGenRegisterInfo.inc"
#define GET_INSTRINFO_ENUM
#include "MipsGenInstrInfo.inc"
void Mips_init_mri(MCRegisterInfo *MRI)
{
MCRegisterInfo_InitMCRegisterInfo(MRI, MipsRegDesc, sizeof(MipsRegDesc),
0, 0, MipsMCRegisterClasses,
ARR_SIZE(MipsMCRegisterClasses), 0, 0,
MipsRegDiffLists, 0,
MipsSubRegIdxLists,
ARR_SIZE(MipsSubRegIdxLists), 0);
}
const char *Mips_reg_name(csh handle, unsigned int reg)
{
int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax;
return Mips_LLVM_getRegisterName(reg,
syntax_opt & CS_OPT_SYNTAX_NOREGNAME);
}
void Mips_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
{
// Not used by Mips. Information is set after disassembly.
}
static const char *const insn_name_maps[] = {
#include "MipsGenCSMappingInsnName.inc"
};
#ifndef CAPSTONE_DIET
static const name_map insn_alias_mnem_map[] = {
#include "MipsGenCSAliasMnemMap.inc"
// The followings aliases are not generated by LLVM table gen.
{ MIPS_INS_ALIAS_B, "b" }, // beq
{ MIPS_INS_ALIAS_BEQZ, "beqz" }, // beq
{ MIPS_INS_ALIAS_BNEZ, "bnez" }, // bne
{ MIPS_INS_ALIAS_LI, "li" }, // addiu
{ MIPS_INS_ALIAS_END, NULL },
};
#endif
const char *Mips_insn_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
if (id < MIPS_INS_ALIAS_END && id > MIPS_INS_ALIAS_BEGIN) {
if (id - MIPS_INS_ALIAS_BEGIN >= ARR_SIZE(insn_alias_mnem_map))
return NULL;
return insn_alias_mnem_map[id - MIPS_INS_ALIAS_BEGIN - 1].name;
}
if (id >= MIPS_INS_ENDING)
return NULL;
if (id < ARR_SIZE(insn_name_maps))
return insn_name_maps[id];
// not found
return NULL;
#else
return NULL;
#endif
}
#ifndef CAPSTONE_DIET
static const name_map group_name_maps[] = {
{ MIPS_GRP_INVALID, NULL },
{ MIPS_GRP_JUMP, "jump" },
{ MIPS_GRP_CALL, "call" },
{ MIPS_GRP_RET, "return" },
{ MIPS_GRP_INT, "int" },
{ MIPS_GRP_IRET, "iret" },
{ MIPS_GRP_PRIVILEGE, "privilege" },
{ MIPS_GRP_BRANCH_RELATIVE, "branch_relative" },
// architecture-specific groups
#include "MipsGenCSFeatureName.inc"
};
#endif
const char *Mips_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
}
const insn_map mips_insns[] = {
#include "MipsGenCSMappingInsn.inc"
};
void Mips_reg_access(const cs_insn *insn, cs_regs regs_read,
uint8_t *regs_read_count, cs_regs regs_write,
uint8_t *regs_write_count)
{
uint8_t i;
uint8_t read_count, write_count;
cs_mips *mips = &(insn->detail->mips);
read_count = insn->detail->regs_read_count;
write_count = insn->detail->regs_write_count;
// implicit registers
memcpy(regs_read, insn->detail->regs_read,
read_count * sizeof(insn->detail->regs_read[0]));
memcpy(regs_write, insn->detail->regs_write,
write_count * sizeof(insn->detail->regs_write[0]));
// explicit registers
for (i = 0; i < mips->op_count; i++) {
cs_mips_op *op = &(mips->operands[i]);
switch ((int)op->type) {
case MIPS_OP_REG:
if ((op->access & CS_AC_READ) &&
!arr_exist(regs_read, read_count, op->reg)) {
regs_read[read_count] = (uint16_t)op->reg;
read_count++;
}
if ((op->access & CS_AC_WRITE) &&
!arr_exist(regs_write, write_count, op->reg)) {
regs_write[write_count] = (uint16_t)op->reg;
write_count++;
}
break;
case MIPS_OP_MEM:
// registers appeared in memory references always being read
if ((op->mem.base != MIPS_REG_INVALID) &&
!arr_exist(regs_read, read_count, op->mem.base)) {
regs_read[read_count] = (uint16_t)op->mem.base;
read_count++;
}
if ((insn->detail->writeback) &&
(op->mem.base != MIPS_REG_INVALID) &&
!arr_exist(regs_write, write_count, op->mem.base)) {
regs_write[write_count] =
(uint16_t)op->mem.base;
write_count++;
}
default:
break;
}
}
*regs_read_count = read_count;
*regs_write_count = write_count;
}
void Mips_set_instr_map_data(MCInst *MI)
{
// Fixes for missing groups.
if (MCInst_getOpcode(MI) == Mips_JR) {
unsigned Reg = MCInst_getOpVal(MI, 0);
switch (Reg) {
case MIPS_REG_RA:
case MIPS_REG_RA_64:
add_group(MI, MIPS_GRP_RET);
break;
}
}
map_cs_id(MI, mips_insns, ARR_SIZE(mips_insns));
map_implicit_reads(MI, mips_insns);
map_implicit_writes(MI, mips_insns);
map_groups(MI, mips_insns);
}
bool Mips_getInstruction(csh handle, const uint8_t *code, size_t code_len,
MCInst *instr, uint16_t *size, uint64_t address,
void *info)
{
uint64_t size64;
Mips_init_cs_detail(instr);
instr->MRI = (MCRegisterInfo *)info;
map_set_fill_detail_ops(instr, true);
DecodeStatus Result = Mips_LLVM_getInstruction(instr, &size64, code, code_len,
address,
info);
*size = size64;
if (Result != MCDisassembler_Fail) {
Mips_set_instr_map_data(instr);
}
if (Result == MCDisassembler_SoftFail) {
MCInst_setSoftFail(instr);
}
return Result != MCDisassembler_Fail;
}
void Mips_printer(MCInst *MI, SStream *O, void * /* MCRegisterInfo* */ info)
{
MCRegisterInfo *MRI = (MCRegisterInfo *)info;
MI->MRI = MRI;
Mips_LLVM_printInst(MI, MI->address, O);
#ifndef CAPSTONE_DIET
map_set_alias_id(MI, O, insn_alias_mnem_map,
ARR_SIZE(insn_alias_mnem_map));
#endif
}
static void Mips_setup_op(cs_mips_op *op)
{
memset(op, 0, sizeof(cs_mips_op));
op->type = MIPS_OP_INVALID;
}
void Mips_init_cs_detail(MCInst *MI)
{
if (detail_is_set(MI)) {
unsigned int i;
memset(get_detail(MI), 0,
offsetof(cs_detail, mips) + sizeof(cs_mips));
for (i = 0; i < ARR_SIZE(Mips_get_detail(MI)->operands); i++)
Mips_setup_op(&Mips_get_detail(MI)->operands[i]);
}
}
static const map_insn_ops insn_operands[] = {
#include "MipsGenCSMappingInsnOp.inc"
};
static void Mips_set_detail_op_mem_reg(MCInst *MI, unsigned OpNum, mips_reg Reg)
{
Mips_get_detail_op(MI, 0)->type = MIPS_OP_MEM;
Mips_get_detail_op(MI, 0)->mem.base = Reg;
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
}
static void Mips_set_detail_op_mem_disp(MCInst *MI, unsigned OpNum, int64_t Imm)
{
Mips_get_detail_op(MI, 0)->type = MIPS_OP_MEM;
Mips_get_detail_op(MI, 0)->mem.disp = Imm;
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
}
static void Mips_set_detail_op_imm(MCInst *MI, unsigned OpNum, int64_t Imm)
{
if (!detail_is_set(MI))
return;
if (doing_mem(MI)) {
Mips_set_detail_op_mem_disp(MI, OpNum, Imm);
return;
}
Mips_get_detail_op(MI, 0)->type = MIPS_OP_IMM;
Mips_get_detail_op(MI, 0)->imm = Imm;
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
Mips_inc_op_count(MI);
}
static void Mips_set_detail_op_uimm(MCInst *MI, unsigned OpNum, uint64_t Imm)
{
if (!detail_is_set(MI))
return;
if (doing_mem(MI)) {
Mips_set_detail_op_mem_disp(MI, OpNum, Imm);
return;
}
Mips_get_detail_op(MI, 0)->type = MIPS_OP_IMM;
Mips_get_detail_op(MI, 0)->uimm = Imm;
Mips_get_detail_op(MI, 0)->is_unsigned = true;
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
Mips_inc_op_count(MI);
}
static void Mips_set_detail_op_reg(MCInst *MI, unsigned OpNum, mips_reg Reg,
bool is_reglist)
{
if (!detail_is_set(MI))
return;
if (doing_mem(MI)) {
Mips_set_detail_op_mem_reg(MI, OpNum, Reg);
return;
}
CS_ASSERT((map_get_op_type(MI, OpNum) & ~CS_OP_MEM) == CS_OP_REG);
Mips_get_detail_op(MI, 0)->type = MIPS_OP_REG;
Mips_get_detail_op(MI, 0)->reg = Reg;
Mips_get_detail_op(MI, 0)->is_reglist = is_reglist;
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
Mips_inc_op_count(MI);
}
static void Mips_set_detail_op_operand(MCInst *MI, unsigned OpNum)
{
cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
int64_t value = MCInst_getOpVal(MI, OpNum);
if (op_type == CS_OP_IMM) {
Mips_set_detail_op_imm(MI, OpNum, value);
} else if (op_type == CS_OP_REG) {
Mips_set_detail_op_reg(MI, OpNum, value, false);
} else
printf("Operand type %d not handled!\n", op_type);
}
static void Mips_set_detail_op_jump(MCInst *MI, unsigned OpNum)
{
cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
if (op_type == CS_OP_IMM) {
uint64_t Base = MI->address & ~0x0fffffffull;
uint64_t Target = Base | (uint64_t)MCInst_getOpVal(MI, OpNum);
Mips_set_detail_op_uimm(MI, OpNum, Target);
} else if (op_type == CS_OP_REG) {
Mips_set_detail_op_reg(MI, OpNum, MCInst_getOpVal(MI, OpNum),
false);
} else
printf("Operand type %d not handled!\n", op_type);
}
static void Mips_set_detail_op_branch(MCInst *MI, unsigned OpNum)
{
cs_op_type op_type = map_get_op_type(MI, OpNum) & ~CS_OP_MEM;
if (op_type == CS_OP_IMM) {
uint64_t Target = MI->address + MCInst_getOpVal(MI, OpNum);
Mips_set_detail_op_uimm(MI, OpNum, Target);
} else if (op_type == CS_OP_REG) {
Mips_set_detail_op_reg(MI, OpNum, MCInst_getOpVal(MI, OpNum),
false);
} else
printf("Operand type %d not handled!\n", op_type);
}
static void Mips_set_detail_op_unsigned(MCInst *MI, unsigned OpNum)
{
Mips_set_detail_op_uimm(MI, OpNum, MCInst_getOpVal(MI, OpNum));
}
static void Mips_set_detail_op_unsigned_offset(MCInst *MI, unsigned OpNum,
unsigned Bits, uint64_t Offset)
{
uint64_t Imm = MCInst_getOpVal(MI, OpNum);
Imm -= Offset;
Imm &= (((uint64_t)1) << Bits) - 1;
Imm += Offset;
Mips_set_detail_op_uimm(MI, OpNum, Imm);
}
static void Mips_set_detail_op_mem_nanomips(MCInst *MI, unsigned OpNum)
{
CS_ASSERT(doing_mem(MI));
MCOperand *Op = MCInst_getOperand(MI, OpNum);
Mips_get_detail_op(MI, 0)->type = MIPS_OP_MEM;
// Base is a register, but nanoMips uses the Imm value as register.
Mips_get_detail_op(MI, 0)->mem.base = MCOperand_getImm(Op);
Mips_get_detail_op(MI, 0)->access = map_get_op_access(MI, OpNum);
}
static void Mips_set_detail_op_reglist(MCInst *MI, unsigned OpNum,
bool isNanoMips)
{
if (isNanoMips) {
for (unsigned i = OpNum; i < MCInst_getNumOperands(MI); i++) {
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i),
true);
}
return;
}
// -2 because register List is always first operand of instruction
// and it is always followed by memory operand (base + offset).
for (unsigned i = OpNum, e = MCInst_getNumOperands(MI) - 2; i != e;
++i) {
Mips_set_detail_op_reg(MI, i, MCInst_getOpVal(MI, i), true);
}
}
static void Mips_set_detail_op_unsigned_address(MCInst *MI, unsigned OpNum)
{
uint64_t Target = MI->address + (uint64_t)MCInst_getOpVal(MI, OpNum);
Mips_set_detail_op_imm(MI, OpNum, Target);
}
void Mips_add_cs_detail(MCInst *MI, mips_op_group op_group, va_list args)
{
if (!detail_is_set(MI) || !map_fill_detail_ops(MI))
return;
unsigned OpNum = va_arg(args, unsigned);
switch (op_group) {
default:
printf("Operand group %d not handled!\n", op_group);
return;
case Mips_OP_GROUP_MemOperand:
// this is only used by nanoMips.
return Mips_set_detail_op_mem_nanomips(MI, OpNum);
case Mips_OP_GROUP_BranchOperand:
return Mips_set_detail_op_branch(MI, OpNum);
case Mips_OP_GROUP_JumpOperand:
return Mips_set_detail_op_jump(MI, OpNum);
case Mips_OP_GROUP_Operand:
return Mips_set_detail_op_operand(MI, OpNum);
case Mips_OP_GROUP_UImm_1_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 1, 0);
case Mips_OP_GROUP_UImm_2_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 2, 0);
case Mips_OP_GROUP_UImm_3_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 3, 0);
case Mips_OP_GROUP_UImm_32_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 32, 0);
case Mips_OP_GROUP_UImm_16_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 16, 0);
case Mips_OP_GROUP_UImm_8_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 8, 0);
case Mips_OP_GROUP_UImm_5_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 0);
case Mips_OP_GROUP_UImm_6_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 0);
case Mips_OP_GROUP_UImm_4_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 4, 0);
case Mips_OP_GROUP_UImm_7_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 7, 0);
case Mips_OP_GROUP_UImm_10_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 10, 0);
case Mips_OP_GROUP_UImm_6_1:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 1);
case Mips_OP_GROUP_UImm_5_1:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 1);
case Mips_OP_GROUP_UImm_5_33:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 33);
case Mips_OP_GROUP_UImm_5_32:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 5, 32);
case Mips_OP_GROUP_UImm_6_2:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 6, 2);
case Mips_OP_GROUP_UImm_2_1:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 2, 1);
case Mips_OP_GROUP_UImm_0_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 0, 0);
case Mips_OP_GROUP_UImm_26_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 26, 0);
case Mips_OP_GROUP_UImm_12_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 12, 0);
case Mips_OP_GROUP_UImm_20_0:
return Mips_set_detail_op_unsigned_offset(MI, OpNum, 20, 0);
case Mips_OP_GROUP_RegisterList:
return Mips_set_detail_op_reglist(MI, OpNum, false);
case Mips_OP_GROUP_NanoMipsRegisterList:
return Mips_set_detail_op_reglist(MI, OpNum, true);
case Mips_OP_GROUP_PCRel:
/* fall-thru */
case Mips_OP_GROUP_Hi20PCRel:
return Mips_set_detail_op_unsigned_address(MI, OpNum);
case Mips_OP_GROUP_Hi20:
return Mips_set_detail_op_unsigned(MI, OpNum);
}
}
void Mips_set_mem_access(MCInst *MI, bool status)
{
if (!detail_is_set(MI))
return;
set_doing_mem(MI, status);
if (status) {
if (Mips_get_detail(MI)->op_count > 0 &&
Mips_get_detail_op(MI, -1)->type == MIPS_OP_MEM &&
Mips_get_detail_op(MI, -1)->mem.disp == 0) {
// Previous memory operand not done yet. Select it.
Mips_dec_op_count(MI);
return;
}
// Init a new one.
Mips_get_detail_op(MI, 0)->type = MIPS_OP_MEM;
Mips_get_detail_op(MI, 0)->mem.base = MIPS_REG_INVALID;
Mips_get_detail_op(MI, 0)->mem.disp = 0;
#ifndef CAPSTONE_DIET
uint8_t access =
map_get_op_access(MI, Mips_get_detail(MI)->op_count);
Mips_get_detail_op(MI, 0)->access = access;
#endif
} else {
// done, select the next operand slot
Mips_inc_op_count(MI);
}
}
#endif