Skip to content

Commit

Permalink
Refactor Alpha files
Browse files Browse the repository at this point in the history
  • Loading branch information
R33v0LT committed Aug 6, 2023
1 parent 5213d5a commit 99436a1
Show file tree
Hide file tree
Showing 21 changed files with 2,236 additions and 1,940 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ if (CAPSTONE_ALPHA_SUPPORT)
arch/Alpha/AlphaLinkage.h
arch/Alpha/AlphaMapping.h
arch/Alpha/AlphaModule.h
arch/Alpha/AlphaGenCSMappingInsnOp.inc
arch/Alpha/AlphaGenCSMappingInsn.inc
arch/Alpha/AlphaGenCSMappingInsnName.inc
)
set(TEST_SOURCES ${TEST_SOURCES} test_alpha.c)
endif ()
Expand Down
1 change: 1 addition & 0 deletions Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,4 @@ const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
DEFINE_get_detail_op(arm, ARM);
DEFINE_get_detail_op(ppc, PPC);
DEFINE_get_detail_op(aarch64, AArch64);
DEFINE_get_detail_op(alpha, Alpha);
2 changes: 2 additions & 0 deletions Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size);
DECL_get_detail_op(arm, ARM);
DECL_get_detail_op(ppc, PPC);
DECL_get_detail_op(aarch64, AArch64);
DECL_get_detail_op(alpha, Alpha);

/// Increments the detail->arch.op_count by one.
#define DEFINE_inc_detail_op_count(arch, ARCH) \
Expand Down Expand Up @@ -156,6 +157,7 @@ static inline void set_doing_mem(const MCInst *MI, bool status)
DEFINE_get_arch_detail(arm, ARM);
DEFINE_get_arch_detail(ppc, PPC);
DEFINE_get_arch_detail(aarch64, AArch64);
DEFINE_get_arch_detail(alpha, Alpha);

static inline bool detail_is_set(const MCInst *MI)
{
Expand Down
85 changes: 38 additions & 47 deletions arch/Alpha/AlphaDisassembler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
#include "../../utils.h"

#include "../../MCFixedLenDisassembler.h"
#include "../../MCDisassembler.h"
#include "../../Mapping.h"

#include "AlphaDisassembler.h"
#include "AlphaLinkage.h"

static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder);
uint64_t Address,
const void *Decoder);

static DecodeStatus DecodeF4RCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder);
uint64_t Address,
const void *Decoder);

static DecodeStatus DecodeF8RCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder);
uint64_t Address,
const void *Decoder);

#include "AlphaGenDisassemblerTables.inc"

Expand All @@ -35,10 +35,10 @@ static DecodeStatus DecodeF8RCRegisterClass(MCInst *Inst, unsigned RegNo,
#include "AlphaGenRegisterInfo.inc"

static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder)
uint64_t Address,
const void *Decoder)
{
if (RegNo > 31)
if (RegNo > 31)
return MCDisassembler_Fail;

unsigned Register = GPRC[RegNo];
Expand All @@ -47,10 +47,10 @@ static DecodeStatus DecodeGPRCRegisterClass(MCInst *Inst, unsigned RegNo,
}

static DecodeStatus DecodeF4RCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder)
uint64_t Address,
const void *Decoder)
{
if (RegNo > 31)
if (RegNo > 31)
return MCDisassembler_Fail;

unsigned Register = F4RC[RegNo];
Expand All @@ -59,10 +59,10 @@ static DecodeStatus DecodeF4RCRegisterClass(MCInst *Inst, unsigned RegNo,
}

static DecodeStatus DecodeF8RCRegisterClass(MCInst *Inst, unsigned RegNo,
uint64_t Address,
const void *Decoder)
uint64_t Address,
const void *Decoder)
{
if (RegNo > 31)
if (RegNo > 31)
return MCDisassembler_Fail;

unsigned Register = F8RC[RegNo];
Expand All @@ -74,49 +74,40 @@ static DecodeStatus DecodeF8RCRegisterClass(MCInst *Inst, unsigned RegNo,

#include "AlphaGenInstrInfo.inc"

static inline bool tryGetInstruction32(const uint8_t *code, size_t code_len,
MCInst *MI, uint16_t *size,
uint64_t address, void *info,
const uint8_t *decoderTable32)
DecodeStatus Alpha_LLVM_getInstruction(csh handle, const uint8_t *Bytes,
size_t ByteLen, MCInst *MI,
uint16_t *Size, uint64_t Address,
void *Info)
{
uint32_t insn = readBytes32(MI, code);
DecodeStatus Result;

// Calling the auto-generated decoder function.
Result = decodeInstruction_4(decoderTable32, MI, insn, address, NULL);
if (Result != MCDisassembler_Fail) {
*size = 4;
return true;
if (!handle) {
return MCDisassembler_Fail;
}
return false;
}

bool Alpha_getInstruction(csh handle, const uint8_t *Bytes, size_t ByteLen,
MCInst *MI, uint16_t *Size, uint64_t Address,
void *Info)
{
if (!handle) {
return false;
if (ByteLen < 4) {
*Size = 0;
return MCDisassembler_Fail;
}

if (MI->flat_insn->detail) {
memset(MI->flat_insn->detail, 0, sizeof(cs_detail));
}
uint32_t Insn = readBytes32(MI, Bytes);
// Calling the auto-generated decoder function.
DecodeStatus Result =
decodeInstruction_4(DecoderTable32, MI, Insn, Address, NULL);

if (ByteLen < 4) {
return MCDisassembler_Fail;
if (Result != MCDisassembler_Fail) {
*Size = 4;
return Result;
}
return tryGetInstruction32(Bytes, ByteLen, MI, Size, Address, Info,
DecoderTable32);

*Size = 4;
return MCDisassembler_Fail;
}

void Alpha_init(MCRegisterInfo *MRI)
{
MCRegisterInfo_InitMCRegisterInfo(
MRI, AlphaRegDesc, ARR_SIZE(AlphaRegDesc), 0, 0,
AlphaMCRegisterClasses, ARR_SIZE(AlphaMCRegisterClasses), 0,
0, AlphaRegDiffLists, 0, AlphaSubRegIdxLists, 1, 0);
MRI, AlphaRegDesc, ARR_SIZE(AlphaRegDesc), 0, 0, AlphaMCRegisterClasses,
ARR_SIZE(AlphaMCRegisterClasses), 0, 0, AlphaRegDiffLists, 0,
AlphaSubRegIdxLists, 1, 0);
}

#endif
9 changes: 3 additions & 6 deletions arch/Alpha/AlphaDisassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
#include <stdint.h>
#endif

#include <capstone/capstone.h>
#include "../../MCRegisterInfo.h"
#include "../../MCDisassembler.h"
#include "../../MCInst.h"
#include "../../MCRegisterInfo.h"
#include <capstone/capstone.h>

void Alpha_init(MCRegisterInfo *MRI);

bool Alpha_getInstruction(csh ud, const uint8_t *code, size_t code_len,
MCInst *instr, uint16_t *size, uint64_t address,
void *info);

#endif // CS_ALPHADISASSEMBLER_H
Loading

0 comments on commit 99436a1

Please sign in to comment.