capstone/arch/TriCore/TriCoreInstPrinter.c

483 lines
11 KiB
C
Raw Normal View History

2016-05-15 20:13:19 +08:00
//===- TriCoreInstPrinter.cpp - Convert TriCore MCInst to assembly syntax -===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class prints an TriCore MCInst to a .s file.
//
//===----------------------------------------------------------------------===//
/* Capstone Disassembly Engine */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
#ifdef CAPSTONE_HAS_TRICORE
2023-03-25 06:26:09 +08:00
#include <platform.h>
2016-05-15 20:13:19 +08:00
#include "../../MCInst.h"
Architecture updater (auto-sync) - Updating ARM (#1949) * Add auto-sync updater. * Update Capstone core with auto-sync changes. * Update ARM via auto-sync. * Make changes to arch modules which are introduced by auto-sync. * Update tests for ARM. * Fix build warnings for make * Remove meson.build * Print shift amount in decimal * Patch non LLVM register alias. * Change type of immediate operand to unsiged (due to: #771) * Replace all occurances of a register with its alias. * Fix printing of signed imms * Print rotate amount in decimal * CHange imm type to int64_t to match LLVM imm type. * Fix search for register names, by completing string first. * Print ModImm operands always in decimal * Use number format of previous capstone version. * Correct implicit writes and update_flags according to SBit. * Add missing test for RegImmShift * Reverse incorrect comparision. * Set shift information for move instructions. * Set mem access for all memory operands * Set subtracted flag if offset is negative. * Add flag for post-index memory operands. * Add detail op for BX_RET and MOVPCLR * Use instruction post_index operand. * Add VPOP and VPUSH as unique CS IDs. * Add shifting info for MOVsr. * Add TODOs. * Add in LLVM hardcoded operands to detail. * Move detail editing from InstPrinter to Mapping * Formatting * Add removed check. * Add writeback register and constraints to RFEI instructions. * Translate shift immediate * Print negative immediates * Remove duplicate invalid entry * Add CS groups to instructions * Fix write attriutes of stores. * Add missing names of added instructions * Fix LLVM bug * Add more post_index flags * http -> https * Make generated functions static * Remove tab prefix for alias instructions. * Set ValidateMCOperand to NULL. * Fix AddrMode3Operand operands * Allow getting system and banked register name via API * Add writeback to STC/LDC instructions. * Fix (hopefully) last case where disp is negative and subtracted = true * Remove accidentially introduced regressions
2023-07-19 17:56:27 +08:00
#include "../../Mapping.h"
2016-05-15 20:13:19 +08:00
#include "../../MathExtras.h"
2023-11-30 00:20:44 +08:00
2016-05-15 20:13:19 +08:00
#include "TriCoreMapping.h"
#include "TriCoreLinkage.h"
2016-05-15 20:13:19 +08:00
2023-03-23 20:17:04 +08:00
static const char *getRegisterName(unsigned RegNo);
2023-03-23 23:50:16 +08:00
static void printInstruction(MCInst *, uint64_t, SStream *);
2016-08-04 22:23:48 +08:00
static void printOperand(MCInst *MI, int OpNum, SStream *O);
2016-05-15 20:13:19 +08:00
#define GET_INSTRINFO_ENUM
2023-03-23 23:50:16 +08:00
#include "TriCoreGenInstrInfo.inc"
2016-05-15 20:13:19 +08:00
#define GET_REGINFO_ENUM
2023-03-23 23:50:16 +08:00
#include "TriCoreGenRegisterInfo.inc"
2016-05-15 20:13:19 +08:00
static uint32_t wrapping_u32(int64_t x)
2023-11-30 00:20:44 +08:00
{
x %= (int64_t)(UINT32_MAX);
2023-11-30 00:20:44 +08:00
return (uint32_t)x;
}
static bool fill_mem(MCInst *MI, unsigned int reg, int64_t disp);
2023-04-10 07:48:21 +08:00
2023-11-30 00:20:44 +08:00
static inline void set_mem(cs_tricore_op *op, uint8_t base, int64_t disp)
{
op->type |= TRICORE_OP_MEM;
op->mem.base = base;
op->mem.disp = disp;
}
2023-04-10 07:48:21 +08:00
static inline void fill_reg(MCInst *MI, uint32_t reg)
2023-04-20 21:55:37 +08:00
{
if (!detail_is_set(MI))
2023-04-20 21:55:37 +08:00
return;
cs_tricore_op *op = TriCore_get_detail_op(MI, 0);
op->type = TRICORE_OP_REG;
op->reg = reg;
TriCore_inc_op_count(MI);
2023-03-31 18:26:31 +08:00
}
2023-11-30 00:20:44 +08:00
static inline void fill_imm(MCInst *MI, int64_t imm)
2023-04-20 21:55:37 +08:00
{
if (!detail_is_set(MI))
2023-04-20 21:55:37 +08:00
return;
cs_tricore *tricore = TriCore_get_detail(MI);
if (tricore->op_count >= 1) {
cs_tricore_op *op = TriCore_get_detail_op(MI, -1);
if (op->type == TRICORE_OP_REG && fill_mem(MI, op->reg, imm))
return;
2023-04-20 21:55:37 +08:00
}
cs_tricore_op *op = TriCore_get_detail_op(MI, 0);
op->type = TRICORE_OP_IMM;
op->imm = imm;
2023-03-31 18:26:31 +08:00
tricore->op_count++;
}
2023-11-30 00:20:44 +08:00
static bool fill_mem(MCInst *MI, unsigned int reg, int64_t disp)
2023-04-20 21:55:37 +08:00
{
if (!detail_is_set(MI))
return false;
switch (MI->flat_insn->id) {
2023-05-01 22:52:47 +08:00
case TRICORE_INS_LDMST:
case TRICORE_INS_LDLCX:
case TRICORE_INS_LD_A:
case TRICORE_INS_LD_B:
case TRICORE_INS_LD_BU:
case TRICORE_INS_LD_H:
case TRICORE_INS_LD_HU:
case TRICORE_INS_LD_D:
case TRICORE_INS_LD_DA:
case TRICORE_INS_LD_W:
case TRICORE_INS_LD_Q:
case TRICORE_INS_STLCX:
case TRICORE_INS_STUCX:
case TRICORE_INS_ST_A:
case TRICORE_INS_ST_B:
case TRICORE_INS_ST_H:
case TRICORE_INS_ST_D:
case TRICORE_INS_ST_DA:
case TRICORE_INS_ST_W:
case TRICORE_INS_ST_Q:
case TRICORE_INS_CACHEI_I:
case TRICORE_INS_CACHEI_W:
case TRICORE_INS_CACHEI_WI:
case TRICORE_INS_CACHEA_I:
case TRICORE_INS_CACHEA_W:
case TRICORE_INS_CACHEA_WI:
case TRICORE_INS_CMPSWAP_W:
case TRICORE_INS_SWAP_A:
case TRICORE_INS_SWAP_W:
case TRICORE_INS_SWAPMSK_W:
case TRICORE_INS_LEA:
case TRICORE_INS_LHA: {
switch (MCInst_getOpcode(MI)) {
case TriCore_LDMST_abs:
case TriCore_LDLCX_abs:
case TriCore_LD_A_abs:
case TriCore_LD_B_abs:
case TriCore_LD_BU_abs:
case TriCore_LD_H_abs:
case TriCore_LD_HU_abs:
case TriCore_LD_D_abs:
case TriCore_LD_DA_abs:
case TriCore_LD_W_abs:
case TriCore_LD_Q_abs:
case TriCore_STLCX_abs:
case TriCore_STUCX_abs:
case TriCore_ST_A_abs:
case TriCore_ST_B_abs:
case TriCore_ST_H_abs:
case TriCore_ST_D_abs:
case TriCore_ST_DA_abs:
case TriCore_ST_W_abs:
case TriCore_ST_Q_abs:
case TriCore_SWAP_A_abs:
case TriCore_SWAP_W_abs:
case TriCore_LEA_abs:
case TriCore_LHA_abs: {
2023-04-20 21:55:37 +08:00
return false;
}
2023-04-10 07:48:21 +08:00
}
cs_tricore_op *op = TriCore_get_detail_op(MI, -1);
op->type = 0;
set_mem(op, reg, disp);
2023-04-20 21:55:37 +08:00
return true;
}
2023-04-10 07:48:21 +08:00
}
return false;
}
2023-04-20 21:55:37 +08:00
static void printOperand(MCInst *MI, int OpNum, SStream *O)
{
2023-03-26 06:46:28 +08:00
if (OpNum >= MI->size)
return;
MCOperand *Op = MCInst_getOperand(MI, OpNum);
2023-03-26 06:46:28 +08:00
if (MCOperand_isReg(Op)) {
unsigned reg = MCOperand_getReg(Op);
2023-07-01 09:13:14 +08:00
SStream_concat0(O, getRegisterName(reg));
fill_reg(MI, reg);
2023-03-26 06:46:28 +08:00
} else if (MCOperand_isImm(Op)) {
int64_t Imm = MCOperand_getImm(Op);
printUInt32Bang(O, wrapping_u32(Imm));
2023-11-30 00:20:44 +08:00
fill_imm(MI, Imm);
2023-03-26 06:46:28 +08:00
}
}
2023-04-20 21:55:37 +08:00
static void print_sign_ext(MCInst *MI, int OpNum, SStream *O, unsigned n)
{
2023-03-26 06:46:28 +08:00
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t imm = MCOperand_getImm(MO);
int32_t res = SignExtend32(wrapping_u32(imm), n);
2023-11-30 00:20:44 +08:00
printInt32Bang(O, res);
fill_imm(MI, res);
2023-03-26 06:46:28 +08:00
} else
printOperand(MI, OpNum, O);
2023-03-25 06:26:09 +08:00
}
2023-11-30 00:20:44 +08:00
static void off4_fixup(MCInst *MI, int64_t *off4)
2023-04-20 21:55:37 +08:00
{
switch (MCInst_getOpcode(MI)) {
case TriCore_LD_A_slro:
case TriCore_LD_A_sro:
case TriCore_LD_W_slro:
case TriCore_LD_W_sro:
case TriCore_ST_A_sro:
case TriCore_ST_A_ssro:
case TriCore_ST_W_sro:
case TriCore_ST_W_ssro: {
2023-11-30 00:20:44 +08:00
*off4 = *off4 * 4;
2023-04-20 21:55:37 +08:00
break;
}
case TriCore_LD_H_sro:
case TriCore_LD_H_slro:
case TriCore_ST_H_sro:
case TriCore_ST_H_ssro: {
2023-11-30 00:20:44 +08:00
*off4 = *off4 * 2;
2023-04-20 21:55:37 +08:00
break;
}
}
}
2023-11-30 00:20:44 +08:00
static void const8_fixup(MCInst *MI, int64_t *const8)
{
switch (MCInst_getOpcode(MI)) {
case TriCore_LD_A_sc:
case TriCore_ST_A_sc:
case TriCore_ST_W_sc:
case TriCore_LD_W_sc: {
2023-11-30 00:20:44 +08:00
*const8 = *const8 * 4;
break;
}
}
}
2023-04-20 21:55:37 +08:00
static void print_zero_ext(MCInst *MI, int OpNum, SStream *O, unsigned n)
{
2023-03-26 06:46:28 +08:00
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t imm = MCOperand_getImm(MO);
2023-03-29 12:31:35 +08:00
for (unsigned i = n + 1; i < 32; ++i) {
2023-11-30 00:20:44 +08:00
imm &= ~(1LL << i);
2023-03-26 06:46:28 +08:00
}
if (n == 4) {
off4_fixup(MI, &imm);
}
if (n == 8) {
const8_fixup(MI, &imm);
}
printUInt32Bang(O, wrapping_u32(imm));
fill_imm(MI, imm);
2023-03-26 06:46:28 +08:00
} else
printOperand(MI, OpNum, O);
}
2023-04-20 21:55:37 +08:00
static void printOff18Imm(MCInst *MI, int OpNum, SStream *O)
{
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t imm = MCOperand_getImm(MO);
imm = ((wrapping_u32(imm) & 0x3C000) << 14) |
(wrapping_u32(imm) & 0x3fff);
printUInt32Bang(O, wrapping_u32(imm));
2023-11-30 00:20:44 +08:00
fill_imm(MI, imm);
} else
printOperand(MI, OpNum, O);
}
2023-11-30 00:20:44 +08:00
// PC + sext(disp) * 2
2024-10-19 12:05:02 +08:00
#define DISP_SEXT_2ALIGN(N) ((int64_t)(MI->address) + SignExtend64(disp, N) * 2)
2023-11-30 00:20:44 +08:00
2023-04-20 21:55:37 +08:00
static void printDisp24Imm(MCInst *MI, int OpNum, SStream *O)
{
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t disp = MCOperand_getImm(MO);
int64_t res = 0;
switch (MCInst_getOpcode(MI)) {
case TriCore_CALL_b:
case TriCore_FCALL_b: {
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(24);
2023-04-20 21:55:37 +08:00
break;
}
case TriCore_CALLA_b:
case TriCore_FCALLA_b:
case TriCore_JA_b:
case TriCore_JLA_b:
2024-10-19 12:05:02 +08:00
// {disp24[23:20], 7b0000000, disp24[19:0], 1b0}
res = ((disp & 0xf00000ULL) << 8) |
((disp & 0xfffffULL) << 1);
2023-04-20 21:55:37 +08:00
break;
case TriCore_J_b:
case TriCore_JL_b:
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(24);
2023-04-20 21:55:37 +08:00
break;
}
printUInt32Bang(O, wrapping_u32(res));
2023-11-30 00:20:44 +08:00
fill_imm(MI, res);
} else
printOperand(MI, OpNum, O);
}
2023-04-20 21:55:37 +08:00
static void printDisp15Imm(MCInst *MI, int OpNum, SStream *O)
{
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t disp = MCOperand_getImm(MO);
int64_t res = 0;
switch (MCInst_getOpcode(MI)) {
case TriCore_LOOP_brr:
case TriCore_LOOPU_brr:
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(15);
2023-11-30 00:20:44 +08:00
break;
case TriCore_JEQ_brc:
case TriCore_JEQ_brr:
case TriCore_JEQ_A_brr:
case TriCore_JGE_brc:
case TriCore_JGE_brr:
case TriCore_JGE_U_brc:
case TriCore_JGE_U_brr:
case TriCore_JLT_brc:
case TriCore_JLT_brr:
case TriCore_JLT_U_brc:
case TriCore_JLT_U_brr:
case TriCore_JNE_brc:
case TriCore_JNE_brr:
case TriCore_JNE_A_brr:
case TriCore_JNED_brc:
case TriCore_JNED_brr:
case TriCore_JNEI_brc:
case TriCore_JNEI_brr:
case TriCore_JNZ_A_brr:
case TriCore_JNZ_T_brn:
case TriCore_JZ_A_brr:
case TriCore_JZ_T_brn:
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(15);
2023-04-20 21:55:37 +08:00
break;
default:
// handle other cases, if any
break;
}
printUInt32Bang(O, wrapping_u32(res));
2023-11-30 00:20:44 +08:00
fill_imm(MI, res);
} else
printOperand(MI, OpNum, O);
}
2023-04-20 21:55:37 +08:00
static void printDisp8Imm(MCInst *MI, int OpNum, SStream *O)
{
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t disp = MCOperand_getImm(MO);
int64_t res = 0;
switch (MCInst_getOpcode(MI)) {
case TriCore_CALL_sb:
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(8);
2023-04-20 21:55:37 +08:00
break;
case TriCore_J_sb:
case TriCore_JNZ_sb:
case TriCore_JZ_sb:
2024-10-19 12:05:02 +08:00
res = DISP_SEXT_2ALIGN(8);
2023-04-20 21:55:37 +08:00
break;
default:
// handle other cases, if any
break;
}
printUInt32Bang(O, wrapping_u32(res));
2023-11-30 00:20:44 +08:00
fill_imm(MI, res);
} else
printOperand(MI, OpNum, O);
}
2023-04-20 21:55:37 +08:00
static void printDisp4Imm(MCInst *MI, int OpNum, SStream *O)
{
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t disp = MCOperand_getImm(MO);
int64_t res = 0;
switch (MCInst_getOpcode(MI)) {
case TriCore_JEQ_sbc1:
case TriCore_JEQ_sbr1:
case TriCore_JGEZ_sbr:
case TriCore_JGTZ_sbr:
case TriCore_JLEZ_sbr:
case TriCore_JLTZ_sbr:
case TriCore_JNE_sbc1:
case TriCore_JNE_sbr1:
case TriCore_JNZ_sbr:
case TriCore_JNZ_A_sbr:
case TriCore_JNZ_T_sbrn:
case TriCore_JZ_sbr:
case TriCore_JZ_A_sbr:
case TriCore_JZ_T_sbrn:
2023-11-30 00:20:44 +08:00
// PC + zero_ext(disp4) * 2;
res = (int64_t)(MI->address) + disp * 2;
2023-04-20 21:55:37 +08:00
break;
case TriCore_JEQ_sbc2:
case TriCore_JEQ_sbr2:
case TriCore_JNE_sbc2:
case TriCore_JNE_sbr2:
2023-11-30 00:20:44 +08:00
// PC + zero_ext(disp4 + 16) * 2;
res = (int64_t)(MI->address) + ((disp + 16) * 2);
2023-04-20 21:55:37 +08:00
break;
case TriCore_LOOP_sbr:
2023-11-30 00:20:44 +08:00
// PC + {27b111111111111111111111111111, disp4, 0};
res = (int64_t)MI->address +
OneExtend32(wrapping_u32(disp) << 1, 5);
2023-04-20 21:55:37 +08:00
break;
default:
// handle other cases, if any
break;
}
printUInt32Bang(O, wrapping_u32(res));
2023-11-30 00:20:44 +08:00
fill_imm(MI, res);
} else
printOperand(MI, OpNum, O);
}
2023-07-01 09:13:14 +08:00
#define printSExtImm_(n) \
2023-04-20 21:55:37 +08:00
static void printSExtImm_##n(MCInst *MI, int OpNum, SStream *O) \
2023-07-01 09:13:14 +08:00
{ \
print_sign_ext(MI, OpNum, O, n); \
2023-04-20 21:55:37 +08:00
}
2023-03-26 06:46:28 +08:00
2023-07-01 09:13:14 +08:00
#define printZExtImm_(n) \
2023-04-20 21:55:37 +08:00
static void printZExtImm_##n(MCInst *MI, int OpNum, SStream *O) \
2023-07-01 09:13:14 +08:00
{ \
print_zero_ext(MI, OpNum, O, n); \
2023-04-20 21:55:37 +08:00
}
2023-03-25 06:26:09 +08:00
2023-04-23 14:43:14 +08:00
// clang-format off
2023-04-08 03:09:32 +08:00
2023-04-23 14:43:14 +08:00
printSExtImm_(16)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printSExtImm_(10)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printSExtImm_(9)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printSExtImm_(4)
2023-03-26 06:46:28 +08:00
2023-04-23 14:43:14 +08:00
printZExtImm_(16)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printZExtImm_(9)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printZExtImm_(8)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printZExtImm_(4)
2023-11-30 00:20:44 +08:00
2023-04-23 14:43:14 +08:00
printZExtImm_(2);
2023-03-26 06:46:28 +08:00
2023-04-23 14:43:14 +08:00
// clang-format on
2023-03-26 06:46:28 +08:00
2023-04-23 14:43:14 +08:00
static void printOExtImm_4(MCInst *MI, int OpNum, SStream *O)
2023-04-20 21:55:37 +08:00
{
2023-04-10 03:18:12 +08:00
MCOperand *MO = MCInst_getOperand(MI, OpNum);
if (MCOperand_isImm(MO)) {
2023-11-30 00:20:44 +08:00
int64_t disp = MCOperand_getImm(MO);
int64_t res = (int64_t)MI->address +
(int64_t)OneExtend64(disp << 1, 5);
printUInt32Bang(O, wrapping_u32(res));
2023-11-30 00:20:44 +08:00
fill_imm(MI, res);
2023-04-10 03:18:12 +08:00
} else
printOperand(MI, OpNum, O);
}
2023-03-23 23:50:16 +08:00
/// Returned by getMnemonic() of the AsmPrinters.
typedef struct {
const char *first; // Mnemonic
2023-07-01 09:13:14 +08:00
uint64_t second; // Bits
2023-03-23 23:50:16 +08:00
} MnemonicBitsInfo;
2016-05-15 20:13:19 +08:00
#include "TriCoreGenAsmWriter.inc"
const char *TriCore_LLVM_getRegisterName(unsigned int id)
2023-04-20 21:55:37 +08:00
{
2023-04-10 05:55:33 +08:00
#ifndef CAPSTONE_DIET
return getRegisterName(id);
#else
return NULL;
#endif
}
void TriCore_LLVM_printInst(MCInst *MI, uint64_t Address, SStream *O)
2023-04-20 21:55:37 +08:00
{
printInstruction(MI, Address, O);
TriCore_set_access(MI);
2016-05-15 20:13:19 +08:00
}
#endif // CAPSTONE_HAS_TRICORE