Skip to content

Commit

Permalink
simplify ARCH_group_name() by using lookup table as suggested by @lea…
Browse files Browse the repository at this point in the history
…rn_more. also added the missing group name for GRP_PRIVILEGE
  • Loading branch information
aquynh committed Apr 27, 2015
1 parent 03d1e1f commit 1182d25
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 69 deletions.
12 changes: 2 additions & 10 deletions arch/AArch64/AArch64Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,28 +840,20 @@ static name_map group_name_maps[] = {
{ ARM64_GRP_JUMP, "jump" },
{ ARM64_GRP_CALL, "call" },
{ ARM64_GRP_RET, "return" },
{ ARM64_GRP_PRIVILEGE, "privilege" },

// architecture-specific groups
{ ARM64_GRP_CRYPTO, "crypto" },
{ ARM64_GRP_FPARMV8, "fparmv8" },
{ ARM64_GRP_NEON, "neon" },
{ ARM64_GRP_CRC, "crc" },

};
#endif

const char *AArch64_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= ARM64_GRP_ENDING || (id > ARM64_GRP_RET && id < ARM64_GRP_CRYPTO))
return NULL;

// NOTE: when new generic groups are added, 4 must be changed accordingly
if (id >= ARM64_GRP_CRYPTO)
return group_name_maps[id - ARM64_GRP_CRYPTO + 4].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
11 changes: 2 additions & 9 deletions arch/ARM/ARMMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ static name_map group_name_maps[] = {
// generic groups
{ ARM_GRP_INVALID, NULL },
{ ARM_GRP_JUMP, "jump" },
{ ARM_GRP_PRIVILEGE, "privilege" },

// architecture-specific groups
{ ARM_GRP_CRYPTO, "crypto" },
Expand Down Expand Up @@ -808,15 +809,7 @@ static name_map group_name_maps[] = {
const char *ARM_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= ARM_GRP_ENDING || (id > ARM_GRP_JUMP && id < ARM_GRP_CRYPTO))
return NULL;

// NOTE: when new generic groups are added, 2 must be changed accordingly
if (id >= ARM_GRP_CRYPTO)
return group_name_maps[id - ARM_GRP_CRYPTO + 2].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
10 changes: 1 addition & 9 deletions arch/Mips/MipsMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,15 +991,7 @@ static name_map group_name_maps[] = {
const char *Mips_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= MIPS_GRP_ENDING || (id > MIPS_GRP_JUMP && id < MIPS_GRP_BITCOUNT))
return NULL;

// NOTE: when new generic groups are added, 2 must be changed accordingly
if (id >= 128)
return group_name_maps[id - 128 + 2].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
10 changes: 1 addition & 9 deletions arch/PowerPC/PPCMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1462,15 +1462,7 @@ static name_map group_name_maps[] = {
const char *PPC_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= PPC_GRP_ENDING || (id > PPC_GRP_JUMP && id < PPC_GRP_ALTIVEC))
return NULL;

// NOTE: when new generic groups are added, 2 must be changed accordingly
if (id >= PPC_GRP_ALTIVEC)
return group_name_maps[id - PPC_GRP_ALTIVEC + 2].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
10 changes: 1 addition & 9 deletions arch/Sparc/SparcMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,7 @@ static name_map group_name_maps[] = {
const char *Sparc_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= SPARC_GRP_ENDING || (id > SPARC_GRP_JUMP && id < SPARC_GRP_HARDQUAD))
return NULL;

// NOTE: when new generic groups are added, 2 must be changed accordingly
if (id >= 128)
return group_name_maps[id - 128 + 2].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
10 changes: 1 addition & 9 deletions arch/SystemZ/SystemZMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,15 +839,7 @@ static name_map group_name_maps[] = {
const char *SystemZ_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= SYSZ_GRP_ENDING || (id > SYSZ_GRP_JUMP && id < SYSZ_GRP_DISTINCTOPS))
return NULL;

// NOTE: when new generic groups are added, 2 must be changed accordingly
if (id >= 128)
return group_name_maps[id - 128 + 2].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
11 changes: 2 additions & 9 deletions arch/X86/X86Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,7 @@ static name_map group_name_maps[] = {
{ X86_GRP_RET, "ret" },
{ X86_GRP_INT, "int" },
{ X86_GRP_IRET, "iret" },
{ X86_GRP_PRIVILEGE, "privilege" },

// architecture-specific groups
{ X86_GRP_VM, "vm" },
Expand Down Expand Up @@ -2249,15 +2250,7 @@ static name_map group_name_maps[] = {
const char *X86_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
// verify group id
if (id >= X86_GRP_ENDING || (id > X86_GRP_IRET && id < X86_GRP_VM))
return NULL;

// NOTE: when new generic groups are added, 6 must be changed accordingly
if (id >= 128)
return group_name_maps[id - 128 + 6].name;
else
return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
5 changes: 1 addition & 4 deletions arch/XCore/XCoreMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,7 @@ static name_map group_name_maps[] = {
const char *XCore_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
if (id >= XCORE_GRP_ENDING)
return NULL;

return group_name_maps[id].name;
return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
return NULL;
#endif
Expand Down
2 changes: 1 addition & 1 deletion suite/test_group_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run(self):
name = self.data.get(group_id)
res = _cs.cs_group_name(cap.csh, group_id)
if res != name:
print("ERROR: expected '%s', but got '%s'" %(name, res))
print("ERROR: id = %u expected '%s', but got '%s'" %(group_id, name, res))
print("")

arm_dict = {
Expand Down
14 changes: 14 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ int name2id(name_map* map, int max, const char *name)
return -1;
}

char *id2name(name_map* map, int max, const unsigned int id)
{
int i;

for (i = 0; i < max; i++) {
if (map[i].id == id) {
return map[i].name;
}
}

// nothing match
return NULL;
}

// count number of positive members in a list.
// NOTE: list must be guaranteed to end in 0
unsigned int count_positive(uint16_t *list)
Expand Down
4 changes: 4 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ typedef struct name_map {
// return 0 if not found
int name2id(name_map* map, int max, const char *name);

// map ID to a name
// return NULL if not found
char *id2name(name_map* map, int max, const unsigned int id);

// count number of positive members in a list.
// NOTE: list must be guaranteed to end in 0
unsigned int count_positive(uint16_t *list);
Expand Down

0 comments on commit 1182d25

Please sign in to comment.